CHeckstyle and JUnit for base package in ONAP-REST
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / CreateOptimizationController.java
index 74a1ed0..0e42c78 100644 (file)
@@ -34,7 +34,6 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
-import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -56,8 +55,6 @@ import lombok.Getter;
 import lombok.Setter;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
@@ -72,7 +69,7 @@ import org.json.JSONArray;
 import org.json.JSONObject;
 import org.onap.policy.common.logging.flexlogger.FlexLogger;
 import org.onap.policy.common.logging.flexlogger.Logger;
-import org.onap.policy.rest.XACMLRestProperties;
+import org.onap.policy.rest.XacmlRestProperties;
 import org.onap.policy.rest.adapter.PolicyRestAdapter;
 import org.onap.policy.rest.dao.CommonClassDao;
 import org.onap.policy.rest.jpa.MicroserviceHeaderdeFaults;
@@ -81,6 +78,7 @@ import org.onap.policy.rest.jpa.PolicyEntity;
 import org.onap.policy.rest.util.MSAttributeObject;
 import org.onap.policy.rest.util.MSModelUtils;
 import org.onap.policy.rest.util.MSModelUtils.MODEL_TYPE;
+import org.onap.policy.utils.PolicyUtils;
 import org.onap.portalsdk.core.controller.RestrictedBaseController;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
@@ -122,9 +120,7 @@ public class CreateOptimizationController extends RestrictedBaseController {
     public static final String MANYFALSE = ":MANY-false";
     public static final String MODEL = "model";
     public static final String MANY = "MANY-";
-    public static final String UTF8 = "UTF-8";
     public static final String MODELNAME = "modelName";
-    public static final String APPLICATIONJSON = "application / json";
 
     @Autowired
     private CreateOptimizationController(CommonClassDao commonClassDao) {
@@ -144,6 +140,13 @@ public class CreateOptimizationController extends RestrictedBaseController {
     private Map<String, LinkedList<String>> arrayTextList = new HashMap<>();
     CreateDcaeMicroServiceController msController = new CreateDcaeMicroServiceController();
 
+    /**
+     * setDataToPolicyRestAdapter.
+     *
+     * @param policyData PolicyRestAdapter
+     * @param root JsonNode
+     * @return PolicyRestAdapter
+     */
     public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
         String jsonContent = null;
         try {
@@ -290,9 +293,9 @@ public class CreateOptimizationController extends RestrictedBaseController {
             jsonModel = finalJsonObject.toString();
         }
 
-        response.setCharacterEncoding(UTF8);
-        response.setContentType(APPLICATIONJSON);
-        request.setCharacterEncoding(UTF8);
+        response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
+        response.setContentType(PolicyUtils.APPLICATION_JSON);
+        request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
         List<Object> list = new ArrayList<>();
         String responseString = mapper.writeValueAsString(returnModel);
         JSONObject json = null;
@@ -479,9 +482,9 @@ public class CreateOptimizationController extends RestrictedBaseController {
         final String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
         final String servicename = value.split("-v")[0];
 
-        response.setCharacterEncoding(UTF8);
-        response.setContentType(APPLICATIONJSON);
-        request.setCharacterEncoding(UTF8);
+        response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
+        response.setContentType(PolicyUtils.APPLICATION_JSON);
+        request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
         List<Object> list = new ArrayList<>();
         list.add(new JSONObject("{optimizationModelVersionData: "
                 + mapper.writeValueAsString(getVersionList(servicename)) + "}"));
@@ -559,56 +562,15 @@ public class CreateOptimizationController extends RestrictedBaseController {
         if (target == null) {
             return;
         }
-        // Under target we have AnyOFType
-        List<AnyOfType> anyOfList = target.getAnyOf();
-        if (anyOfList == null) {
-            return;
-        }
-        Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
-        while (iterAnyOf.hasNext()) {
-            AnyOfType anyOf = iterAnyOf.next();
-            // Under AnyOFType we have AllOFType
-            List<AllOfType> allOfList = anyOf.getAllOf();
-            if (allOfList == null) {
-                continue;
-            }
-            Iterator<AllOfType> iterAllOf = allOfList.iterator();
-            while (iterAllOf.hasNext()) {
-                AllOfType allOf = iterAllOf.next();
-                // Under AllOFType we have Match
-                List<MatchType> matchList = allOf.getMatch();
-                if (matchList == null) {
-                    continue;
-                }
-                Iterator<MatchType> iterMatch = matchList.iterator();
-                while (matchList.size() > 1 && iterMatch.hasNext()) {
-                    MatchType match = iterMatch.next();
+        for (AnyOfType anyOf : target.getAnyOf()) {
+            for (AllOfType allOf : anyOf.getAllOf()) {
+                for (MatchType match : allOf.getMatch()) {
                     //
                     // Under the match we have attribute value and
                     // attributeDesignator. So,finally down to the actual attribute.
                     //
-                    AttributeValueType attributeValue = match.getAttributeValue();
-                    String value = (String) attributeValue.getContent().get(0);
-                    AttributeDesignatorType designator = match.getAttributeDesignator();
-                    String attributeId = designator.getAttributeId();
-                    // First match in the target is OnapName, so set that value.
-                    if ("ONAPName".equals(attributeId)) {
-                        policyAdapter.setOnapName(value);
-                    }
-                    if ("RiskType".equals(attributeId)) {
-                        policyAdapter.setRiskType(value);
-                    }
-                    if ("RiskLevel".equals(attributeId)) {
-                        policyAdapter.setRiskLevel(value);
-                    }
-                    if ("guard".equals(attributeId)) {
-                        policyAdapter.setGuard(value);
-                    }
-                    if ("TTLDate".equals(attributeId) && !value.contains("NA")) {
-                        PolicyController controller = new PolicyController();
-                        String newDate = controller.convertDate(value);
-                        policyAdapter.setTtlDate(newDate);
-                    }
+                    policyAdapter.setupUsingAttribute(match.getAttributeDesignator().getAttributeId(),
+                            (String) match.getAttributeValue().getContent().get(0));
                 }
                 readFile(policyAdapter, entity);
             }
@@ -698,9 +660,9 @@ public class CreateOptimizationController extends RestrictedBaseController {
         }
 
         if (!errorMsg.isEmpty()) {
-            response.setCharacterEncoding(UTF8);
-            response.setContentType(APPLICATIONJSON);
-            request.setCharacterEncoding(UTF8);
+            response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
+            response.setContentType(PolicyUtils.APPLICATION_JSON);
+            request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
             response.getWriter().write(new JSONObject().put("errorMsg", errorMsg).toString());
             return;
         }
@@ -768,9 +730,9 @@ public class CreateOptimizationController extends RestrictedBaseController {
 
         }
 
-        response.setCharacterEncoding(UTF8);
-        response.setContentType(APPLICATIONJSON);
-        request.setCharacterEncoding(UTF8);
+        response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
+        response.setContentType(PolicyUtils.APPLICATION_JSON);
+        request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
 
         ObjectMapper mapper = new ObjectMapper();
         JSONObject json = new JSONObject();
@@ -843,9 +805,6 @@ public class CreateOptimizationController extends RestrictedBaseController {
 
         classMap.putAll(tempMap);
         LOGGER.info(tempMap);
-
-        return;
-
     }
 
     private List<File> listModelFiles(String directoryName) {
@@ -891,7 +850,7 @@ public class CreateOptimizationController extends RestrictedBaseController {
 
     private OptimizationObject setOptimizationObjectValues(PolicyRestAdapter policyAdapter) {
         OptimizationObject optimizationObject = new OptimizationObject();
-        optimizationObject.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_OOF));
+        optimizationObject.setTemplateVersion(XACMLProperties.getProperty(XacmlRestProperties.TEMPLATE_VERSION_OOF));
 
         if (policyAdapter.getServiceType() != null) {
             optimizationObject.setService(policyAdapter.getServiceType());