Remove auto generated rule to the drl
[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     
118         // Utility to read json data from the existing file to a string
119         static String readFile(String path, Charset encoding) throws IOException {
120                 byte[] encoded = Files.readAllBytes(Paths.get(path));
121                 return new String(encoded, encoding);
122         }
123         
124         // Saving the Configurations file at server location for config policy.
125         protected void saveConfigurations(String policyName, String ruleBody) {
126             if (policyName.endsWith(".xml")) {
127             policyName = policyName.substring(0, policyName.lastIndexOf(".xml"));
128         }
129                 try (PrintWriter out = new PrintWriter(CONFIG_HOME + File.separator + policyName + ".txt")) {
130                         String expandedBody=expandConfigBody(ruleBody,policyAdapter.getBrmsParamBody());
131                         out.println(expandedBody);
132                         policyAdapter.setJsonBody(expandedBody);
133                         policyAdapter.setConfigBodyData(expandedBody);
134                         out.close();
135                 } catch (Exception e) {
136                         PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "CreateBrmsParamPolicy", "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("//")) {
218                                                 if(!(line.contains("http://") || line.contains("https://"))){
219                                                         line = line.split("\\/\\/")[0];
220                                                 }
221                                         }
222                                         if (line.contains("/*")) {
223                                                 comment = true;
224                                                 if (line.contains("*/")) {
225                                                         try {
226                                                                 comment = false;
227                                                                 line = line.split("\\/\\*")[0]
228                                                                                 + line.split("\\*\\/")[1].replace("*/", "");
229                                                         } catch (Exception e) {
230                                                                 LOGGER.debug(e);
231                                                                 line = line.split("\\/\\*")[0];
232                                                         }
233                                                 } else {
234                                                         line = line.split("\\/\\*")[0];
235                                                 }
236                                         }
237                                         if (line.contains("*/")) {
238                                                 comment = false;
239                                                 try {
240                                                         line = line.split("\\*\\/")[1].replace("*/", "");
241                                                 } catch (Exception e) {
242                                                         LOGGER.debug(e);
243                                                         line = "";
244                                                 }
245                                         }
246                                         if (comment) {
247                                                 continue;
248                                         }
249                                         if (flag) {
250                                                 params.append(line);
251                                         }
252                                         if (line.contains("declare Params")) {
253                                                 params.append(line);
254                                                 flag = true;
255                                         }
256                                         if (line.contains("end") && flag) {
257                                                 break;
258                                         }
259                                 }
260                                 String param = params.toString().replace("declare Params", "").replace("end", "")
261                                                 .replaceAll("\\s+", "");
262                                 String[] components = param.split(":");
263                                 String caption = "";
264                                 for (int i = 0; i < components.length; i++) {
265                                         String type = "";
266                                         if (i == 0) {
267                                                 caption = components[i];
268                                         }
269                                         if(caption.equals("")){
270                                                 break;
271                                         }
272                                         String nextComponent = "";
273                                         try {
274                                                 nextComponent = components[i + 1];
275                                         } catch (Exception e) {
276                                                 LOGGER.debug(e);
277                                                 nextComponent = components[i];
278                                         }
279                                         //If the type is of type String then we add the UI Item and type to the map. 
280                                         if (nextComponent.startsWith("String")) {
281                                                 type = "String";
282                                                 mapFieldType.put(caption, type);
283                                                 caption = nextComponent.replace("String", "");
284                                         } else if (nextComponent.startsWith("int")) {
285                                                 type = "int";
286                                                 mapFieldType.put(caption, type);
287                                                 caption = nextComponent.replace("int", "");
288                                         }
289                                 }
290                         } catch (Exception e) {
291                                 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "CreateBrmsParamPolicy", "Exception parsing file in findType");
292                         }
293                 }
294                 return mapFieldType;
295         }
296         
297         // This is the method for preparing the policy for saving. We have broken it
298         // out
299         // separately because the fully configured policy is used for multiple
300         // things
301         @Override
302         public boolean prepareToSave() throws PAPException {
303                 
304                 if (isPreparedToSave()) {
305                         // we have already done this
306                         return true;
307                 }
308
309                 int version = 0;
310                 String policyID = policyAdapter.getPolicyID();
311                 version = policyAdapter.getHighestVersion();
312
313                 // Create the Instance for pojo, PolicyType object is used in
314                 // marshalling.
315                 if (policyAdapter.getPolicyType().equals("Config")) {
316                         PolicyType policyConfig = new PolicyType();
317
318                         policyConfig.setVersion(Integer.toString(version));
319                         policyConfig.setPolicyId(policyID);
320                         policyConfig.setTarget(new TargetType());
321                         policyAdapter.setData(policyConfig);
322                 }
323
324                 policyName = policyAdapter.getNewFileName();
325                 
326                 if (policyAdapter.getData() != null) {  
327                         Map<String,String> ruleAndUIValue= policyAdapter.getBrmsParamBody();
328                         String templateValue= ruleAndUIValue.get("templateName");
329                         String valueFromDictionary= getValueFromDictionary(templateValue);
330                         
331                         StringBuilder body = new StringBuilder();
332                         
333                         try {
334                             body.append("/* Autogenerated Code Please Don't change/remove this comment section. This is for the UI purpose. \n\t " +
335                                     "<$%BRMSParamTemplate=" + templateValue + "%$> \n");
336                             body.append("<%$Values=");
337                             for (Map.Entry<String, String> entry : ruleAndUIValue.entrySet()) {
338                                 String uiKey = entry.getKey();
339                                 if(!"templateName".equals(uiKey)) {
340                                     body.append(uiKey+":-:"+entry.getValue()+":|:");
341                                 }
342                             }
343                             body.append("$%> \n*/ \n");
344                             body.append(valueFromDictionary + "\n");
345                         }
346                         catch (Exception e) {
347                             PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "CreateBrmsParamPolicy", "Exception saving policy");
348                         }
349                         
350                         saveConfigurations(policyName,body.toString());
351                         
352                         // Make sure the filename ends with an extension
353                         if (!policyName.endsWith(".xml")) {
354                                 policyName = policyName + ".xml";
355                         }
356
357                         PolicyType configPolicy = (PolicyType) policyAdapter.getData();
358
359                         configPolicy.setDescription(policyAdapter.getPolicyDescription());
360
361                         configPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId());
362
363                         AllOfType allOfOne = new AllOfType();
364
365                         String fileName = policyAdapter.getNewFileName();
366                         String name = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
367                         if ((name == null) || (name.equals(""))) {
368                                 name = fileName.substring(fileName.lastIndexOf("/") + 1,
369                                                 fileName.length());
370                         }
371                         allOfOne.getMatch().add(createMatch("PolicyName", name));
372                         
373                         
374                         AllOfType allOf = new AllOfType();
375
376                         // Match for ONAPName
377                         allOf.getMatch().add(createMatch("ONAPName", policyAdapter.getOnapName()));
378                         allOf.getMatch().add(createMatch("ConfigName", policyAdapter.getConfigName()));
379                         // Match for riskType
380                         allOf.getMatch().add(createDynamicMatch("RiskType", policyAdapter.getRiskType()));
381                         // Match for riskLevel
382                         allOf.getMatch().add(createDynamicMatch("RiskLevel", String.valueOf(policyAdapter.getRiskLevel())));
383                         // Match for riskguard
384                         allOf.getMatch().add(createDynamicMatch("guard", policyAdapter.getGuard()));
385                         // Match for ttlDate
386                         allOf.getMatch().add(createDynamicMatch("TTLDate", policyAdapter.getTtlDate()));
387                         AnyOfType anyOf = new AnyOfType();
388                         anyOf.getAllOf().add(allOfOne);
389                         anyOf.getAllOf().add(allOf);
390
391                         TargetType target = new TargetType();
392                         target.getAnyOf().add(anyOf);
393
394                         // Adding the target to the policy element
395                         configPolicy.setTarget((TargetType) target);
396
397                         RuleType rule = new RuleType();
398                         rule.setRuleId(policyAdapter.getRuleID());
399
400                         rule.setEffect(EffectType.PERMIT);
401
402                         // Create Target in Rule
403                         AllOfType allOfInRule = new AllOfType();
404
405                         // Creating match for ACCESS in rule target
406                         MatchType accessMatch = new MatchType();
407                         AttributeValueType accessAttributeValue = new AttributeValueType();
408                         accessAttributeValue.setDataType(STRING_DATATYPE);
409                         accessAttributeValue.getContent().add("ACCESS");
410                         accessMatch.setAttributeValue(accessAttributeValue);
411                         AttributeDesignatorType accessAttributeDesignator = new AttributeDesignatorType();
412                         URI accessURI = null;
413                         try {
414                                 accessURI = new URI(ACTION_ID);
415                         } catch (URISyntaxException e) {
416                                 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "CreateBrmsParamPolicy", "Exception creating ACCESS URI");
417                         }
418                         accessAttributeDesignator.setCategory(CATEGORY_ACTION);
419                         accessAttributeDesignator.setDataType(STRING_DATATYPE);
420                         accessAttributeDesignator.setAttributeId(new IdentifierImpl(
421                                         accessURI).stringValue());
422                         accessMatch.setAttributeDesignator(accessAttributeDesignator);
423                         accessMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE);
424
425                         // Creating Config Match in rule Target
426                         MatchType configMatch = new MatchType();
427                         AttributeValueType configAttributeValue = new AttributeValueType();
428                         configAttributeValue.setDataType(STRING_DATATYPE);
429
430                         configAttributeValue.getContent().add("Config");
431
432                         configMatch.setAttributeValue(configAttributeValue);
433                         AttributeDesignatorType configAttributeDesignator = new AttributeDesignatorType();
434                         URI configURI = null;
435                         try {
436                                 configURI = new URI(RESOURCE_ID);
437                         } catch (URISyntaxException e) {
438                                 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "CreateBrmsParamPolicy", "Exception creating Config URI");
439                         }
440
441                         configAttributeDesignator.setCategory(CATEGORY_RESOURCE);
442                         configAttributeDesignator.setDataType(STRING_DATATYPE);
443                         configAttributeDesignator.setAttributeId(new IdentifierImpl(configURI).stringValue());
444                         configMatch.setAttributeDesignator(configAttributeDesignator);
445                         configMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE);
446
447                         allOfInRule.getMatch().add(accessMatch);
448                         allOfInRule.getMatch().add(configMatch);
449
450                         AnyOfType anyOfInRule = new AnyOfType();
451                         anyOfInRule.getAllOf().add(allOfInRule);
452
453                         TargetType targetInRule = new TargetType();
454                         targetInRule.getAnyOf().add(anyOfInRule);
455
456                         rule.setTarget(targetInRule);
457                         rule.setAdviceExpressions(getAdviceExpressions(version, policyName));
458
459                         configPolicy
460                                         .getCombinerParametersOrRuleCombinerParametersOrVariableDefinition()
461                                         .add(rule);
462                         policyAdapter.setPolicyData(configPolicy);
463
464                 } else {
465                         PolicyLogger.error("Unsupported data object."
466                                         + policyAdapter.getData().getClass().getCanonicalName());
467                 }
468                 setPreparedToSave(true);
469                 return true;
470         }
471
472         // Data required for Advice part is setting here.
473         private AdviceExpressionsType getAdviceExpressions(int version,
474                         String fileName) {
475
476                 //Policy Config ID Assignment
477                 AdviceExpressionsType advices = new AdviceExpressionsType();
478                 AdviceExpressionType advice = new AdviceExpressionType();
479                 advice.setAdviceId("BRMSPARAMID");
480                 advice.setAppliesTo(EffectType.PERMIT);
481                 // For Configuration
482                 AttributeAssignmentExpressionType assignment1 = new AttributeAssignmentExpressionType();
483                 assignment1.setAttributeId("type");
484                 assignment1.setCategory(CATEGORY_RESOURCE);
485                 assignment1.setIssuer("");
486                 AttributeValueType configNameAttributeValue = new AttributeValueType();
487                 configNameAttributeValue.setDataType(STRING_DATATYPE);
488                 configNameAttributeValue.getContent().add("Configuration");
489                 assignment1.setExpression(new ObjectFactory()
490                                 .createAttributeValue(configNameAttributeValue));
491                 advice.getAttributeAssignmentExpression().add(assignment1);
492
493                 // For Config file Url if configurations are provided.
494                 // URL ID Assignment
495                 AttributeAssignmentExpressionType assignment2 = new AttributeAssignmentExpressionType();
496                 assignment2.setAttributeId("URLID");
497                 assignment2.setCategory(CATEGORY_RESOURCE);
498                 assignment2.setIssuer("");
499                 AttributeValueType attributeValue = new AttributeValueType();
500                 attributeValue.setDataType(URI_DATATYPE);
501
502                 String content = CONFIG_URL + "/Config/"+ getConfigFile(policyName);
503
504                 attributeValue.getContent().add(content);
505                 assignment2.setExpression(new ObjectFactory()
506                                 .createAttributeValue(attributeValue));
507                 advice.getAttributeAssignmentExpression().add(assignment2);
508
509                 // Policy Name Assignment
510                 AttributeAssignmentExpressionType assignment3 = new AttributeAssignmentExpressionType();
511                 assignment3.setAttributeId("PolicyName");
512                 assignment3.setCategory(CATEGORY_RESOURCE);
513                 assignment3.setIssuer("");
514                 AttributeValueType attributeValue3 = new AttributeValueType();
515                 attributeValue3.setDataType(STRING_DATATYPE);
516                 fileName = FilenameUtils.removeExtension(fileName);
517                 fileName = fileName + ".xml";
518                 String name = fileName.substring(fileName.lastIndexOf("\\") + 1,
519                                 fileName.length());
520                 if ((name == null) || (name.equals(""))) {
521                         name = fileName.substring(fileName.lastIndexOf("/") + 1,
522                                         fileName.length());
523                 }
524                 attributeValue3.getContent().add(name);
525                 assignment3.setExpression(new ObjectFactory()
526                                 .createAttributeValue(attributeValue3));
527                 advice.getAttributeAssignmentExpression().add(assignment3);
528
529                 // Version Number Assignment
530                 AttributeAssignmentExpressionType assignment4 = new AttributeAssignmentExpressionType();
531                 assignment4.setAttributeId("VersionNumber");
532                 assignment4.setCategory(CATEGORY_RESOURCE);
533                 assignment4.setIssuer("");
534                 AttributeValueType configNameAttributeValue4 = new AttributeValueType();
535                 configNameAttributeValue4.setDataType(STRING_DATATYPE);
536                 configNameAttributeValue4.getContent().add(Integer.toString(version));
537                 assignment4.setExpression(new ObjectFactory()
538                                 .createAttributeValue(configNameAttributeValue4));
539                 advice.getAttributeAssignmentExpression().add(assignment4);
540
541                 // Onap Name Assignment
542                 AttributeAssignmentExpressionType assignment5 = new AttributeAssignmentExpressionType();
543                 assignment5.setAttributeId("matching:" + ONAPID);
544                 assignment5.setCategory(CATEGORY_RESOURCE);
545                 assignment5.setIssuer("");
546                 AttributeValueType configNameAttributeValue5 = new AttributeValueType();
547                 configNameAttributeValue5.setDataType(STRING_DATATYPE);
548                 configNameAttributeValue5.getContent().add(policyAdapter.getOnapName());
549                 assignment5.setExpression(new ObjectFactory()
550                                 .createAttributeValue(configNameAttributeValue5));
551                 advice.getAttributeAssignmentExpression().add(assignment5);
552                 
553                 
554                 //Config Name Assignment
555                 AttributeAssignmentExpressionType assignment6 = new AttributeAssignmentExpressionType();
556                 assignment6.setAttributeId("matching:" +CONFIGID);
557                 assignment6.setCategory(CATEGORY_RESOURCE);
558                 assignment6.setIssuer("");
559                 AttributeValueType configNameAttributeValue6 = new AttributeValueType();
560                 configNameAttributeValue6.setDataType(STRING_DATATYPE);
561                 configNameAttributeValue6.getContent().add(policyAdapter.getConfigName());
562                 assignment6.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue6));
563                 advice.getAttributeAssignmentExpression().add(assignment6);
564         // Adding Controller Information. 
565         if(policyAdapter.getBrmsController()!=null){
566             BRMSDictionaryController brmsDicitonaryController = new BRMSDictionaryController();
567             advice.getAttributeAssignmentExpression().add(
568                         createResponseAttributes("controller:"+ policyAdapter.getBrmsController(), 
569                                 brmsDicitonaryController.getControllerDataByID(policyAdapter.getBrmsController()).getController()));
570         }
571         
572         // Adding Dependencies. 
573         if(policyAdapter.getBrmsDependency()!=null){
574             BRMSDictionaryController brmsDicitonaryController = new BRMSDictionaryController();
575             ArrayList<String> dependencies = new ArrayList<>();
576             StringBuilder key = new StringBuilder();
577             for(String dependencyName: policyAdapter.getBrmsDependency()){
578                 dependencies.add(brmsDicitonaryController.getDependencyDataByID(dependencyName).getDependency());
579                 key.append(dependencyName + ",");
580             }
581             advice.getAttributeAssignmentExpression().add(
582                         createResponseAttributes("dependencies:"+key.toString(), dependencies.toString()));
583         }
584         
585         // Dynamic Field Config Attributes. 
586                 Map<String, String> dynamicFieldConfigAttributes = policyAdapter.getDynamicFieldConfigAttributes();
587                 for (Entry<String, String> map : dynamicFieldConfigAttributes.entrySet()) {
588                         advice.getAttributeAssignmentExpression().add(createResponseAttributes("key:"+map.getKey(), map.getValue()));
589                 }
590                 
591                 //Risk Attributes
592                 AttributeAssignmentExpressionType assignment8 = new AttributeAssignmentExpressionType();
593                 assignment8.setAttributeId("RiskType");
594                 assignment8.setCategory(CATEGORY_RESOURCE);
595                 assignment8.setIssuer("");
596
597                 AttributeValueType configNameAttributeValue8 = new AttributeValueType();
598                 configNameAttributeValue8.setDataType(STRING_DATATYPE);
599                 configNameAttributeValue8.getContent().add(policyAdapter.getRiskType());
600                 assignment8.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue8));
601
602                 advice.getAttributeAssignmentExpression().add(assignment8);
603                 
604                 AttributeAssignmentExpressionType assignment9 = new AttributeAssignmentExpressionType();
605                 assignment9.setAttributeId("RiskLevel");
606                 assignment9.setCategory(CATEGORY_RESOURCE);
607                 assignment9.setIssuer("");
608
609                 AttributeValueType configNameAttributeValue9 = new AttributeValueType();
610                 configNameAttributeValue9.setDataType(STRING_DATATYPE);
611                 configNameAttributeValue9.getContent().add(policyAdapter.getRiskLevel());
612                 assignment9.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue9));
613
614                 advice.getAttributeAssignmentExpression().add(assignment9);     
615
616                 AttributeAssignmentExpressionType assignment10 = new AttributeAssignmentExpressionType();
617                 assignment10.setAttributeId("guard");
618                 assignment10.setCategory(CATEGORY_RESOURCE);
619                 assignment10.setIssuer("");
620
621                 AttributeValueType configNameAttributeValue10 = new AttributeValueType();
622                 configNameAttributeValue10.setDataType(STRING_DATATYPE);
623                 configNameAttributeValue10.getContent().add(policyAdapter.getGuard());
624                 assignment10.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue10));
625
626                 advice.getAttributeAssignmentExpression().add(assignment10);
627
628                 AttributeAssignmentExpressionType assignment11 = new AttributeAssignmentExpressionType();
629                 assignment11.setAttributeId("TTLDate");
630                 assignment11.setCategory(CATEGORY_RESOURCE);
631                 assignment11.setIssuer("");
632
633                 AttributeValueType configNameAttributeValue11 = new AttributeValueType();
634                 configNameAttributeValue11.setDataType(STRING_DATATYPE);
635                 configNameAttributeValue11.getContent().add(policyAdapter.getTtlDate());
636                 assignment11.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue11));
637
638                 advice.getAttributeAssignmentExpression().add(assignment11);
639
640                 advices.getAdviceExpression().add(advice);
641                 return advices;
642         }
643
644         @Override
645         public Object getCorrectPolicyDataObject() {            
646                 return policyAdapter.getData();
647         }
648         
649     private AttributeAssignmentExpressionType  createResponseAttributes(String key, String value){
650         AttributeAssignmentExpressionType assignment7 = new AttributeAssignmentExpressionType();
651         assignment7.setAttributeId(key);
652         assignment7.setCategory(CATEGORY_RESOURCE);
653         assignment7.setIssuer("");
654         AttributeValueType configNameAttributeValue7 = new AttributeValueType();
655         configNameAttributeValue7.setDataType(STRING_DATATYPE);
656         configNameAttributeValue7.getContent().add(value);
657         assignment7.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue7));
658         return assignment7;
659     }
660 }