Policy 1707 commit to LF
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / components / DecisionPolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.policy.pap.xacml.rest.components;
22
23 import java.net.URI;
24 import java.net.URISyntaxException;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.util.HashMap;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Map;
31
32 import javax.persistence.EntityManager;
33 import javax.persistence.Query;
34
35 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
36 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
37 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
38 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableDefinitionType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableReferenceType;
52
53 import org.openecomp.policy.common.logging.eelf.MessageCodes;
54 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
55 import org.openecomp.policy.pap.xacml.rest.XACMLPapServlet;
56 import org.openecomp.policy.pap.xacml.rest.util.JPAUtils;
57 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
58 import org.openecomp.policy.rest.jpa.Datatype;
59 import org.openecomp.policy.rest.jpa.DecisionSettings;
60 import org.openecomp.policy.rest.jpa.FunctionDefinition;
61 import org.openecomp.policy.xacml.std.pip.engines.aaf.AAFEngine;
62
63 import com.att.research.xacml.std.IdentifierImpl;
64
65 public class DecisionPolicy extends Policy {
66
67         public static final String FUNCTION_NOT = "urn:oasis:names:tc:xacml:1.0:function:not";
68         private static final String AAFProvider = "AAF";
69         
70         List<String> dynamicLabelRuleAlgorithms = new LinkedList<String>();
71         List<String> dynamicFieldComboRuleAlgorithms = new LinkedList<String>();
72         List<String> dynamicFieldOneRuleAlgorithms = new LinkedList<String>();
73         List<String> dynamicFieldTwoRuleAlgorithms = new LinkedList<String>();
74         List<String> dataTypeList = new LinkedList<String>();
75         
76         protected Map<String, String> dropDownMap = new HashMap<String, String>();
77         
78
79         public DecisionPolicy() {
80                 super();
81         }
82         
83         public DecisionPolicy(PolicyRestAdapter policyAdapter){
84                 this.policyAdapter = policyAdapter;
85         }
86         
87         @Override
88         public Map<String, String> savePolicies() throws Exception {
89
90                 Map<String, String> successMap = new HashMap<String,String>();
91                 if(isPolicyExists()){
92                         successMap.put("EXISTS", "This Policy already exist on the PAP");
93                         return successMap;
94                 }
95
96                 if(!isPreparedToSave()){
97                         //Prep and configure the policy for saving
98                         prepareToSave();
99                 }
100
101                 // Until here we prepared the data and here calling the method to create xml.
102                 Path newPolicyPath = null;
103                 newPolicyPath = Paths.get(policyAdapter.getNewFileName());
104
105                 successMap = createPolicy(newPolicyPath,getCorrectPolicyDataObject());  
106                 return successMap;              
107         }
108         
109         //This is the method for preparing the policy for saving.  We have broken it out
110         //separately because the fully configured policy is used for multiple things
111         @Override
112         public boolean prepareToSave() throws Exception{
113
114                 if(isPreparedToSave()){
115                         //we have already done this
116                         return true;
117                 }
118                 
119                 int version = 0;
120                 String policyID = policyAdapter.getPolicyID();
121                 version = policyAdapter.getHighestVersion();
122                 
123                 // Create the Instance for pojo, PolicyType object is used in marshalling.
124                 if (policyAdapter.getPolicyType().equals("Decision")) {
125                         PolicyType policyConfig = new PolicyType();
126
127                         policyConfig.setVersion(Integer.toString(version));
128                         policyConfig.setPolicyId(policyID);
129                         policyConfig.setTarget(new TargetType());
130                         policyAdapter.setData(policyConfig);
131                 }
132                 policyName = policyAdapter.getNewFileName();
133                 
134                 if (policyAdapter.getData() != null) {
135                         PolicyType decisionPolicy = (PolicyType)  policyAdapter.getData();
136                         
137                         decisionPolicy.setDescription(policyAdapter.getPolicyDescription());
138                         
139                         decisionPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId());
140                         AllOfType allOfOne = new AllOfType();
141                         String fileName = policyAdapter.getNewFileName();
142                         String name = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
143                         if ((name == null) || (name.equals(""))) {
144                                 name = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());
145                         }
146                         allOfOne.getMatch().add(createMatch("PolicyName", name));
147                         
148                         AllOfType allOf = new AllOfType();
149                         
150                         // Match for Ecomp
151                         allOf.getMatch().add(createMatch("ECOMPName", (policyAdapter.getEcompName())));
152                         
153                         Map<String, String> dynamicFieldComponentAttributes = policyAdapter.getDynamicFieldConfigAttributes();
154                         if(policyAdapter.getRuleProvider()!=null && policyAdapter.getRuleProvider().equals(AAFProvider)){
155                                 dynamicFieldComponentAttributes = new HashMap<String,String>();
156                         }
157                         
158                         // If there is any dynamic field attributes create the matches here
159                         for (String keyField : dynamicFieldComponentAttributes.keySet()) {
160                                 String key = keyField;
161                                 String value = dynamicFieldComponentAttributes.get(key);
162                                 MatchType dynamicMatch = createDynamicMatch(key, value);
163                                 allOf.getMatch().add(dynamicMatch);
164                         }
165
166                         AnyOfType anyOf = new AnyOfType();
167                         anyOf.getAllOf().add(allOfOne);
168                         anyOf.getAllOf().add(allOf);
169
170                         TargetType target = new TargetType();
171                         target.getAnyOf().add(anyOf);
172                         decisionPolicy.setTarget(target);
173
174                         Map<String, String> dynamicFieldDecisionSettings = policyAdapter.getDynamicSettingsMap();
175                         
176                         //dynamicVariableList = policyAdapter.getDynamicVariableList();
177                         if(policyAdapter.getProviderComboBox()!=null && policyAdapter.getProviderComboBox().equals(AAFProvider)){
178                                 dynamicFieldDecisionSettings = new HashMap<String,String>();
179                         }
180                         
181                         // settings are dynamic so check how many rows are added and add all
182                         for (String keyField : dynamicFieldDecisionSettings.keySet()) {
183                                 String key = keyField;
184                                 String value = dynamicFieldDecisionSettings.get(key);
185                                 //String dataType = (String) dynamicVariableList.get(counter);
186                                 String dataType = getDataType(key);
187                                 VariableDefinitionType dynamicVariable = createDynamicVariable(key, value, dataType);
188                                 decisionPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(dynamicVariable);
189                         }
190
191                         createRule(decisionPolicy, true);
192                         createRule(decisionPolicy, false);
193                 }
194
195                 setPreparedToSave(true);
196                 return true;
197         }
198         
199         private DecisionSettings findDecisionSettingsBySettingId(String settingId) {
200                 DecisionSettings decisionSetting = null;
201                 
202                 EntityManager em = XACMLPapServlet.getEmf().createEntityManager();
203                 Query getDecisionSettings = em.createNamedQuery("DecisionSettings.findAll");
204                 List<?> decisionSettingsList = getDecisionSettings.getResultList();
205                 
206                 for (Object id : decisionSettingsList) {
207                         decisionSetting = (DecisionSettings) id;
208                         if (decisionSetting.getXacmlId().equals(settingId)) {
209                                 break;
210                         }
211                 }
212                 return decisionSetting;
213         }
214         
215         private void createRule(PolicyType decisionPolicy, boolean permitRule) {
216                 RuleType rule = new RuleType();
217                         
218                 rule.setRuleId(policyAdapter.getRuleID());
219                         
220                 if (permitRule) {
221                         rule.setEffect(EffectType.PERMIT);
222                 } else {
223                         rule.setEffect(EffectType.DENY);
224                 }
225                 rule.setTarget(new TargetType());
226
227                 // Create Target in Rule
228                 AllOfType allOfInRule = new AllOfType();
229
230                 // Creating match for ACCESS in rule target
231                 MatchType accessMatch = new MatchType();
232                 AttributeValueType accessAttributeValue = new AttributeValueType();
233                 accessAttributeValue.setDataType(STRING_DATATYPE);
234                 accessAttributeValue.getContent().add("DECIDE");
235                 accessMatch.setAttributeValue(accessAttributeValue);
236                 AttributeDesignatorType accessAttributeDesignator = new AttributeDesignatorType();
237                 URI accessURI = null;
238                 try {
239                         accessURI = new URI(ACTION_ID);
240                 } catch (URISyntaxException e) {
241                         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "DecisionPolicy", "Exception creating ACCESS URI");
242                 }
243                 accessAttributeDesignator.setCategory(CATEGORY_ACTION);
244                 accessAttributeDesignator.setDataType(STRING_DATATYPE);
245                 accessAttributeDesignator.setAttributeId(new IdentifierImpl(accessURI).stringValue());
246                 accessMatch.setAttributeDesignator(accessAttributeDesignator);
247                 accessMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE);
248                 
249                 dynamicLabelRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmLabels();
250                 dynamicFieldComboRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmCombo();
251                 dynamicFieldOneRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmField1();
252                 dynamicFieldTwoRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmField2();
253                 dropDownMap = createDropDownMap();
254                 
255                 if(policyAdapter.getRuleProvider()!=null && policyAdapter.getRuleProvider().equals(AAFProvider)){
256                         // Values for AAF Provider are here for XML Creation. 
257                         ConditionType condition = new ConditionType();
258                         ApplyType decisionApply = new ApplyType();
259                         String selectedFunction = "boolean-equal";
260                         
261                         AttributeValueType value1 = new AttributeValueType();
262                         value1.setDataType(BOOLEAN_DATATYPE);
263                         value1.getContent().add("true");
264                         
265                         AttributeDesignatorType value2 = new AttributeDesignatorType();
266                         value2.setAttributeId(AAFEngine.AAF_RESULT);
267                         value2.setCategory(CATEGORY_RESOURCE);
268                         value2.setDataType(BOOLEAN_DATATYPE);
269                         value2.setMustBePresent(false);
270                         
271                         ApplyType innerDecisionApply = new ApplyType();
272                         innerDecisionApply.setFunctionId(FUNCTION_BOOLEAN_ONE_AND_ONLY);
273                         innerDecisionApply.getExpression().add(new ObjectFactory().createAttributeDesignator(value2));
274                         
275                         decisionApply.setFunctionId(dropDownMap.get(selectedFunction));
276                         decisionApply.getExpression().add(new ObjectFactory().createAttributeValue(value1));
277                         decisionApply.getExpression().add(new ObjectFactory().createApply(innerDecisionApply));
278                         condition.setExpression(new ObjectFactory().createApply(decisionApply));
279                         if (!permitRule) {
280                                 ApplyType notOuterApply = new ApplyType();
281                                 notOuterApply.setFunctionId(FUNCTION_NOT);
282                                 notOuterApply.getExpression().add(condition.getExpression());
283                                 condition.setExpression(new ObjectFactory().createApply(notOuterApply));
284                         }
285                         rule.setCondition(condition);
286                         allOfInRule.getMatch().add(accessMatch);
287
288                         AnyOfType anyOfInRule = new AnyOfType();
289                         anyOfInRule.getAllOf().add(allOfInRule);
290
291                         TargetType targetInRule = new TargetType();
292                         targetInRule.getAnyOf().add(anyOfInRule);
293
294                         rule.setTarget(targetInRule);
295                         if(!permitRule){
296                                 AdviceExpressionsType adviceExpressions = new AdviceExpressionsType();
297                                 AdviceExpressionType adviceExpression = new AdviceExpressionType();
298                                 adviceExpression.setAdviceId(AAFProvider);
299                                 adviceExpression.setAppliesTo(EffectType.DENY);
300                                 AttributeAssignmentExpressionType assignment = new AttributeAssignmentExpressionType();
301                                 assignment.setAttributeId("aaf.response");
302                                 assignment.setCategory(CATEGORY_RESOURCE);
303                                 AttributeDesignatorType value = new AttributeDesignatorType();
304                                 value.setAttributeId(AAFEngine.AAF_RESPONSE);
305                                 value.setCategory(CATEGORY_RESOURCE);
306                                 value.setDataType(STRING_DATATYPE);
307                                 value.setMustBePresent(false);
308                                 assignment.setExpression(new ObjectFactory().createAttributeDesignator(value));
309                                 adviceExpression.getAttributeAssignmentExpression().add(assignment);
310                                 adviceExpressions.getAdviceExpression().add(adviceExpression);
311                                 rule.setAdviceExpressions(adviceExpressions);
312                         }
313                         decisionPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
314                         policyAdapter.setPolicyData(decisionPolicy);
315                         
316                 }else if (dynamicLabelRuleAlgorithms != null && dynamicLabelRuleAlgorithms.size() > 0) {
317                         boolean isCompound = false;
318                         ConditionType condition = new ConditionType();
319                         int index = dynamicFieldOneRuleAlgorithms.size() - 1;
320                         
321                         for (String labelAttr : dynamicLabelRuleAlgorithms) {
322                                 // if the rule algorithm as a label means it is a compound
323                                 if (dynamicFieldOneRuleAlgorithms.get(index).toString().equals(labelAttr)) {
324                                         ApplyType decisionApply = new ApplyType();
325
326                                         String selectedFunction = (String) dynamicFieldComboRuleAlgorithms.get(index).toString();
327                                         String value1 = (String) dynamicFieldOneRuleAlgorithms.get(index).toString();
328                                         String value2 = dynamicFieldTwoRuleAlgorithms.get(index).toString();
329                                         decisionApply.setFunctionId(dropDownMap.get(selectedFunction));
330                                         decisionApply.getExpression().add(new ObjectFactory().createApply(getInnerDecisionApply(value1)));
331                                         decisionApply.getExpression().add(new ObjectFactory().createApply(getInnerDecisionApply(value2)));
332                                         condition.setExpression(new ObjectFactory().createApply(decisionApply));
333                                         isCompound = true;
334                                 }
335
336                                 // if rule algorithm not a compound
337                                 if (!isCompound) {
338                                         condition.setExpression(new ObjectFactory().createApply(getInnerDecisionApply(dynamicLabelRuleAlgorithms.get(index).toString())));
339                                 }
340                         }
341                         if (!permitRule) {
342                                 ApplyType notOuterApply = new ApplyType();
343                                 notOuterApply.setFunctionId(FUNCTION_NOT);
344                                 notOuterApply.getExpression().add(condition.getExpression());
345                                 condition.setExpression(new ObjectFactory().createApply(notOuterApply));
346                         }
347                         rule.setCondition(condition);
348                         allOfInRule.getMatch().add(accessMatch);
349
350                         AnyOfType anyOfInRule = new AnyOfType();
351                         anyOfInRule.getAllOf().add(allOfInRule);
352
353                         TargetType targetInRule = new TargetType();
354                         targetInRule.getAnyOf().add(anyOfInRule);
355
356                         rule.setTarget(targetInRule);
357
358                         decisionPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
359                         policyAdapter.setPolicyData(decisionPolicy);
360                         
361                 } else {
362                         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Unsupported data object."+ policyAdapter.getData().getClass().getCanonicalName());
363                 }
364
365         }
366         
367         // if compound setting the inner apply here
368         protected ApplyType getInnerDecisionApply(String value1Label) {
369                 ApplyType decisionApply = new ApplyType();
370                 int index = 0;
371                 // check the index for the label.
372                 for (String labelAttr : dynamicLabelRuleAlgorithms) {
373                         if (labelAttr.equals(value1Label)) {
374                                 String value1 = (String) dynamicFieldOneRuleAlgorithms.get(index).toString();
375                                 populateDataTypeList(value1);
376
377                                 // check if the row contains label again
378                                 for (String labelValue : dynamicLabelRuleAlgorithms) {
379                                         if (labelValue.equals(value1)) {
380                                                 return getCompoundDecisionApply(index);
381                                         }
382                                 }
383
384                                 // Getting the values from the form.
385                                 String functionKey = (String) dynamicFieldComboRuleAlgorithms.get(index).toString();
386                                 String value2 = dynamicFieldTwoRuleAlgorithms.get(index).toString();
387                                 decisionApply.setFunctionId(dropDownMap.get(functionKey));
388                                 // if two text field are rule attributes.
389                                 if ((value1.contains(RULE_VARIABLE)) && (value2.contains(RULE_VARIABLE))) {
390                                         ApplyType innerDecisionApply1 = new ApplyType();
391                                         ApplyType       innerDecisionApply2      = new  ApplyType();                                                      
392                                         AttributeDesignatorType  attributeDesignator1     = new AttributeDesignatorType();                                                               
393                                         AttributeDesignatorType  attributeDesignator2     = new AttributeDesignatorType();                                                                                       
394                                         //If selected function is Integer function set integer functionID
395                                         if(functionKey.toLowerCase().contains("integer")){
396                                                 innerDecisionApply1.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY );
397                                                 innerDecisionApply2.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
398                                                 attributeDesignator1.setDataType(INTEGER_DATATYPE);
399                                                 attributeDesignator2.setDataType(INTEGER_DATATYPE);
400                                         } else{
401                                                 //If selected function is not a Integer function set String functionID
402                                                 innerDecisionApply1.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
403                                                 innerDecisionApply2.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
404                                                 attributeDesignator1.setDataType(STRING_DATATYPE);
405                                                 attributeDesignator2.setDataType(STRING_DATATYPE);
406                                         }
407                                         attributeDesignator1.setCategory(CATEGORY_RESOURCE);
408                                         attributeDesignator2.setCategory(CATEGORY_RESOURCE);
409                                         //Here set actual field values
410                                         attributeDesignator1.setAttributeId(value1.  contains("resource:")?value1.substring( 9):value1.substring(8));
411                                         attributeDesignator2.setAttributeId(value1.  contains("resource:")?value1.substring( 9):value1.substring(8));                                                    
412                                         innerDecisionApply1.getExpression().add(new ObjectFactory().createAttributeDesignator( attributeDesignator1));
413                                         innerDecisionApply2.getExpression().add(new ObjectFactory().createAttributeDesignator( attributeDesignator2));                                                    
414                                         decisionApply.getExpression().add(new ObjectFactory().createApply(innerDecisionApply1));
415                                         decisionApply.getExpression().add(new ObjectFactory().createApply(innerDecisionApply2));                                                          
416                                 } else {
417                                         // if either of one text field is rule attribute.
418                                         if (!value1.startsWith("S_")) {
419                                                 ApplyType innerDecisionApply = new ApplyType();
420                                                 AttributeDesignatorType attributeDesignator = new AttributeDesignatorType();
421                                                 AttributeValueType decisionConditionAttributeValue = new AttributeValueType();
422
423                                                 if (functionKey.toLowerCase().contains("integer")) {
424                                                         innerDecisionApply.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
425                                                         decisionConditionAttributeValue.setDataType(INTEGER_DATATYPE);
426                                                         attributeDesignator.setDataType(INTEGER_DATATYPE);
427                                                 } else {
428                                                         innerDecisionApply.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
429                                                         decisionConditionAttributeValue.setDataType(STRING_DATATYPE);
430                                                         attributeDesignator.setDataType(STRING_DATATYPE);
431                                                 }
432
433                                                 String attributeId = null;
434                                                 String attributeValue = null;
435
436                                                 // Find which textField has rule attribute and set it as
437                                                 // attributeId and the other as attributeValue.
438                                                 attributeId = value1;
439                                                 attributeValue = value2;
440                         
441                                                 if (attributeId != null) {
442                                                         attributeDesignator.setCategory(CATEGORY_RESOURCE);
443                                                         attributeDesignator.setAttributeId(attributeId);
444                                                 }
445                                                 decisionConditionAttributeValue.getContent().add(attributeValue);
446                                                 innerDecisionApply.getExpression().add(new ObjectFactory().createAttributeDesignator(attributeDesignator));
447                                                 decisionApply.getExpression().add(new ObjectFactory().createAttributeValue(decisionConditionAttributeValue));
448                                                 decisionApply.getExpression().add(new ObjectFactory().createApply(innerDecisionApply));
449                                         } else {
450                                                 value1 = value1.substring(2, value1.length());
451                                                 VariableReferenceType variableReferenceType = new VariableReferenceType();
452                                                 variableReferenceType.setVariableId(value1);
453                                                 
454                                                 String dataType = dataTypeList.get(index);
455
456                                                 AttributeValueType decisionConditionAttributeValue = new AttributeValueType();
457                                                 decisionConditionAttributeValue.setDataType(dataType);
458                                                 decisionConditionAttributeValue.getContent().add(value2);
459                                                 decisionApply.getExpression().add(new ObjectFactory().createVariableReference(variableReferenceType));
460                                                 decisionApply.getExpression().add(new ObjectFactory().createAttributeValue(decisionConditionAttributeValue));
461                                         }
462                                 }
463                         }
464                         index++;
465                 }
466                 return decisionApply;
467         }
468
469         // if the rule algorithm is multiple compound one setting the apply
470         protected ApplyType getCompoundDecisionApply(int index) {
471                 ApplyType decisionApply = new ApplyType();
472                 String selectedFunction = dynamicFieldComboRuleAlgorithms.get(index).toString();
473                 String value1 = dynamicFieldOneRuleAlgorithms.get(index).toString();
474                 String value2 = dynamicFieldTwoRuleAlgorithms.get(index).toString();
475                 decisionApply.setFunctionId(dropDownMap.get(selectedFunction));
476                 decisionApply.getExpression().add(new ObjectFactory().createApply(getInnerDecisionApply(value1)));
477                 decisionApply.getExpression().add(new ObjectFactory().createApply(getInnerDecisionApply(value2)));
478                 return decisionApply;
479         }
480         
481         private VariableDefinitionType createDynamicVariable(String key, String value, String dataType) {
482                 VariableDefinitionType dynamicVariable = new VariableDefinitionType();
483                 AttributeValueType dynamicAttributeValue = new AttributeValueType();
484
485                 dynamicAttributeValue.setDataType(dataType);
486                 dynamicAttributeValue.getContent().add(value);
487
488                 dynamicVariable.setVariableId(key);
489                 dynamicVariable.setExpression(new ObjectFactory().createAttributeValue(dynamicAttributeValue));
490
491                 return dynamicVariable;
492
493         }       
494         
495         private void populateDataTypeList(String value1) {
496                 
497                 ///String value1 = dynamicFieldDecisionOneRuleAlgorithms.get(index).getValue().toString();
498                 String dataType = null;
499
500                 if(value1.contains("S_")) {
501                         value1 = value1.substring(2, value1.length());
502                         DecisionSettings decisionSettings = findDecisionSettingsBySettingId(value1);
503                         if (decisionSettings != null && decisionSettings.getDatatypeBean().getShortName().equals("string")) {
504                                 dataType = STRING_DATATYPE;
505                         } else if (decisionSettings != null && decisionSettings.getDatatypeBean().getShortName().equals("boolean")) {
506                                 dataType = BOOLEAN_DATATYPE;
507                         } else {
508                                 dataType = INTEGER_DATATYPE;
509                         }
510                 } else {
511                         dataType = "OTHER";
512                 }
513
514                 dataTypeList.add(dataType);
515         }
516         
517         private Map<String,String> createDropDownMap(){
518                 JPAUtils jpaUtils = null;
519                 try {
520                         jpaUtils = JPAUtils.getJPAUtilsInstance(XACMLPapServlet.getEmf());
521                 } catch (Exception e) {
522                         e.printStackTrace();
523                 }
524                 Map<Datatype, List<FunctionDefinition>> functionMap = jpaUtils.getFunctionDatatypeMap();
525                 Map<String, String> dropDownMap = new HashMap<String, String>();
526                 for (Datatype id : functionMap.keySet()) {
527                         List<FunctionDefinition> functionDefinitions = (List<FunctionDefinition>) functionMap
528                                         .get(id);
529                         for (FunctionDefinition functionDef : functionDefinitions) {
530                                 dropDownMap.put(functionDef.getShortname(),functionDef.getXacmlid());
531                         }
532                 }
533                 
534                 return dropDownMap;
535         }
536         
537         private String getDataType(String key) {
538                 
539                 DecisionSettings decisionSettings = findDecisionSettingsBySettingId(key);
540                 String dataType = null;
541                 
542                 if (decisionSettings != null && decisionSettings.getDatatypeBean().getShortName().equals("string")) {
543                         dataType = STRING_DATATYPE;
544                 } else if (decisionSettings != null && decisionSettings.getDatatypeBean().getShortName().equals("boolean")) {
545                         dataType = BOOLEAN_DATATYPE;
546                 } else {
547                         dataType = INTEGER_DATATYPE;
548                 }
549                 
550                 return dataType;
551         }
552
553         @Override
554         public Object getCorrectPolicyDataObject() {
555                 return policyAdapter.getData();
556         }
557         
558 }