Tizen 2.1 base
[framework/security/security-server.git] / ace / engine / Condition.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 //
17 // File: Condition.cpp
18 // Author: notroot
19 //
20 // Created on June 3, 2009, 9:00 AM
21 //
22
23 #include <iostream>
24 #include <dpl/log/log.h>
25 #include <dpl/foreach.h>
26 #include <ace/Condition.h>
27
28 /**
29  * Check if attribute in condition matches the values obtained from PIP
30  * attrSet - attributes from PIP
31  */
32
33 Attribute::MatchResult Condition::evaluateCondition(
34         const AttributeSet * attrSet) const
35 {
36     //Condition may include either matches of attributes or other conditions
37     //in this method all attributes are matched at first and if possible the
38     //condition is evaluated. If evaluation is not possible based solely on
39     //attributes then we start recursion into child conditions.
40
41     Attribute::MatchResult match;
42     bool undeterminedMatchFound = false;
43     bool isFinalMatch = false;
44
45     LogDebug("Attributes to be matched");
46     printAttributes(*attrSet);
47     LogDebug("Condition attributes values");
48     printAttributes(attributes);
49
50     if (this->isEmpty()) {
51         LogDebug("Condition is empty, returning true");
52         //Condition is empty, it means it evaluates to TRUE
53         return Attribute::MatchResult::MRTrue;
54     }
55
56     match = evaluateAttributes(attrSet, isFinalMatch, undeterminedMatchFound);
57     if (isFinalMatch) {
58         LogDebug("Evaluate attributes returning verdict" ) ; //<< match);
59         return match;
60     }
61
62     match = evaluateChildConditions(attrSet,
63                                     isFinalMatch,
64                                     undeterminedMatchFound);
65     if (isFinalMatch) {
66         LogDebug("Evaluate child conditions returning verdict" ); // << match);
67         return match;
68     }
69
70     if (undeterminedMatchFound) {
71         //If any  child condition/attribute-match was undetermined and
72         //so far we couldn't make a decision then we must return undetermined
73         LogDebug("Evaluate condition returning MRUndetermined");
74         return Attribute::MatchResult::MRUndetermined;
75     }
76
77     if (this->isAndCondition()) {
78         match = Attribute::MatchResult::MRTrue;
79     } else if (this->isOrCondition()) {
80         match = Attribute::MatchResult::MRFalse;
81     } else {
82         Assert(false && "Condition has to be either AND or OR");
83     }
84     return match;
85 }
86
87 // KW Attribute::MatchResult Condition::performORalgorithm(const std::set<Attribute>* attrSet) const{
88 // KW
89 // KW     Attribute::MatchResult match;
90 // KW     bool undeterminedMatchFound = false;
91 // KW     bool isFinalMatch = false;
92 // KW
93 // KW     LogDebug("Performing OR algorithm");
94 // KW
95 // KW     match = evaluateAttributes(attrSet, isFinalMatch, undeterminedMatchFound);
96 // KW     if(isFinalMatch){
97 // KW         LogDebug("OR algorithm evaluate attributes returning verdict" << match);
98 // KW         return match;
99 // KW     }
100 // KW
101 // KW     match = evaluateChildConditions(attrSet, isFinalMatch, undeterminedMatchFound);
102 // KW     if(isFinalMatch){
103 // KW         return match;
104 // KW     }
105 // KW
106 // KW     if(undeterminedMatchFound){
107 // KW         //If any  child condition/attribute-match was undetermined and
108 // KW         //so far we couldn't make a decision then we must return undetermined
109 // KW         LogDebug("OR algorithm returning MRUndetermined");
110 // KW         return Attribute::MRUndetermined;
111 // KW     }
112 // KW
113 // KW     LogDebug("OR algorithm returning MRFalse");
114 // KW     return Attribute::MRFalse;
115 // KW }
116
117 // KW Attribute::MatchResult Condition::performANDalgorithm(const std::set<Attribute>* attrSet) const{
118 // KW
119 // KW
120 // KW     Attribute::MatchResult match;
121 // KW     bool undeterminedMatchFound = false;
122 // KW     bool isFinalMatch = false;
123 // KW
124 // KW     LogDebug("Performing AND algorithm");
125 // KW     match = evaluateAttributes(attrSet, isFinalMatch, undeterminedMatchFound);
126 // KW     if(isFinalMatch){
127 // KW         LogDebug("AND algorithm evaluate attributes returning verdict" << match);
128 // KW         return match;
129 // KW     }
130 // KW     match = evaluateChildConditions(attrSet, isFinalMatch, undeterminedMatchFound);
131 // KW     if(isFinalMatch){
132 // KW         LogDebug("AND algorithm evaluate child returning verdict " << match);
133 // KW         return match;
134 // KW     }
135 // KW     if(undeterminedMatchFound){
136 // KW         //If any child condition/attribute-match was undetermined and
137 // KW         //so far we couldn't make a decision then we must return undetermined
138 // KW         LogDebug("AND algorithm returning Undetermined");
139 // KW         return Attribute::MRUndetermined;
140 // KW     }
141 // KW
142 // KW     LogDebug("AND algorithm returning MRTrue");
143 // KW     return Attribute::MRTrue;
144 // KW
145 // KW }
146
147 Attribute::MatchResult Condition::evaluateAttributes(
148         const AttributeSet * attrSet,
149         bool& isFinalMatch,
150         bool & undeterminedMatchFound) const
151 {
152     Attribute::MatchResult match = Attribute::MatchResult::MRUndetermined;
153
154     std::list<Attribute>::const_iterator condIt = this->attributes.begin();
155     while (condIt != this->attributes.end()) {
156         //Find the value of needed attribute, based on attribute name
157         AttributeSet::const_iterator attr =
158                 std::find_if(attrSet->begin(),
159                              attrSet->end(),
160                              AceDB::BaseAttribute::UnaryPredicate(&(*condIt)));
161         if (attr == attrSet->end()) {
162             LogError("Couldn't find required attribute. This should not happen");
163             Assert(
164                 false &&
165                 "Couldn't find attribute required in condition. This should not happen"
166                 "This means that some attributes has not been obtained from PIP");
167             //Return undetermined here because it seems one of the attributes is unknown/undetermined
168             isFinalMatch = true;
169             match = Attribute::MatchResult::MRUndetermined;
170             break;
171         }
172
173         match = condIt->matchAttributes(&(*(*attr)));
174         if ((match == Attribute::MatchResult::MRFalse) && isAndCondition()) {
175             //FALSE match found in AND condition
176             isFinalMatch = true;
177             break;
178         } else if ((match == Attribute::MatchResult::MRTrue) && isOrCondition()) {
179             //TRUE match found in OR condition
180             isFinalMatch = true;
181             break;
182         } else if (match == Attribute::MatchResult::MRUndetermined) {
183             //Just mark that there was undetermined value found
184             undeterminedMatchFound = true;
185         }
186         ++condIt;
187     }
188
189     return match;
190 }
191
192 Attribute::MatchResult Condition::evaluateChildConditions(
193         const AttributeSet * attrSet,
194         bool& isFinalMatch,
195         bool & undefinedMatchFound) const
196 {
197     Attribute::MatchResult match = Attribute::MatchResult::MRUndetermined;
198
199     std::list<Condition>::const_iterator it = conditions.begin();
200     while (it != conditions.end()) {
201         match = it->evaluateCondition(attrSet);
202
203         if ((match == Attribute::MatchResult::MRFalse) && isAndCondition()) {
204             //FALSE match found in AND condition
205             LogDebug("Child conditions results MRFalse)");
206             isFinalMatch = true;
207             break;
208         } else if ((match == Attribute::MatchResult::MRTrue) && isOrCondition()) {
209             //TRUE match found in OR condition
210             LogDebug("Child conditions result MRTrue");
211             isFinalMatch = true;
212             break;
213         } else if (match == Attribute::MatchResult::MRUndetermined) {
214             undefinedMatchFound = true;
215         }
216         ++it;
217     }
218
219     return match;
220 }
221
222 void Condition::getAttributes(AttributeSet * attrSet)
223 {
224     //Get attributes from current condition
225     FOREACH (it, attributes)
226     {
227         AceDB::BaseAttributePtr attr(new Attribute(it->getName(), it->getMatchFunction(), it->getType()));
228         attrSet->insert(attr);
229     }
230     //Get attributes from any child conditions
231     FOREACH (it, conditions)
232     {
233         it->getAttributes(attrSet);
234     }
235 }
236