2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controller;
23 import com.att.research.xacml.util.XACMLProperties;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.DeserializationFeature;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.fasterxml.jackson.databind.ObjectWriter;
29 import com.google.gson.Gson;
31 import java.io.BufferedInputStream;
32 import java.io.BufferedOutputStream;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.OutputStream;
37 import java.util.ArrayList;
38 import java.util.Enumeration;
39 import java.util.HashMap;
40 import java.util.HashSet;
41 import java.util.Iterator;
42 import java.util.LinkedHashMap;
43 import java.util.LinkedList;
44 import java.util.List;
46 import java.util.Map.Entry;
48 import java.util.UUID;
49 import java.util.zip.ZipEntry;
50 import java.util.zip.ZipFile;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
56 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
57 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
58 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
59 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
60 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
61 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
62 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
64 import org.apache.commons.compress.utils.IOUtils;
65 import org.apache.commons.fileupload.FileItem;
66 import org.apache.commons.fileupload.FileUploadException;
67 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
68 import org.apache.commons.fileupload.servlet.ServletFileUpload;
69 import org.apache.commons.lang.StringUtils;
70 import org.json.JSONArray;
71 import org.json.JSONObject;
72 import org.onap.policy.common.logging.flexlogger.FlexLogger;
73 import org.onap.policy.common.logging.flexlogger.Logger;
74 import org.onap.policy.rest.XACMLRestProperties;
75 import org.onap.policy.rest.adapter.PolicyRestAdapter;
76 import org.onap.policy.rest.dao.CommonClassDao;
77 import org.onap.policy.rest.jpa.MicroserviceHeaderdeFaults;
78 import org.onap.policy.rest.jpa.OptimizationModels;
79 import org.onap.policy.rest.jpa.PolicyEntity;
80 import org.onap.policy.rest.util.MSAttributeObject;
81 import org.onap.policy.rest.util.MSModelUtils;
82 import org.onap.policy.rest.util.MSModelUtils.MODEL_TYPE;
83 import org.onap.portalsdk.core.controller.RestrictedBaseController;
84 import org.springframework.beans.factory.annotation.Autowired;
85 import org.springframework.stereotype.Controller;
86 import org.springframework.web.bind.annotation.RequestMapping;
87 import org.springframework.web.servlet.ModelAndView;
91 public class CreateOptimizationController extends RestrictedBaseController {
92 private static final Logger LOGGER = FlexLogger.getLogger(CreateOptimizationController.class);
94 private static final int BUFFER = 2048;
96 private static CommonClassDao commonClassDao;
98 public static CommonClassDao getCommonClassDao() {
99 return commonClassDao;
102 private OptimizationModels newModel;
103 private String newFile;
104 private String directory;
105 private List<String> modelList = new ArrayList<>();
106 private List<String> dirDependencyList = new ArrayList<>();
107 private LinkedHashMap<String, MSAttributeObject> classMap = new LinkedHashMap<>();
108 String referenceAttributes;
109 String attributeString;
110 Set<String> allManyTrueKeys = new HashSet<>();
112 public static final String DATATYPE = "data_types.policy.data.";
113 public static final String PROPERTIES = ".properties.";
114 public static final String TYPE = ".type";
115 public static final String STRING = "string";
116 public static final String INTEGER = "integer";
117 public static final String LIST = "list";
118 public static final String DEFAULT = ".default";
119 public static final String REQUIRED = ".required";
120 public static final String MATCHABLE = ".matchable";
121 public static final String MANYFALSE = ":MANY-false";
122 public static final String MODEL = "model";
123 public static final String MANY = "MANY-";
124 public static final String UTF8 = "UTF-8";
125 public static final String MODELNAME = "modelName";
126 public static final String APPLICATIONJSON = "application / json";
129 private CreateOptimizationController(CommonClassDao commonClassDao) {
130 setCommonClassDao(commonClassDao);
133 public static void setCommonClassDao(CommonClassDao commonClassDao) {
134 CreateOptimizationController.commonClassDao = commonClassDao;
137 public CreateOptimizationController() {
141 protected PolicyRestAdapter policyAdapter = null;
142 private Map<String, String> attributesListRefMap = new HashMap<>();
143 private Map<String, LinkedList<String>> arrayTextList = new HashMap<>();
144 CreateDcaeMicroServiceController msController = new CreateDcaeMicroServiceController();
147 * setDataToPolicyRestAdapter.
149 * @param policyData PolicyRestAdapter
150 * @param root JsonNode
151 * @return PolicyRestAdapter
153 public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
154 String jsonContent = null;
156 LOGGER.info("policyJSON :" + (root.get("policyJSON")).toString());
158 String tempJson = root.get("policyJSON").toString();
160 // ---replace empty value with the value below before calling decodeContent method.
161 String dummyValue = "*empty-value*" + UUID.randomUUID().toString();
162 LOGGER.info("dummyValue:" + dummyValue);
164 StringUtils.replaceEach(tempJson, new String[] {"\"\""}, new String[] {"\"" + dummyValue + "\""});
165 ObjectMapper mapper = new ObjectMapper();
166 JsonNode tempJsonNode = mapper.readTree(tempJson);
167 jsonContent = msController.decodeContent(tempJsonNode).toString();
168 constructJson(policyData, jsonContent, dummyValue);
169 } catch (Exception e) {
170 LOGGER.error("Error while decoding microservice content", e);
176 private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue) {
177 ObjectWriter om = new ObjectMapper().writer();
179 OptimizationObject optimizationObject = setOptimizationObjectValues(policyAdapter);
181 optimizationObject.setContent(jsonContent);
184 json = om.writeValueAsString(optimizationObject);
185 } catch (JsonProcessingException e) {
186 LOGGER.error("Error writing out the object", e);
188 LOGGER.info("input json: " + json);
189 LOGGER.info("input jsonContent: " + jsonContent);
190 String cleanJson = msController.cleanUpJson(json);
192 // --- reset empty value back after called cleanUPJson method and before calling removeNullAttributes
194 StringUtils.replaceEach(cleanJson, new String[] {"\"" + dummyValue + "\""}, new String[] {"\"\""});
195 LOGGER.info("tempJson: " + tempJson);
196 cleanJson = msController.removeNullAttributes(tempJson);
197 policyAdapter.setJsonBody(cleanJson);
198 return policyAdapter;
202 * getOptimizationTemplateData.
204 * @param request HttpServletRequest
205 * @param response HttpServletResponse
206 * @return ModelAndView
207 * @throws IOException IOException
210 value = {"/policyController/getOptimizationTemplateData.htm"},
211 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
212 public ModelAndView getOptimizationTemplateData(HttpServletRequest request, HttpServletResponse response)
214 CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController();
215 ObjectMapper mapper = new ObjectMapper();
216 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
217 JsonNode root = mapper.readTree(request.getReader());
219 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
220 String servicename = value.split("-v")[0];
221 String version = null;
222 if (value.contains("-v")) {
223 version = value.split("-v")[1];
226 OptimizationModels returnModel = getAttributeObject(servicename, version);
228 MicroserviceHeaderdeFaults returnHeaderDefauls = getHeaderDefaultsObject(value);
229 JSONObject jsonHdDefaultObj = null;
230 if (returnHeaderDefauls != null) {
231 jsonHdDefaultObj = new JSONObject();
232 jsonHdDefaultObj.put("onapName", returnHeaderDefauls.getOnapName());
233 jsonHdDefaultObj.put("guard", returnHeaderDefauls.getGuard());
234 jsonHdDefaultObj.put("riskLevel", returnHeaderDefauls.getRiskLevel());
235 jsonHdDefaultObj.put("riskType", returnHeaderDefauls.getRiskType());
236 jsonHdDefaultObj.put("priority", returnHeaderDefauls.getPriority());
239 String headDefautlsData = "";
240 if (jsonHdDefaultObj != null) {
241 headDefautlsData = jsonHdDefaultObj.toString();
242 LOGGER.info("returnHeaderDefauls headDefautlsData: " + headDefautlsData);
244 headDefautlsData = "null";
247 // Get all keys with "MANY-true" defined in their value from subAttribute
248 Set<String> allkeys = null;
249 if (returnModel.getSubattributes() != null && !returnModel.getSubattributes().isEmpty()) {
250 getAllKeys(new JSONObject(returnModel.getSubattributes()));
251 allkeys = allManyTrueKeys;
252 allManyTrueKeys = new HashSet<>();
253 LOGGER.info("allkeys : " + allkeys);
256 // Get element order info
257 String dataOrderInfo = returnModel.getDataOrderInfo();
258 if (dataOrderInfo != null && !dataOrderInfo.startsWith("\"")) {
259 dataOrderInfo = "\"" + dataOrderInfo + "\"";
262 String nameOfTrueKeys = "";
263 if (allkeys != null) {
264 nameOfTrueKeys = allkeys.toString();
267 String jsonModel = createOptimizationJson(returnModel);
269 JSONObject jsonObject = new JSONObject(jsonModel);
271 JSONObject finalJsonObject = null;
272 if (allkeys != null) {
273 Iterator<String> iter = allkeys.iterator();
274 while (iter.hasNext()) {
275 // Convert to array values for MANY-true keys
276 finalJsonObject = controller.convertToArrayElement(jsonObject, iter.next());
280 if (finalJsonObject != null) {
281 LOGGER.info(finalJsonObject.toString());
282 jsonModel = finalJsonObject.toString();
285 // get all properties with "MANY-true" defined in Ref_attributes
286 Set<String> manyTrueProperties = controller.getManyTrueProperties(returnModel.getRefattributes());
287 JSONObject jsonObj = new JSONObject(jsonModel);
288 for (String s : manyTrueProperties) {
290 // convert to array element for MANY-true properties
291 finalJsonObject = controller.convertToArrayElement(jsonObj, s.trim());
294 if (finalJsonObject != null) {
295 LOGGER.info(finalJsonObject.toString());
296 jsonModel = finalJsonObject.toString();
299 response.setCharacterEncoding(UTF8);
300 response.setContentType(APPLICATIONJSON);
301 request.setCharacterEncoding(UTF8);
302 List<Object> list = new ArrayList<>();
303 String responseString = mapper.writeValueAsString(returnModel);
304 JSONObject json = null;
305 if ("".equals(nameOfTrueKeys)) {
306 json = new JSONObject("{optimizationModelData: " + responseString + ",jsonValue: " + jsonModel
307 + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:" + headDefautlsData + "}");
309 json = new JSONObject("{optimizationModelData: " + responseString + ",jsonValue: " + jsonModel
310 + ",allManyTrueKeys: " + allManyTrueKeys + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:"
311 + headDefautlsData + "}");
314 response.getWriter().write(list.toString());
318 @SuppressWarnings({"rawtypes", "unchecked"})
319 private String createOptimizationJson(OptimizationModels returnModel) {
320 Map<String, String> attributeMap = new HashMap<>();
321 Map<String, String> refAttributeMap = new HashMap<>();
323 String attribute = returnModel.getAttributes();
324 if (attribute != null) {
325 attribute = attribute.trim();
327 String refAttribute = returnModel.getRefattributes();
328 if (refAttribute != null) {
329 refAttribute = refAttribute.trim();
332 String enumAttribute = returnModel.getEnumValues();
333 if (enumAttribute != null) {
334 enumAttribute = enumAttribute.trim();
337 CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController();
338 if (!StringUtils.isEmpty(attribute)) {
339 attributeMap = controller.convert(attribute, ",");
342 if (!StringUtils.isEmpty(refAttribute)) {
343 refAttributeMap = controller.convert(refAttribute, ",");
346 Gson gson = new Gson();
348 String subAttributes = returnModel.getSubattributes();
349 if (subAttributes != null) {
350 subAttributes = subAttributes.trim();
355 Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
357 JSONObject object = new JSONObject();
360 for (Entry<String, String> keySet : attributeMap.entrySet()) {
361 array = new JSONArray();
362 String value = keySet.getValue();
363 if ("true".equalsIgnoreCase(keySet.getValue().split(MANY)[1])) {
365 object.put(keySet.getKey().trim(), array);
367 object.put(keySet.getKey().trim(), value.trim());
371 for (Entry<String, String> keySet : refAttributeMap.entrySet()) {
372 array = new JSONArray();
373 String value = keySet.getValue().split(":")[0];
374 if (gsonObject.containsKey(value)) {
375 if ("true".equalsIgnoreCase(keySet.getValue().split(MANY)[1])) {
376 array.put(recursiveReference(value, gsonObject, enumAttribute));
377 object.put(keySet.getKey().trim(), array);
379 object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
382 if ("true".equalsIgnoreCase(keySet.getValue().split(MANY)[1])) {
383 array.put(value.trim());
384 object.put(keySet.getKey().trim(), array);
386 object.put(keySet.getKey().trim(), value.trim());
391 return object.toString();
394 @SuppressWarnings("unchecked")
395 private JSONObject recursiveReference(String name, Map<String, String> subAttributeMap, String enumAttribute) {
396 JSONObject object = new JSONObject();
397 Map<String, String> map;
398 Object returnClass = subAttributeMap.get(name);
399 map = (Map<String, String>) returnClass;
402 for (Entry<String, String> m : map.entrySet()) {
403 String[] splitValue = m.getValue().split(":");
404 array = new JSONArray();
405 if (subAttributeMap.containsKey(splitValue[0])) {
406 if ("true".equalsIgnoreCase(m.getValue().split(MANY)[1])) {
407 array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
408 object.put(m.getKey().trim(), array);
410 object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
413 if ("true".equalsIgnoreCase(m.getValue().split(MANY)[1])) {
414 array.put(splitValue[0].trim());
415 object.put(m.getKey().trim(), array);
417 object.put(m.getKey().trim(), splitValue[0].trim());
425 // call this method to start the recursive
426 private Set<String> getAllKeys(JSONObject json) {
427 return getAllKeys(json, new HashSet<>());
430 private Set<String> getAllKeys(JSONArray arr) {
431 return getAllKeys(arr, new HashSet<>());
434 private Set<String> getAllKeys(JSONArray arr, Set<String> keys) {
435 for (int i = 0; i < arr.length(); i++) {
436 Object obj = arr.get(i);
437 if (obj instanceof JSONObject) {
438 keys.addAll(getAllKeys(arr.getJSONObject(i)));
440 if (obj instanceof JSONArray) {
441 keys.addAll(getAllKeys(arr.getJSONArray(i)));
448 // this method returns a set of keys with "MANY-true" defined in their value.
449 private Set<String> getAllKeys(JSONObject json, Set<String> keys) {
450 for (String key : json.keySet()) {
451 Object obj = json.get(key);
452 if (obj instanceof String && ((String) obj).contains("MANY-true")) {
453 LOGGER.info("key : " + key);
454 LOGGER.info("obj : " + obj);
455 allManyTrueKeys.add(key);
457 if (obj instanceof JSONObject) {
458 keys.addAll(getAllKeys(json.getJSONObject(key)));
460 if (obj instanceof JSONArray) {
461 keys.addAll(getAllKeys(json.getJSONArray(key)));
469 * getModelServiceVersionData.
471 * @param request HttpServletRequest
472 * @param response HttpServletResponse
473 * @return ModelAndView
474 * @throws IOException IOException
477 value = {"/policyController/getModelServiceVersionData.htm"},
478 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
479 public ModelAndView getModelServiceVersionData(HttpServletRequest request, HttpServletResponse response)
481 ObjectMapper mapper = new ObjectMapper();
482 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
483 JsonNode root = mapper.readTree(request.getReader());
485 final String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
486 final String servicename = value.split("-v")[0];
488 response.setCharacterEncoding(UTF8);
489 response.setContentType(APPLICATIONJSON);
490 request.setCharacterEncoding(UTF8);
491 List<Object> list = new ArrayList<>();
492 list.add(new JSONObject("{optimizationModelVersionData: "
493 + mapper.writeValueAsString(getVersionList(servicename)) + "}"));
494 response.getWriter().write(list.toString());
498 private Set<String> getVersionList(String name) {
499 OptimizationModels workingModel;
500 Set<String> list = new HashSet<>();
501 List<Object> optimizationModelsData = commonClassDao.getDataById(OptimizationModels.class, MODELNAME, name);
502 for (int i = 0; i < optimizationModelsData.size(); i++) {
503 workingModel = (OptimizationModels) optimizationModelsData.get(i);
504 if (workingModel.getVersion() != null) {
505 list.add(workingModel.getVersion());
513 private OptimizationModels getAttributeObject(String name, String version) {
514 OptimizationModels workingModel = new OptimizationModels();
515 List<Object> optimizationModelsData = commonClassDao.getDataById(OptimizationModels.class, MODELNAME, name);
516 for (int i = 0; i < optimizationModelsData.size(); i++) {
517 workingModel = (OptimizationModels) optimizationModelsData.get(i);
518 if (version != null) {
519 if (workingModel.getVersion() != null) {
520 if (workingModel.getVersion().equals(version)) {
534 private MicroserviceHeaderdeFaults getHeaderDefaultsObject(String modelName) {
535 return (MicroserviceHeaderdeFaults) commonClassDao.getEntityItem(MicroserviceHeaderdeFaults.class, MODELNAME,
540 * prePopulatePolicyData.
542 * @param policyAdapter PolicyRestAdapter
543 * @param entity PolicyEntity
545 public void prePopulatePolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
546 if (! (policyAdapter.getPolicyData() instanceof PolicyType)) {
549 Object policyData = policyAdapter.getPolicyData();
550 PolicyType policy = (PolicyType) policyData;
551 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
552 String policyNameValue =
553 policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("OOF_") + 4);
554 policyAdapter.setPolicyName(policyNameValue);
555 String description = "";
557 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
558 } catch (Exception e) {
559 LOGGER.error("Error while collecting the description tag in " + policyNameValue, e);
560 description = policy.getDescription();
562 policyAdapter.setPolicyDescription(description);
563 // Get the target data under policy.
564 TargetType target = policy.getTarget();
565 if (target == null) {
568 for (AnyOfType anyOf : target.getAnyOf()) {
569 for (AllOfType allOf : anyOf.getAllOf()) {
570 for (MatchType match : allOf.getMatch()) {
572 // Under the match we have attribute value and
573 // attributeDesignator. So,finally down to the actual attribute.
575 AttributeValueType attributeValue = match.getAttributeValue();
576 String value = (String) attributeValue.getContent().get(0);
577 AttributeDesignatorType designator = match.getAttributeDesignator();
578 String attributeId = designator.getAttributeId();
579 // First match in the target is OnapName, so set that value.
580 if ("ONAPName".equals(attributeId)) {
581 policyAdapter.setOnapName(value);
583 if ("RiskType".equals(attributeId)) {
584 policyAdapter.setRiskType(value);
586 if ("RiskLevel".equals(attributeId)) {
587 policyAdapter.setRiskLevel(value);
589 if ("guard".equals(attributeId)) {
590 policyAdapter.setGuard(value);
592 if ("TTLDate".equals(attributeId) && !value.contains("NA")) {
593 PolicyController controller = new PolicyController();
594 String newDate = controller.convertDate(value);
595 policyAdapter.setTtlDate(newDate);
598 readFile(policyAdapter, entity);
603 @SuppressWarnings("unchecked")
604 private void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
605 String policyScopeName = null;
606 ObjectMapper mapper = new ObjectMapper();
608 OptimizationObject optimizationBody =
609 mapper.readValue(entity.getConfigurationData().getConfigBody(), OptimizationObject.class);
610 policyScopeName = msController.getPolicyScope(optimizationBody.getPolicyScope());
611 policyAdapter.setPolicyScope(policyScopeName);
613 policyAdapter.setPriority(optimizationBody.getPriority());
615 if (optimizationBody.getVersion() != null) {
616 policyAdapter.setServiceType(optimizationBody.getService());
617 policyAdapter.setVersion(optimizationBody.getVersion());
619 policyAdapter.setServiceType(optimizationBody.getService());
621 if (optimizationBody.getContent() != null) {
622 LinkedHashMap<String, Object> data = new LinkedHashMap<>();
623 LinkedHashMap<String, ?> map = (LinkedHashMap<String, ?>) optimizationBody.getContent();
624 msController.readRecursivlyJSONContent(map, data);
625 policyAdapter.setRuleData(data);
628 } catch (Exception e) {
637 * @param request HttpServletRequest
638 * @param response HttpServletResponse
639 * @throws IOException IOException
640 * @throws FileUploadException FileUploadException
643 value = {"/oof_dictionary/set_ModelData"},
644 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
645 public void setModelData(HttpServletRequest request, HttpServletResponse response)
646 throws IOException, FileUploadException {
647 modelList = new ArrayList<>();
648 dirDependencyList = new ArrayList<>();
649 classMap = new LinkedHashMap<>();
650 List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
654 String errorMsg = "";
655 for (FileItem item : items) {
656 if (item.getName().endsWith(".zip") || item.getName().endsWith(".xmi") || item.getName().endsWith(".yml")) {
657 this.newModel = new OptimizationModels();
659 File file = new File(item.getName());
660 OutputStream outputStream = new FileOutputStream(file);
661 IOUtils.copy(item.getInputStream(), outputStream);
662 outputStream.close();
663 this.newFile = file.toString();
664 this.newModel.setModelName(this.newFile.split("-v")[0]);
666 if (this.newFile.contains("-v")) {
667 if (item.getName().endsWith(".zip")) {
668 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".zip", ""));
670 } else if (item.getName().endsWith(".yml")) {
671 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".yml", ""));
674 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".xmi", ""));
677 } catch (Exception e) {
678 LOGGER.error("Upload error : ", e);
679 errorMsg = "Upload error:" + e.getMessage();
685 if (!errorMsg.isEmpty()) {
686 response.setCharacterEncoding(UTF8);
687 response.setContentType(APPLICATIONJSON);
688 request.setCharacterEncoding(UTF8);
689 response.getWriter().write(new JSONObject().put("errorMsg", errorMsg).toString());
693 List<File> fileList = new ArrayList<>();
694 MSModelUtils modelUtil = new MSModelUtils();
695 this.directory = MODEL;
697 extractFolder(this.newFile);
698 fileList = listModelFiles(this.directory);
700 modelUtil.parseTosca(this.newFile);
702 File file = new File(this.newFile);
708 // Process Main Model file first
709 classMap = new LinkedHashMap<>();
710 for (File file : fileList) {
711 if (!file.isDirectory() && file.getName().endsWith(".xmi")) {
712 retrieveDependency(file.toString());
716 modelList = createList();
718 msController.cleanUp(this.newFile);
719 msController.cleanUp(directory);
722 modelList.add(this.newModel.getModelName());
723 String className = this.newModel.getModelName();
724 MSAttributeObject optimizationAttributes = new MSAttributeObject();
725 optimizationAttributes.setClassName(className);
727 LinkedHashMap<String, String> returnAttributeList = new LinkedHashMap<>();
728 returnAttributeList.put(className, modelUtil.getAttributeString());
729 optimizationAttributes.setAttribute(returnAttributeList);
731 optimizationAttributes.setSubClass(modelUtil.getRetmap());
733 optimizationAttributes.setMatchingSet(modelUtil.getMatchableValues());
735 LinkedHashMap<String, String> returnReferenceList = new LinkedHashMap<>();
736 returnReferenceList.put(className, modelUtil.getReferenceAttributes());
737 optimizationAttributes.setRefAttribute(returnReferenceList);
739 if (!"".equals(modelUtil.getListConstraints())) {
740 LinkedHashMap<String, String> enumList = new LinkedHashMap<>();
741 String[] listArray = modelUtil.getListConstraints().split("#");
742 for (String str : listArray) {
743 String[] strArr = str.split("=");
744 if (strArr.length > 1) {
745 enumList.put(strArr[0], strArr[1]);
748 optimizationAttributes.setEnumType(enumList);
751 classMap = new LinkedHashMap<>();
752 classMap.put(className, optimizationAttributes);
756 response.setCharacterEncoding(UTF8);
757 response.setContentType(APPLICATIONJSON);
758 request.setCharacterEncoding(UTF8);
760 ObjectMapper mapper = new ObjectMapper();
761 JSONObject json = new JSONObject();
762 json.put("classListDatas", modelList);
763 json.put("modelDatas", mapper.writeValueAsString(classMap));
764 json.put("modelType", modelType);
765 json.put("dataOrderInfo", modelUtil.getDataOrderInfo());
767 response.getWriter().write(json.toString());
771 * Unzip file and store in the model directory for processing
773 @SuppressWarnings("rawtypes")
774 private void extractFolder(String zipFile) {
775 File file = new File(zipFile);
777 try (ZipFile zip = new ZipFile(file)) {
778 String newPath = MODEL + File.separator + zipFile.substring(0, zipFile.length() - 4);
779 this.directory = MODEL + File.separator + zipFile.substring(0, zipFile.length() - 4);
780 msController.checkZipDirectory(this.directory);
781 new File(newPath).mkdir();
782 Enumeration zipFileEntries = zip.entries();
784 // Process each entry
785 while (zipFileEntries.hasMoreElements()) {
786 // grab a zip file entry
787 ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
788 String currentEntry = entry.getName();
789 File destFile = new File(MODEL + File.separator + currentEntry);
790 File destinationParent = destFile.getParentFile();
792 destinationParent.mkdirs();
794 if (!entry.isDirectory()) {
795 BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
797 byte[] data = new byte[BUFFER];
798 try (FileOutputStream fos = new FileOutputStream(destFile);
799 BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) {
800 while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
801 dest.write(data, 0, currentByte);
804 } catch (IOException e) {
805 LOGGER.error("Failed to write zip contents to {}" + destFile + e);
807 // PLD should I throw e?
813 if (currentEntry.endsWith(".zip")) {
814 extractFolder(destFile.getAbsolutePath());
817 } catch (IOException e) {
818 LOGGER.error("Failed to unzip model file " + zipFile, e);
822 private void retrieveDependency(String workingFile) {
824 MSModelUtils utils = new MSModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName());
825 Map<String, MSAttributeObject> tempMap;
827 tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
829 classMap.putAll(tempMap);
830 LOGGER.info(tempMap);
833 private List<File> listModelFiles(String directoryName) {
834 File fileDirectory = new File(directoryName);
835 List<File> resultList = new ArrayList<>();
836 for (File file : fileDirectory.listFiles()) {
838 resultList.add(file);
839 } else if (file.isDirectory()) {
840 dirDependencyList.add(file.getName());
841 resultList.addAll(listModelFiles(file.getAbsolutePath()));
847 private List<String> createList() {
848 List<String> list = new ArrayList<>();
849 for (Entry<String, MSAttributeObject> entrySet : classMap.entrySet()) {
850 if (entrySet.getValue().isPolicyTempalate()) {
851 list.add(entrySet.getKey());
856 if (list.isEmpty()) {
857 if (classMap.containsKey(this.newModel.getModelName())) {
858 list.add(this.newModel.getModelName());
866 public Map<String, String> getAttributesListRefMap() {
867 return attributesListRefMap;
870 public Map<String, LinkedList<String>> getArrayTextList() {
871 return arrayTextList;
874 private OptimizationObject setOptimizationObjectValues(PolicyRestAdapter policyAdapter) {
875 OptimizationObject optimizationObject = new OptimizationObject();
876 optimizationObject.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_OOF));
878 if (policyAdapter.getServiceType() != null) {
879 optimizationObject.setService(policyAdapter.getServiceType());
880 optimizationObject.setVersion(policyAdapter.getVersion());
882 if (policyAdapter.getPolicyName() != null) {
883 optimizationObject.setPolicyName(policyAdapter.getPolicyName());
885 if (policyAdapter.getPolicyDescription() != null) {
886 optimizationObject.setDescription(policyAdapter.getPolicyDescription());
888 if (policyAdapter.getPriority() != null) {
889 optimizationObject.setPriority(policyAdapter.getPriority());
891 optimizationObject.setPriority("9999");
893 if (policyAdapter.getRiskLevel() != null) {
894 optimizationObject.setRiskLevel(policyAdapter.getRiskLevel());
896 if (policyAdapter.getRiskType() != null) {
897 optimizationObject.setRiskType(policyAdapter.getRiskType());
899 if (policyAdapter.getGuard() != null) {
900 optimizationObject.setGuard(policyAdapter.getGuard());
902 return optimizationObject;
908 class OptimizationObject {
910 private String service;
911 private String policyName;
912 private String description;
913 private String templateVersion;
914 private String version;
915 private String priority;
916 private String policyScope;
917 private String riskType;
918 private String riskLevel;
919 private String guard = null;
920 private Object content;