X-Git-Url: https://gerrit.onap.org/r/gitweb?p=policy%2Fengine.git;a=blobdiff_plain;f=ONAP-PAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpap%2Fxacml%2Frest%2Fcomponents%2FCreateNewMicroServiceModel.java;h=ea5e8aa431a7e25a3530223af4c4385e2dec25cc;hp=abb425131abe3b4ec0264b5fe184256024f9db40;hb=refs%2Fchanges%2F84%2F98384%2F3;hpb=8cf5ed300190ab386d95758e6d4be6c38e67123d diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateNewMicroServiceModel.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateNewMicroServiceModel.java index abb425131..ea5e8aa43 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateNewMicroServiceModel.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateNewMicroServiceModel.java @@ -2,15 +2,16 @@ * ============LICENSE_START======================================================= * ONAP-PAP-REST * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -21,6 +22,8 @@ package org.onap.policy.pap.xacml.rest.components; +import com.google.gson.Gson; + import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; @@ -50,13 +53,10 @@ import org.onap.policy.rest.util.MSAttributeObject; import org.onap.policy.rest.util.MSModelUtils; import org.onap.policy.rest.util.MSModelUtils.MODEL_TYPE; -import com.google.gson.Gson; - public class CreateNewMicroServiceModel { private static final Logger logger = FlexLogger.getLogger(CreateNewMicroServiceModel.class); private MicroServiceModels newModel = null; - private HashMap classMap = new HashMap<>(); - + private HashMap classMap = new HashMap<>(); private MSModelUtils utils = new MSModelUtils(XACMLPapServlet.getMsOnapName(), XACMLPapServlet.getMsPolicyName()); @@ -64,7 +64,17 @@ public class CreateNewMicroServiceModel { super(); } - public CreateNewMicroServiceModel(String importFile, String modelName, String description, String version, String randomID) { + /** + * Instantiates a new creates the new micro service model. + * + * @param importFile the import file + * @param modelName the model name + * @param description the description + * @param version the version + * @param randomID the random ID + */ + public CreateNewMicroServiceModel(String importFile, String modelName, String description, String version, + String randomID) { this.newModel = new MicroServiceModels(); this.newModel.setVersion(version); @@ -75,24 +85,24 @@ public class CreateNewMicroServiceModel { String cleanUpFile = null; Map tempMap = new HashMap<>(); - //Need to delete the file - if (importFile.contains(".zip")){ + // Need to delete the file + if (importFile.contains(".zip")) { extractFolder(randomID + ".zip"); File directory = new File("ExtractDir" + File.separator + randomID); List fileList = listModelFiles(directory.toString()); - //get all the files from a director + // get all the files from a director processFiles(modelName, fileList); doCleanUpFiles(randomID); - }else { - if(importFile.contains(".yml")){ + } else { + if (importFile.contains(".yml")) { - processYmlModel("ExtractDir" + File.separator + randomID+".yml", modelName); - cleanUpFile = "ExtractDir" + File.separator + randomID+".yml"; + processYmlModel("ExtractDir" + File.separator + randomID + ".yml", modelName); + cleanUpFile = "ExtractDir" + File.separator + randomID + ".yml"; - }else{ - tempMap = utils.processEpackage("ExtractDir" + File.separator + randomID+".xmi", MODEL_TYPE.XMI); + } else { + tempMap = utils.processEpackage("ExtractDir" + File.separator + randomID + ".xmi", MODEL_TYPE.XMI); classMap.putAll(tempMap); - cleanUpFile = "ExtractDir" + File.separator + randomID+".xmi"; + cleanUpFile = "ExtractDir" + File.separator + randomID + ".xmi"; } File deleteFile = new File(cleanUpFile); @@ -102,16 +112,16 @@ public class CreateNewMicroServiceModel { private void processFiles(String modelName, List fileList) { Map tempMap; - for (File file : fileList){ - if (file.isFile()){ - int i = file.getName().lastIndexOf('.'); - String type = file.getName().substring(i+1); + for (File file : fileList) { + if (file.isFile()) { + int indx = file.getName().lastIndexOf('.'); + String type = file.getName().substring(indx + 1); - if("yml".equalsIgnoreCase(type)){ + if ("yml".equalsIgnoreCase(type)) { processYmlModel(file.toString(), modelName); - }else{ + } else { tempMap = utils.processEpackage(file.getAbsolutePath(), MODEL_TYPE.XMI); classMap.putAll(tempMap); @@ -133,17 +143,16 @@ public class CreateNewMicroServiceModel { } } - private void processYmlModel(String fileName, String modelName){ + private void processYmlModel(String fileName, String modelName) { try { - utils.parseTosca(fileName); - MSAttributeObject msAttributes= new MSAttributeObject(); + MSAttributeObject msAttributes = new MSAttributeObject(); msAttributes.setClassName(modelName); - LinkedHashMap returnAttributeList =new LinkedHashMap<>(); + LinkedHashMap returnAttributeList = new LinkedHashMap<>(); returnAttributeList.put(modelName, utils.getAttributeString()); msAttributes.setAttribute(returnAttributeList); @@ -151,24 +160,31 @@ public class CreateNewMicroServiceModel { msAttributes.setMatchingSet(utils.getMatchableValues()); - LinkedHashMap returnReferenceList =new LinkedHashMap<>(); + LinkedHashMap returnReferenceList = new LinkedHashMap<>(); returnReferenceList.put(modelName, utils.getReferenceAttributes()); msAttributes.setRefAttribute(returnReferenceList); - if(!PolicyDBDao.isNullOrEmpty(utils.getListConstraints())){ - LinkedHashMap enumList =new LinkedHashMap<>(); - String[] listArray=utils.getListConstraints().split("#"); - for(String str:listArray){ - String[] strArr= str.split("="); - if(strArr.length>1){ + if (!PolicyDbDao.isNullOrEmpty(utils.getListConstraints())) { + LinkedHashMap enumList = new LinkedHashMap<>(); + String[] listArray = utils.getListConstraints().split("#"); + for (String str : listArray) { + String[] strArr = str.split("="); + if (strArr.length > 1) { enumList.put(strArr[0], strArr[1]); } } msAttributes.setEnumType(enumList); } + if (utils.getJsonRuleFormation() != null) { + msAttributes.setRuleFormation(utils.getJsonRuleFormation()); + } + + if (utils.getDataOrderInfo() != null) { + msAttributes.setDataOrderInfo(utils.getDataOrderInfo()); + } - classMap=new LinkedHashMap<>(); + classMap = new LinkedHashMap<>(); classMap.put(modelName, msAttributes); } catch (Exception e) { @@ -196,14 +212,14 @@ public class CreateNewMicroServiceModel { int BUFFER = 2048; File file = new File(zipFile); - try(ZipFile zip = new ZipFile("ExtractDir" + File.separator +file)) { + try (ZipFile zip = new ZipFile("ExtractDir" + File.separator + file)) { - String newPath = zipFile.substring(0, zipFile.length() - 4); + String newPath = zipFile.substring(0, zipFile.length() - 4); new File(newPath).mkdir(); Enumeration zipFileEntries = zip.entries(); // Process each entry - while (zipFileEntries.hasMoreElements()){ + while (zipFileEntries.hasMoreElements()) { // grab a zip file entry ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); @@ -212,14 +228,13 @@ public class CreateNewMicroServiceModel { destinationParent.mkdirs(); - if (!entry.isDirectory()){ - BufferedInputStream is = new BufferedInputStream(zip - .getInputStream(entry)); + if (!entry.isDirectory()) { + BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; byte data[] = new byte[BUFFER]; - try(FileOutputStream fos = new FileOutputStream(destFile); - BufferedOutputStream dest = new BufferedOutputStream(fos, 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); @@ -229,7 +244,7 @@ public class CreateNewMicroServiceModel { is.close(); } - if (currentEntry.endsWith(".zip")){ + if (currentEntry.endsWith(".zip")) { extractFolder(destFile.getAbsolutePath()); } } @@ -241,60 +256,69 @@ public class CreateNewMicroServiceModel { public Map addValuesToNewModel(String type) { Map successMap = new HashMap<>(); - MSAttributeObject mainClass = null; + MSAttributeObject mainClass = null; List dependency = null; String subAttribute = null; - if (!classMap.containsKey(this.newModel.getModelName())){ - logger.error("Model Provided does not contain the service name provided in request. Unable to import new model"); - PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "AddValuesToNewModel", "Unable to pull out required values, file missing service name provided in request"); + if (!classMap.containsKey(this.newModel.getModelName())) { + logger.error( + "Model Provided does not contain the service name provided in request. Unable to import new model"); + PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "AddValuesToNewModel", + "Unable to pull out required values, file missing service name provided in request"); successMap.put("error", "MISSING"); return successMap; } mainClass = classMap.get(this.newModel.getModelName()); - - if(".yml".equalsIgnoreCase(type)){ + if (".yml".equalsIgnoreCase(type)) { newModel.setDependency("[]"); - if(mainClass.getSubClass() != null){ - String value = new Gson().toJson(mainClass.getSubClass()); - newModel.setSub_attributes(value); + if (mainClass.getSubClass() != null) { + String value = new Gson().toJson(mainClass.getSubClass()); + newModel.setSubAttributes(value); } - if(mainClass.getAttribute() != null){ - String attributes= mainClass.getAttribute().toString().replace("{", "").replace("}", ""); - int equalsIndexForAttributes= attributes.indexOf("="); - String atttributesAfterFirstEquals= attributes.substring(equalsIndexForAttributes+1); + if (mainClass.getAttribute() != null) { + String attributes = mainClass.getAttribute().toString().replace("{", "").replace("}", ""); + int equalsIndexForAttributes = attributes.indexOf("="); + String atttributesAfterFirstEquals = attributes.substring(equalsIndexForAttributes + 1); this.newModel.setAttributes(atttributesAfterFirstEquals); } - if(mainClass.getRefAttribute() != null){ - String refAttributes= mainClass.getRefAttribute().toString().replace("{", "").replace("}", ""); - int equalsIndex= refAttributes.indexOf("="); - String refAttributesAfterFirstEquals= refAttributes.substring(equalsIndex+1); - this.newModel.setRef_attributes(refAttributesAfterFirstEquals); + if (mainClass.getRefAttribute() != null) { + String refAttributes = mainClass.getRefAttribute().toString().replace("{", "").replace("}", ""); + int equalsIndex = refAttributes.indexOf("="); + String refAttributesAfterFirstEquals = refAttributes.substring(equalsIndex + 1); + this.newModel.setRefAttributes(refAttributesAfterFirstEquals); } - if(mainClass.getEnumType() != null){ + if (mainClass.getEnumType() != null) { this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", "")); } - if(mainClass.getMatchingSet() != null){ + if (mainClass.getMatchingSet() != null) { this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", "")); } + if (mainClass.getRuleFormation() != null) { + this.newModel.setRuleFormation(mainClass.getRuleFormation()); + } + + if (mainClass.getDataOrderInfo() != null) { + this.newModel.setDataOrderInfo(mainClass.getDataOrderInfo()); + } - }else{ + } else { - String dependTemp = StringUtils.replaceEach(mainClass.getDependency(), new String[]{"[", "]", " "}, new String[]{"", "", ""}); + String dependTemp = StringUtils.replaceEach(mainClass.getDependency(), new String[] {"[", "]", " "}, + new String[] {"", "", ""}); this.newModel.setDependency(dependTemp); - if (this.newModel.getDependency() != null && !this.newModel.getDependency().isEmpty()){ + if (this.newModel.getDependency() != null && !this.newModel.getDependency().isEmpty()) { dependency = new ArrayList(Arrays.asList(dependTemp.split(","))); dependency = utils.getFullDependencyList(dependency, classMap); - if (!dependency.isEmpty()){ - for (String element : dependency){ + if (!dependency.isEmpty()) { + for (String element : dependency) { MSAttributeObject temp = new MSAttributeObject(); - if (classMap.containsKey(element)){ + if (classMap.containsKey(element)) { temp = classMap.get(element); mainClass.addAllRefAttribute(temp.getRefAttribute()); mainClass.addAllAttribute(temp.getAttribute()); @@ -304,20 +328,21 @@ public class CreateNewMicroServiceModel { } subAttribute = utils.createSubAttributes(dependency, classMap, this.newModel.getModelName()); - this.newModel.setSub_attributes(subAttribute); - if(mainClass.getAttribute() != null && !mainClass.getAttribute().isEmpty()){ + this.newModel.setSubAttributes(subAttribute); + if (mainClass.getAttribute() != null && !mainClass.getAttribute().isEmpty()) { this.newModel.setAttributes(mainClass.getAttribute().toString().replace("{", "").replace("}", "")); } - if(mainClass.getRefAttribute() != null && !mainClass.getRefAttribute().isEmpty()){ - this.newModel.setRef_attributes(mainClass.getRefAttribute().toString().replace("{", "").replace("}", "")); + if (mainClass.getRefAttribute() != null && !mainClass.getRefAttribute().isEmpty()) { + this.newModel + .setRefAttributes(mainClass.getRefAttribute().toString().replace("{", "").replace("}", "")); } - if(mainClass.getEnumType() != null && !mainClass.getEnumType().isEmpty()){ + if (mainClass.getEnumType() != null && !mainClass.getEnumType().isEmpty()) { this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", "")); } - if(mainClass.getMatchingSet() != null && !mainClass.getMatchingSet().isEmpty()){ + if (mainClass.getMatchingSet() != null && !mainClass.getMatchingSet().isEmpty()) { this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", "")); } } @@ -326,14 +351,15 @@ public class CreateNewMicroServiceModel { } - public Map saveImportService(){ + public Map saveImportService() { String modelName = this.newModel.getModelName(); String imported_by = "API"; String version = this.newModel.getVersion(); Map successMap = new HashMap<>(); CommonClassDaoImpl dbConnection = new CommonClassDaoImpl(); - List result = dbConnection.getDataById(MicroServiceModels.class, "modelName:version", modelName+":"+version); - if(result.isEmpty()){ + List result = + dbConnection.getDataById(MicroServiceModels.class, "modelName:version", modelName + ":" + version); + if (result == null || result.isEmpty()) { MicroServiceModels model = new MicroServiceModels(); model.setModelName(modelName); model.setVersion(version); @@ -342,16 +368,18 @@ public class CreateNewMicroServiceModel { model.setDependency(this.newModel.getDependency()); model.setDescription(this.newModel.getDescription()); model.setEnumValues(this.newModel.getEnumValues()); - model.setRef_attributes(this.newModel.getRef_attributes()); - model.setSub_attributes(this.newModel.getSub_attributes()); + model.setRefAttributes(this.newModel.getRefAttributes()); + model.setSubAttributes(this.newModel.getSubAttributes()); model.setDataOrderInfo(this.newModel.getDataOrderInfo()); + model.setDecisionModel(this.newModel.isDecisionModel()); + model.setRuleFormation(this.newModel.getRuleFormation()); UserInfo userInfo = new UserInfo(); userInfo.setUserLoginId(imported_by); userInfo.setUserName(imported_by); model.setUserCreatedBy(userInfo); dbConnection.save(model); successMap.put("success", "success"); - }else{ + } else { successMap.put("DBError", "EXISTS"); logger.error("Import new service failed. Service already exists"); }