Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / components / DecisionPolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2019 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.onap.policy.pap.xacml.rest.components;
22
23 import com.att.research.xacml.api.XACML3;
24 import com.att.research.xacml.api.pap.PAPException;
25 import com.att.research.xacml.std.IdentifierImpl;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.File;
29 import java.io.IOException;
30 import java.net.URI;
31 import java.net.URISyntaxException;
32 import java.nio.charset.StandardCharsets;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.HashMap;
39 import java.util.LinkedList;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.UUID;
43
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
54 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
55 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
56 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
57 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
58 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
59 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
60 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableDefinitionType;
61 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableReferenceType;
62
63 import org.apache.commons.lang3.StringEscapeUtils;
64 import org.onap.policy.common.logging.eelf.MessageCodes;
65 import org.onap.policy.common.logging.eelf.PolicyLogger;
66 import org.onap.policy.common.logging.flexlogger.FlexLogger;
67 import org.onap.policy.common.logging.flexlogger.Logger;
68 import org.onap.policy.controlloop.policy.builder.BuilderException;
69 import org.onap.policy.controlloop.policy.builder.Results;
70 import org.onap.policy.controlloop.policy.guard.Constraint;
71 import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
72 import org.onap.policy.controlloop.policy.guard.Guard;
73 import org.onap.policy.controlloop.policy.guard.GuardPolicy;
74 import org.onap.policy.controlloop.policy.guard.MatchParameters;
75 import org.onap.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
76 import org.onap.policy.rest.adapter.PolicyRestAdapter;
77 import org.onap.policy.rest.dao.CommonClassDao;
78 import org.onap.policy.rest.jpa.DecisionSettings;
79 import org.onap.policy.rest.jpa.FunctionDefinition;
80 import org.onap.policy.utils.PolicyUtils;
81 import org.onap.policy.xacml.api.XACMLErrorConstants;
82 import org.onap.policy.xacml.std.pip.engines.aaf.AAFEngine;
83 import org.onap.policy.xacml.util.XACMLPolicyScanner;
84 import org.springframework.beans.factory.annotation.Autowired;
85 import org.springframework.stereotype.Component;
86
87 @Component
88 public class DecisionPolicy extends Policy {
89
90     private static final Logger LOGGER = FlexLogger.getLogger(DecisionPolicy.class);
91
92     public static final String FUNCTION_NOT = "urn:oasis:names:tc:xacml:1.0:function:not";
93     private static final String AAFPROVIDER = "AAF";
94     public static final String GUARD_YAML = "GUARD_YAML";
95     public static final String GUARD_BL_YAML = "GUARD_BL_YAML";
96     public static final String GUARD_MIN_MAX = "GUARD_MIN_MAX";
97     public static final String RAINY_DAY = "Rainy_Day";
98     public static final String MS_MODEL = "MicroService_Model";
99     private static final String XACML_GUARD_TEMPLATE = "Decision_GuardPolicyTemplate.xml";
100     private static final String XACML_BLGUARD_TEMPLATE = "Decision_GuardBLPolicyTemplate.xml";
101     private static final String XACML_GUARD_MIN_MAX_TEMPLATE = "Decision_GuardMinMaxPolicyTemplate.xml";
102
103     private static final String ONAPNAME = "ONAPName";
104     private static final String POLICY_NAME = "PolicyName";
105     private static final String DESCRIPTION = "description";
106
107     List<String> dynamicLabelRuleAlgorithms = new LinkedList<>();
108     List<String> dynamicFieldComboRuleAlgorithms = new LinkedList<>();
109     List<String> dynamicFieldOneRuleAlgorithms = new LinkedList<>();
110     List<String> dynamicFieldTwoRuleAlgorithms = new LinkedList<>();
111     List<String> dataTypeList = new LinkedList<>();
112
113     private static CommonClassDao commonClassDao;
114
115     public DecisionPolicy() {
116         super();
117     }
118
119     @Autowired
120     public DecisionPolicy(CommonClassDao commonClassDao) {
121         DecisionPolicy.commonClassDao = commonClassDao;
122     }
123
124     public DecisionPolicy(PolicyRestAdapter policyAdapter) {
125         this.policyAdapter = policyAdapter;
126         this.commonClassDao = commonClassDao;
127     }
128
129     @Override
130     public Map<String, String> savePolicies() throws PAPException {
131
132         Map<String, String> successMap = new HashMap<>();
133         if (isPolicyExists()) {
134             successMap.put("EXISTS", "This Policy already exist on the PAP");
135             return successMap;
136         }
137
138         if (!isPreparedToSave()) {
139             // Prep and configure the policy for saving
140             prepareToSave();
141         }
142
143         // Until here we prepared the data and here calling the method to create xml.
144         Path newPolicyPath = null;
145         newPolicyPath = Paths.get(policyAdapter.getNewFileName());
146
147         successMap = createPolicy(newPolicyPath, getCorrectPolicyDataObject());
148         return successMap;
149     }
150
151     /**
152      * Scan the Raw Policy data and set to PolicyAdapter.
153      */
154     private void readRawPolicyData() {
155         Object policy;
156         if ("API".equalsIgnoreCase(policyAdapter.getApiflag())) {
157             policy = XACMLPolicyScanner.readPolicy(new ByteArrayInputStream(
158                     StringEscapeUtils.unescapeXml(policyAdapter.getRawXacmlPolicy()).getBytes(StandardCharsets.UTF_8)));
159         } else {
160             policy = XACMLPolicyScanner.readPolicy(
161                     new ByteArrayInputStream(policyAdapter.getRawXacmlPolicy().getBytes(StandardCharsets.UTF_8)));
162         }
163         String policyRawDesc;
164         if (policy instanceof PolicySetType) {
165             policyRawDesc = ((PolicySetType) policy).getDescription() + "@#RuleProvider@#Decision_Raw@#RuleProvider@#";
166             ((PolicySetType) policy).setDescription(policyRawDesc);
167         } else {
168             policyRawDesc = ((PolicyType) policy).getDescription() + "@#RuleProvider@#Decision_Raw@#RuleProvider@#";
169             ((PolicyType) policy).setDescription(policyRawDesc);
170         }
171         policyAdapter.setPolicyData(policy);
172         policyAdapter.setData(policy);
173         setPreparedToSave(true);
174
175     }
176
177     // This is the method for preparing the policy for saving. We have broken it out
178     // separately because the fully configured policy is used for multiple things
179     @Override
180     public boolean prepareToSave() throws PAPException {
181
182         if (isPreparedToSave()) {
183             // we have already done this
184             return true;
185         }
186
187         int version = 0;
188         String policyID = policyAdapter.getPolicyID();
189         version = policyAdapter.getHighestVersion();
190
191         if ("Raw".equals(policyAdapter.getRuleProvider())) {
192             readRawPolicyData();
193             return true;
194         }
195         // Create the Instance for pojo, PolicyType object is used in marshalling.
196         if ("Decision".equals(policyAdapter.getPolicyType())) {
197             PolicyType policyConfig = new PolicyType();
198
199             policyConfig.setVersion(Integer.toString(version));
200             policyConfig.setPolicyId(policyID);
201             policyConfig.setTarget(new TargetType());
202             policyAdapter.setData(policyConfig);
203         }
204         policyName = policyAdapter.getNewFileName();
205
206         if (policyAdapter.getRuleProvider().equals(GUARD_YAML) || policyAdapter.getRuleProvider().equals(GUARD_BL_YAML)
207                 || policyAdapter.getRuleProvider().equals(GUARD_MIN_MAX)) {
208
209             Map<String, String> yamlParams = new HashMap<>();
210             String blackListEntryType =
211                     policyAdapter.getBlackListEntryType() != null ? policyAdapter.getBlackListEntryType()
212                             : "Use Manual Entry";
213             String description = policyAdapter.getPolicyDescription() != null ? policyAdapter.getPolicyDescription()
214                     : "YAML Guard Policy";
215             yamlParams.put(DESCRIPTION, description + "@blEntry@" + blackListEntryType + "@blEntry@");
216             String fileName = policyAdapter.getNewFileName();
217             String name = fileName.substring(fileName.lastIndexOf('\\') + 1, fileName.length());
218             if ((name == null) || ("".equals(name))) {
219                 name = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.length());
220             }
221             yamlParams.put(POLICY_NAME, name);
222             yamlParams.put(ONAPNAME, policyAdapter.getOnapName());
223             Map<String, String> params = policyAdapter.getDynamicFieldConfigAttributes();
224             yamlParams.putAll(params);
225             // Call YAML to XACML
226             try {
227                 PolicyType decisionPolicy = getGuardPolicy(yamlParams, policyAdapter.getRuleProvider());
228                 decisionPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId());
229                 decisionPolicy.setVersion(Integer.toString(version));
230                 policyAdapter.setPolicyData(decisionPolicy);
231                 policyAdapter.setData(decisionPolicy);
232             } catch (BuilderException e) {
233                 LOGGER.error(e);
234                 throw new PAPException(e);
235             }
236         } else if (policyAdapter.getData() != null) {
237             PolicyType decisionPolicy = (PolicyType) policyAdapter.getData();
238
239             decisionPolicy.setDescription(policyAdapter.getPolicyDescription());
240
241             decisionPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId());
242             AllOfType allOfOne = new AllOfType();
243             String fileName = policyAdapter.getNewFileName();
244             String name = fileName.substring(fileName.lastIndexOf('\\') + 1, fileName.length());
245             if ((name == null) || ("".equals(name))) {
246                 name = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.length());
247             }
248             allOfOne.getMatch().add(createMatch(POLICY_NAME, name));
249
250             AllOfType allOf = new AllOfType();
251
252             // Match for Onap
253             allOf.getMatch().add(createMatch(ONAPNAME, (policyAdapter.getOnapName())));
254
255             Map<String, String> dynamicFieldComponentAttributes = policyAdapter.getDynamicFieldConfigAttributes();
256             if (policyAdapter.getRuleProvider() != null && policyAdapter.getRuleProvider().equals(AAFPROVIDER)) {
257                 dynamicFieldComponentAttributes = new HashMap<>();
258             }
259
260             // If there is any dynamic field attributes create the matches here
261             for (String keyField : dynamicFieldComponentAttributes.keySet()) {
262                 String key = keyField;
263                 String value = dynamicFieldComponentAttributes.get(key);
264                 MatchType dynamicMatch = createDynamicMatch(key, value);
265                 allOf.getMatch().add(dynamicMatch);
266             }
267
268             AnyOfType anyOf = new AnyOfType();
269             anyOf.getAllOf().add(allOfOne);
270             anyOf.getAllOf().add(allOf);
271
272             TargetType target = new TargetType();
273             target.getAnyOf().add(anyOf);
274             decisionPolicy.setTarget(target);
275
276             Map<String, String> dynamicFieldDecisionSettings = policyAdapter.getDynamicSettingsMap();
277             if (policyAdapter.getRuleProvider() != null && (policyAdapter.getRuleProvider().equals(AAFPROVIDER)
278                     || policyAdapter.getRuleProvider().equals(RAINY_DAY))) {
279                 dynamicFieldDecisionSettings = new HashMap<>();
280             }
281
282             // settings are dynamic so check how many rows are added and add all
283             for (String keyField : dynamicFieldDecisionSettings.keySet()) {
284                 String key = keyField;
285                 String value = dynamicFieldDecisionSettings.get(key);
286                 String dataType = getDataType(key);
287                 VariableDefinitionType dynamicVariable = createDynamicVariable(key, value, dataType);
288                 decisionPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(dynamicVariable);
289             }
290
291             Map<String, String> dynamicFieldTreatmentAttributes = policyAdapter.getRainydayMap();
292             if (policyAdapter.getRuleProvider().equals(RAINY_DAY)) {
293                 for (String keyField : dynamicFieldTreatmentAttributes.keySet()) {
294                     String errorcode = keyField;
295                     String treatment = dynamicFieldTreatmentAttributes.get(errorcode);
296                     createRainydayRule(decisionPolicy, errorcode, treatment, true);
297                 }
298             } else {
299                 createRule(decisionPolicy, true);
300                 createRule(decisionPolicy, false);
301             }
302
303         }
304         setPreparedToSave(true);
305         return true;
306     }
307
308     public PolicyType getGuardPolicy(Map<String, String> yamlParams, String ruleProvider) throws BuilderException {
309         try {
310             ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
311             MatchParameters matchParameters = new MatchParameters(yamlParams.get("actor"), yamlParams.get("recipe"));
312             matchParameters.setControlLoopName(yamlParams.get("clname"));
313             if (yamlParams.containsKey("targets")) {
314                 String targetString = yamlParams.get("targets");
315                 List<String> targets = null;
316                 if (targetString != null && !targetString.isEmpty()) {
317                     if (targetString.contains(",")) {
318                         targets = Arrays.asList(targetString.split(","));
319                     } else {
320                         targets = new ArrayList<>();
321                         targets.add(targetString);
322                     }
323                 }
324                 matchParameters.setTargets(targets);
325             }
326             GuardPolicy policy1 = new GuardPolicy(
327                     (policyAdapter.getUuid() != null ? policyAdapter.getUuid() : UUID.randomUUID().toString()),
328                     yamlParams.get(POLICY_NAME), yamlParams.get(DESCRIPTION), matchParameters);
329             builder = builder.addGuardPolicy(policy1);
330             Map<String, String> activeTimeRange = new HashMap<>();
331             activeTimeRange.put("start", yamlParams.get("guardActiveStart"));
332             activeTimeRange.put("end", yamlParams.get("guardActiveEnd"));
333             String blackListString = yamlParams.get("blackList");
334             List<String> blackList = null;
335             if (blackListString != null && !blackListString.trim().isEmpty()) {
336                 if (blackListString.contains(",")) {
337                     blackList = Arrays.asList(blackListString.split(","));
338                 } else {
339                     blackList = new ArrayList<>();
340                     blackList.add(blackListString);
341                 }
342             }
343             if (yamlParams.containsKey("appendBlackList")) {
344                 String appendBlackListString = yamlParams.get("appendBlackList");
345                 List<String> appendBlackList = null;
346                 if (appendBlackListString != null && !appendBlackListString.trim().isEmpty()) {
347                     appendBlackList = Arrays.asList(appendBlackListString.split(","));
348                     for (int i = 0; i < appendBlackList.size(); i++) {
349                         blackList.remove(appendBlackList.get(i));
350                     }
351                 }
352             }
353             File templateFile;
354             Path xacmlTemplatePath;
355             ClassLoader classLoader = getClass().getClassLoader();
356             Constraint cons = new Constraint();
357             switch (ruleProvider) {
358                 case GUARD_BL_YAML:
359                     templateFile = new File(classLoader.getResource(XACML_BLGUARD_TEMPLATE).getFile());
360                     xacmlTemplatePath = templateFile.toPath();
361                     cons.setActive_time_range(activeTimeRange);
362                     if (blackList == null || blackList.isEmpty()) {
363                         throw new BuilderException("blackList is required");
364                     }
365                     cons.setBlacklist(blackList);
366                     break;
367                 case GUARD_MIN_MAX:
368                     templateFile = new File(classLoader.getResource(XACML_GUARD_MIN_MAX_TEMPLATE).getFile());
369                     xacmlTemplatePath = templateFile.toPath();
370                     cons = new Constraint(Integer.parseInt(yamlParams.get("min")),
371                             Integer.parseInt(yamlParams.get("max")), activeTimeRange);
372                     break;
373                 default:
374                     templateFile = new File(classLoader.getResource(XACML_GUARD_TEMPLATE).getFile());
375                     xacmlTemplatePath = templateFile.toPath();
376                     Map<String, String> timeWindow = new HashMap<>();
377                     if (!PolicyUtils.isInteger(yamlParams.get("timeWindow"))) {
378                         throw new BuilderException("time window is not in Integer format.");
379                     }
380                     String timeUnits = yamlParams.get("timeUnits");
381                     if (timeUnits == null
382                             || !("minute".equalsIgnoreCase(timeUnits) || "hour".equalsIgnoreCase(timeUnits)
383                                     || "day".equalsIgnoreCase(timeUnits) || "week".equalsIgnoreCase(timeUnits)
384                                     || "month".equalsIgnoreCase(timeUnits) || "year".equalsIgnoreCase(timeUnits))) {
385                         throw new BuilderException("time Units is not in proper format.");
386                     }
387                     timeWindow.put("value", yamlParams.get("timeWindow"));
388                     timeWindow.put("units", yamlParams.get("timeUnits"));
389                     cons = new Constraint(Integer.parseInt(yamlParams.get("limit")), timeWindow, activeTimeRange);
390                     break;
391             }
392
393             builder = builder.addLimitConstraint(policy1.getId(), cons);
394             // Build the specification
395             Results results = builder.buildSpecification();
396             // YAML TO XACML
397             ControlLoopGuard yamlGuardObject = SafePolicyBuilder.loadYamlGuard(results.getSpecification());
398             String xacmlTemplateContent;
399             try {
400                 xacmlTemplateContent = new String(Files.readAllBytes(xacmlTemplatePath));
401                 HashMap<String, String> yamlSpecs = new HashMap<>();
402                 yamlSpecs.put(POLICY_NAME, yamlParams.get(POLICY_NAME));
403                 yamlSpecs.put(DESCRIPTION, yamlParams.get(DESCRIPTION));
404                 yamlSpecs.put(ONAPNAME, yamlParams.get(ONAPNAME));
405                 GuardPolicy guard = yamlGuardObject.getGuards().get(0);
406
407                 yamlSpecs.put("actor", guard.getMatch_parameters().getActor());
408                 yamlSpecs.put("recipe", guard.getMatch_parameters().getRecipe());
409                 yamlSpecs.put("clname", guard.getMatch_parameters().getControlLoopName());
410
411                 Constraint constraints = guard.getLimit_constraints().get(0);
412                 if (constraints.getFreq_limit_per_target() != null) {
413                     yamlSpecs.put("limit", constraints.getFreq_limit_per_target().toString());
414                 }
415                 if (constraints.getTime_window() != null) {
416                     yamlSpecs.put("twValue", constraints.getTime_window().get("value"));
417                     yamlSpecs.put("twUnits", constraints.getTime_window().get("units"));
418                 }
419
420                 if (constraints.getMaxVnfCount() != null) {
421                     yamlSpecs.put("max", constraints.getMaxVnfCount().toString());
422                 }
423                 if (constraints.getMinVnfCount() != null) {
424                     yamlSpecs.put("min", constraints.getMinVnfCount().toString());
425                 }
426
427                 yamlSpecs.put("guardActiveStart", constraints.getActive_time_range().get("start"));
428                 yamlSpecs.put("guardActiveEnd", constraints.getActive_time_range().get("end"));
429                 String xacmlPolicyContent = SafePolicyBuilder.generateXacmlGuard(xacmlTemplateContent, yamlSpecs,
430                         constraints.getBlacklist(), guard.getMatch_parameters().getTargets());
431
432                 // Convert the Policy into Stream input to Policy Adapter.
433                 Object policy = XACMLPolicyScanner
434                         .readPolicy(new ByteArrayInputStream(xacmlPolicyContent.getBytes(StandardCharsets.UTF_8)));
435
436                 return (PolicyType) policy;
437             } catch (IOException e) {
438                 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage(),
439                         e);
440             }
441         } catch (BuilderException e) {
442             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage(), e);
443             throw e;
444         }
445         return null;
446     }
447
448     private DecisionSettings findDecisionSettingsBySettingId(String settingId) {
449         return (DecisionSettings) commonClassDao.getEntityItem(DecisionSettings.class, "xacml_id", settingId);
450     }
451
452     private void createRule(PolicyType decisionPolicy, boolean permitRule) {
453         RuleType rule = new RuleType();
454
455         rule.setRuleId(policyAdapter.getRuleID());
456
457         if (permitRule) {
458             rule.setEffect(EffectType.PERMIT);
459         } else {
460             rule.setEffect(EffectType.DENY);
461         }
462         rule.setTarget(new TargetType());
463
464         // Create Target in Rule
465         AllOfType allOfInRule = new AllOfType();
466
467         // Creating match for ACCESS in rule target
468         MatchType accessMatch = new MatchType();
469         AttributeValueType accessAttributeValue = new AttributeValueType();
470         accessAttributeValue.setDataType(STRING_DATATYPE);
471         accessAttributeValue.getContent().add("DECIDE");
472         accessMatch.setAttributeValue(accessAttributeValue);
473         AttributeDesignatorType accessAttributeDesignator = new AttributeDesignatorType();
474         URI accessURI = null;
475         try {
476             accessURI = new URI(ACTION_ID);
477         } catch (URISyntaxException e) {
478             PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "DecisionPolicy", "Exception creating ACCESS URI");
479         }
480         accessAttributeDesignator.setCategory(CATEGORY_ACTION);
481         accessAttributeDesignator.setDataType(STRING_DATATYPE);
482         accessAttributeDesignator.setAttributeId(new IdentifierImpl(accessURI).stringValue());
483         accessMatch.setAttributeDesignator(accessAttributeDesignator);
484         accessMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE);
485
486         dynamicLabelRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmLabels();
487         dynamicFieldComboRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmCombo();
488         dynamicFieldOneRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmField1();
489         dynamicFieldTwoRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmField2();
490
491         if (policyAdapter.getRuleProvider() != null && policyAdapter.getRuleProvider().equals(AAFPROVIDER)) {
492             // Values for AAF Provider are here for XML Creation.
493             ConditionType condition = new ConditionType();
494             ApplyType decisionApply = new ApplyType();
495
496             AttributeValueType value1 = new AttributeValueType();
497             value1.setDataType(BOOLEAN_DATATYPE);
498             value1.getContent().add("true");
499
500             AttributeDesignatorType value2 = new AttributeDesignatorType();
501             value2.setAttributeId(AAFEngine.AAF_RESULT);
502             value2.setCategory(CATEGORY_RESOURCE);
503             value2.setDataType(BOOLEAN_DATATYPE);
504             value2.setMustBePresent(false);
505
506             ApplyType innerDecisionApply = new ApplyType();
507             innerDecisionApply.setFunctionId(FUNCTION_BOOLEAN_ONE_AND_ONLY);
508             innerDecisionApply.getExpression().add(new ObjectFactory().createAttributeDesignator(value2));
509
510             decisionApply.setFunctionId(XACML3.ID_FUNCTION_BOOLEAN_EQUAL.stringValue());
511             decisionApply.getExpression().add(new ObjectFactory().createAttributeValue(value1));
512             decisionApply.getExpression().add(new ObjectFactory().createApply(innerDecisionApply));
513             condition.setExpression(new ObjectFactory().createApply(decisionApply));
514             if (!permitRule) {
515                 ApplyType notOuterApply = new ApplyType();
516                 notOuterApply.setFunctionId(FUNCTION_NOT);
517                 notOuterApply.getExpression().add(condition.getExpression());
518                 condition.setExpression(new ObjectFactory().createApply(notOuterApply));
519             }
520             rule.setCondition(condition);
521             allOfInRule.getMatch().add(accessMatch);
522
523             AnyOfType anyOfInRule = new AnyOfType();
524             anyOfInRule.getAllOf().add(allOfInRule);
525
526             TargetType targetInRule = new TargetType();
527             targetInRule.getAnyOf().add(anyOfInRule);
528
529             rule.setTarget(targetInRule);
530             if (!permitRule) {
531                 AdviceExpressionsType adviceExpressions = new AdviceExpressionsType();
532                 AdviceExpressionType adviceExpression = new AdviceExpressionType();
533                 adviceExpression.setAdviceId(AAFPROVIDER);
534                 adviceExpression.setAppliesTo(EffectType.DENY);
535                 AttributeAssignmentExpressionType assignment = new AttributeAssignmentExpressionType();
536                 assignment.setAttributeId("aaf.response");
537                 assignment.setCategory(CATEGORY_RESOURCE);
538                 AttributeDesignatorType value = new AttributeDesignatorType();
539                 value.setAttributeId(AAFEngine.AAF_RESPONSE);
540                 value.setCategory(CATEGORY_RESOURCE);
541                 value.setDataType(STRING_DATATYPE);
542                 value.setMustBePresent(false);
543                 assignment.setExpression(new ObjectFactory().createAttributeDesignator(value));
544                 adviceExpression.getAttributeAssignmentExpression().add(assignment);
545                 adviceExpressions.getAdviceExpression().add(adviceExpression);
546                 rule.setAdviceExpressions(adviceExpressions);
547             }
548             decisionPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
549             policyAdapter.setPolicyData(decisionPolicy);
550
551         } else if (dynamicLabelRuleAlgorithms != null && !dynamicLabelRuleAlgorithms.isEmpty()) {
552             boolean isCompound = false;
553             ConditionType condition = new ConditionType();
554             int index = dynamicFieldOneRuleAlgorithms.size() - 1;
555
556             for (String labelAttr : dynamicLabelRuleAlgorithms) {
557                 // if the rule algorithm as a label means it is a compound
558                 if (dynamicFieldOneRuleAlgorithms.get(index).equals(labelAttr)) {
559                     ApplyType decisionApply = new ApplyType();
560
561                     String selectedFunction = dynamicFieldComboRuleAlgorithms.get(index);
562                     String value1 = dynamicFieldOneRuleAlgorithms.get(index);
563                     String value2 = dynamicFieldTwoRuleAlgorithms.get(index);
564                     decisionApply.setFunctionId(getFunctionDefinitionId(selectedFunction));
565                     decisionApply.getExpression().add(new ObjectFactory().createApply(getInnerDecisionApply(value1)));
566                     decisionApply.getExpression().add(new ObjectFactory().createApply(getInnerDecisionApply(value2)));
567                     condition.setExpression(new ObjectFactory().createApply(decisionApply));
568                     isCompound = true;
569                 }
570
571                 // if rule algorithm not a compound
572                 if (!isCompound) {
573                     condition.setExpression(new ObjectFactory()
574                             .createApply(getInnerDecisionApply(dynamicLabelRuleAlgorithms.get(index))));
575                 }
576             }
577             if (!permitRule) {
578                 ApplyType notOuterApply = new ApplyType();
579                 notOuterApply.setFunctionId(FUNCTION_NOT);
580                 notOuterApply.getExpression().add(condition.getExpression());
581                 condition.setExpression(new ObjectFactory().createApply(notOuterApply));
582             }
583             rule.setCondition(condition);
584             allOfInRule.getMatch().add(accessMatch);
585
586             AnyOfType anyOfInRule = new AnyOfType();
587             anyOfInRule.getAllOf().add(allOfInRule);
588
589             TargetType targetInRule = new TargetType();
590             targetInRule.getAnyOf().add(anyOfInRule);
591
592             rule.setTarget(targetInRule);
593
594             decisionPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
595             policyAdapter.setPolicyData(decisionPolicy);
596
597         } else {
598             PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Unsupported data object."
599                     + policyAdapter.getData().getClass().getCanonicalName());
600         }
601
602     }
603
604     private void createRainydayRule(PolicyType decisionPolicy, String errorcode, String treatment, boolean permitRule) {
605         RuleType rule = new RuleType();
606
607         rule.setRuleId(UUID.randomUUID().toString());
608
609         if (permitRule) {
610             rule.setEffect(EffectType.PERMIT);
611         } else {
612             rule.setEffect(EffectType.DENY);
613         }
614         rule.setTarget(new TargetType());
615
616         // Create Target in Rule
617         AllOfType allOfInRule = new AllOfType();
618
619         // Creating match for DECIDE in rule target
620         MatchType accessMatch = new MatchType();
621         AttributeValueType accessAttributeValue = new AttributeValueType();
622         accessAttributeValue.setDataType(STRING_DATATYPE);
623         accessAttributeValue.getContent().add("DECIDE");
624         accessMatch.setAttributeValue(accessAttributeValue);
625         AttributeDesignatorType accessAttributeDesignator = new AttributeDesignatorType();
626         URI accessURI = null;
627         try {
628             accessURI = new URI(ACTION_ID);
629         } catch (URISyntaxException e) {
630             PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "DecisionPolicy", "Exception creating ACCESS URI");
631         }
632         accessAttributeDesignator.setCategory(CATEGORY_ACTION);
633         accessAttributeDesignator.setDataType(STRING_DATATYPE);
634         accessAttributeDesignator.setAttributeId(new IdentifierImpl(accessURI).stringValue());
635         accessMatch.setAttributeDesignator(accessAttributeDesignator);
636         accessMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE);
637
638         allOfInRule.getMatch().add(accessMatch);
639
640         // Creating match for ErrorCode in rule target
641         MatchType errorcodeMatch = new MatchType();
642         AttributeValueType errorcodeAttributeValue = new AttributeValueType();
643         errorcodeAttributeValue.setDataType(STRING_DATATYPE);
644         errorcodeAttributeValue.getContent().add(errorcode);
645         errorcodeMatch.setAttributeValue(errorcodeAttributeValue);
646         AttributeDesignatorType errorcodeAttributeDesignator = new AttributeDesignatorType();
647         errorcodeAttributeDesignator.setCategory(CATEGORY_RESOURCE);
648         errorcodeAttributeDesignator.setDataType(STRING_DATATYPE);
649         errorcodeAttributeDesignator.setAttributeId("ErrorCode");
650         errorcodeMatch.setAttributeDesignator(errorcodeAttributeDesignator);
651         errorcodeMatch.setMatchId(FUNCTION_STRING_REGEXP_MATCH);
652
653         allOfInRule.getMatch().add(errorcodeMatch);
654
655         AnyOfType anyOfInRule = new AnyOfType();
656         anyOfInRule.getAllOf().add(allOfInRule);
657
658         TargetType targetInRule = new TargetType();
659         targetInRule.getAnyOf().add(anyOfInRule);
660
661         rule.setTarget(targetInRule);
662
663         AdviceExpressionsType adviceExpressions = new AdviceExpressionsType();
664         AdviceExpressionType adviceExpression = new AdviceExpressionType();
665         adviceExpression.setAdviceId(RAINY_DAY);
666         adviceExpression.setAppliesTo(EffectType.PERMIT);
667
668         AttributeAssignmentExpressionType assignment = new AttributeAssignmentExpressionType();
669         assignment.setAttributeId("treatment");
670         assignment.setCategory(CATEGORY_RESOURCE);
671
672         AttributeValueType treatmentAttributeValue = new AttributeValueType();
673         treatmentAttributeValue.setDataType(STRING_DATATYPE);
674         treatmentAttributeValue.getContent().add(treatment);
675         assignment.setExpression(new ObjectFactory().createAttributeValue(treatmentAttributeValue));
676
677         adviceExpression.getAttributeAssignmentExpression().add(assignment);
678         adviceExpressions.getAdviceExpression().add(adviceExpression);
679         rule.setAdviceExpressions(adviceExpressions);
680         decisionPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
681         policyAdapter.setPolicyData(decisionPolicy);
682
683     }
684
685     // if compound setting the inner apply here
686     protected ApplyType getInnerDecisionApply(String value1Label) {
687         ApplyType decisionApply = new ApplyType();
688         int index = 0;
689         // check the index for the label.
690         for (String labelAttr : dynamicLabelRuleAlgorithms) {
691             if (labelAttr.equals(value1Label)) {
692                 String attributeId = dynamicFieldOneRuleAlgorithms.get(index);
693                 populateDataTypeList(attributeId);
694
695                 // check if the row contains label again
696                 for (String labelValue : dynamicLabelRuleAlgorithms) {
697                     if (labelValue.equals(attributeId)) {
698                         return getCompoundDecisionApply(index);
699                     }
700                 }
701
702                 // Getting the values from the form.
703                 String functionKey = dynamicFieldComboRuleAlgorithms.get(index);
704                 String attributeValue = dynamicFieldTwoRuleAlgorithms.get(index);
705                 decisionApply.setFunctionId(getFunctionDefinitionId(functionKey));
706                 // if two text field are rule attributes.
707                 if ((attributeId.contains(RULE_VARIABLE)) && (attributeValue.contains(RULE_VARIABLE))) {
708                     applyTwoTextFieldRuleAttribute(decisionApply, attributeId, functionKey);
709                 } else {
710                     // if either of one text field is rule attribute.
711                     if (!attributeId.startsWith("S_")) {
712                         ApplyType innerDecisionApply = generateApplyTypeDataType(functionKey);
713                         AttributeDesignatorType attributeDesignator = generateAttributeDesignatorDataType(functionKey);
714                         AttributeValueType decisionConditionAttributeValue =
715                                 generateAttributeValueTypeDataType(functionKey);
716
717                         if (attributeId != null) {
718                             attributeDesignator.setCategory(CATEGORY_RESOURCE);
719                             attributeDesignator.setAttributeId(attributeId);
720                         }
721                         decisionConditionAttributeValue.getContent().add(attributeValue);
722                         innerDecisionApply.getExpression()
723                                 .add(new ObjectFactory().createAttributeDesignator(attributeDesignator));
724                         decisionApply.getExpression()
725                                 .add(new ObjectFactory().createAttributeValue(decisionConditionAttributeValue));
726                         decisionApply.getExpression().add(new ObjectFactory().createApply(innerDecisionApply));
727                     } else {
728                         attributeId = attributeId.substring(2, attributeId.length());
729                         VariableReferenceType variableReferenceType = new VariableReferenceType();
730                         variableReferenceType.setVariableId(attributeId);
731
732                         String dataType = dataTypeList.get(index);
733
734                         AttributeValueType decisionConditionAttributeValue = new AttributeValueType();
735                         decisionConditionAttributeValue.setDataType(dataType);
736                         decisionConditionAttributeValue.getContent().add(attributeValue);
737                         decisionApply.getExpression()
738                                 .add(new ObjectFactory().createVariableReference(variableReferenceType));
739                         decisionApply.getExpression()
740                                 .add(new ObjectFactory().createAttributeValue(decisionConditionAttributeValue));
741                     }
742                 }
743             }
744             index++;
745         }
746         return decisionApply;
747     }
748
749     // if the rule algorithm is multiple compound one setting the apply
750     protected ApplyType getCompoundDecisionApply(int index) {
751         ApplyType decisionApply = new ApplyType();
752         String selectedFunction = dynamicFieldComboRuleAlgorithms.get(index);
753         String value1 = dynamicFieldOneRuleAlgorithms.get(index);
754         String value2 = dynamicFieldTwoRuleAlgorithms.get(index);
755         decisionApply.setFunctionId(getFunctionDefinitionId(selectedFunction));
756         decisionApply.getExpression().add(new ObjectFactory().createApply(getInnerDecisionApply(value1)));
757         decisionApply.getExpression().add(new ObjectFactory().createApply(getInnerDecisionApply(value2)));
758         return decisionApply;
759     }
760
761     private VariableDefinitionType createDynamicVariable(String key, String value, String dataType) {
762         VariableDefinitionType dynamicVariable = new VariableDefinitionType();
763         AttributeValueType dynamicAttributeValue = new AttributeValueType();
764
765         dynamicAttributeValue.setDataType(dataType);
766         dynamicAttributeValue.getContent().add(value);
767
768         dynamicVariable.setVariableId(key);
769         dynamicVariable.setExpression(new ObjectFactory().createAttributeValue(dynamicAttributeValue));
770
771         return dynamicVariable;
772
773     }
774
775     private void populateDataTypeList(String value1) {
776         String dataType = null;
777         if (value1.contains("S_")) {
778             value1 = value1.substring(2, value1.length());
779             DecisionSettings decisionSettings = findDecisionSettingsBySettingId(value1.substring(2, value1.length()));
780             if (decisionSettings != null && "string".equals(decisionSettings.getDatatypeBean().getShortName())) {
781                 dataType = STRING_DATATYPE;
782             } else if (decisionSettings != null
783                     && "boolean".equals(decisionSettings.getDatatypeBean().getShortName())) {
784                 dataType = BOOLEAN_DATATYPE;
785             } else {
786                 dataType = INTEGER_DATATYPE;
787             }
788         } else {
789             dataType = "OTHER";
790         }
791
792         dataTypeList.add(dataType);
793     }
794
795     private String getDataType(String key) {
796
797         DecisionSettings decisionSettings = findDecisionSettingsBySettingId(key);
798         String dataType = null;
799
800         if (decisionSettings != null && "string".equals(decisionSettings.getDatatypeBean().getShortName())) {
801             dataType = STRING_DATATYPE;
802         } else if (decisionSettings != null && "boolean".equals(decisionSettings.getDatatypeBean().getShortName())) {
803             dataType = BOOLEAN_DATATYPE;
804         } else {
805             dataType = INTEGER_DATATYPE;
806         }
807
808         return dataType;
809     }
810
811     @Override
812     public Object getCorrectPolicyDataObject() {
813         return policyAdapter.getData();
814     }
815
816     public String getFunctionDefinitionId(String key) {
817         FunctionDefinition object =
818                 (FunctionDefinition) commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", key);
819         if (object != null) {
820             return object.getXacmlid();
821         }
822         return null;
823     }
824
825     private AttributeDesignatorType generateAttributeDesignatorDataType(String functionKey) {
826         AttributeDesignatorType attributeDesignator = new AttributeDesignatorType();
827         switch (functionKey.toLowerCase()) {
828             case "integer":
829                 attributeDesignator.setDataType(INTEGER_DATATYPE);
830                 break;
831             default:
832                 attributeDesignator.setDataType(STRING_DATATYPE);
833         }
834         return attributeDesignator;
835     }
836
837     private ApplyType generateApplyTypeDataType(String functionKey) {
838         ApplyType applyType = new ApplyType();
839         switch (functionKey.toLowerCase()) {
840             case "integer":
841                 applyType.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
842                 break;
843             default:
844                 applyType.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
845         }
846         return applyType;
847     }
848
849     private AttributeValueType generateAttributeValueTypeDataType(String functionKey) {
850         AttributeValueType applyType = new AttributeValueType();
851         switch (functionKey.toLowerCase()) {
852             case "integer":
853                 applyType.setDataType(INTEGER_DATATYPE);
854                 break;
855             default:
856                 applyType.setDataType(STRING_DATATYPE);
857         }
858         return applyType;
859     }
860
861     private void applyTwoTextFieldRuleAttribute(ApplyType decisionApply, String value1, String functionKey) {
862         decisionApply.getExpression()
863                 .add(new ObjectFactory().createApply(generateApplyTypeDataType(value1, functionKey)));
864         decisionApply.getExpression()
865                 .add(new ObjectFactory().createApply(generateApplyTypeDataType(value1, functionKey)));
866     }
867
868     private ApplyType generateApplyTypeDataType(String value1, String functionKey) {
869         ApplyType innerApply = generateApplyTypeDataType(functionKey);
870         AttributeDesignatorType attributeDesignator = generateAttributeDesignatorDataType(functionKey);
871
872         attributeDesignator.setCategory(CATEGORY_RESOURCE);
873         // Here set actual field values
874         attributeDesignator.setAttributeId(value1.contains("resource:") ? value1.substring(9) : value1.substring(8));
875         innerApply.getExpression().add(new ObjectFactory().createAttributeDesignator(attributeDesignator));
876         return innerApply;
877     }
878
879 }