Merge "Fix audit log to contain ending timestamp"
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / CreateDcaeMicroServiceController.java
index ff5ccff..a40b48a 100644 (file)
@@ -73,7 +73,6 @@ 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.controller.PolicyController;
 import org.onap.policy.rest.XACMLRestProperties;
 import org.onap.policy.rest.adapter.PolicyRestAdapter;
 import org.onap.policy.rest.dao.CommonClassDao;
@@ -83,8 +82,8 @@ 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.openecomp.portalsdk.core.controller.RestrictedBaseController;
-import org.openecomp.portalsdk.core.web.support.JsonMessage;
+import org.onap.portalsdk.core.controller.RestrictedBaseController;
+import org.onap.portalsdk.core.web.support.JsonMessage;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.MediaType;
 import org.springframework.stereotype.Controller;
@@ -141,6 +140,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
        Set<String> uniqueDataKeys= new HashSet<>();
        StringBuilder dataListBuffer=new StringBuilder();
        List<String> dataConstraints= new ArrayList <>();
+       Set<String> allManyTrueKeys= new HashSet <>();
        
        public static final String DATATYPE  = "data_types.policy.data.";
        public static final String PROPERTIES=".properties.";
@@ -411,23 +411,17 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
        }
        
  
+       @SuppressWarnings("unchecked")
        public Map<String, String> load(String fileName) throws IOException { 
                File newConfiguration = new File(fileName);
-               InputStream is = null;
-               try {
-                       is = new FileInputStream(newConfiguration);
+               Yaml yaml = new Yaml();
+               Map<Object, Object> yamlMap = null;
+               try(InputStream is = new FileInputStream(newConfiguration)){
+                       yamlMap = (Map<Object, Object>) yaml.load(is); 
                } catch (FileNotFoundException e) {
                        LOGGER.error(e);
                }
 
-               Yaml yaml = new Yaml();
-
-               Map<Object, Object> yamlMap = null;
-               try{
-                   yamlMap = (Map<Object, Object>) yaml.load(is); 
-               }catch(Exception e){
-                       LOGGER.error("load:", e);
-               }
                StringBuilder sb = new StringBuilder(); 
                Map<String, String> settings = new HashMap<>(); 
                if (yamlMap == null) { 
@@ -714,17 +708,25 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                                //The "." in the value determines if its a string or a user defined type.  
                                                if (!value.contains(".")){
                                                        //This is string
-                                                       constraints.add(keyValues.get(key));
+                                                       if(StringUtils.isNumeric(key) ){  //only integer key for the value of Constrains 
+                                                           constraints.add(keyValues.get(key));
+                                                       }
                                                }else{
-                                                       //This is userdefined string
+                                                       //This is user defined string
                                                        String trimValue=value.substring(value.lastIndexOf('.')+1);
                                                        StringBuilder referenceIndividualStringBuilder= new StringBuilder();
                                                        referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-true");
                                                        referenceStringBuilder.append(referenceIndividualStringBuilder+",");
                                                }
-                                       }
+                                       }                               
+
                                }
 
+                               if(keyValues.get("type").equalsIgnoreCase(LIST)){
+                                       if(constraints == null || constraints.isEmpty()){
+                                               referenceStringBuilder.append(keySetString+"=MANY-true"+",");
+                                       }
+                               }
                        }else{
                                //User defined Datatype. 
                                String value=keyValues.get("type");
@@ -997,11 +999,13 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                MicroServiceModels returnModel = getAttributeObject(servicename, version);
                
                
-               //get all keys with "MANY-true" defined in their value from subAttribute
+               //Get all keys with "MANY-true" defined in their value from subAttribute 
                Set<String> allkeys = null;
                if(returnModel.getSub_attributes() != null && !returnModel.getSub_attributes().isEmpty()){
-                       JSONObject json = new JSONObject(returnModel.getSub_attributes());              
-                       allkeys = getAllKeys(json);
+                       JSONObject json = new JSONObject(returnModel.getSub_attributes());      
+                       getAllKeys(json); 
+                       allkeys = allManyTrueKeys;
+                       allManyTrueKeys = new  HashSet <>();
                        LOGGER.info("allkeys : " + allkeys);
                }
                
@@ -1018,7 +1022,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                if(allkeys != null){
                        Iterator<String> iter = allkeys.iterator();
                        while(iter.hasNext()){
-                               //convert to array values for MANY-true keys
+                               //Convert to array values for MANY-true keys
                                finalJsonObject = convertToArrayElement(jsonObject, iter.next());
                        }
                }
@@ -1040,7 +1044,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                        
                        if(finalJsonObject != null){
                            LOGGER.info(finalJsonObject.toString());
-                           jsonModel  = finalJsonObject.toString();
+                           jsonModel = finalJsonObject.toString();
                        }
                }
                
@@ -1050,7 +1054,12 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                List<Object>  list = new ArrayList<>();
                PrintWriter out = response.getWriter();
                String responseString = mapper.writeValueAsString(returnModel);
-               JSONObject j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",allManyTrueKeys: " + allManyTrueKeys+ "}");
+               JSONObject j = null;
+               if("".equals(allManyTrueKeys)){
+                       j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + "}");     
+               }else{
+                       j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",allManyTrueKeys: " + allManyTrueKeys+ "}");     
+               }
                list.add(j);
                out.write(list.toString());
                return null;
@@ -1159,19 +1168,6 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                return object;
        }
        
-       //call this method to check if the key is in the many-true key set 
-       private boolean isKeyFound(Set<String> allManyTruekeys, String key){
-               
-            if(allManyTruekeys != null && key != null){
-               Iterator<String> iter = allManyTruekeys.iterator();
-               while(iter.hasNext()){
-                       if(key.equals(iter.next())){
-                               return true;
-                       }
-               }
-            }
-               return false;
-       }
        
        public static JSONObject convertToArrayElement(JSONObject json, String keyValue) {
            return convertToArrayElement(json, new HashSet<>(), keyValue);
@@ -1245,7 +1241,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                if(obj instanceof String && ((String) obj).contains("MANY-true")){
                        LOGGER.info("key : " + key);
                        LOGGER.info("obj : " + obj);
-                   keys.addAll(json.keySet());
+                       allManyTrueKeys.add(key);
                }
                if (obj instanceof JSONObject) keys.addAll(getAllKeys(json.getJSONObject(key)));
                if (obj instanceof JSONArray) keys.addAll(getAllKeys(json.getJSONArray(key)));
@@ -1574,10 +1570,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                                else {
                                                        this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".xmi", ""));
                                                }
-                                       }else{
-                                               errorMsg = "Upload error: The file name should contain '-v', such as xxx-v1802.yml";
-                                       }
-                               
+                                       }                               
                                }catch(Exception e){
                                        LOGGER.error("Upload error : ", e);
                                        errorMsg = "Upload error:" + e.getMessage();
@@ -1594,14 +1587,13 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                        response.setContentType("application / json");
                        request.setCharacterEncoding("UTF-8");
                        
-                       ObjectMapper mapper = new ObjectMapper();
                        JSONObject j = new JSONObject();
                        j.put("errorMsg", errorMsg);
                        out.write(j.toString());
                        return;
                }
                
-               List<File> fileList = new ArrayList<>();;
+               List<File> fileList = new ArrayList<>();
                this.directory = "model";
                if (zip){
                        extractFolder(this.newFile);