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=422c18a316f28e2c10e0436822d2ba16676f576b;hb=6e00f46e0c2ee4dbbdb0e222e6f361210d1a0da1;hp=e9a121b97eb23cefddab66060fd24b1ac813ca27;hpb=a6b8bc8875402ab721a8529926d01b2565d0f595;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 e9a121b97..422c18a31 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 @@ -113,7 +113,7 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType; @RequestMapping("/") public class CreateDcaeMicroServiceController extends RestrictedBaseController { private static final Logger LOGGER = FlexLogger.getLogger(CreateDcaeMicroServiceController.class); - + private Map matchableValues; private static CommonClassDao commonClassDao; public static CommonClassDao getCommonClassDao() { @@ -151,14 +151,16 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { public static final String DEFAULT=".default"; public static final String REQUIRED=".required"; public static final String MANYFALSE=":MANY-false"; - + public static final String MATCHABLE=".matchable"; @Autowired private CreateDcaeMicroServiceController(CommonClassDao commonClassDao){ CreateDcaeMicroServiceController.commonClassDao = commonClassDao; } - public CreateDcaeMicroServiceController(){} + public CreateDcaeMicroServiceController(){ + // Empty Constructor + } protected PolicyRestAdapter policyAdapter = null; private int priorityCount; @@ -189,8 +191,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } private GroupPolicyScopeList getPolicyObject(String policyScope) { - GroupPolicyScopeList groupList= (GroupPolicyScopeList) commonClassDao.getEntityItem(GroupPolicyScopeList.class, "name", policyScope); - return groupList; + return (GroupPolicyScopeList) commonClassDao.getEntityItem(GroupPolicyScopeList.class, "name", policyScope); } private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue) { @@ -432,7 +433,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { return settings; } - public Map load(byte[] source) throws IOException { + public Map load(byte[] source) { Yaml yaml = new Yaml(); @SuppressWarnings("unchecked") Map yamlMap = (Map) yaml.load(Arrays.toString(source)); @@ -519,6 +520,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { HashMap parseDataNodes(Map map){ HashMap dataMapForJson=new HashMap <>(); + matchableValues = new HashMap <>(); for(String uniqueDataKey: uniqueDataKeys){ if(uniqueDataKey.contains("%")){ String[] uniqueDataKeySplit= uniqueDataKey.split("%"); @@ -537,6 +539,15 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { String requiredValue= map.get(findRequired); LOGGER.info("requiredValue is:"+ requiredValue); + String matchable =DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+MATCHABLE; + + String matchableValue= map.get(matchable); + + if("true".equalsIgnoreCase(matchableValue)){ + String key=uniqueDataKeySplit[uniqueDataKeySplit.length -1]; + matchableValues.put(key, "matching-true"); + } + StringBuilder attributeIndividualStringBuilder= new StringBuilder(); attributeIndividualStringBuilder.append(typeValue+":defaultValue-"); attributeIndividualStringBuilder.append(defaultValue+":required-"); @@ -544,6 +555,8 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { dataMapForJson.put(uniqueDataKey, attributeIndividualStringBuilder.toString()); } 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){ @@ -568,6 +581,9 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { break; } else{ + if(constraintsValue.contains("=")){ + constraintsValue = constraintsValue.replace("=", "equal-sign"); + } dataConstraints.add(constraintsValue); dataListBuffer.append(constraintsValue+","); } @@ -587,6 +603,8 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString()); } + }else{ + matchableValues.put(uniqueDataKey, "matching-true"); } } return dataMapForJson; @@ -694,15 +712,22 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { attributeIndividualStringBuilder.append(keyValues.get("default")+":required-"); attributeIndividualStringBuilder.append(keyValues.get("required")+":MANY-false"); attributeStringBuilder.append(attributeIndividualStringBuilder+","); - + if("true".equalsIgnoreCase(keyValues.get("matchable"))){ + matchableValues.put(keySetString, "matching-true"); + } } else if(keyValues.get("type") != null && keyValues.get("type").equalsIgnoreCase(LIST)){ + + if("true".equalsIgnoreCase(keyValues.get("matchable"))){ + matchableValues.put(keySetString, "matching-true"); + } //List Datatype Set keys= keyValues.keySet(); Iterator itr=keys.iterator(); + boolean isDefinedType = false; while(itr.hasNext()){ String key= itr.next(); - if((!("type").equals(key) ||("required").equals(key))) + if(!("type").equals(key) ||("required").equals(key)) { String value= keyValues.get(key); //The "." in the value determines if its a string or a user defined type. @@ -712,23 +737,26 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { constraints.add(keyValues.get(key)); } }else{ - //This is user defined string + //This is user defined type String trimValue=value.substring(value.lastIndexOf('.')+1); StringBuilder referenceIndividualStringBuilder= new StringBuilder(); referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-true"); referenceStringBuilder.append(referenceIndividualStringBuilder+","); + isDefinedType = true; } } } - if(keyValues.get("type").equalsIgnoreCase(LIST)){ - if(constraints == null || constraints.isEmpty()){ + if(!isDefinedType && keyValues.get("type").equalsIgnoreCase(LIST) && + (constraints == null || constraints.isEmpty()) ) { //type is list but no constraints defined. referenceStringBuilder.append(keySetString+"=MANY-true"+","); - } } }else{ //User defined Datatype. + if("true".equalsIgnoreCase(keyValues.get("matchable"))){ + matchableValues.put(keySetString, "matching-true"); + } String value=keyValues.get("type"); if(value != null && !value.isEmpty()){ String trimValue=value.substring(value.lastIndexOf('.')+1); @@ -830,8 +858,10 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { JsonNodeFactory nodeFactory = JsonNodeFactory.instance; ObjectNode node = nodeFactory.objectNode(); String prevKey = null; - String presKey = null; - for(String key: element.keySet()){ + String presKey; + for(Entry entry: element.entrySet()){ + String key = entry.getKey(); + String value = entry.getValue(); if(key.contains(".")){ presKey = key.substring(0,key.indexOf('.')); }else if(key.contains("@")){ @@ -859,7 +889,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { nodeKey = key.substring(0,key.indexOf('.')); } if(nodeKey.equals(key.substring(0,key.indexOf('.')))){ - node.put(key.substring(key.indexOf('.')+1), element.get(key)); + node.put(key.substring(key.indexOf('.')+1), value); }else{ if(node.size()!=0){ if(nodeKey.contains("@")){ @@ -888,54 +918,30 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { if(nodeKey.contains("@")){ arryKey = nodeKey.substring(0,nodeKey.indexOf('@')); } - node.put(key.substring(key.indexOf('.')+1), element.get(key)); + node.put(key.substring(key.indexOf('.')+1), value); } - }else if(node.size()!=0){ - if(nodeKey.contains("@")){ - if(arryKey==null){ - 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('@')))){ - jsonArray.put(decodeContent(node)); - } - jsonResult.put(arryKey, jsonArray); - jsonArray = new JSONArray(); - arryKey = nodeKey.substring(0,nodeKey.indexOf('@')); - }else{ - isArray = false; - jsonResult.put(nodeKey, decodeContent(node)); - } - node = nodeFactory.objectNode(); - if(key.contains("@")){ - isArray = true; - if(key.endsWith("@0")|| jsonArray==null){ + }else { + if(node.size()!=0){ + if(nodeKey.contains("@")){ + if(arryKey==null){ + 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('@')))){ + jsonArray.put(decodeContent(node)); + } + jsonResult.put(arryKey, jsonArray); jsonArray = new JSONArray(); - } - }else if(!key.contains("@")){ - isArray = false; - } - if(isArray){ - if(oldValue==null){ - oldValue = key.substring(0,key.indexOf('@')); - } - if(oldValue!=prevKey){ - oldValue = key.substring(0,key.indexOf('@')); - } - if(oldValue.equals(key.substring(0,key.indexOf('@')))){ - jsonArray.put(element.get(key)); + arryKey = nodeKey.substring(0,nodeKey.indexOf('@')); }else{ - jsonResult.put(oldValue, jsonArray); - jsonArray = new JSONArray(); + isArray = false; + jsonResult.put(nodeKey, decodeContent(node)); } - oldValue = key.substring(0,key.indexOf('@')); - }else{ - jsonResult.put(key, element.get(key)); + node = nodeFactory.objectNode(); } - }else{ if(key.contains("@")){ isArray = true; if(key.endsWith("@0")|| jsonArray==null){ @@ -952,14 +958,14 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { oldValue = key.substring(0,key.indexOf('@')); } if(oldValue.equals(key.substring(0,key.indexOf('@')))){ - jsonArray.put(element.get(key)); + jsonArray.put(value); }else{ jsonResult.put(oldValue, jsonArray); jsonArray = new JSONArray(); } oldValue = key.substring(0,key.indexOf('@')); }else{ - jsonResult.put(key, element.get(key)); + jsonResult.put(key, value); } } } @@ -1009,9 +1015,9 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { LOGGER.info("allkeys : " + allkeys); } - String allManyTrueKeys = ""; + String nameOfTrueKey = ""; if(allkeys != null){ - allManyTrueKeys = allkeys.toString(); + nameOfTrueKey = allkeys.toString(); } String jsonModel = createMicroSeriveJson(returnModel, allkeys); @@ -1054,11 +1060,11 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { List list = new ArrayList<>(); PrintWriter out = response.getWriter(); String responseString = mapper.writeValueAsString(returnModel); - JSONObject j = null; - if("".equals(allManyTrueKeys)){ + JSONObject j; + if("".equals(nameOfTrueKey)){ j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + "}"); }else{ - j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",allManyTrueKeys: " + allManyTrueKeys+ "}"); + j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",allManyTrueKeys: " + nameOfTrueKey+ "}"); } list.add(j); out.write(list.toString()); @@ -1099,12 +1105,12 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class); JSONObject object = new JSONObject(); - JSONArray array = new JSONArray(); + JSONArray array; for (Entry keySet : attributeMap.entrySet()){ array = new JSONArray(); String value = keySet.getValue(); - if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){ + if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])){ array.put(value); object.put(keySet.getKey().trim(), array); }else { @@ -1116,14 +1122,14 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { array = new JSONArray(); String value = keySet.getValue().split(":")[0]; if (gsonObject.containsKey(value)){ - if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){ + if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])){ array.put(recursiveReference(value, gsonObject, enumAttribute)); object.put(keySet.getKey().trim(), array); }else { object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute)); } }else { - if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){ + if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])){ array.put(value.trim()); object.put(keySet.getKey().trim(), array); }else { @@ -1140,23 +1146,23 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { @SuppressWarnings("unchecked") private JSONObject recursiveReference(String name, Map subAttributeMap, String enumAttribute) { JSONObject object = new JSONObject(); - Map map = new HashMap<>(); + Map map; Object returnClass = subAttributeMap.get(name); map = (Map) returnClass; - JSONArray array = new JSONArray(); + JSONArray array; for( Entry m:map.entrySet()){ String[] splitValue = m.getValue().split(":"); array = new JSONArray(); if (subAttributeMap.containsKey(splitValue[0])){ - if (m.getValue().split("MANY-")[1].equalsIgnoreCase("true")){ + if ("true".equalsIgnoreCase(m.getValue().split("MANY-")[1])){ array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute)); object.put(m.getKey().trim(), array); }else { object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute)); } } else{ - if (m.getValue().split("MANY-")[1].equalsIgnoreCase("true")){ + if ("true".equalsIgnoreCase(m.getValue().split("MANY-")[1])){ array.put(splitValue[0].trim()); object.put(m.getKey().trim(), array); }else { @@ -1204,7 +1210,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { if(referAttributes != null){ String[] referAarray = referAttributes.split(","); - String []element= null; + String []element; for(int i=0; i 1 && element[1].contains("MANY-true")){ @@ -1258,7 +1264,7 @@ 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]; Set returnList = getVersionList(servicename); response.setCharacterEncoding("UTF-8"); @@ -1274,7 +1280,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } private Set getVersionList(String name) { - MicroServiceModels workingModel = new MicroServiceModels(); + MicroServiceModels workingModel; Set list = new HashSet<>(); List microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name); for (int i = 0; i < microServiceModelsData.size(); i++) { @@ -1374,28 +1380,28 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { AttributeDesignatorType designator = match.getAttributeDesignator(); String attributeId = designator.getAttributeId(); // First match in the target is OnapName, so set that value. - if (attributeId.equals("ONAPName")) { + if ("ONAPName".equals(attributeId)) { policyAdapter.setOnapName(value); } - if (attributeId.equals("ConfigName")){ + if ("ConfigName".equals(attributeId)){ policyAdapter.setConfigName(value); } - if (attributeId.equals("uuid")){ + if ("uuid".equals(attributeId)){ policyAdapter.setUuid(value); } - if (attributeId.equals("location")){ + if ("location".equals(attributeId)){ policyAdapter.setLocation(value); } - if (attributeId.equals("RiskType")){ + if ("RiskType".equals(attributeId)){ policyAdapter.setRiskType(value); } - if (attributeId.equals("RiskLevel")){ + if ("RiskLevel".equals(attributeId)){ policyAdapter.setRiskLevel(value); } - if (attributeId.equals("guard")){ + if ("guard".equals(attributeId)){ policyAdapter.setGuard(value); } - if (attributeId.equals("TTLDate") && !value.contains("NA")){ + if ("TTLDate".equals(attributeId) && !value.contains("NA")){ PolicyController controller = new PolicyController(); String newDate = controller.convertDate(value); policyAdapter.setTtlDate(newDate); @@ -1426,7 +1432,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { String policyScopeName = null; ObjectMapper mapper = new ObjectMapper(); try { - DCAEMicroServiceObject msBody = (DCAEMicroServiceObject) mapper.readValue(entity.getConfigurationData().getConfigBody(), DCAEMicroServiceObject.class); + DCAEMicroServiceObject msBody = mapper.readValue(entity.getConfigurationData().getConfigBody(), DCAEMicroServiceObject.class); policyScopeName = getPolicyScope(msBody.getPolicyScope()); policyAdapter.setPolicyScope(policyScopeName); @@ -1459,8 +1465,8 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { if(value instanceof LinkedHashMap){ LinkedHashMap secondObjec = new LinkedHashMap<>(); readRecursivlyJSONContent((LinkedHashMap) value, secondObjec); - for(String objKey: secondObjec.keySet()){ - data.put(key+"." +objKey, secondObjec.get(objKey)); + for( Entry entry : secondObjec.entrySet()){ + data.put(key+"." + entry.getKey(), entry.getValue()); } }else if(value instanceof ArrayList){ ArrayList jsonArrayVal = (ArrayList)value; @@ -1469,8 +1475,8 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { if(arrayvalue instanceof LinkedHashMap){ LinkedHashMap newData = new LinkedHashMap<>(); readRecursivlyJSONContent((LinkedHashMap) arrayvalue, newData); - for(String objKey: newData.keySet()){ - data.put(key+"@"+i+"." +objKey, newData.get(objKey)); + for(Entry entry: newData.entrySet()){ + data.put(key+"@"+i+"." +entry.getKey(), entry.getValue()); } }else if(arrayvalue instanceof ArrayList){ ArrayList jsonArrayVal1 = (ArrayList)value; @@ -1500,7 +1506,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { //Convert the map values and set into JSON body public Map convertMap(Map attributesMap, Map attributesRefMap) { Map attribute = new HashMap<>(); - String temp = null; + StringBuilder temp; String key; String value; for (Entry entry : attributesMap.entrySet()) { @@ -1523,9 +1529,10 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { temp = null; for (Object textList : arrayList.getValue()){ if (temp == null){ - temp = "[" + textList; + temp = new StringBuilder(); + temp.append("[" + textList); }else{ - temp = temp + "," + textList; + temp.append("," + textList); } } attribute.put(key, temp+ "]"); @@ -1604,7 +1611,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { File file = new File(this.newFile); fileList.add(file); } - String modelType= ""; + String modelType; if(! yml){ modelType="xmi"; //Process Main Model file first @@ -1631,7 +1638,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { msAttributes.setAttribute(returnAttributeList); msAttributes.setSubClass(this.retmap); - + msAttributes.setMatchingSet(matchableValues); HashMap returnReferenceList =new HashMap<>(); returnReferenceList.put(className, this.referenceAttributes); msAttributes.setRefAttribute(returnReferenceList); @@ -1675,9 +1682,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { int BUFFER = 2048; File file = new File(zipFile); - ZipFile zip = null; - try { - zip = new ZipFile(file); + try (ZipFile zip = new ZipFile(file)) { String newPath = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4); this.directory = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4); checkZipDirectory(this.directory); @@ -1697,15 +1702,20 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { if (!entry.isDirectory()){ BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; - byte data[] = new byte[BUFFER]; - FileOutputStream fos = new FileOutputStream(destFile); - BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); - while ((currentByte = is.read(data, 0, BUFFER)) != -1) { - dest.write(data, 0, currentByte); + byte[] data = new byte[BUFFER]; + try (FileOutputStream fos = new FileOutputStream(destFile); + BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) { + while ((currentByte = is.read(data, 0, BUFFER)) != -1) { + dest.write(data, 0, currentByte); + } + dest.flush(); + } catch (IOException e) { + LOGGER.error("Failed to write zip contents to {}" + destFile + e); + // + // PLD should I throw e? + // + throw e; } - dest.flush(); - dest.close(); - is.close(); } if (currentEntry.endsWith(".zip")){ @@ -1714,20 +1724,13 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } } catch (IOException e) { LOGGER.error("Failed to unzip model file " + zipFile, e); - }finally{ - try { - if(zip != null) - zip.close(); - } catch (IOException e) { - LOGGER.error("Exception Occured While closing zipfile " + e); - } } } private void retreiveDependency(String workingFile, Boolean modelClass) { MSModelUtils utils = new MSModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName()); - HashMap tempMap = new HashMap<>(); + Map tempMap; tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI); @@ -1739,9 +1742,9 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } private List listModelFiles(String directoryName) { - File directory = new File(directoryName); + File fileDirectory = new File(directoryName); List resultList = new ArrayList<>(); - File[] fList = directory.listFiles(); + File[] fList = fileDirectory.listFiles(); for (File file : fList) { if (file.isFile()) { resultList.add(file);