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