X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=POLICY-SDK-APP%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fcontroller%2FCreateDcaeMicroServiceController.java;h=ff5ccffb033327567d4d6498e4bdc51019574995;hb=deb35bccd7b0d360668f393fe5b45b735b7956b2;hp=815b88f7000dfa9534743044c8c4e7c0f1e8b13f;hpb=7ea81288603904f0cb6f57936da44d6a3dd521a6;p=policy%2Fengine.git 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 815b88f70..ff5ccffb0 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 @@ -31,6 +31,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; +import java.io.StringReader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -47,9 +48,17 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; +import java.util.UUID; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonArrayBuilder; +import javax.json.JsonObject; +import javax.json.JsonObjectBuilder; +import javax.json.JsonReader; +import javax.json.JsonValue; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -64,6 +73,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.controller.PolicyController; import org.onap.policy.rest.XACMLRestProperties; import org.onap.policy.rest.adapter.PolicyRestAdapter; import org.onap.policy.rest.dao.CommonClassDao; @@ -159,8 +169,18 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { String jsonContent = null; try{ - jsonContent = decodeContent(root.get("policyJSON")).toString(); - constructJson(policyData, jsonContent); + LOGGER.info("policyJSON :" + (root.get("policyJSON")).toString()); + + String tempJson = root.get("policyJSON").toString(); + + //---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); + jsonContent = decodeContent(tempJsonNode).toString(); + constructJson(policyData, jsonContent, dummyValue); }catch(Exception e){ LOGGER.error("Error while decoding microservice content", e); } @@ -173,7 +193,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { return groupList; } - private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent) { + private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue) { ObjectWriter om = new ObjectMapper().writer(); String json=""; DCAEMicroServiceObject microServiceObject = new DCAEMicroServiceObject(); @@ -228,9 +248,13 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } catch (JsonProcessingException e) { LOGGER.error("Error writing out the object", e); } - LOGGER.info(json); + LOGGER.info("input json: " + json); + LOGGER.info("input jsonContent: " + jsonContent); String cleanJson = cleanUPJson(json); - cleanJson = removeNullAttributes(cleanJson); + //--- reset empty value back after called cleanUPJson method and before calling removeNullAttributes + String tempJson = StringUtils.replaceEach(cleanJson, new String[]{"\""+dummyValue+"\""}, new String[]{"\"\""}); + LOGGER.info("tempJson: " + tempJson); + cleanJson = removeNullAttributes(tempJson); policyAdapter.setJsonBody(cleanJson); return policyAdapter; } @@ -243,10 +267,30 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { JsonNode returnNode = mapper.readTree(cleanJson); Iterator> fieldsIterator = rootNode.fields(); boolean remove = false; + JsonObject removed = null; + boolean contentChanged = false; while (fieldsIterator.hasNext()) { Map.Entry field = fieldsIterator.next(); final String key = field.getKey(); final JsonNode value = field.getValue(); + if("content".equalsIgnoreCase(key)){ + String contentStr = value.toString(); + 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); remove = true; @@ -255,12 +299,91 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { if (remove){ cleanJson = returnNode.toString(); } + + if(contentChanged){ + //set modified content to cleanJson + JSONObject jObject = new JSONObject(cleanJson); + jObject.put("content",removed.toString()); + cleanJson = cleanUPJson(jObject.toString()); + } + } catch (IOException e) { LOGGER.error("Error writing out the JsonNode",e); } return cleanJson; } + public static JsonArray removeNull(JsonArray array) { + JsonArrayBuilder builder = Json.createArrayBuilder(); + int i = 0; + for (Iterator it = array.iterator(); it.hasNext(); ++i) { + JsonValue value = it.next(); + switch (value.getValueType()) { + case ARRAY: + JsonArray a = removeNull(array.getJsonArray(i)); + if (!a.isEmpty()) + builder.add(a); + break; + case OBJECT: + JsonObject object = removeNull(array.getJsonObject(i)); + if (!object.isEmpty()) + builder.add(object); + break; + case STRING: + String s = array.getString(i); + if (s != null && !s.isEmpty()) + builder.add(s); + break; + case NUMBER: + builder.add(array.getJsonNumber(i)); + break; + case TRUE: + case FALSE: + builder.add(array.getBoolean(i)); + break; + case NULL: + break; + } + } + return builder.build(); + } + + public static JsonObject removeNull(JsonObject obj) { + JsonObjectBuilder builder = Json.createObjectBuilder(); + for (Iterator> it = obj.entrySet().iterator(); it.hasNext();) { + Entry e = it.next(); + String key = e.getKey(); + JsonValue value = e.getValue(); + switch (value.getValueType()) { + case ARRAY: + JsonArray array = removeNull(obj.getJsonArray(key)); + if (!array.isEmpty()) + builder.add(key, array); + break; + case OBJECT: + JsonObject object = removeNull(obj.getJsonObject(key)); + if (!object.isEmpty()) + builder.add(key, object); + break; + case STRING: + String s = obj.getString(key); + if (s != null && !s.isEmpty()) + builder.add(key, s); + break; + case NUMBER: + builder.add(key, obj.getJsonNumber(key)); + break; + case TRUE: + case FALSE: + builder.add(key, obj.getBoolean(key)); + break; + case NULL: + break; + } + } + return builder.build(); + } + // Second index of dot should be returned. public int stringBetweenDots(String str){ String stringToSearch=str; @@ -298,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) { @@ -403,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) ) { @@ -421,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){ @@ -563,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+"="); @@ -574,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(); @@ -600,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){ @@ -839,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)); } @@ -864,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"); @@ -872,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(); @@ -944,6 +1122,8 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } } } + + return object.toString(); } @@ -978,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}) @@ -1125,7 +1400,8 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { policyAdapter.setGuard(value); } if (attributeId.equals("TTLDate") && !value.contains("NA")){ - String newDate = convertDate(value, true); + PolicyController controller = new PolicyController(); + String newDate = controller.convertDate(value); policyAdapter.setTtlDate(newDate); } } @@ -1138,15 +1414,6 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } } } - - private String convertDate(String dateTTL, boolean portalType) { - String formateDate = null; - String[] date = dateTTL.split("T"); - String[] parts = date[0].split("-"); - - formateDate = parts[2] + "-" + parts[1] + "-" + parts[0]; - return formateDate; - } public static Map convert(String str, String split) { Map map = new HashMap<>(); @@ -1273,9 +1540,18 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { @RequestMapping(value={"/ms_dictionary/set_MSModelData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST}) public void SetMSModelData(HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException{ + modelList = new ArrayList<>(); + dirDependencyList = new ArrayList<>(); + classMap = new HashMap<>(); + retmap = new HashMap<>(); + uniqueKeys= new HashSet<>(); + uniqueDataKeys= new HashSet<>(); + dataListBuffer=new StringBuilder(); + dataConstraints= new ArrayList <>(); 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(); @@ -1298,14 +1574,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){