6a0c92650f3c29d5dcc9768138040a39b3d07be3
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / components / CreateBrmsParamPolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2018 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 java.io.File;
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.net.URI;
27 import java.net.URISyntaxException;
28 import java.nio.charset.Charset;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Map.Entry;
38 import java.util.UUID;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
41
42 import javax.script.SimpleBindings;
43
44 import org.apache.commons.io.FilenameUtils;
45 import org.onap.policy.common.logging.eelf.MessageCodes;
46 import org.onap.policy.common.logging.eelf.PolicyLogger;
47 import org.onap.policy.common.logging.flexlogger.FlexLogger;
48 import org.onap.policy.common.logging.flexlogger.Logger;
49 import org.onap.policy.pap.xacml.rest.controller.BRMSDictionaryController;
50 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
51 import org.onap.policy.rest.adapter.PolicyRestAdapter;
52 import org.onap.policy.rest.jpa.BRMSParamTemplate;
53
54 import com.att.research.xacml.api.pap.PAPException;
55 import com.att.research.xacml.std.IdentifierImpl;
56
57 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
58 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
59 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
60 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
61 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
62 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
63 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
64 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
65 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
66 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
67 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
68 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
69 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
70
71 public class CreateBrmsParamPolicy extends Policy {
72
73     private static final Logger LOGGER = FlexLogger.getLogger(CreateBrmsParamPolicy.class);
74
75     public CreateBrmsParamPolicy() {
76         super();
77     }
78
79     public CreateBrmsParamPolicy(PolicyRestAdapter policyAdapter) {
80         this.policyAdapter = policyAdapter;
81         this.policyAdapter.setConfigType(policyAdapter.getConfigType());
82
83     }
84
85     public String expandConfigBody(String ruleContents, Map<String, String> brmsParamBody) {
86
87         Map<String, String> copyMap = new HashMap<>();
88         copyMap.putAll(brmsParamBody);
89         copyMap.put("policyName", policyName.substring(0, policyName.replace(".xml", "").lastIndexOf('.')));
90         copyMap.put("policyScope", policyAdapter.getDomainDir());
91         copyMap.put("policyVersion", policyAdapter.getHighestVersion().toString());
92         copyMap.put("unique", ("p" + policyName + UUID.randomUUID().toString()).replaceAll("[^A-Za-z0-9]", ""));
93
94         //Finding all the keys in the Map data-structure.
95         Iterator<String> iterator = copyMap.keySet().iterator();
96         Pattern p;
97         Matcher m;
98         while (iterator.hasNext()) {
99             //Converting the first character of the key into a lower case.
100             String input = iterator.next();
101             String output = Character.toLowerCase(input.charAt(0)) +
102                     (input.length() > 1 ? input.substring(1) : "");
103             //Searching for a pattern in the String using the key.
104             p = Pattern.compile("\\$\\{" + output + "\\}");
105             m = p.matcher(ruleContents);
106             //Replacing the value with the inputs provided by the user in the editor.
107             String finalInput = copyMap.get(input);
108             if (finalInput.contains("$")) {
109                 finalInput = finalInput.replace("$", "\\$");
110             }
111             ruleContents = m.replaceAll(finalInput);
112         }
113         return ruleContents;
114     }
115
116
117     // Utility to read json data from the existing file to a string
118     static String readFile(String path, Charset encoding) throws IOException {
119         byte[] encoded = Files.readAllBytes(Paths.get(path));
120         return new String(encoded, encoding);
121     }
122
123     // Saving the Configurations file at server location for config policy.
124     protected void saveConfigurations(String policyName, String ruleBody) {
125         if (policyName.endsWith(".xml")) {
126             policyName = policyName.substring(0, policyName.lastIndexOf(".xml"));
127         }
128         try (PrintWriter out = new PrintWriter(CONFIG_HOME + File.separator + policyName + ".txt")) {
129             String expandedBody = expandConfigBody(ruleBody, policyAdapter.getBrmsParamBody());
130             out.println(expandedBody);
131             policyAdapter.setJsonBody(expandedBody);
132             policyAdapter.setConfigBodyData(expandedBody);
133             out.close();
134         } catch (Exception e) {
135             PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "CreateBrmsParamPolicy",
136                     "Exception saving configuration file");
137         }
138     }
139
140
141     // Here we are adding the extension for the configurations file based on the
142     // config type selection for saving.
143     private String getConfigFile(String filename) {
144         filename = FilenameUtils.removeExtension(filename);
145         if (filename.endsWith(".txt")) {
146             filename = filename.substring(0, filename.length() - 3);
147         }
148
149         filename = filename + ".txt";
150         return filename;
151     }
152
153     // Validations for Config form
154     public boolean validateConfigForm() {
155
156         // Validating mandatory Fields.
157         isValidForm = true;
158         return isValidForm;
159
160     }
161
162     @Override
163     public Map<String, String> savePolicies() throws PAPException {
164
165         Map<String, String> successMap = new HashMap<>();
166         if (isPolicyExists()) {
167             successMap.put("EXISTS", "This Policy already exist on the PAP");
168             return successMap;
169         }
170
171         if (!isPreparedToSave()) {
172             prepareToSave();
173         }
174         // Until here we prepared the data and here calling the method to create
175         // xml.
176         Path newPolicyPath = null;
177         newPolicyPath = Paths.get(policyAdapter.getNewFileName());
178         successMap = createPolicy(newPolicyPath, getCorrectPolicyDataObject());
179         if (successMap == null) {
180             successMap = new HashMap<>();
181             PolicyLogger.error("Failed to Update the Database Dictionary Tables.");
182             successMap.put("error", "DB UPDATE");
183         }
184         return successMap;
185     }
186
187     private String getValueFromDictionary(String templateName) {
188         String ruleTemplate = null;
189         CommonClassDaoImpl dbConnection = new CommonClassDaoImpl();
190         String queryString = "from BRMSParamTemplate where param_template_name= :templateName";
191         SimpleBindings params = new SimpleBindings();
192         params.put("templateName", templateName);
193         List<Object> result = dbConnection.getDataByQuery(queryString, params);
194         if (!result.isEmpty()) {
195             BRMSParamTemplate template = (BRMSParamTemplate) result.get(0);
196             ruleTemplate = template.getRule();
197         }
198         return ruleTemplate;
199     }
200
201     protected Map<String, String> findType(String rule) {
202         Map<String, String> mapFieldType = new HashMap<>();
203         if (rule != null) {
204             try {
205                 StringBuilder params = new StringBuilder();
206                 Boolean flag = false;
207                 Boolean comment = false;
208                 String lines[] = rule.split("\n");
209                 for (String line : lines) {
210                     if (line.isEmpty() || line.startsWith("//")) {
211                         continue;
212                     }
213                     if (line.startsWith("/*")) {
214                         comment = true;
215                         continue;
216                     }
217                     if (line.contains("//") && !(line.contains("http://") || line.contains("https://"))) {
218                         line = line.split("\\/\\/")[0];
219                     }
220                     if (line.contains("/*")) {
221                         comment = true;
222                         if (line.contains("*/")) {
223                             try {
224                                 comment = false;
225                                 line = line.split("\\/\\*")[0]
226                                         + line.split("\\*\\/")[1].replace("*/", "");
227                             } catch (Exception e) {
228                                 LOGGER.debug(e);
229                                 line = line.split("\\/\\*")[0];
230                             }
231                         } else {
232                             line = line.split("\\/\\*")[0];
233                         }
234                     }
235                     if (line.contains("*/")) {
236                         comment = false;
237                         try {
238                             line = line.split("\\*\\/")[1].replace("*/", "");
239                         } catch (Exception e) {
240                             LOGGER.debug(e);
241                             line = "";
242                         }
243                     }
244                     if (comment) {
245                         continue;
246                     }
247                     if (flag) {
248                         params.append(line);
249                     }
250                     if (line.contains("declare Params")) {
251                         params.append(line);
252                         flag = true;
253                     }
254                     if (line.contains("end") && flag) {
255                         break;
256                     }
257                 }
258                 String param = params.toString().replace("declare Params", "").replace("end", "")
259                         .replaceAll("\\s+", "");
260                 String[] components = param.split(":");
261                 String caption = "";
262                 for (int i = 0; i < components.length; i++) {
263                     String type = "";
264                     if (i == 0) {
265                         caption = components[i];
266                     }
267                     if (caption.equals("")) {
268                         break;
269                     }
270                     String nextComponent = "";
271                     try {
272                         nextComponent = components[i + 1];
273                     } catch (Exception e) {
274                         LOGGER.debug(e);
275                         nextComponent = components[i];
276                     }
277                     //If the type is of type String then we add the UI Item and type to the map.
278                     if (nextComponent.startsWith("String")) {
279                         type = "String";
280                         mapFieldType.put(caption, type);
281                         caption = nextComponent.replace("String", "");
282                     } else if (nextComponent.startsWith("int")) {
283                         type = "int";
284                         mapFieldType.put(caption, type);
285                         caption = nextComponent.replace("int", "");
286                     }
287                 }
288             } catch (Exception e) {
289                 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "CreateBrmsParamPolicy",
290                         "Exception parsing file in findType");
291             }
292         }
293         return mapFieldType;
294     }
295
296     // This is the method for preparing the policy for saving. We have broken it
297     // out
298     // separately because the fully configured policy is used for multiple
299     // things
300     @Override
301     public boolean prepareToSave() throws PAPException {
302
303         if (isPreparedToSave()) {
304             // we have already done this
305             return true;
306         }
307
308         int version = 0;
309         String policyID = policyAdapter.getPolicyID();
310         version = policyAdapter.getHighestVersion();
311
312         // Create the Instance for pojo, PolicyType object is used in
313         // marshalling.
314         if (policyAdapter.getPolicyType().equals("Config")) {
315             PolicyType policyConfig = new PolicyType();
316
317             policyConfig.setVersion(Integer.toString(version));
318             policyConfig.setPolicyId(policyID);
319             policyConfig.setTarget(new TargetType());
320             policyAdapter.setData(policyConfig);
321         }
322
323         policyName = policyAdapter.getNewFileName();
324
325         if (policyAdapter.getData() != null) {
326             Map<String, String> ruleAndUIValue = policyAdapter.getBrmsParamBody();
327             String templateValue = ruleAndUIValue.get("templateName");
328             String valueFromDictionary = getValueFromDictionary(templateValue);
329
330             StringBuilder body = new StringBuilder();
331
332             try {
333                 body.append(
334                         "/* Autogenerated Code Please Don't change/remove this comment section. This is for the UI " +
335                                 "purpose. \n\t " +
336                                 "<$%BRMSParamTemplate=" + templateValue + "%$> \n");
337                 body.append("<%$Values=");
338                 for (Map.Entry<String, String> entry : ruleAndUIValue.entrySet()) {
339                     String uiKey = entry.getKey();
340                     if (!"templateName".equals(uiKey)) {
341                         body.append(uiKey + ":-:" + entry.getValue() + ":|:");
342                     }
343                 }
344                 body.append("$%> \n*/ \n");
345                 body.append(valueFromDictionary + "\n");
346             } catch (Exception e) {
347                 PolicyLogger
348                         .error(MessageCodes.ERROR_PROCESS_FLOW, e, "CreateBrmsParamPolicy", "Exception saving policy");
349             }
350
351             saveConfigurations(policyName, body.toString());
352
353             // Make sure the filename ends with an extension
354             if (!policyName.endsWith(".xml")) {
355                 policyName = policyName + ".xml";
356             }
357
358             PolicyType configPolicy = (PolicyType) policyAdapter.getData();
359
360             configPolicy.setDescription(policyAdapter.getPolicyDescription());
361
362             configPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId());
363
364             AllOfType allOfOne = new AllOfType();
365
366             String fileName = policyAdapter.getNewFileName();
367             String name = fileName.substring(fileName.lastIndexOf("\\") + 1);
368             if ((name == null) || (name.equals(""))) {
369                 name = fileName.substring(fileName.lastIndexOf("/") + 1
370                 );
371             }
372             allOfOne.getMatch().add(createMatch("PolicyName", name));
373
374
375             AllOfType allOf = new AllOfType();
376
377             // Match for ONAPName
378             allOf.getMatch().add(createMatch("ONAPName", policyAdapter.getOnapName()));
379             allOf.getMatch().add(createMatch("ConfigName", policyAdapter.getConfigName()));
380             // Match for riskType
381             allOf.getMatch().add(createDynamicMatch("RiskType", policyAdapter.getRiskType()));
382             // Match for riskLevel
383             allOf.getMatch().add(createDynamicMatch("RiskLevel", String.valueOf(policyAdapter.getRiskLevel())));
384             // Match for riskguard
385             allOf.getMatch().add(createDynamicMatch("guard", policyAdapter.getGuard()));
386             // Match for ttlDate
387             allOf.getMatch().add(createDynamicMatch("TTLDate", policyAdapter.getTtlDate()));
388             AnyOfType anyOf = new AnyOfType();
389             anyOf.getAllOf().add(allOfOne);
390             anyOf.getAllOf().add(allOf);
391
392             TargetType target = new TargetType();
393             target.getAnyOf().add(anyOf);
394
395             // Adding the target to the policy element
396             configPolicy.setTarget((TargetType) target);
397
398             RuleType rule = new RuleType();
399             rule.setRuleId(policyAdapter.getRuleID());
400
401             rule.setEffect(EffectType.PERMIT);
402
403             // Create Target in Rule
404             AllOfType allOfInRule = new AllOfType();
405
406             // Creating match for ACCESS in rule target
407             MatchType accessMatch = new MatchType();
408             AttributeValueType accessAttributeValue = new AttributeValueType();
409             accessAttributeValue.setDataType(STRING_DATATYPE);
410             accessAttributeValue.getContent().add("ACCESS");
411             accessMatch.setAttributeValue(accessAttributeValue);
412             AttributeDesignatorType accessAttributeDesignator = new AttributeDesignatorType();
413             URI accessURI = null;
414             try {
415                 accessURI = new URI(ACTION_ID);
416             } catch (URISyntaxException e) {
417                 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "CreateBrmsParamPolicy",
418                         "Exception creating ACCESS URI");
419             }
420             accessAttributeDesignator.setCategory(CATEGORY_ACTION);
421             accessAttributeDesignator.setDataType(STRING_DATATYPE);
422             accessAttributeDesignator.setAttributeId(new IdentifierImpl(
423                     accessURI).stringValue());
424             accessMatch.setAttributeDesignator(accessAttributeDesignator);
425             accessMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE);
426
427             // Creating Config Match in rule Target
428             MatchType configMatch = new MatchType();
429             AttributeValueType configAttributeValue = new AttributeValueType();
430             configAttributeValue.setDataType(STRING_DATATYPE);
431
432             configAttributeValue.getContent().add("Config");
433
434             configMatch.setAttributeValue(configAttributeValue);
435             AttributeDesignatorType configAttributeDesignator = new AttributeDesignatorType();
436             URI configURI = null;
437             try {
438                 configURI = new URI(RESOURCE_ID);
439             } catch (URISyntaxException e) {
440                 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "CreateBrmsParamPolicy",
441                         "Exception creating Config URI");
442             }
443
444             configAttributeDesignator.setCategory(CATEGORY_RESOURCE);
445             configAttributeDesignator.setDataType(STRING_DATATYPE);
446             configAttributeDesignator.setAttributeId(new IdentifierImpl(configURI).stringValue());
447             configMatch.setAttributeDesignator(configAttributeDesignator);
448             configMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE);
449
450             allOfInRule.getMatch().add(accessMatch);
451             allOfInRule.getMatch().add(configMatch);
452
453             AnyOfType anyOfInRule = new AnyOfType();
454             anyOfInRule.getAllOf().add(allOfInRule);
455
456             TargetType targetInRule = new TargetType();
457             targetInRule.getAnyOf().add(anyOfInRule);
458
459             rule.setTarget(targetInRule);
460             rule.setAdviceExpressions(getAdviceExpressions(version, policyName));
461
462             configPolicy
463                     .getCombinerParametersOrRuleCombinerParametersOrVariableDefinition()
464                     .add(rule);
465             policyAdapter.setPolicyData(configPolicy);
466
467         } else {
468             PolicyLogger.error("Unsupported data object."
469                     + policyAdapter.getData().getClass().getCanonicalName());
470         }
471         setPreparedToSave(true);
472         return true;
473     }
474
475     // Data required for Advice part is setting here.
476     private AdviceExpressionsType getAdviceExpressions(int version,
477                                                        String fileName) {
478
479         //Policy Config ID Assignment
480         AdviceExpressionsType advices = new AdviceExpressionsType();
481         AdviceExpressionType advice = new AdviceExpressionType();
482         advice.setAdviceId("BRMSPARAMID");
483         advice.setAppliesTo(EffectType.PERMIT);
484         // For Configuration
485         AttributeAssignmentExpressionType assignment1 = new AttributeAssignmentExpressionType();
486         assignment1.setAttributeId("type");
487         assignment1.setCategory(CATEGORY_RESOURCE);
488         assignment1.setIssuer("");
489         AttributeValueType configNameAttributeValue = new AttributeValueType();
490         configNameAttributeValue.setDataType(STRING_DATATYPE);
491         configNameAttributeValue.getContent().add("Configuration");
492         assignment1.setExpression(new ObjectFactory()
493                 .createAttributeValue(configNameAttributeValue));
494         advice.getAttributeAssignmentExpression().add(assignment1);
495
496         // For Config file Url if configurations are provided.
497         // URL ID Assignment
498         AttributeAssignmentExpressionType assignment2 = new AttributeAssignmentExpressionType();
499         assignment2.setAttributeId("URLID");
500         assignment2.setCategory(CATEGORY_RESOURCE);
501         assignment2.setIssuer("");
502         AttributeValueType attributeValue = new AttributeValueType();
503         attributeValue.setDataType(URI_DATATYPE);
504
505         String content = CONFIG_URL + "/Config/" + getConfigFile(policyName);
506
507         attributeValue.getContent().add(content);
508         assignment2.setExpression(new ObjectFactory()
509                 .createAttributeValue(attributeValue));
510         advice.getAttributeAssignmentExpression().add(assignment2);
511
512         // Policy Name Assignment
513         AttributeAssignmentExpressionType assignment3 = new AttributeAssignmentExpressionType();
514         assignment3.setAttributeId("PolicyName");
515         assignment3.setCategory(CATEGORY_RESOURCE);
516         assignment3.setIssuer("");
517         AttributeValueType attributeValue3 = new AttributeValueType();
518         attributeValue3.setDataType(STRING_DATATYPE);
519         fileName = FilenameUtils.removeExtension(fileName);
520         fileName = fileName + ".xml";
521         String name = fileName.substring(fileName.lastIndexOf("\\") + 1
522         );
523         if ((name == null) || (name.equals(""))) {
524             name = fileName.substring(fileName.lastIndexOf("/") + 1
525             );
526         }
527         attributeValue3.getContent().add(name);
528         assignment3.setExpression(new ObjectFactory()
529                 .createAttributeValue(attributeValue3));
530         advice.getAttributeAssignmentExpression().add(assignment3);
531
532         // Version Number Assignment
533         AttributeAssignmentExpressionType assignment4 = new AttributeAssignmentExpressionType();
534         assignment4.setAttributeId("VersionNumber");
535         assignment4.setCategory(CATEGORY_RESOURCE);
536         assignment4.setIssuer("");
537         AttributeValueType configNameAttributeValue4 = new AttributeValueType();
538         configNameAttributeValue4.setDataType(STRING_DATATYPE);
539         configNameAttributeValue4.getContent().add(Integer.toString(version));
540         assignment4.setExpression(new ObjectFactory()
541                 .createAttributeValue(configNameAttributeValue4));
542         advice.getAttributeAssignmentExpression().add(assignment4);
543
544         // Onap Name Assignment
545         AttributeAssignmentExpressionType assignment5 = new AttributeAssignmentExpressionType();
546         assignment5.setAttributeId("matching:" + ONAPID);
547         assignment5.setCategory(CATEGORY_RESOURCE);
548         assignment5.setIssuer("");
549         AttributeValueType configNameAttributeValue5 = new AttributeValueType();
550         configNameAttributeValue5.setDataType(STRING_DATATYPE);
551         configNameAttributeValue5.getContent().add(policyAdapter.getOnapName());
552         assignment5.setExpression(new ObjectFactory()
553                 .createAttributeValue(configNameAttributeValue5));
554         advice.getAttributeAssignmentExpression().add(assignment5);
555
556
557         //Config Name Assignment
558         AttributeAssignmentExpressionType assignment6 = new AttributeAssignmentExpressionType();
559         assignment6.setAttributeId("matching:" + CONFIGID);
560         assignment6.setCategory(CATEGORY_RESOURCE);
561         assignment6.setIssuer("");
562         AttributeValueType configNameAttributeValue6 = new AttributeValueType();
563         configNameAttributeValue6.setDataType(STRING_DATATYPE);
564         configNameAttributeValue6.getContent().add(policyAdapter.getConfigName());
565         assignment6.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue6));
566         advice.getAttributeAssignmentExpression().add(assignment6);
567         // Adding Controller Information. 
568         if (policyAdapter.getBrmsController() != null) {
569             BRMSDictionaryController brmsDicitonaryController = new BRMSDictionaryController();
570             advice.getAttributeAssignmentExpression().add(
571                     createResponseAttributes("controller:" + policyAdapter.getBrmsController(),
572                             brmsDicitonaryController.getControllerDataByID(policyAdapter.getBrmsController())
573                                     .getController()));
574         }
575
576         // Adding Dependencies. 
577         if (policyAdapter.getBrmsDependency() != null) {
578             BRMSDictionaryController brmsDicitonaryController = new BRMSDictionaryController();
579             ArrayList<String> dependencies = new ArrayList<>();
580             StringBuilder key = new StringBuilder();
581             for (String dependencyName : policyAdapter.getBrmsDependency()) {
582                 dependencies.add(brmsDicitonaryController.getDependencyDataByID(dependencyName).getDependency());
583                 key.append(dependencyName + ",");
584             }
585             advice.getAttributeAssignmentExpression().add(
586                     createResponseAttributes("dependencies:" + key.toString(), dependencies.toString()));
587         }
588
589         // Dynamic Field Config Attributes. 
590         Map<String, String> dynamicFieldConfigAttributes = policyAdapter.getDynamicFieldConfigAttributes();
591         for (Entry<String, String> map : dynamicFieldConfigAttributes.entrySet()) {
592             advice.getAttributeAssignmentExpression()
593                     .add(createResponseAttributes("key:" + map.getKey(), map.getValue()));
594         }
595
596         //Risk Attributes
597         AttributeAssignmentExpressionType assignment8 = new AttributeAssignmentExpressionType();
598         assignment8.setAttributeId("RiskType");
599         assignment8.setCategory(CATEGORY_RESOURCE);
600         assignment8.setIssuer("");
601
602         AttributeValueType configNameAttributeValue8 = new AttributeValueType();
603         configNameAttributeValue8.setDataType(STRING_DATATYPE);
604         configNameAttributeValue8.getContent().add(policyAdapter.getRiskType());
605         assignment8.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue8));
606
607         advice.getAttributeAssignmentExpression().add(assignment8);
608
609         AttributeAssignmentExpressionType assignment9 = new AttributeAssignmentExpressionType();
610         assignment9.setAttributeId("RiskLevel");
611         assignment9.setCategory(CATEGORY_RESOURCE);
612         assignment9.setIssuer("");
613
614         AttributeValueType configNameAttributeValue9 = new AttributeValueType();
615         configNameAttributeValue9.setDataType(STRING_DATATYPE);
616         configNameAttributeValue9.getContent().add(policyAdapter.getRiskLevel());
617         assignment9.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue9));
618
619         advice.getAttributeAssignmentExpression().add(assignment9);
620
621         AttributeAssignmentExpressionType assignment10 = new AttributeAssignmentExpressionType();
622         assignment10.setAttributeId("guard");
623         assignment10.setCategory(CATEGORY_RESOURCE);
624         assignment10.setIssuer("");
625
626         AttributeValueType configNameAttributeValue10 = new AttributeValueType();
627         configNameAttributeValue10.setDataType(STRING_DATATYPE);
628         configNameAttributeValue10.getContent().add(policyAdapter.getGuard());
629         assignment10.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue10));
630
631         advice.getAttributeAssignmentExpression().add(assignment10);
632
633         AttributeAssignmentExpressionType assignment11 = new AttributeAssignmentExpressionType();
634         assignment11.setAttributeId("TTLDate");
635         assignment11.setCategory(CATEGORY_RESOURCE);
636         assignment11.setIssuer("");
637
638         AttributeValueType configNameAttributeValue11 = new AttributeValueType();
639         configNameAttributeValue11.setDataType(STRING_DATATYPE);
640         configNameAttributeValue11.getContent().add(policyAdapter.getTtlDate());
641         assignment11.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue11));
642
643         advice.getAttributeAssignmentExpression().add(assignment11);
644
645         advices.getAdviceExpression().add(advice);
646         return advices;
647     }
648
649     @Override
650     public Object getCorrectPolicyDataObject() {
651         return policyAdapter.getData();
652     }
653
654     private AttributeAssignmentExpressionType createResponseAttributes(String key, String value) {
655         AttributeAssignmentExpressionType assignment7 = new AttributeAssignmentExpressionType();
656         assignment7.setAttributeId(key);
657         assignment7.setCategory(CATEGORY_RESOURCE);
658         assignment7.setIssuer("");
659         AttributeValueType configNameAttributeValue7 = new AttributeValueType();
660         configNameAttributeValue7.setDataType(STRING_DATATYPE);
661         configNameAttributeValue7.getContent().add(value);
662         assignment7.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue7));
663         return assignment7;
664     }
665 }