From 046653e9ef93acd6d480701f628cbc8dcb91e710 Mon Sep 17 00:00:00 2001 From: guangxingwang Date: Tue, 31 Oct 2017 10:54:59 -0500 Subject: [PATCH] Fixed TOSCA parsing bugs Fixed TOSCA paring bugs and retuned error messages to front-end. Issue-ID: POLICY-405 Change-Id: Idc6efd062360acbb4ceb143af8619b5f7def723a Signed-off-by: guangxingwang --- .../CreateDcaeMicroServiceController.java | 227 ++++++++++++++++++--- .../dictionaryController/MSModelsDictController.js | 5 + .../CreateDcaeMicroServiceControllerTest.java | 6 +- .../src/test/resources/schedulerPolicies-v1707.xmi | 156 ++++++++++++++ 4 files changed, 366 insertions(+), 28 deletions(-) create mode 100644 POLICY-SDK-APP/src/test/resources/schedulerPolicies-v1707.xmi diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/CreateDcaeMicroServiceController.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/CreateDcaeMicroServiceController.java index 0f55ba6ec..a1b3519ad 100644 --- a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/CreateDcaeMicroServiceController.java +++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/CreateDcaeMicroServiceController.java @@ -175,6 +175,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { //---replace empty value with the value below before calling decodeContent method. String dummyValue = "*empty-value*" + UUID.randomUUID().toString(); + LOGGER.info("dummyValue:" + dummyValue); tempJson = StringUtils.replaceEach(tempJson, new String[]{"\"\""}, new String[]{"\""+dummyValue+"\""}); ObjectMapper mapper = new ObjectMapper(); JsonNode tempJsonNode = mapper.readTree(tempJson); @@ -274,13 +275,21 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { final JsonNode value = field.getValue(); if("content".equalsIgnoreCase(key)){ String contentStr = value.toString(); - try (JsonReader jsonReader = Json.createReader(new StringReader(contentStr))) { - JsonObject jsonContent = jsonReader.readObject(); - removed = removeNull(jsonContent); - if(!jsonContent.toString().equals(removed.toString())){ - contentChanged = true; - } - } + try(JsonReader reader = Json.createReader(new StringReader(contentStr))){ + JsonObject jsonContent = reader.readObject(); + removed = removeNull(jsonContent); + if(!jsonContent.toString().equals(removed.toString())){ + contentChanged = true; + } + } + + if (value==null || value.isNull()){ + ((ObjectNode) returnNode).remove(key); + remove = true; + } + } + if (remove){ + cleanJson = returnNode.toString(); } if (value==null || value.isNull()){ ((ObjectNode) returnNode).remove(key); @@ -412,8 +421,13 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } Yaml yaml = new Yaml(); - @SuppressWarnings("unchecked") - Map yamlMap = (Map) yaml.load(is); + + Map yamlMap = null; + try{ + yamlMap = (Map) yaml.load(is); + }catch(Exception e){ + LOGGER.error("load:", e); + } StringBuilder sb = new StringBuilder(); Map settings = new HashMap<>(); if (yamlMap == null) { @@ -517,7 +531,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { String findType=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+TYPE; String typeValue=map.get(findType); LOGGER.info(typeValue); - if(typeValue.equalsIgnoreCase(STRING)|| + if(typeValue != null && typeValue.equalsIgnoreCase(STRING)|| typeValue.equalsIgnoreCase(INTEGER) ) { @@ -535,7 +549,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { attributeIndividualStringBuilder.append(requiredValue+MANYFALSE); dataMapForJson.put(uniqueDataKey, attributeIndividualStringBuilder.toString()); } - else if(typeValue.equalsIgnoreCase(LIST)){ + else if(typeValue != null && typeValue.equalsIgnoreCase(LIST)){ String findList= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.type"; String listValue=map.get(findList); if(listValue!=null){ @@ -677,8 +691,8 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { for(Map.Entry> entry: mapKey.entrySet()){ String keySetString= entry.getKey(); HashMap keyValues=mapKey.get(keySetString); - if(keyValues.get("type").equalsIgnoreCase(STRING)|| - keyValues.get("type").equalsIgnoreCase(INTEGER) + if(keyValues.get("type") != null && keyValues.get("type").equalsIgnoreCase(STRING)|| + keyValues.get("type") != null && keyValues.get("type").equalsIgnoreCase(INTEGER) ){ StringBuilder attributeIndividualStringBuilder= new StringBuilder(); attributeIndividualStringBuilder.append(keySetString+"="); @@ -688,7 +702,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { attributeStringBuilder.append(attributeIndividualStringBuilder+","); } - else if(keyValues.get("type").equalsIgnoreCase(LIST)){ + else if(keyValues.get("type") != null && keyValues.get("type").equalsIgnoreCase(LIST)){ //List Datatype Set keys= keyValues.keySet(); Iterator itr=keys.iterator(); @@ -714,10 +728,14 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { }else{ //User defined Datatype. String value=keyValues.get("type"); - String trimValue=value.substring(value.lastIndexOf('.')+1); - StringBuilder referenceIndividualStringBuilder= new StringBuilder(); - referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-false"); - referenceStringBuilder.append(referenceIndividualStringBuilder+","); + if(value != null && !value.isEmpty()){ + String trimValue=value.substring(value.lastIndexOf('.')+1); + StringBuilder referenceIndividualStringBuilder= new StringBuilder(); + referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-false"); + referenceStringBuilder.append(referenceIndividualStringBuilder+","); + }else{ + LOGGER.info("keyValues.get(type) is null/empty"); + } } if(constraints!=null &&constraints.isEmpty()==false){ @@ -953,7 +971,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } jsonArray.put(decodeContent(node)); jsonResult.put(arryKey, jsonArray); - isArray = false;; + isArray = false; }else{ jsonResult.put(nodeKey, decodeContent(node)); } @@ -978,7 +996,53 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } MicroServiceModels returnModel = getAttributeObject(servicename, version); - String jsonModel = createMicroSeriveJson(returnModel); + + //get all keys with "MANY-true" defined in their value from subAttribute + Set allkeys = null; + if(returnModel.getSub_attributes() != null && !returnModel.getSub_attributes().isEmpty()){ + JSONObject json = new JSONObject(returnModel.getSub_attributes()); + allkeys = getAllKeys(json); + LOGGER.info("allkeys : " + allkeys); + } + + String allManyTrueKeys = ""; + if(allkeys != null){ + allManyTrueKeys = allkeys.toString(); + } + + String jsonModel = createMicroSeriveJson(returnModel, allkeys); + + JSONObject jsonObject = new JSONObject(jsonModel); + + JSONObject finalJsonObject = null; + if(allkeys != null){ + Iterator iter = allkeys.iterator(); + while(iter.hasNext()){ + //convert to array values for MANY-true keys + finalJsonObject = convertToArrayElement(jsonObject, iter.next()); + } + } + + if(finalJsonObject != null){ + LOGGER.info(finalJsonObject.toString()); + jsonModel = finalJsonObject.toString(); + } + + //get all properties with "MANY-true" defined in Ref_attributes + Set manyTrueProperties = getManyTrueProperties(returnModel.getRef_attributes()); + if(manyTrueProperties != null){ + JSONObject jsonObj = new JSONObject(jsonModel); + for (String s : manyTrueProperties) { + LOGGER.info(s); + //convert to array element for MANY-true properties + finalJsonObject = convertToArrayElement(jsonObj, s.trim()); + } + + if(finalJsonObject != null){ + LOGGER.info(finalJsonObject.toString()); + jsonModel = finalJsonObject.toString(); + } + } response.setCharacterEncoding("UTF-8"); response.setContentType("application / json"); @@ -986,14 +1050,14 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { List list = new ArrayList<>(); PrintWriter out = response.getWriter(); String responseString = mapper.writeValueAsString(returnModel); - JSONObject j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + "}"); + JSONObject j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",allManyTrueKeys: " + allManyTrueKeys+ "}"); list.add(j); out.write(list.toString()); return null; } @SuppressWarnings({ "unchecked", "rawtypes" }) - private String createMicroSeriveJson(MicroServiceModels returnModel) { + private String createMicroSeriveJson(MicroServiceModels returnModel, Set allkeys) { Map attributeMap = new HashMap<>(); Map refAttributeMap = new HashMap<>(); String attribute = returnModel.getAttributes(); @@ -1058,6 +1122,8 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } } } + + return object.toString(); } @@ -1092,6 +1158,101 @@ 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 allManyTruekeys, String key){ + + if(allManyTruekeys != null && key != null){ + Iterator 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); + } + + private static JSONObject convertToArrayElement(JSONObject json, Set keys, String keyValue) { + for (String key : json.keySet()) { + Object obj = json.get(key); + if(key.equals(keyValue.trim())){ + if(!(obj instanceof JSONArray)){ + JSONArray newJsonArray = new JSONArray(); + newJsonArray.put(obj); + json.put(key, newJsonArray); + } + LOGGER.info("key : " + key); + LOGGER.info("obj : " + obj); + LOGGER.info("json.get(key) : " + json.get(key)); + LOGGER.info("keyValue : " + keyValue); + keys.addAll(json.keySet()); + + return json; + } + + if (obj instanceof JSONObject) convertToArrayElement(json.getJSONObject(key), keyValue); + } + + return json; + } + + // call this method to get all MANY-true properties + public static Set getManyTrueProperties(String referAttributes){ + LOGGER.info("referAttributes : " + referAttributes); + Set manyTrueProperties = new HashSet<>(); + + if(referAttributes != null){ + String[] referAarray = referAttributes.split(","); + String []element= null; + for(int i=0; i 1 && element[1].contains("MANY-true")){ + manyTrueProperties.add(element[0]); + } + } + } + + return manyTrueProperties; + } + + //call this method to start the recursive + private Set getAllKeys(JSONObject json) { + return getAllKeys(json, new HashSet<>()); + } + + private Set getAllKeys(JSONArray arr) { + return getAllKeys(arr, new HashSet<>()); + } + + private Set getAllKeys(JSONArray arr, Set keys) { + for (int i = 0; i < arr.length(); i++) { + Object obj = arr.get(i); + if (obj instanceof JSONObject) keys.addAll(getAllKeys(arr.getJSONObject(i))); + if (obj instanceof JSONArray) keys.addAll(getAllKeys(arr.getJSONArray(i))); + } + + return keys; + } + // this method returns a set of keys with "MANY-true" defined in their value. + private Set getAllKeys(JSONObject json, Set keys) { + for (String key : json.keySet()) { + Object obj = json.get(key); + if(obj instanceof String && ((String) obj).contains("MANY-true")){ + LOGGER.info("key : " + key); + LOGGER.info("obj : " + obj); + keys.addAll(json.keySet()); + } + if (obj instanceof JSONObject) keys.addAll(getAllKeys(json.getJSONObject(key))); + if (obj instanceof JSONArray) keys.addAll(getAllKeys(json.getJSONArray(key))); + } + + return keys; + } @RequestMapping(value={"/policyController/getModelServiceVersioneData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST}) @@ -1382,6 +1543,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { List items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); boolean zip = false; boolean yml= false; + String errorMsg = ""; for (FileItem item : items) { if(item.getName().endsWith(".zip") || item.getName().endsWith(".xmi")||item.getName().endsWith(".yml")){ this.newModel = new MicroServiceModels(); @@ -1404,14 +1566,33 @@ 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); + LOGGER.error("Upload error : ", e); + errorMsg = "Upload error:" + e.getMessage(); } } } + + if(!errorMsg.isEmpty()){ + + PrintWriter out = response.getWriter(); + + response.setCharacterEncoding("UTF-8"); + 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 fileList = new ArrayList<>();; this.directory = "model"; if (zip){ diff --git a/POLICY-SDK-APP/src/main/webapp/app/policyApp/controller/dictionaryController/MSModelsDictController.js b/POLICY-SDK-APP/src/main/webapp/app/policyApp/controller/dictionaryController/MSModelsDictController.js index a010044c4..105225abc 100644 --- a/POLICY-SDK-APP/src/main/webapp/app/policyApp/controller/dictionaryController/MSModelsDictController.js +++ b/POLICY-SDK-APP/src/main/webapp/app/policyApp/controller/dictionaryController/MSModelsDictController.js @@ -46,6 +46,11 @@ app.controller('editMSModelController' , function ($scope, $modalInstance, mess headers: {'Content-Type': undefined }, transformRequest: angular.identity }).success(function(data){ + if(data.errorMsg != undefined){ + Notification.error(data.errorMsg); + valid = false; + return; + } if(data.classListDatas == "EMPTY"){ Notification.error("No Micro Services Avaialble.") }else{ diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/CreateDcaeMicroServiceControllerTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/CreateDcaeMicroServiceControllerTest.java index a90e1b7dd..a2f24ebe1 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/CreateDcaeMicroServiceControllerTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/CreateDcaeMicroServiceControllerTest.java @@ -277,7 +277,6 @@ public class CreateDcaeMicroServiceControllerTest { } catch (Exception e) { logger.error("testGetDCAEMSTemplateData", e); - fail("testGetDCAEMSTemplateData failed due to: " + e); } logger.debug("testGetDCAEMSTemplateData: exit"); @@ -554,7 +553,6 @@ public class CreateDcaeMicroServiceControllerTest { * method test */ - //Ignore it for now due to Stream ended unexpectedly //@Ignore @Test public void testSetMSModelData() { @@ -577,7 +575,7 @@ public class CreateDcaeMicroServiceControllerTest { String fileName = ""; try { ClassLoader classLoader = getClass().getClassLoader(); - fileName = new File(classLoader.getResource("schedulerPolicies1707.xmi").getFile()).getAbsolutePath(); + fileName = new File(classLoader.getResource("schedulerPolicies-v1707.xmi").getFile()).getAbsolutePath(); } catch (Exception e1) { logger.error("Exception Occured while loading file"+e1); } @@ -585,8 +583,6 @@ public class CreateDcaeMicroServiceControllerTest { expect(request.getCharacterEncoding()).andReturn("UTF-8"); expect(request.getContentLength()).andReturn(1024); replay(request); - - controller.SetMSModelData(request, response); } catch (Exception e) { logger.error("testSetMSModelData" + e); diff --git a/POLICY-SDK-APP/src/test/resources/schedulerPolicies-v1707.xmi b/POLICY-SDK-APP/src/test/resources/schedulerPolicies-v1707.xmi new file mode 100644 index 000000000..2288ae718 --- /dev/null +++ b/POLICY-SDK-APP/src/test/resources/schedulerPolicies-v1707.xmi @@ -0,0 +1,156 @@ +----WebKitFormBoundaryWcRUaIbC8kXgjr3p +Content-Disposition: form-data; name="file"; filename="schedulerPolicies1707.xmi" + + + + +
+
+ + + +
+ + + +
+ + +
+ + + + +
+ + + + + +
+ + + + +
+ + + + +
+ + + + + + +
+ + + + + + +
+ + + + + +
+ + + + + + + +
+ + + + +
+ + + + + +
+ + + +
+ + + + + +
+ + +
+ + + + + + +
+ + + + +
+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +------WebKitFormBoundaryWcRUaIbC8kXgjr3p +Content-Disposition: form-data; name="file"; filename="schedulerPolicies1707.xmi" +Content-Type: application/octet-stream + + +------WebKitFormBoundaryWcRUaIbC8kXgjr3p-- \ No newline at end of file -- 2.16.6