Addressing Technical Debt for POLICY-SDK-APP
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / CreateDcaeMicroServiceController.java
index 43b62b0..e9a121b 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.";
@@ -708,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");
@@ -732,7 +740,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                }
 
                        }
-                       if(constraints!=null &&constraints.isEmpty()==false){
+                       if(constraints!=null && ! constraints.isEmpty()){
                                //List handling. 
                                listBuffer.append(keySetString.toUpperCase()+"=[");
                                for(String str:constraints){
@@ -825,16 +833,16 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                String presKey = null;
                for(String key: element.keySet()){
                        if(key.contains(".")){
-                               presKey = key.substring(0,key.indexOf("."));
+                               presKey = key.substring(0,key.indexOf('.'));
                        }else if(key.contains("@")){
-                               presKey = key.substring(0,key.indexOf("@"));
+                               presKey = key.substring(0,key.indexOf('@'));
                        }else{
                                presKey = key;
                        }
                        // first check if we are different from old.
                        LOGGER.info(key+"\n");
                        if(jsonArray!=null && jsonArray.length()>0 && key.contains("@") && !key.contains(".") && oldValue!=null){
-                               if(!oldValue.equals(key.substring(0,key.indexOf("@")))){
+                               if(!oldValue.equals(key.substring(0,key.indexOf('@')))){
                                        jsonResult.put(oldValue, jsonArray);
                                        jsonArray = new JSONArray();
                                }
@@ -848,55 +856,55 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                        // 
                        if(key.contains(".")){
                                if(nodeKey==null){
-                                       nodeKey = key.substring(0,key.indexOf("."));
+                                       nodeKey = key.substring(0,key.indexOf('.'));
                                }
-                               if(nodeKey.equals(key.substring(0,key.indexOf(".")))){
-                                       node.put(key.substring(key.indexOf(".")+1), element.get(key));
+                               if(nodeKey.equals(key.substring(0,key.indexOf('.')))){
+                                       node.put(key.substring(key.indexOf('.')+1), element.get(key));
                                }else{
                                        if(node.size()!=0){
                                                if(nodeKey.contains("@")){
                                                        if(arryKey==null){
-                                                               arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
+                                                               arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
                                                        }
                                                        if(nodeKey.endsWith("@0")){
                                                                isArray = true;
                                                                jsonArray = new JSONArray();
                                                        }
-                                                       if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
+                                                       if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf('@')))){
                                                                jsonArray.put(decodeContent(node));
                                                        } 
-                                                       if((key.contains("@") && !arryKey.equals(key.substring(0,nodeKey.indexOf("@")))) || !key.contains("@")){
+                                                       if((key.contains("@") && !arryKey.equals(key.substring(0,nodeKey.indexOf('@')))) || !key.contains("@")){
                                                                jsonResult.put(arryKey, jsonArray);
                                                                jsonArray = new JSONArray();
                                                        }
-                                                       arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
+                                                       arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
                                                }else{
                                                        isArray = false;
                                                        jsonResult.put(nodeKey, decodeContent(node));
                                                }
                                                node = nodeFactory.objectNode();
                                        }
-                                       nodeKey = key.substring(0,key.indexOf("."));
+                                       nodeKey = key.substring(0,key.indexOf('.'));
                                        if(nodeKey.contains("@")){
-                                               arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
+                                               arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
                                        }
-                                       node.put(key.substring(key.indexOf(".")+1), element.get(key));
+                                       node.put(key.substring(key.indexOf('.')+1), element.get(key));
                                }
                        }else if(node.size()!=0){
                                if(nodeKey.contains("@")){
                                        if(arryKey==null){
-                                               arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
+                                               arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
                                        }
                                        if(nodeKey.endsWith("@0")){
                                                isArray = true;
                                                jsonArray = new JSONArray();
                                        }
-                                       if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
+                                       if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf('@')))){
                                                jsonArray.put(decodeContent(node));
                                        }
                                        jsonResult.put(arryKey, jsonArray);
                                        jsonArray = new JSONArray();
-                                       arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
+                                       arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
                                }else{
                                        isArray = false;
                                        jsonResult.put(nodeKey, decodeContent(node));
@@ -912,18 +920,18 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                }
                                if(isArray){
                                        if(oldValue==null){
-                                               oldValue = key.substring(0,key.indexOf("@"));
+                                               oldValue = key.substring(0,key.indexOf('@'));
                                        }
                                        if(oldValue!=prevKey){
-                                               oldValue = key.substring(0,key.indexOf("@"));
+                                               oldValue = key.substring(0,key.indexOf('@'));
                                        }
-                                       if(oldValue.equals(key.substring(0,key.indexOf("@")))){
+                                       if(oldValue.equals(key.substring(0,key.indexOf('@')))){
                                                jsonArray.put(element.get(key));
                                        }else{
                                                jsonResult.put(oldValue, jsonArray);
                                                jsonArray = new JSONArray();
                                        }
-                                       oldValue = key.substring(0,key.indexOf("@"));
+                                       oldValue = key.substring(0,key.indexOf('@'));
                                }else{
                                        jsonResult.put(key, element.get(key));
                                }
@@ -938,18 +946,18 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                }
                                if(isArray){
                                        if(oldValue==null){
-                                               oldValue = key.substring(0,key.indexOf("@"));
+                                               oldValue = key.substring(0,key.indexOf('@'));
                                        }
                                        if(oldValue!=prevKey){
-                                               oldValue = key.substring(0,key.indexOf("@"));
+                                               oldValue = key.substring(0,key.indexOf('@'));
                                        }
-                                       if(oldValue.equals(key.substring(0,key.indexOf("@")))){
+                                       if(oldValue.equals(key.substring(0,key.indexOf('@')))){
                                                jsonArray.put(element.get(key));
                                        }else{
                                                jsonResult.put(oldValue, jsonArray);
                                                jsonArray = new JSONArray();
                                        }
-                                       oldValue = key.substring(0,key.indexOf("@"));
+                                       oldValue = key.substring(0,key.indexOf('@'));
                                }else{
                                        jsonResult.put(key, element.get(key));
                                }
@@ -961,7 +969,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                        jsonArray = new JSONArray();
                                }
                                if(arryKey==null){
-                                       arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
+                                       arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
                                }
                                jsonArray.put(decodeContent(node));
                                jsonResult.put(arryKey, jsonArray);
@@ -983,19 +991,21 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                JsonNode root = mapper.readTree(request.getReader());
 
                String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
-               String  servicename = value.toString().split("-v")[0];
+               String  servicename = value.split("-v")[0];
                String version = null;
-               if (value.toString().contains("-v")){
-                       version = value.toString().split("-v")[1];
+               if (value.contains("-v")){
+                       version = value.split("-v")[1];
                }
                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);
                }
                
@@ -1012,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());
                        }
                }
@@ -1034,7 +1044,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                        
                        if(finalJsonObject != null){
                            LOGGER.info(finalJsonObject.toString());
-                           jsonModel  = finalJsonObject.toString();
+                           jsonModel = finalJsonObject.toString();
                        }
                }
                
@@ -1044,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;
@@ -1153,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);
@@ -1239,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)));
@@ -1508,12 +1510,12 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                }
                for (Entry<String, String> entryRef : attributesRefMap.entrySet()) {
                        key = entryRef.getKey();
-                       value = entryRef.getValue().toString();
+                       value = entryRef.getValue();
                        attribute.put(key, value);
                }
                for (Entry<String, String> entryList : attributesListRefMap.entrySet()) {
                        key = entryList.getKey();
-                       value = entryList.getValue().toString();
+                       value = entryList.getValue();
                        attribute.put(key, value);
                }
                for (Entry<String, LinkedList<String>> arrayList : arrayTextList.entrySet()){
@@ -1555,23 +1557,20 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                        IOUtils.copy(item.getInputStream(), outputStream);
                                        outputStream.close();
                                        this.newFile = file.toString();
-                                       this.newModel.setModelName(this.newFile.toString().split("-v")[0]);
+                                       this.newModel.setModelName(this.newFile.split("-v")[0]);
                                
-                                       if (this.newFile.toString().contains("-v")){
+                                       if (this.newFile.contains("-v")){
                                                if (item.getName().endsWith(".zip")){
-                                                       this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".zip", ""));
+                                                       this.newModel.setVersion(this.newFile.split("-v")[1].replace(".zip", ""));
                                                        zip = true;
                                                }else if(item.getName().endsWith(".yml")){
-                                                       this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".yml", ""));
+                                                       this.newModel.setVersion(this.newFile.split("-v")[1].replace(".yml", ""));
                                                        yml = true;
                                                }
                                                else {
-                                                       this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".xmi", ""));
+                                                       this.newModel.setVersion(this.newFile.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();
@@ -1588,26 +1587,25 @@ 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);
                        fileList = listModelFiles(this.directory);
-               }else if (yml==true){
+               }else if (yml){
                        parseTosca(this.newFile);
                }else {
                        File file = new File(this.newFile);
                        fileList.add(file);
                }
                String modelType= "";
-               if(yml==false){
+               if(! yml){
                        modelType="xmi";
                        //Process Main Model file first
                        classMap = new HashMap<>();
@@ -1635,7 +1633,6 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                        msAttributes.setSubClass(this.retmap);
                        
                        HashMap<String, String> returnReferenceList =new HashMap<>();
-                       //String[] referenceArray=this.referenceAttributes.split("=");
                        returnReferenceList.put(className, this.referenceAttributes);
                        msAttributes.setRefAttribute(returnReferenceList);