Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / components / ConfigPolicy.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.BufferedWriter;
24 import java.io.File;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.io.StringReader;
28 import java.net.URI;
29 import java.net.URISyntaxException;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.Scanner;
35 import java.util.StringTokenizer;
36
37 import javax.xml.parsers.ParserConfigurationException;
38 import javax.xml.parsers.SAXParser;
39 import javax.xml.parsers.SAXParserFactory;
40
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
54
55 import org.apache.commons.io.FilenameUtils;
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58 import org.openecomp.policy.pap.xacml.rest.adapters.PolicyRestAdapter;
59 import org.openecomp.policy.common.logging.eelf.MessageCodes;
60 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
61
62 import org.xml.sax.ErrorHandler;
63 import org.xml.sax.InputSource;
64 import org.xml.sax.SAXException;
65 import org.xml.sax.SAXParseException;
66 import org.xml.sax.XMLReader;
67
68 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
69 import com.att.research.xacml.std.IdentifierImpl;
70
71 import org.openecomp.policy.common.logging.flexlogger.FlexLogger; 
72 import org.openecomp.policy.common.logging.flexlogger.Logger; 
73
74 public class ConfigPolicy extends Policy {
75
76         /**
77          * Config Fields
78          */
79         private static final Logger logger = FlexLogger.getLogger(ConfigPolicy.class);
80
81         public static final String JSON_CONFIG = "JSON";
82         public static final String XML_CONFIG = "XML";
83         public static final String PROPERTIES_CONFIG = "PROPERTIES";
84         public static final String OTHER_CONFIG = "OTHER";
85
86         private String configBodyData;
87
88         public ConfigPolicy() {
89                 super();
90         }
91         
92         public ConfigPolicy(PolicyRestAdapter policyAdapter){
93                 this.policyAdapter = policyAdapter;
94         }
95
96         // Saving the Configurations file at server location for config policy.
97         protected void saveConfigurations(String policyName) {
98                 final Path gitPath = Paths.get(policyAdapter.getUserGitPath().toString());
99                 String policyDir = policyAdapter.getParentPath().toString();
100                 int startIndex = policyDir.indexOf(gitPath.toString()) + gitPath.toString().length() + 1;
101                 policyDir = policyDir.substring(startIndex, policyDir.length());
102                 logger.info("print the main domain value"+policyDir);
103                 String path = policyDir.replace('\\', '.');
104                 if(path.contains("/")){
105                         path = policyDir.replace('/', '.');
106                         logger.info("print the path:" +path);
107                 }
108                 
109                 try {
110                         File file;
111                         String configFileName = getConfigFile(policyName);
112                         if(CONFIG_HOME.contains("\\"))
113                         {
114                          file = new File(CONFIG_HOME + "\\" + path + "."+ configFileName);
115                         }
116                         else
117                         {
118                          file = new File(CONFIG_HOME + "/" + path + "."+ configFileName);
119                         }
120                         
121                         // if file doesnt exists, then create it
122                         if (!file.exists()) {
123                                 file.createNewFile();
124                         }
125
126
127                         File configHomeDir = new File(CONFIG_HOME);
128                         File[] listOfFiles = configHomeDir.listFiles();
129                         if (listOfFiles != null){
130                                 for(File eachFile : listOfFiles){
131                                         if(eachFile.isFile()){
132                                                 String fileNameWithoutExtension = FilenameUtils.removeExtension(eachFile.getName());
133                                                 String configFileNameWithoutExtension = FilenameUtils.removeExtension(path + "." + configFileName);
134                                                 if (fileNameWithoutExtension.equals(configFileNameWithoutExtension)){
135                                                         //delete the file
136                                                         eachFile.delete();
137                                                 }
138                                         }
139                                 }
140                         }
141
142                         FileWriter fw = new FileWriter(file.getAbsoluteFile());
143                         BufferedWriter bw = new BufferedWriter(fw);
144                         bw.write(configBodyData);
145                         bw.close();
146                         if (logger.isDebugEnabled()) {
147                                 logger.debug("Configuration is succesfully saved");
148                         }
149                 } catch (IOException e) {
150                         e.printStackTrace();
151                 }
152         }
153
154         // Here we are adding the extension for the configurations file based on the
155         // config type selection for saving.
156         private String getConfigFile(String filename) {
157                 filename = FilenameUtils.removeExtension(filename);
158                 if (filename.endsWith(".xml")) {
159                         filename = filename.substring(0, filename.length() - 4);
160                 }
161                 String id = policyAdapter.getConfigType();
162
163                 if (id != null) {
164                         if (id.equalsIgnoreCase(JSON_CONFIG)) {
165                                 filename = filename + ".json";
166                         }
167                         if (id.equalsIgnoreCase(XML_CONFIG)) {
168                                 filename = filename + ".xml";
169                         }
170                         if (id.equalsIgnoreCase(PROPERTIES_CONFIG)) {
171                                 filename = filename + ".properties";
172                         }
173                         if (id.equalsIgnoreCase(OTHER_CONFIG)) {
174                                 filename = filename + ".txt";
175                         }
176                 }
177                 return filename;
178         }
179
180         // Validations for Config form
181         /*
182          * FORM VALIDATION WILL BE DONE BY THE PAP-ADMIN before creating JSON object... 
183          * BODY VALIDATION WILL BE DONE BY THE PAP-REST after receiving and deserializing the JSON object
184          */
185         public boolean validateConfigForm() {
186                 
187                 isValidForm = true;
188                 
189                 /*
190                  * Validate Text Area Body
191                  */
192                 configBodyData = policyAdapter.getConfigBodyData();
193                 String id = policyAdapter.getConfigType();
194                 if (id != null) {
195                         if (id.equals(JSON_CONFIG)) {
196                                 if (!isJSONValid(configBodyData)) {
197                                         isValidForm = false;
198                                 }
199                         } else if (id.equals(XML_CONFIG)) {
200                                 if (!isXMLValid(configBodyData)) {
201                                         isValidForm = false;
202                                 }
203                         } else if (id.equals(PROPERTIES_CONFIG)) {
204                                 if (!isPropValid(configBodyData)||configBodyData.equals("")) {
205                                         isValidForm = false;
206                                 } 
207                         } else if (id.equals(OTHER_CONFIG)) {
208                                 if (configBodyData.equals("")) {
209                                         isValidForm = false;
210                                 }
211                         }
212                 }
213                 return isValidForm;
214
215         }
216
217         // Validation for XML.
218         private boolean isXMLValid(String data) {
219
220                 SAXParserFactory factory = SAXParserFactory.newInstance();
221                 factory.setValidating(false);
222                 factory.setNamespaceAware(true);
223                 try {
224                         SAXParser parser = factory.newSAXParser();
225                         XMLReader reader = parser.getXMLReader();
226                         reader.setErrorHandler(new XMLErrorHandler());
227                         reader.parse(new InputSource(new StringReader(data)));
228                 } catch (ParserConfigurationException e) {
229                         return false;
230                 } catch (SAXException e) {
231                         return false;
232                 } catch (IOException e) {
233                         return false;
234                 }
235                 return true;
236
237         }
238
239         // Validation for Properties file.
240         public boolean isPropValid(String prop) {
241
242                 Scanner scanner = new Scanner(prop);
243                 while (scanner.hasNextLine()) {
244                         String line = scanner.nextLine();
245                         line.replaceAll("\\s+", "");
246                         if (line.startsWith("#")) {
247                                 continue;
248                         } else {
249                                 if (line.contains("=")) {
250                                         String[] parts = line.split("=");
251                                         if (parts.length < 2) {
252                                                 scanner.close();
253                                                 return false;
254                                         }
255                                 } else {
256                                         scanner.close();
257                                         return false;
258                                 }
259                         }
260                 }
261                 scanner.close();
262                 return true;
263
264         }
265
266         public class XMLErrorHandler implements ErrorHandler {
267
268                 public void warning(SAXParseException e) throws SAXException {
269                         System.out.println(e.getMessage());
270                 }
271
272                 public void error(SAXParseException e) throws SAXException {
273                         System.out.println(e.getMessage());
274                 }
275
276                 public void fatalError(SAXParseException e) throws SAXException {
277                         System.out.println(e.getMessage());
278                 }
279
280         }
281
282         @Override
283         public Map<String, String> savePolicies() throws Exception {
284                 
285                 Map<String, String> successMap = new HashMap<String,String>();
286                 if(isPolicyExists()){
287                         successMap.put("EXISTS", "This Policy already exist on the PAP");
288                         return successMap;
289                 }
290                 
291                 if(!isPreparedToSave()){
292                         //Prep and configure the policy for saving
293                         prepareToSave();
294                 }
295
296                 // Until here we prepared the data and here calling the method to create xml.
297                 Path newPolicyPath = null;
298                 newPolicyPath = Paths.get(policyAdapter.getParentPath().toString(), policyName);
299                 successMap = createPolicy(newPolicyPath,getCorrectPolicyDataObject() );         
300                 if (successMap.containsKey("success")) {
301                         Path finalPolicyPath = getFinalPolicyPath();
302                         policyAdapter.setFinalPolicyPath(finalPolicyPath.toString());
303                 }
304                 return successMap;              
305         }
306         
307         //This is the method for preparing the policy for saving.  We have broken it out
308         //separately because the fully configured policy is used for multiple things
309         @Override
310         public boolean prepareToSave() throws Exception{
311
312                 if(isPreparedToSave()){
313                         //we have already done this
314                         return true;
315                 }
316                 
317                 int version = 0;
318                 String policyID = policyAdapter.getPolicyID();
319
320                 if (policyAdapter.isEditPolicy()) {
321                         version = policyAdapter.getHighestVersion() + 1;
322                 } else {
323                         version = 1;
324                 }
325                 
326                 // Create the Instance for pojo, PolicyType object is used in marshalling.
327                 if (policyAdapter.getPolicyType().equals("Config")) {
328                         PolicyType policyConfig = new PolicyType();
329
330                         policyConfig.setVersion(Integer.toString(version));
331                         policyConfig.setPolicyId(policyID);
332                         policyConfig.setTarget(new TargetType());
333                         policyAdapter.setData(policyConfig);
334                 }
335                 
336                 if (policyAdapter.getData() != null) {
337                         
338                         // Save off everything
339                         // making ready all the required elements to generate the action policy xml.
340                         // Get the uniqueness for policy name.
341                         Path newFile = getNextFilename(Paths.get(policyAdapter.getParentPath().toString()), policyAdapter.getPolicyType(), policyAdapter.getPolicyName(), version);
342                         if (newFile == null) {
343                                 //TODO:EELF Cleanup - Remove logger
344                                 //logger.error("File already exists");
345                                 PolicyLogger.error("File alrady exists");
346                                 setPolicyExists(true);
347                                 return false;
348                         }
349                         policyName = newFile.getFileName().toString();
350                         
351                         // Body is optional so checking.
352                         configBodyData = policyAdapter.getConfigBodyData();
353                         if (!configBodyData.equals("")) {
354                                 // Save the Configurations file with the policy name with extention based on selection.
355                                 saveConfigurations(policyName);
356                         }
357                         
358                         // Make sure the filename ends with an extension
359                         if (policyName.endsWith(".xml") == false) {
360                                 policyName = policyName + ".xml";
361                         }
362                         
363         
364                         PolicyType configPolicy = (PolicyType) policyAdapter.getData();
365                         
366                         configPolicy.setDescription(policyAdapter.getPolicyDescription());
367
368                         configPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId());
369                         AllOfType allOfOne = new AllOfType();
370                         final Path gitPath = Paths.get(policyAdapter.getUserGitPath().toString());
371                         String policyDir = policyAdapter.getParentPath().toString();
372                         int startIndex = policyDir.indexOf(gitPath.toString()) + gitPath.toString().length() + 1;
373                         policyDir = policyDir.substring(startIndex, policyDir.length());
374                         logger.info("print the main domain value "+policyDir);
375                         String path = policyDir.replace('\\', '.');
376                         if(path.contains("/")){
377                                 path = policyDir.replace('/', '.');
378                                 logger.info("print the path:" +path);
379                         }
380                         String fileName = FilenameUtils.removeExtension(policyName);
381                         fileName = path + "." + fileName + ".xml";
382                         String name = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
383                         if ((name == null) || (name.equals(""))) {
384                                 name = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());
385                         }
386                         allOfOne.getMatch().add(createMatch("PolicyName", name));
387                         AllOfType allOf = new AllOfType();
388                         
389                         // Adding the matches to AllOfType element Match for Ecomp
390                         allOf.getMatch().add(createMatch("ECOMPName", policyAdapter.getEcompName()));
391                         // Match for riskType
392                         allOf.getMatch().add(
393                                         createDynamicMatch("RiskType", policyAdapter.getRiskType()));
394                         // Match for riskLevel
395                         allOf.getMatch().add(
396                                         createDynamicMatch("RiskLevel", String.valueOf(policyAdapter.getRiskLevel())));
397                         // Match for riskguard
398                         allOf.getMatch().add(
399                                         createDynamicMatch("guard", policyAdapter.getGuard()));
400                         // Match for ttlDate
401                         allOf.getMatch().add(
402                                         createDynamicMatch("TTLDate", policyAdapter.getTtlDate()));
403                         // Match for ConfigName
404                         allOf.getMatch().add(createMatch("ConfigName", policyAdapter.getConfigName()));
405                         
406                         Map<String, String> dynamicFieldConfigAttributes = policyAdapter.getDynamicFieldConfigAttributes();
407                         
408                         // If there is any dynamic field create the matches here
409                         for (String keyField : dynamicFieldConfigAttributes.keySet()) {
410                                 String key = keyField;
411                                 String value = dynamicFieldConfigAttributes.get(key);
412                                 MatchType dynamicMatch = createDynamicMatch(key, value);
413                                 allOf.getMatch().add(dynamicMatch);
414                         }
415
416                         AnyOfType anyOf = new AnyOfType();
417                         anyOf.getAllOf().add(allOfOne);
418                         anyOf.getAllOf().add(allOf);
419
420                         TargetType target = new TargetType();
421                         ((TargetType) target).getAnyOf().add(anyOf);
422                         
423                         // Adding the target to the policy element
424                         configPolicy.setTarget((TargetType) target);
425
426                         RuleType rule = new RuleType();
427                         rule.setRuleId(policyAdapter.getRuleID());
428                         
429                         rule.setEffect(EffectType.PERMIT);
430                         
431                         // Create Target in Rule
432                         AllOfType allOfInRule = new AllOfType();
433
434                         // Creating match for ACCESS in rule target
435                         MatchType accessMatch = new MatchType();
436                         AttributeValueType accessAttributeValue = new AttributeValueType();
437                         accessAttributeValue.setDataType(STRING_DATATYPE);
438                         accessAttributeValue.getContent().add("ACCESS");
439                         accessMatch.setAttributeValue(accessAttributeValue);
440                         AttributeDesignatorType accessAttributeDesignator = new AttributeDesignatorType();
441                         URI accessURI = null;
442                         try {
443                                 accessURI = new URI(ACTION_ID);
444                         } catch (URISyntaxException e) {
445                                 //TODO:EELF Cleanup - Remove logger
446                                 //logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e.getStackTrace());
447                                 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "ConfigPolicy", "Exception creating ACCESS URI");
448                         }
449                         accessAttributeDesignator.setCategory(CATEGORY_ACTION);
450                         accessAttributeDesignator.setDataType(STRING_DATATYPE);
451                         accessAttributeDesignator.setAttributeId(new IdentifierImpl(accessURI).stringValue());
452                         accessMatch.setAttributeDesignator(accessAttributeDesignator);
453                         accessMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE);
454
455                         // Creating Config Match in rule Target
456                         MatchType configMatch = new MatchType();
457                         AttributeValueType configAttributeValue = new AttributeValueType();
458                         configAttributeValue.setDataType(STRING_DATATYPE);
459                         configAttributeValue.getContent().add("Config");
460                         configMatch.setAttributeValue(configAttributeValue);
461                         AttributeDesignatorType configAttributeDesignator = new AttributeDesignatorType();
462                         URI configURI = null;
463                         try {
464                                 configURI = new URI(RESOURCE_ID);
465                         } catch (URISyntaxException e) {
466                                 //TODO:EELF Cleanup - Remove logger
467                                 //logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e.getStackTrace());
468                                 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "ConfigPolicy", "Exception creating Config URI");
469                         }
470                         configAttributeDesignator.setCategory(CATEGORY_RESOURCE);
471                         configAttributeDesignator.setDataType(STRING_DATATYPE);
472                         configAttributeDesignator.setAttributeId(new IdentifierImpl(configURI).stringValue());
473                         configMatch.setAttributeDesignator(configAttributeDesignator);
474                         configMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE);
475
476                         allOfInRule.getMatch().add(accessMatch);
477                         allOfInRule.getMatch().add(configMatch);
478
479                         AnyOfType anyOfInRule = new AnyOfType();
480                         anyOfInRule.getAllOf().add(allOfInRule);
481
482                         TargetType targetInRule = new TargetType();
483                         targetInRule.getAnyOf().add(anyOfInRule);
484
485                         rule.setTarget(targetInRule);
486                         rule.setAdviceExpressions(getAdviceExpressions(version, policyName));
487
488                         configPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
489                         policyAdapter.setPolicyData(configPolicy);
490
491                 } else {
492                         //TODO:EELF Cleanup - Remove logger
493                         //logger.error("Unsupported data object." + policyAdapter.getData().getClass().getCanonicalName());
494                         PolicyLogger.error("Unsupported data object." + policyAdapter.getData().getClass().getCanonicalName());
495                 }
496                 setPreparedToSave(true);
497                 return true;
498         }
499
500         // Data required for Advice part is setting here.
501         private AdviceExpressionsType getAdviceExpressions(int version, String fileName) {
502                 AdviceExpressionsType advices = new AdviceExpressionsType();
503                 AdviceExpressionType advice = new AdviceExpressionType();
504                 advice.setAdviceId("configID");
505                 advice.setAppliesTo(EffectType.PERMIT);
506                 
507                 // For Configuration
508                 AttributeAssignmentExpressionType assignment1 = new AttributeAssignmentExpressionType();
509                 assignment1.setAttributeId("type");
510                 assignment1.setCategory(CATEGORY_RESOURCE);
511                 assignment1.setIssuer("");
512
513                 AttributeValueType configNameAttributeValue = new AttributeValueType();
514                 configNameAttributeValue.setDataType(STRING_DATATYPE);
515                 configNameAttributeValue.getContent().add("Configuration");
516                 assignment1.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue));
517
518                 advice.getAttributeAssignmentExpression().add(assignment1);
519                 final Path gitPath = Paths.get(policyAdapter.getUserGitPath().toString());
520                 
521                 // For Config file Url if configurations are provided.
522                 if (policyAdapter.getConfigType() != null) {
523                         AttributeAssignmentExpressionType assignment2 = new AttributeAssignmentExpressionType();
524                         assignment2.setAttributeId("URLID");
525                         assignment2.setCategory(CATEGORY_RESOURCE);
526                         assignment2.setIssuer("");
527
528                         AttributeValueType AttributeValue = new AttributeValueType();
529                         AttributeValue.setDataType(URI_DATATYPE);
530                         String policyDir1 = policyAdapter.getParentPath().toString();
531                         int startIndex1 = policyDir1.indexOf(gitPath.toString()) + gitPath.toString().length() + 1;
532                         policyDir1 = policyDir1.substring(startIndex1, policyDir1.length());
533                         logger.info("print the main domain value"+policyDir1);
534                         String path = policyDir1.replace('\\', '.');
535                         if(path.contains("/")){
536                                 path = policyDir1.replace('/', '.');
537                                 logger.info("print the path:" +path);
538                         }
539
540                         String content = "$URL" + "/Config/" + path + "." + getConfigFile(policyName);
541                         System.out.println("URL value :" + content);
542                         AttributeValue.getContent().add(content);
543                         assignment2.setExpression(new ObjectFactory().createAttributeValue(AttributeValue));
544
545                         advice.getAttributeAssignmentExpression().add(assignment2);
546                         AttributeAssignmentExpressionType assignment3 = new AttributeAssignmentExpressionType();
547                         assignment3.setAttributeId("PolicyName");
548                         assignment3.setCategory(CATEGORY_RESOURCE);
549                         assignment3.setIssuer("");
550
551                         AttributeValueType attributeValue3 = new AttributeValueType();
552                         attributeValue3.setDataType(STRING_DATATYPE);
553                         String policyDir = policyAdapter.getParentPath().toString();
554                         int startIndex = policyDir.indexOf(gitPath.toString()) + gitPath.toString().length() + 1;
555                         policyDir = policyDir.substring(startIndex, policyDir.length());
556                         StringTokenizer tokenizer = null;
557                         StringBuffer buffer = new StringBuffer();
558                         if (policyDir.contains("\\")) {
559                                 tokenizer = new StringTokenizer(policyDir, "\\");
560                         } else {
561                                 tokenizer = new StringTokenizer(policyDir, "/");
562                         }
563                         if (tokenizer != null) {
564                                 while (tokenizer.hasMoreElements()) {
565                                         String value = tokenizer.nextToken();
566                                         buffer.append(value);
567                                         buffer.append(".");
568                                 }
569                         }
570                         fileName = FilenameUtils.removeExtension(fileName);
571                         fileName = buffer.toString() + fileName + ".xml";
572                         String name = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
573                         if ((name == null) || (name.equals(""))) {
574                                 name = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());
575                         }
576                         attributeValue3.getContent().add(name);
577                         assignment3.setExpression(new ObjectFactory().createAttributeValue(attributeValue3));
578                         advice.getAttributeAssignmentExpression().add(assignment3);
579
580                         AttributeAssignmentExpressionType assignment4 = new AttributeAssignmentExpressionType();
581                         assignment4.setAttributeId("VersionNumber");
582                         assignment4.setCategory(CATEGORY_RESOURCE);
583                         assignment4.setIssuer("");
584
585                         AttributeValueType configNameAttributeValue4 = new AttributeValueType();
586                         configNameAttributeValue4.setDataType(STRING_DATATYPE);
587                         configNameAttributeValue4.getContent().add(Integer.toString(version));
588                         assignment4.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue4));
589
590                         advice.getAttributeAssignmentExpression().add(assignment4);
591
592                         AttributeAssignmentExpressionType assignment5 = new AttributeAssignmentExpressionType();
593                         assignment5.setAttributeId("matching:" + this.ECOMPID);
594                         assignment5.setCategory(CATEGORY_RESOURCE);
595                         assignment5.setIssuer("");
596
597                         AttributeValueType configNameAttributeValue5 = new AttributeValueType();
598                         configNameAttributeValue5.setDataType(STRING_DATATYPE);
599                         configNameAttributeValue5.getContent().add(policyAdapter.getEcompName());
600                         assignment5.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue5));
601
602                         advice.getAttributeAssignmentExpression().add(assignment5);
603
604                         AttributeAssignmentExpressionType assignment6 = new AttributeAssignmentExpressionType();
605                         assignment6.setAttributeId("matching:" + this.CONFIGID);
606                         assignment6.setCategory(CATEGORY_RESOURCE);
607                         assignment6.setIssuer("");
608
609                         AttributeValueType configNameAttributeValue6 = new AttributeValueType();
610                         configNameAttributeValue6.setDataType(STRING_DATATYPE);
611                         configNameAttributeValue6.getContent().add(policyAdapter.getConfigName());
612                         assignment6.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue6));
613
614                         advice.getAttributeAssignmentExpression().add(assignment6);
615
616                         Map<String, String> dynamicFieldConfigAttributes = policyAdapter.getDynamicFieldConfigAttributes();
617                         for (String keyField : dynamicFieldConfigAttributes.keySet()) {
618                                 String key = keyField;
619                                 String value = dynamicFieldConfigAttributes.get(key);
620                                 AttributeAssignmentExpressionType assignment7 = new AttributeAssignmentExpressionType();
621                                 assignment7.setAttributeId("matching:" + key);
622                                 assignment7.setCategory(CATEGORY_RESOURCE);
623                                 assignment7.setIssuer("");
624
625                                 AttributeValueType configNameAttributeValue7 = new AttributeValueType();
626                                 configNameAttributeValue7.setDataType(STRING_DATATYPE);
627                                 configNameAttributeValue7.getContent().add(value);
628                                 assignment7.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue7));
629
630                                 advice.getAttributeAssignmentExpression().add(assignment7);
631                         }
632                 }
633                 
634                 //Risk Attributes
635                 AttributeAssignmentExpressionType assignment8 = new AttributeAssignmentExpressionType();
636                 assignment8.setAttributeId("RiskType");
637                 assignment8.setCategory(CATEGORY_RESOURCE);
638                 assignment8.setIssuer("");
639
640                 AttributeValueType configNameAttributeValue8 = new AttributeValueType();
641                 configNameAttributeValue8.setDataType(STRING_DATATYPE);
642                 configNameAttributeValue8.getContent().add(policyAdapter.getRiskType());
643                 assignment8.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue8));
644
645                 advice.getAttributeAssignmentExpression().add(assignment8);
646                 
647                 AttributeAssignmentExpressionType assignment9 = new AttributeAssignmentExpressionType();
648                 assignment9.setAttributeId("RiskLevel");
649                 assignment9.setCategory(CATEGORY_RESOURCE);
650                 assignment9.setIssuer("");
651
652                 AttributeValueType configNameAttributeValue9 = new AttributeValueType();
653                 configNameAttributeValue9.setDataType(STRING_DATATYPE);
654                 configNameAttributeValue9.getContent().add(policyAdapter.getRiskLevel());
655                 assignment9.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue9));
656
657                 advice.getAttributeAssignmentExpression().add(assignment9);     
658
659                 AttributeAssignmentExpressionType assignment10 = new AttributeAssignmentExpressionType();
660                 assignment10.setAttributeId("guard");
661                 assignment10.setCategory(CATEGORY_RESOURCE);
662                 assignment10.setIssuer("");
663
664                 AttributeValueType configNameAttributeValue10 = new AttributeValueType();
665                 configNameAttributeValue10.setDataType(STRING_DATATYPE);
666                 configNameAttributeValue10.getContent().add(policyAdapter.getGuard());
667                 assignment10.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue10));
668
669                 advice.getAttributeAssignmentExpression().add(assignment10);
670                 
671                 AttributeAssignmentExpressionType assignment11 = new AttributeAssignmentExpressionType();
672                 assignment11.setAttributeId("TTLDate");
673                 assignment11.setCategory(CATEGORY_RESOURCE);
674                 assignment11.setIssuer("");
675
676                 AttributeValueType configNameAttributeValue11 = new AttributeValueType();
677                 configNameAttributeValue11.setDataType(STRING_DATATYPE);
678                 configNameAttributeValue11.getContent().add(policyAdapter.getTtlDate());
679                 assignment11.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue11));
680
681                 advice.getAttributeAssignmentExpression().add(assignment11);
682                 
683
684                 advices.getAdviceExpression().add(advice);
685                 return advices;
686         }
687
688         @Override
689         public Object getCorrectPolicyDataObject() {
690                 return policyAdapter.getPolicyData();
691         }
692
693 }