Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / components / MicroServiceConfigPolicy.java
index 26c7ed8..5120d25 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP-PAP-REST
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.pap.xacml.rest.components;
 
+import com.att.research.xacml.api.pap.PAPException;
+import com.att.research.xacml.std.IdentifierImpl;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Splitter;
+
 import java.io.File;
 import java.io.IOException;
 import java.io.PrintWriter;
@@ -33,22 +39,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
-import org.apache.commons.io.FilenameUtils;
-import org.apache.commons.lang.StringUtils;
-import org.onap.policy.common.logging.eelf.MessageCodes;
-import org.onap.policy.common.logging.eelf.PolicyLogger;
-import org.onap.policy.common.logging.flexlogger.FlexLogger;
-import org.onap.policy.common.logging.flexlogger.Logger;
-import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
-import org.onap.policy.rest.adapter.PolicyRestAdapter;
-import org.onap.policy.rest.jpa.MicroServiceModels;
-
-import com.att.research.xacml.api.pap.PAPException;
-import com.att.research.xacml.std.IdentifierImpl;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.google.common.base.Splitter;
-
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
@@ -61,7 +51,17 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType; 
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
+
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.lang.StringUtils;
+import org.onap.policy.common.logging.eelf.MessageCodes;
+import org.onap.policy.common.logging.eelf.PolicyLogger;
+import org.onap.policy.common.logging.flexlogger.FlexLogger;
+import org.onap.policy.common.logging.flexlogger.Logger;
+import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
+import org.onap.policy.rest.adapter.PolicyRestAdapter;
+import org.onap.policy.rest.jpa.MicroServiceModels;
 
 public class MicroServiceConfigPolicy extends Policy {
 
@@ -70,7 +70,7 @@ public class MicroServiceConfigPolicy extends Policy {
     private static Map<String, String> mapAttribute = new HashMap<>();
     private static Map<String, String> mapMatch = new HashMap<>();
 
-    private static synchronized Map<String, String> getMatchMap () {
+    private static synchronized Map<String, String> getMatchMap() {
         return mapMatch;
     }
 
@@ -82,34 +82,33 @@ public class MicroServiceConfigPolicy extends Policy {
         super();
     }
 
-    public MicroServiceConfigPolicy(PolicyRestAdapter policyAdapter){
+    public MicroServiceConfigPolicy(PolicyRestAdapter policyAdapter) {
         this.policyAdapter = policyAdapter;
     }
 
-    //save configuration of the policy based on the policyname
+    // save configuration of the policy based on the policyname
     private void saveConfigurations(String policyName, String jsonBody) {
-            if(policyName.endsWith(".xml")){
-                policyName = policyName.replace(".xml", "");
-            }
-        try (PrintWriter out = new PrintWriter(CONFIG_HOME + File.separator + policyName +".json")){
+        if (policyName.endsWith(".xml")) {
+            policyName = policyName.replace(".xml", "");
+        }
+        try (PrintWriter out = new PrintWriter(CONFIG_HOME + File.separator + policyName + ".json")) {
             out.println(jsonBody);
         } catch (Exception e) {
-            LOGGER.error("Exception Occured While writing Configuration data"+e);
+            LOGGER.error("Exception Occured While writing Configuration data" + e);
         }
     }
 
-
     @Override
     public Map<String, String> savePolicies() throws PAPException {
 
         Map<String, String> successMap = new HashMap<>();
-        if(isPolicyExists()){
+        if (isPolicyExists()) {
             successMap.put("EXISTS", "This Policy already exist on the PAP");
             return successMap;
         }
 
-        if(!isPreparedToSave()){
-            //Prep and configure the policy for saving
+        if (!isPreparedToSave()) {
+            // Prep and configure the policy for saving
             prepareToSave();
         }
 
@@ -117,18 +116,18 @@ public class MicroServiceConfigPolicy extends Policy {
         Path newPolicyPath = null;
         newPolicyPath = Paths.get(policyAdapter.getNewFileName());
 
-        successMap = createPolicy(newPolicyPath,getCorrectPolicyDataObject());
+        successMap = createPolicy(newPolicyPath, getCorrectPolicyDataObject());
 
         return successMap;
     }
 
-    //This is the method for preparing the policy for saving.  We have broken it out
-    //separately because the fully configured policy is used for multiple things
+    // This is the method for preparing the policy for saving. We have broken it out
+    // separately because the fully configured policy is used for multiple things
     @Override
-    public boolean prepareToSave() throws PAPException{
+    public boolean prepareToSave() throws PAPException {
 
-        if(isPreparedToSave()){
-            //we have already done this
+        if (isPreparedToSave()) {
+            // we have already done this
             return true;
         }
 
@@ -156,7 +155,6 @@ public class MicroServiceConfigPolicy extends Policy {
                 policyName = policyName + ".xml";
             }
 
-
             PolicyType configPolicy = (PolicyType) policyAdapter.getData();
 
             configPolicy.setDescription(policyAdapter.getPolicyDescription());
@@ -170,32 +168,33 @@ public class MicroServiceConfigPolicy extends Policy {
                 name = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());
             }
 
-            //setup values for pulling out matching attributes
+            // setup values for pulling out matching attributes
             ObjectMapper mapper = new ObjectMapper();
             String matching = null;
             Map<String, String> matchMap = null;
             try {
                 JsonNode rootNode = mapper.readTree(policyAdapter.getJsonBody());
-                if (policyAdapter.getTtlDate()==null){
+                if (policyAdapter.getTtlDate() == null) {
                     policyAdapter.setTtlDate("NA");
                 }
-                if (policyAdapter.getServiceType().contains("-v")){
+                if (policyAdapter.getServiceType().contains("-v")) {
                     matching = getValueFromDictionary(policyAdapter.getServiceType());
                 } else {
-                    String jsonVersion  = StringUtils.replaceEach(rootNode.get("version").toString(), new String[]{"\""}, new String[]{""});
+                    String jsonVersion = StringUtils.replaceEach(rootNode.get("version").toString(),
+                            new String[] {"\""}, new String[] {""});
                     matching = getValueFromDictionary(policyAdapter.getServiceType() + "-v" + jsonVersion);
                 }
-                if (matching != null && !matching.isEmpty()){
+                if (matching != null && !matching.isEmpty()) {
                     matchMap = Splitter.on(",").withKeyValueSeparator("=").split(matching);
-            setMatchMap(matchMap);
-                    if(policyAdapter.getJsonBody() != null){
-                        pullMatchValue(rootNode);           
+                    setMatchMap(matchMap);
+                    if (policyAdapter.getJsonBody() != null) {
+                        pullMatchValue(rootNode);
                     }
                 }
             } catch (IOException e1) {
                 throw new PAPException(e1);
             }
-            
+
             // Match for policyName
             allOfOne.getMatch().add(createMatch("PolicyName", name));
 
@@ -203,7 +202,7 @@ public class MicroServiceConfigPolicy extends Policy {
 
             // Adding the matches to AllOfType element Match for Onap
             allOf.getMatch().add(createMatch("ONAPName", policyAdapter.getOnapName()));
-            if (matchMap==null || matchMap.isEmpty()){
+            if (matchMap == null || matchMap.isEmpty()) {
                 // Match for ConfigName
                 allOf.getMatch().add(createMatch("ConfigName", policyAdapter.getConfigName()));
                 // Match for Service
@@ -212,29 +211,25 @@ public class MicroServiceConfigPolicy extends Policy {
                 allOf.getMatch().add(createDynamicMatch("uuid", policyAdapter.getUuid()));
                 // Match for location
                 allOf.getMatch().add(createDynamicMatch("location", policyAdapter.getLocation()));
-            }else {
-                for (Entry<String, String> matchValue : matchMap.entrySet()){
+            } else {
+                for (Entry<String, String> matchValue : matchMap.entrySet()) {
                     String value = matchValue.getValue();
                     String key = matchValue.getKey().trim();
-                    if (value.contains("matching-true")){
-                        if (mapAttribute.containsKey(key)){
+                    if (value.contains("matching-true")) {
+                        if (mapAttribute.containsKey(key)) {
                             allOf.getMatch().add(createDynamicMatch(key, mapAttribute.get(key)));
                         }
                     }
                 }
             }
             // Match for riskType
-            allOf.getMatch().add(
-                    createDynamicMatch("RiskType", policyAdapter.getRiskType()));
+            allOf.getMatch().add(createDynamicMatch("RiskType", policyAdapter.getRiskType()));
             // Match for riskLevel
-            allOf.getMatch().add(
-                    createDynamicMatch("RiskLevel", String.valueOf(policyAdapter.getRiskLevel())));
+            allOf.getMatch().add(createDynamicMatch("RiskLevel", String.valueOf(policyAdapter.getRiskLevel())));
             // Match for riskguard
-            allOf.getMatch().add(
-                    createDynamicMatch("guard", policyAdapter.getGuard()));
+            allOf.getMatch().add(createDynamicMatch("guard", policyAdapter.getGuard()));
             // Match for ttlDate
-            allOf.getMatch().add(
-                    createDynamicMatch("TTLDate", policyAdapter.getTtlDate()));
+            allOf.getMatch().add(createDynamicMatch("TTLDate", policyAdapter.getTtlDate()));
 
             AnyOfType anyOf = new AnyOfType();
             anyOf.getAllOf().add(allOfOne);
@@ -265,7 +260,8 @@ public class MicroServiceConfigPolicy extends Policy {
             try {
                 accessURI = new URI(ACTION_ID);
             } catch (URISyntaxException e) {
-                PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "MicroServiceConfigPolicy", "Exception creating ACCESS URI");
+                PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "MicroServiceConfigPolicy",
+                        "Exception creating ACCESS URI");
             }
             accessAttributeDesignator.setCategory(CATEGORY_ACTION);
             accessAttributeDesignator.setDataType(STRING_DATATYPE);
@@ -284,7 +280,8 @@ public class MicroServiceConfigPolicy extends Policy {
             try {
                 configURI = new URI(RESOURCE_ID);
             } catch (URISyntaxException e) {
-                PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "MicroServiceConfigPolicy", "Exception creating Config URI");
+                PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "MicroServiceConfigPolicy",
+                        "Exception creating Config URI");
             }
             configAttributeDesignator.setCategory(CATEGORY_RESOURCE);
             configAttributeDesignator.setDataType(STRING_DATATYPE);
@@ -317,34 +314,36 @@ public class MicroServiceConfigPolicy extends Policy {
     private void pullMatchValue(JsonNode rootNode) {
         Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
         String newValue = null;
-           while (fieldsIterator.hasNext()) {
-               Map.Entry<String, JsonNode> field = fieldsIterator.next();
-               final String key = field.getKey();
-               final JsonNode value = field.getValue();
-               if (value.isContainerNode() && !value.isArray()) {
-                   pullMatchValue(value); // RECURSIVE CALL
-               } else {
-                   newValue = StringUtils.replaceEach(value.toString(), new String[]{"[", "]", "\""}, new String[]{"", "", ""});
-                   mapAttribute.put(key, newValue);
-               }
-           }
-       
-   }
-
-   private String getValueFromDictionary(String service){
-       String ruleTemplate=null;
-       String modelName = service.split("-v")[0];
-       String modelVersion = service.split("-v")[1];
-       
-       CommonClassDaoImpl dbConnection = new CommonClassDaoImpl();
-       List<Object> result = dbConnection.getDataById(MicroServiceModels.class, "modelName:version", modelName+":"+modelVersion);
-       if(result != null && !result.isEmpty()){
-           MicroServiceModels model = (MicroServiceModels) result.get(0);
-           ruleTemplate = model.getAnnotation();
-       }
-       return ruleTemplate;
-   }
-   
+        while (fieldsIterator.hasNext()) {
+            Map.Entry<String, JsonNode> field = fieldsIterator.next();
+            final String key = field.getKey();
+            final JsonNode value = field.getValue();
+            if (value.isContainerNode() && !value.isArray()) {
+                pullMatchValue(value); // RECURSIVE CALL
+            } else {
+                newValue = StringUtils.replaceEach(value.toString(), new String[] {"[", "]", "\""},
+                        new String[] {"", "", ""});
+                mapAttribute.put(key, newValue);
+            }
+        }
+
+    }
+
+    private String getValueFromDictionary(String service) {
+        String ruleTemplate = null;
+        String modelName = service.split("-v")[0];
+        String modelVersion = service.split("-v")[1];
+
+        CommonClassDaoImpl dbConnection = new CommonClassDaoImpl();
+        List<Object> result =
+                dbConnection.getDataById(MicroServiceModels.class, "modelName:version", modelName + ":" + modelVersion);
+        if (result != null && !result.isEmpty()) {
+            MicroServiceModels model = (MicroServiceModels) result.get(0);
+            ruleTemplate = model.getAnnotation();
+        }
+        return ruleTemplate;
+    }
+
     // Data required for Advice part is setting here.
     private AdviceExpressionsType getAdviceExpressions(int version, String fileName) {
         AdviceExpressionsType advices = new AdviceExpressionsType();
@@ -372,12 +371,12 @@ public class MicroServiceConfigPolicy extends Policy {
         AttributeValueType AttributeValue = new AttributeValueType();
         AttributeValue.setDataType(URI_DATATYPE);
         String configName;
-        if(policyName.endsWith(".xml")){
+        if (policyName.endsWith(".xml")) {
             configName = policyName.replace(".xml", "");
-        }else{
+        } else {
             configName = policyName;
         }
-        String content = CONFIG_URL +"/Config/" + configName + ".json";
+        String content = CONFIG_URL + "/Config/" + configName + ".json";
         AttributeValue.getContent().add(content);
         assignment2.setExpression(new ObjectFactory().createAttributeValue(AttributeValue));
 
@@ -427,16 +426,16 @@ public class MicroServiceConfigPolicy extends Policy {
         assignment7.setAttributeId("matching:service");
         assignment7.setCategory(CATEGORY_RESOURCE);
         assignment7.setIssuer("");
+
         AttributeValueType configNameAttributeValue7 = new AttributeValueType();
         configNameAttributeValue7.setDataType(STRING_DATATYPE);
         configNameAttributeValue7.getContent().add(policyAdapter.getServiceType());
         assignment7.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue7));
+
         advice.getAttributeAssignmentExpression().add(assignment7);
 
         Map<String, String> matchMap = getMatchMap();
-        if (matchMap==null || matchMap.isEmpty()){
+        if (matchMap == null || matchMap.isEmpty()) {
             AttributeAssignmentExpressionType assignment6 = new AttributeAssignmentExpressionType();
             assignment6.setAttributeId("matching:" + CONFIGID);
             assignment6.setCategory(CATEGORY_RESOURCE);
@@ -449,7 +448,6 @@ public class MicroServiceConfigPolicy extends Policy {
 
             advice.getAttributeAssignmentExpression().add(assignment6);
 
-
             AttributeAssignmentExpressionType assignment8 = new AttributeAssignmentExpressionType();
             assignment8.setAttributeId("matching:uuid");
             assignment8.setCategory(CATEGORY_RESOURCE);
@@ -474,28 +472,28 @@ public class MicroServiceConfigPolicy extends Policy {
 
             advice.getAttributeAssignmentExpression().add(assignment9);
         } else {
-            for (Entry<String, String> matchValue : matchMap.entrySet()){
+            for (Entry<String, String> matchValue : matchMap.entrySet()) {
                 String value = matchValue.getValue();
                 String key = matchValue.getKey().trim();
-                if (value.contains("matching-true")){
-                    if (mapAttribute.containsKey(key)){
+                if (value.contains("matching-true")) {
+                    if (mapAttribute.containsKey(key)) {
                         AttributeAssignmentExpressionType assignment9 = new AttributeAssignmentExpressionType();
                         assignment9.setAttributeId("matching:" + key);
                         assignment9.setCategory(CATEGORY_RESOURCE);
                         assignment9.setIssuer("");
-                
+
                         AttributeValueType configNameAttributeValue9 = new AttributeValueType();
                         configNameAttributeValue9.setDataType(STRING_DATATYPE);
                         configNameAttributeValue9.getContent().add(mapAttribute.get(key));
                         assignment9.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue9));
-                
+
                         advice.getAttributeAssignmentExpression().add(assignment9);
+
                     }
                 }
             }
         }
-        
+
         AttributeAssignmentExpressionType assignment10 = new AttributeAssignmentExpressionType();
         assignment10.setAttributeId("Priority");
         assignment10.setCategory(CATEGORY_RESOURCE);
@@ -508,7 +506,7 @@ public class MicroServiceConfigPolicy extends Policy {
 
         advice.getAttributeAssignmentExpression().add(assignment10);
 
-        //Risk Attributes
+        // Risk Attributes
         AttributeAssignmentExpressionType assignment11 = new AttributeAssignmentExpressionType();
         assignment11.setAttributeId("RiskType");
         assignment11.setCategory(CATEGORY_RESOURCE);