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=4be31dd1ef53ee77deaa1d134c8394de7601e8a9;hb=HEAD;hp=2549e216700d7f875b40ac9d7941cb0776b870e6;hpb=3c95fa7249cccc3d0eb3c9e89ba708a3167d41ba;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 2549e2167..4be31dd1e 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Engine * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,7 +30,6 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.gson.Gson; - import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; @@ -38,10 +37,12 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.StringReader; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Base64; import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; @@ -58,7 +59,6 @@ import java.util.UUID; import java.util.regex.Pattern; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; - import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonArrayBuilder; @@ -72,12 +72,9 @@ import lombok.Getter; import lombok.Setter; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType; -import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType; -import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType; - import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; @@ -85,20 +82,22 @@ import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringEscapeUtils; 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.rest.XACMLRestProperties; +import org.onap.policy.rest.XacmlRestProperties; import org.onap.policy.rest.adapter.PolicyRestAdapter; import org.onap.policy.rest.dao.CommonClassDao; import org.onap.policy.rest.jpa.GroupPolicyScopeList; import org.onap.policy.rest.jpa.MicroServiceModels; import org.onap.policy.rest.jpa.MicroserviceHeaderdeFaults; 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.onap.policy.rest.util.MsAttributeObject; +import org.onap.policy.rest.util.MsModelUtils; +import org.onap.policy.rest.util.MsModelUtils.ModelType; +import org.onap.policy.utils.PolicyUtils; import org.onap.portalsdk.core.controller.RestrictedBaseController; import org.onap.portalsdk.core.web.support.JsonMessage; import org.springframework.beans.factory.annotation.Autowired; @@ -127,7 +126,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { private String directory; private List modelList = new ArrayList<>(); private List dirDependencyList = new ArrayList<>(); - private LinkedHashMap classMap = new LinkedHashMap<>(); + private LinkedHashMap classMap = new LinkedHashMap<>(); String referenceAttributes; String attributeString; Set allManyTrueKeys = null; @@ -176,9 +175,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { String tempJson = root.get(POLICYJSON).toString(); JSONObject policyJson = new JSONObject(root.get(POLICYJSON).toString()); - if (policyJson != null) { - tempJson = saveOriginalJsonObject(policyJson, jsonStringValues).toString(); - } + tempJson = decodeJsonVal(tempJson, policyJson); // ---replace empty value with the value below before calling decodeContent method. String dummyValue = "*empty-value*" + UUID.randomUUID().toString(); LOGGER.info("dummyValue:" + dummyValue); @@ -199,12 +196,28 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { JSONObject content = contentJson.getJSONObject("content"); content = setOriginalJsonObject(content, jsonStringValues); contentJson.put("content", content); - policyData.setJsonBody(contentJson.toString()); + policyData.setJsonBody(StringEscapeUtils.unescapeJava(contentJson.toString())); } return policyData; } + private String decodeJsonVal(String tempJson, JSONObject policyJson) { + if (policyJson != null) { + for (Object key : policyJson.keySet()) { + String keyStr = (String) key; + Object keyvalue = policyJson.get(keyStr); + String decodedString = keyvalue.toString();//.replace("\"", ""); + if(!decodedString.equals("true") && !decodedString.equals("false")) { + String decodedJson = new String(Base64.getDecoder().decode(decodedString), StandardCharsets.UTF_8).replace("\"", "\\\""); + policyJson.put(keyStr, decodedJson); + } + } + tempJson = saveOriginalJsonObject(policyJson, jsonStringValues).toString(); + } + return tempJson; + } + private JSONObject saveOriginalJsonObject(JSONObject jsonObj, Map jsonStringValues) { for (Object key : jsonObj.keySet()) { String keyStr = (String) key; @@ -269,7 +282,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { String json = ""; DCAEMicroServiceObject microServiceObject = new DCAEMicroServiceObject(); MicroServiceModels returnModel = new MicroServiceModels(); - microServiceObject.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_MS)); + microServiceObject.setTemplateVersion(XACMLProperties.getProperty(XacmlRestProperties.TEMPLATE_VERSION_MS)); if (policyAdapter.getServiceType() != null) { microServiceObject.setService(policyAdapter.getServiceType()); microServiceObject.setVersion(policyAdapter.getVersion()); @@ -565,14 +578,13 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { if (arrOfKeys[i].contains(".")) { arrOfKeys[i] = arrOfKeys[i].substring(arrOfKeys[i].indexOf(".") + 1); if (arrOfKeys[i].equals(key)) { - return StringUtils.replaceEach(jsonStringValues.get(k), new String[] {"\""}, - new String[] {"\\\""}); + return jsonStringValues.get(k); } } } } if (k.endsWith(key)) { - return StringUtils.replaceEach(jsonStringValues.get(k), new String[] {"\""}, new String[] {"\\\""}); + return jsonStringValues.get(k); } } @@ -646,25 +658,12 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { case OBJECT: JsonObject object = removeNull(obj.getJsonObject(key)); if (!object.isEmpty()) { - if (!jsonStringValues.isEmpty()) { - String originalValue = getOriginalValue(key); - if (originalValue != null) { - builder.add(key, object.toString()); - break; - } - } builder.add(key, object); } break; case STRING: String str = obj.getString(key); if (str != null && !str.isEmpty()) { - if (!jsonStringValues.isEmpty()) { - String originalValue = getOriginalValue(key); - if (originalValue != null) { - str = getOriginalValue(key); - } - } builder.add(key, str); } break; @@ -904,8 +903,8 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { // 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()); + if (returnModel.getSubAttributes() != null && !returnModel.getSubAttributes().isEmpty()) { + JSONObject json = new JSONObject(returnModel.getSubAttributes()); getAllKeys(json); allkeys = allManyTrueKeys; allManyTrueKeys = new TreeSet<>(); @@ -943,8 +942,8 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { jsonModel = finalJsonObject.toString(); } - // get all properties with "MANY-true" defined in Ref_attributes - Set manyTrueProperties = getManyTrueProperties(returnModel.getRef_attributes()); + // get all properties with "MANY-true" defined in RefAttributes + Set manyTrueProperties = getManyTrueProperties(returnModel.getRefAttributes()); if (manyTrueProperties != null) { JSONObject jsonObj = new JSONObject(jsonModel); for (String s : manyTrueProperties) { @@ -960,9 +959,9 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } } - response.setCharacterEncoding("UTF-8"); - response.setContentType("application / json"); - request.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); + response.setContentType(PolicyUtils.APPLICATION_JSON); + request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); List list = new ArrayList<>(); String responseString = mapper.writeValueAsString(returnModel); @@ -989,7 +988,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { if (attribute != null) { attribute = attribute.trim(); } - String refAttribute = returnModel.getRef_attributes(); + String refAttribute = returnModel.getRefAttributes(); if (refAttribute != null) { refAttribute = refAttribute.trim(); } @@ -1006,7 +1005,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { Gson gson = new Gson(); - String subAttributes = returnModel.getSub_attributes(); + String subAttributes = returnModel.getSubAttributes(); if (subAttributes != null) { subAttributes = subAttributes.trim(); } else { @@ -1109,7 +1108,13 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } if (obj instanceof JSONArray) { - convertToArrayElement(json.getJSONArray(key).getJSONObject(0), keyValue); + try { + if (json.getJSONArray(key).length() > 0) { + convertToArrayElement(json.getJSONArray(key).getJSONObject(0), keyValue); + } + } catch (Exception ex) { + LOGGER.info("XMI Model load issue : " + ex); + } } } @@ -1183,6 +1188,14 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { return keys; } + /** + * getModelServiceVersionData. + * + * @param request HttpServletRequest + * @param response HttpServletResponse + * @return ModelAndView + * @throws IOException IOException + */ @RequestMapping( value = {"/policyController/getModelServiceVersioneData.htm"}, method = {org.springframework.web.bind.annotation.RequestMethod.POST}) @@ -1191,9 +1204,9 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - response.setCharacterEncoding("UTF-8"); - response.setContentType("application / json"); - request.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); + response.setContentType(PolicyUtils.APPLICATION_JSON); + request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); List list = new ArrayList<>(); String value = mapper.readTree(request.getReader()).get("policyData").toString().replaceAll("^\"|\"$", ""); String servicename = value.split("-v")[0]; @@ -1311,29 +1324,9 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { // Under the match we have attribute value and // attributeDesignator. So,finally down to the actual attribute. // - AttributeValueType attributeValue = match.getAttributeValue(); - String value = (String) attributeValue.getContent().get(0); - AttributeDesignatorType designator = match.getAttributeDesignator(); - String attributeId = designator.getAttributeId(); // First match in the target is OnapName, so set that value. - if ("ONAPName".equals(attributeId)) { - policyAdapter.setOnapName(value); - } else if ("ConfigName".equals(attributeId)) { - policyAdapter.setConfigName(value); - } else if ("uuid".equals(attributeId)) { - policyAdapter.setUuid(value); - } else if ("location".equals(attributeId)) { - policyAdapter.setLocation(value); - } else if ("RiskType".equals(attributeId)) { - policyAdapter.setRiskType(value); - } else if ("RiskLevel".equals(attributeId)) { - policyAdapter.setRiskLevel(value); - } else if ("guard".equals(attributeId)) { - policyAdapter.setGuard(value); - } else if ("TTLDate".equals(attributeId) && !value.contains("NA")) { - String newDate = new PolicyController().convertDate(value); - policyAdapter.setTtlDate(newDate); - } + policyAdapter.setupUsingAttribute(match.getAttributeDesignator().getAttributeId(), + (String) match.getAttributeValue().getContent().get(0)); } readFile(policyAdapter, entity); } @@ -1397,6 +1390,12 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } + /** + * readRecursivlyJSONContent. + * + * @param map Map of String to something + * @param data Map of String to Object + */ @SuppressWarnings({"rawtypes", "unchecked"}) public void readRecursivlyJSONContent(Map map, Map data) { for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) { @@ -1546,7 +1545,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } List fileList = new ArrayList<>(); - MSModelUtils msModelUtils = new MSModelUtils(commonClassDao); + MsModelUtils msModelUtils = new MsModelUtils(commonClassDao); this.directory = "model"; if (zip) { extractFolder(this.newFile); @@ -1586,7 +1585,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { modelType = "yml"; modelList.add(this.newModel.getModelName()); String className = this.newModel.getModelName(); - MSAttributeObject msAttributes = new MSAttributeObject(); + MsAttributeObject msAttributes = new MsAttributeObject(); msAttributes.setClassName(className); LinkedHashMap returnAttributeList = new LinkedHashMap<>(); @@ -1688,10 +1687,10 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { private void retrieveDependency(String workingFile) { - MSModelUtils utils = new MSModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName()); - Map tempMap; + MsModelUtils utils = new MsModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName()); + Map tempMap; - tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI); + tempMap = utils.processEpackage(workingFile, ModelType.XMI); classMap.putAll(tempMap); LOGGER.info(tempMap); @@ -1741,7 +1740,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { private List createList() { List list = new ArrayList<>(); - for (Entry entrySet : classMap.entrySet()) { + for (Entry entrySet : classMap.entrySet()) { if (entrySet.getValue().isPolicyTempalate()) { list.add(entrySet.getKey()); }