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.io.PrintWriter;
38 import java.util.ArrayList;
39 import java.util.Enumeration;
40 import java.util.HashMap;
41 import java.util.HashSet;
42 import java.util.Iterator;
43 import java.util.LinkedHashMap;
44 import java.util.LinkedList;
45 import java.util.List;
47 import java.util.Map.Entry;
49 import java.util.UUID;
50 import java.util.zip.ZipEntry;
51 import java.util.zip.ZipFile;
53 import javax.servlet.http.HttpServletRequest;
54 import javax.servlet.http.HttpServletResponse;
57 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
58 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
59 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
60 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
61 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
62 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
63 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
65 import org.apache.commons.compress.utils.IOUtils;
66 import org.apache.commons.fileupload.FileItem;
67 import org.apache.commons.fileupload.FileUploadException;
68 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
69 import org.apache.commons.fileupload.servlet.ServletFileUpload;
70 import org.apache.commons.lang.StringUtils;
71 import org.json.JSONArray;
72 import org.json.JSONObject;
73 import org.onap.policy.common.logging.flexlogger.FlexLogger;
74 import org.onap.policy.common.logging.flexlogger.Logger;
75 import org.onap.policy.rest.XACMLRestProperties;
76 import org.onap.policy.rest.adapter.PolicyRestAdapter;
77 import org.onap.policy.rest.dao.CommonClassDao;
78 import org.onap.policy.rest.jpa.MicroserviceHeaderdeFaults;
79 import org.onap.policy.rest.jpa.OptimizationModels;
80 import org.onap.policy.rest.jpa.PolicyEntity;
81 import org.onap.policy.rest.util.MSAttributeObject;
82 import org.onap.policy.rest.util.MSModelUtils;
83 import org.onap.policy.rest.util.MSModelUtils.MODEL_TYPE;
84 import org.onap.portalsdk.core.controller.RestrictedBaseController;
85 import org.springframework.beans.factory.annotation.Autowired;
86 import org.springframework.stereotype.Controller;
87 import org.springframework.web.bind.annotation.RequestMapping;
88 import org.springframework.web.servlet.ModelAndView;
92 public class CreateOptimizationController extends RestrictedBaseController {
93 private static final Logger LOGGER = FlexLogger.getLogger(CreateOptimizationController.class);
94 private static CommonClassDao commonClassDao;
96 public static CommonClassDao getCommonClassDao() {
97 return commonClassDao;
100 private OptimizationModels newModel;
101 private String newFile;
102 private String directory;
103 private List<String> modelList = new ArrayList<>();
104 private List<String> dirDependencyList = new ArrayList<>();
105 private LinkedHashMap<String, MSAttributeObject> classMap = new LinkedHashMap<>();
106 String referenceAttributes;
107 String attributeString;
108 Set<String> allManyTrueKeys = new HashSet<>();
110 public static final String DATATYPE = "data_types.policy.data.";
111 public static final String PROPERTIES = ".properties.";
112 public static final String TYPE = ".type";
113 public static final String STRING = "string";
114 public static final String INTEGER = "integer";
115 public static final String LIST = "list";
116 public static final String DEFAULT = ".default";
117 public static final String REQUIRED = ".required";
118 public static final String MATCHABLE = ".matchable";
119 public static final String MANYFALSE = ":MANY-false";
120 public static final String MODEL = "model";
121 public static final String MANY = "MANY-";
122 public static final String UTF8 = "UTF-8";
123 public static final String MODELNAME = "modelName";
124 public static final String APPLICATIONJSON = "application / json";
127 private CreateOptimizationController(CommonClassDao commonClassDao) {
128 setCommonClassDao(commonClassDao);
131 public static void setCommonClassDao(CommonClassDao commonClassDao) {
132 CreateOptimizationController.commonClassDao = commonClassDao;
135 public CreateOptimizationController() {
139 protected PolicyRestAdapter policyAdapter = null;
140 private Map<String, String> attributesListRefMap = new HashMap<>();
141 private Map<String, LinkedList<String>> arrayTextList = new HashMap<>();
142 CreateDcaeMicroServiceController msController = new CreateDcaeMicroServiceController();
144 public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
145 String jsonContent = null;
147 LOGGER.info("policyJSON :" + (root.get("policyJSON")).toString());
149 String tempJson = root.get("policyJSON").toString();
151 // ---replace empty value with the value below before calling decodeContent method.
152 String dummyValue = "*empty-value*" + UUID.randomUUID().toString();
153 LOGGER.info("dummyValue:" + dummyValue);
155 StringUtils.replaceEach(tempJson, new String[] {"\"\""}, new String[] {"\"" + dummyValue + "\""});
156 ObjectMapper mapper = new ObjectMapper();
157 JsonNode tempJsonNode = mapper.readTree(tempJson);
158 jsonContent = msController.decodeContent(tempJsonNode).toString();
159 constructJson(policyData, jsonContent, dummyValue);
160 } catch (Exception e) {
161 LOGGER.error("Error while decoding microservice content", e);
167 private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue) {
168 ObjectWriter om = new ObjectMapper().writer();
170 OptimizationObject optimizationObject = setOptimizationObjectValues(policyAdapter);
172 optimizationObject.setContent(jsonContent);
175 json = om.writeValueAsString(optimizationObject);
176 } catch (JsonProcessingException e) {
177 LOGGER.error("Error writing out the object", e);
179 LOGGER.info("input json: " + json);
180 LOGGER.info("input jsonContent: " + jsonContent);
181 String cleanJson = msController.cleanUPJson(json);
183 // --- reset empty value back after called cleanUPJson method and before calling removeNullAttributes
185 StringUtils.replaceEach(cleanJson, new String[] {"\"" + dummyValue + "\""}, new String[] {"\"\""});
186 LOGGER.info("tempJson: " + tempJson);
187 cleanJson = msController.removeNullAttributes(tempJson);
188 policyAdapter.setJsonBody(cleanJson);
189 return policyAdapter;
193 value = {"/policyController/getOptimizationTemplateData.htm"},
194 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
195 public ModelAndView getOptimizationTemplateData(HttpServletRequest request, HttpServletResponse response)
197 CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController();
198 ObjectMapper mapper = new ObjectMapper();
199 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
200 JsonNode root = mapper.readTree(request.getReader());
202 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
203 String servicename = value.toString().split("-v")[0];
204 String version = null;
205 if (value.toString().contains("-v")) {
206 version = value.toString().split("-v")[1];
209 OptimizationModels returnModel = getAttributeObject(servicename, version);
211 MicroserviceHeaderdeFaults returnHeaderDefauls = getHeaderDefaultsObject(value);
212 JSONObject jsonHdDefaultObj = null;
213 if (returnHeaderDefauls != null) {
214 jsonHdDefaultObj = new JSONObject();
215 jsonHdDefaultObj.put("onapName", returnHeaderDefauls.getOnapName());
216 jsonHdDefaultObj.put("guard", returnHeaderDefauls.getGuard());
217 jsonHdDefaultObj.put("riskLevel", returnHeaderDefauls.getRiskLevel());
218 jsonHdDefaultObj.put("riskType", returnHeaderDefauls.getRiskType());
219 jsonHdDefaultObj.put("priority", returnHeaderDefauls.getPriority());
222 String headDefautlsData = "";
223 if (jsonHdDefaultObj != null) {
224 headDefautlsData = jsonHdDefaultObj.toString();
225 LOGGER.info("returnHeaderDefauls headDefautlsData: " + headDefautlsData);
227 headDefautlsData = "null";
230 // Get all keys with "MANY-true" defined in their value from subAttribute
231 Set<String> allkeys = null;
232 if (returnModel.getSubattributes() != null && !returnModel.getSubattributes().isEmpty()) {
233 JSONObject json = new JSONObject(returnModel.getSubattributes());
235 allkeys = allManyTrueKeys;
236 allManyTrueKeys = new HashSet<>();
237 LOGGER.info("allkeys : " + allkeys);
240 // Get element order info
241 String dataOrderInfo = returnModel.getDataOrderInfo();
242 if (dataOrderInfo != null && !dataOrderInfo.startsWith("\"")) {
243 dataOrderInfo = "\"" + dataOrderInfo + "\"";
246 String nameOfTrueKeys = "";
247 if (allkeys != null) {
248 nameOfTrueKeys = allkeys.toString();
251 String jsonModel = createOptimizationJson(returnModel);
253 JSONObject jsonObject = new JSONObject(jsonModel);
255 JSONObject finalJsonObject = null;
256 if (allkeys != null) {
257 Iterator<String> iter = allkeys.iterator();
258 while (iter.hasNext()) {
259 // Convert to array values for MANY-true keys
260 finalJsonObject = controller.convertToArrayElement(jsonObject, iter.next());
264 if (finalJsonObject != null) {
265 LOGGER.info(finalJsonObject.toString());
266 jsonModel = finalJsonObject.toString();
269 // get all properties with "MANY-true" defined in Ref_attributes
270 Set<String> manyTrueProperties = controller.getManyTrueProperties(returnModel.getRefattributes());
271 JSONObject jsonObj = new JSONObject(jsonModel);
272 for (String s : manyTrueProperties) {
274 // convert to array element for MANY-true properties
275 finalJsonObject = controller.convertToArrayElement(jsonObj, s.trim());
278 if (finalJsonObject != null) {
279 LOGGER.info(finalJsonObject.toString());
280 jsonModel = finalJsonObject.toString();
283 response.setCharacterEncoding(UTF8);
284 response.setContentType(APPLICATIONJSON);
285 request.setCharacterEncoding(UTF8);
286 List<Object> list = new ArrayList<>();
287 PrintWriter out = response.getWriter();
288 String responseString = mapper.writeValueAsString(returnModel);
290 if ("".equals(nameOfTrueKeys)) {
291 j = new JSONObject("{optimizationModelData: " + responseString + ",jsonValue: " + jsonModel
292 + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:" + headDefautlsData + "}");
294 j = new JSONObject("{optimizationModelData: " + responseString + ",jsonValue: " + jsonModel
295 + ",allManyTrueKeys: " + allManyTrueKeys + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:"
296 + headDefautlsData + "}");
299 out.write(list.toString());
303 @SuppressWarnings({"rawtypes", "unchecked"})
304 private String createOptimizationJson(OptimizationModels returnModel) {
305 Map<String, String> attributeMap = new HashMap<>();
306 Map<String, String> refAttributeMap = new HashMap<>();
308 String attribute = returnModel.getAttributes();
309 if (attribute != null) {
310 attribute = attribute.trim();
312 String refAttribute = returnModel.getRefattributes();
313 if (refAttribute != null) {
314 refAttribute = refAttribute.trim();
317 String enumAttribute = returnModel.getEnumValues();
318 if (enumAttribute != null) {
319 enumAttribute = enumAttribute.trim();
322 CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController();
323 if (!StringUtils.isEmpty(attribute)) {
324 attributeMap = controller.convert(attribute, ",");
327 if (!StringUtils.isEmpty(refAttribute)) {
328 refAttributeMap = controller.convert(refAttribute, ",");
331 Gson gson = new Gson();
333 String subAttributes = returnModel.getSubattributes();
334 if (subAttributes != null) {
335 subAttributes = subAttributes.trim();
340 Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
342 JSONObject object = new JSONObject();
345 for (Entry<String, String> keySet : attributeMap.entrySet()) {
346 array = new JSONArray();
347 String value = keySet.getValue();
348 if ("true".equalsIgnoreCase(keySet.getValue().split(MANY)[1])) {
350 object.put(keySet.getKey().trim(), array);
352 object.put(keySet.getKey().trim(), value.trim());
356 for (Entry<String, String> keySet : refAttributeMap.entrySet()) {
357 array = new JSONArray();
358 String value = keySet.getValue().split(":")[0];
359 if (gsonObject.containsKey(value)) {
360 if ("true".equalsIgnoreCase(keySet.getValue().split(MANY)[1])) {
361 array.put(recursiveReference(value, gsonObject, enumAttribute));
362 object.put(keySet.getKey().trim(), array);
364 object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
367 if ("true".equalsIgnoreCase(keySet.getValue().split(MANY)[1])) {
368 array.put(value.trim());
369 object.put(keySet.getKey().trim(), array);
371 object.put(keySet.getKey().trim(), value.trim());
376 return object.toString();
379 @SuppressWarnings("unchecked")
380 private JSONObject recursiveReference(String name, Map<String, String> subAttributeMap, String enumAttribute) {
381 JSONObject object = new JSONObject();
382 Map<String, String> map;
383 Object returnClass = subAttributeMap.get(name);
384 map = (Map<String, String>) returnClass;
387 for (Entry<String, String> m : map.entrySet()) {
388 String[] splitValue = m.getValue().split(":");
389 array = new JSONArray();
390 if (subAttributeMap.containsKey(splitValue[0])) {
391 if ("true".equalsIgnoreCase(m.getValue().split(MANY)[1])) {
392 array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
393 object.put(m.getKey().trim(), array);
395 object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
398 if ("true".equalsIgnoreCase(m.getValue().split(MANY)[1])) {
399 array.put(splitValue[0].trim());
400 object.put(m.getKey().trim(), array);
402 object.put(m.getKey().trim(), splitValue[0].trim());
410 // call this method to start the recursive
411 private Set<String> getAllKeys(JSONObject json) {
412 return getAllKeys(json, new HashSet<>());
415 private Set<String> getAllKeys(JSONArray arr) {
416 return getAllKeys(arr, new HashSet<>());
419 private Set<String> getAllKeys(JSONArray arr, Set<String> keys) {
420 for (int i = 0; i < arr.length(); i++) {
421 Object obj = arr.get(i);
422 if (obj instanceof JSONObject)
423 keys.addAll(getAllKeys(arr.getJSONObject(i)));
424 if (obj instanceof JSONArray)
425 keys.addAll(getAllKeys(arr.getJSONArray(i)));
431 // this method returns a set of keys with "MANY-true" defined in their value.
432 private Set<String> getAllKeys(JSONObject json, Set<String> keys) {
433 for (String key : json.keySet()) {
434 Object obj = json.get(key);
435 if (obj instanceof String && ((String) obj).contains("MANY-true")) {
436 LOGGER.info("key : " + key);
437 LOGGER.info("obj : " + obj);
438 allManyTrueKeys.add(key);
440 if (obj instanceof JSONObject)
441 keys.addAll(getAllKeys(json.getJSONObject(key)));
442 if (obj instanceof JSONArray)
443 keys.addAll(getAllKeys(json.getJSONArray(key)));
450 value = {"/policyController/getModelServiceVersionData.htm"},
451 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
452 public ModelAndView getModelServiceVersionData(HttpServletRequest request, HttpServletResponse response)
454 ObjectMapper mapper = new ObjectMapper();
455 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
456 JsonNode root = mapper.readTree(request.getReader());
458 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
459 String servicename = value.split("-v")[0];
460 Set<String> returnList = getVersionList(servicename);
462 response.setCharacterEncoding(UTF8);
463 response.setContentType(APPLICATIONJSON);
464 request.setCharacterEncoding(UTF8);
465 List<Object> list = new ArrayList<>();
466 PrintWriter out = response.getWriter();
467 String responseString = mapper.writeValueAsString(returnList);
468 JSONObject j = new JSONObject("{optimizationModelVersionData: " + responseString + "}");
470 out.write(list.toString());
474 private Set<String> getVersionList(String name) {
475 OptimizationModels workingModel;
476 Set<String> list = new HashSet<>();
477 List<Object> optimizationModelsData = commonClassDao.getDataById(OptimizationModels.class, MODELNAME, name);
478 for (int i = 0; i < optimizationModelsData.size(); i++) {
479 workingModel = (OptimizationModels) optimizationModelsData.get(i);
480 if (workingModel.getVersion() != null) {
481 list.add(workingModel.getVersion());
489 private OptimizationModels getAttributeObject(String name, String version) {
490 OptimizationModels workingModel = new OptimizationModels();
491 List<Object> optimizationModelsData = commonClassDao.getDataById(OptimizationModels.class, MODELNAME, name);
492 for (int i = 0; i < optimizationModelsData.size(); i++) {
493 workingModel = (OptimizationModels) optimizationModelsData.get(i);
494 if (version != null) {
495 if (workingModel.getVersion() != null) {
496 if (workingModel.getVersion().equals(version)) {
510 private MicroserviceHeaderdeFaults getHeaderDefaultsObject(String modelName) {
511 return (MicroserviceHeaderdeFaults) commonClassDao.getEntityItem(MicroserviceHeaderdeFaults.class, MODELNAME,
515 public void prePopulatePolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
516 if (policyAdapter.getPolicyData() instanceof PolicyType) {
517 Object policyData = policyAdapter.getPolicyData();
518 PolicyType policy = (PolicyType) policyData;
519 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
520 String policyNameValue =
521 policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("OOF_") + 4);
522 policyAdapter.setPolicyName(policyNameValue);
523 String description = "";
525 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
526 } catch (Exception e) {
527 LOGGER.error("Error while collecting the description tag in " + policyNameValue, e);
528 description = policy.getDescription();
530 policyAdapter.setPolicyDescription(description);
531 // Get the target data under policy.
532 TargetType target = policy.getTarget();
533 if (target != null) {
534 // Under target we have AnyOFType
535 List<AnyOfType> anyOfList = target.getAnyOf();
536 if (anyOfList != null) {
537 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
538 while (iterAnyOf.hasNext()) {
539 AnyOfType anyOf = iterAnyOf.next();
540 // Under AnyOFType we have AllOFType
541 List<AllOfType> allOfList = anyOf.getAllOf();
542 if (allOfList != null) {
543 Iterator<AllOfType> iterAllOf = allOfList.iterator();
544 while (iterAllOf.hasNext()) {
545 AllOfType allOf = iterAllOf.next();
546 // Under AllOFType we have Match
547 List<MatchType> matchList = allOf.getMatch();
548 if (matchList != null) {
549 Iterator<MatchType> iterMatch = matchList.iterator();
550 while (matchList.size() > 1 && iterMatch.hasNext()) {
551 MatchType match = iterMatch.next();
553 // Under the match we have attribute value and
554 // attributeDesignator. So,finally down to the actual attribute.
556 AttributeValueType attributeValue = match.getAttributeValue();
557 String value = (String) attributeValue.getContent().get(0);
558 AttributeDesignatorType designator = match.getAttributeDesignator();
559 String attributeId = designator.getAttributeId();
560 // First match in the target is OnapName, so set that value.
561 if ("ONAPName".equals(attributeId)) {
562 policyAdapter.setOnapName(value);
564 if ("RiskType".equals(attributeId)) {
565 policyAdapter.setRiskType(value);
567 if ("RiskLevel".equals(attributeId)) {
568 policyAdapter.setRiskLevel(value);
570 if ("guard".equals(attributeId)) {
571 policyAdapter.setGuard(value);
573 if ("TTLDate".equals(attributeId) && !value.contains("NA")) {
574 PolicyController controller = new PolicyController();
575 String newDate = controller.convertDate(value);
576 policyAdapter.setTtlDate(newDate);
579 readFile(policyAdapter, entity);
589 @SuppressWarnings("unchecked")
590 private void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
591 String policyScopeName = null;
592 ObjectMapper mapper = new ObjectMapper();
594 OptimizationObject optimizationBody =
595 mapper.readValue(entity.getConfigurationData().getConfigBody(), OptimizationObject.class);
596 policyScopeName = msController.getPolicyScope(optimizationBody.getPolicyScope());
597 policyAdapter.setPolicyScope(policyScopeName);
599 policyAdapter.setPriority(optimizationBody.getPriority());
601 if (optimizationBody.getVersion() != null) {
602 policyAdapter.setServiceType(optimizationBody.getService());
603 policyAdapter.setVersion(optimizationBody.getVersion());
605 policyAdapter.setServiceType(optimizationBody.getService());
607 if (optimizationBody.getContent() != null) {
608 LinkedHashMap<String, Object> data = new LinkedHashMap<>();
609 LinkedHashMap<String, ?> map = (LinkedHashMap<String, ?>) optimizationBody.getContent();
610 msController.readRecursivlyJSONContent(map, data);
611 policyAdapter.setRuleData(data);
614 } catch (Exception e) {
621 value = {"/oof_dictionary/set_ModelData"},
622 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
623 public void setModelData(HttpServletRequest request, HttpServletResponse response)
624 throws IOException, FileUploadException {
625 modelList = new ArrayList<>();
626 dirDependencyList = new ArrayList<>();
627 classMap = new LinkedHashMap<>();
628 List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
632 String errorMsg = "";
633 for (FileItem item : items) {
634 if (item.getName().endsWith(".zip") || item.getName().endsWith(".xmi") || item.getName().endsWith(".yml")) {
635 this.newModel = new OptimizationModels();
637 File file = new File(item.getName());
638 OutputStream outputStream = new FileOutputStream(file);
639 IOUtils.copy(item.getInputStream(), outputStream);
640 outputStream.close();
641 this.newFile = file.toString();
642 this.newModel.setModelName(this.newFile.split("-v")[0]);
644 if (this.newFile.contains("-v")) {
645 if (item.getName().endsWith(".zip")) {
646 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".zip", ""));
648 } else if (item.getName().endsWith(".yml")) {
649 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".yml", ""));
652 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".xmi", ""));
655 } catch (Exception e) {
656 LOGGER.error("Upload error : ", e);
657 errorMsg = "Upload error:" + e.getMessage();
663 if (!errorMsg.isEmpty()) {
665 PrintWriter out = response.getWriter();
667 response.setCharacterEncoding(UTF8);
668 response.setContentType(APPLICATIONJSON);
669 request.setCharacterEncoding(UTF8);
671 JSONObject j = new JSONObject();
672 j.put("errorMsg", errorMsg);
673 out.write(j.toString());
677 List<File> fileList = new ArrayList<>();
678 MSModelUtils modelUtil = new MSModelUtils();
679 this.directory = MODEL;
681 extractFolder(this.newFile);
682 fileList = listModelFiles(this.directory);
684 modelUtil.parseTosca(this.newFile);
686 File file = new File(this.newFile);
692 // Process Main Model file first
693 classMap = new LinkedHashMap<>();
694 for (File file : fileList) {
695 if (!file.isDirectory() && file.getName().endsWith(".xmi")) {
696 retrieveDependency(file.toString());
700 modelList = createList();
702 msController.cleanUp(this.newFile);
703 msController.cleanUp(directory);
706 modelList.add(this.newModel.getModelName());
707 String className = this.newModel.getModelName();
708 MSAttributeObject optimizationAttributes = new MSAttributeObject();
709 optimizationAttributes.setClassName(className);
711 LinkedHashMap<String, String> returnAttributeList = new LinkedHashMap<>();
712 returnAttributeList.put(className, modelUtil.getAttributeString());
713 optimizationAttributes.setAttribute(returnAttributeList);
715 optimizationAttributes.setSubClass(modelUtil.getRetmap());
717 optimizationAttributes.setMatchingSet(modelUtil.getMatchableValues());
719 LinkedHashMap<String, String> returnReferenceList = new LinkedHashMap<>();
720 returnReferenceList.put(className, modelUtil.getReferenceAttributes());
721 optimizationAttributes.setRefAttribute(returnReferenceList);
723 if (!"".equals(modelUtil.getListConstraints())) {
724 LinkedHashMap<String, String> enumList = new LinkedHashMap<>();
725 String[] listArray = modelUtil.getListConstraints().split("#");
726 for (String str : listArray) {
727 String[] strArr = str.split("=");
728 if (strArr.length > 1) {
729 enumList.put(strArr[0], strArr[1]);
732 optimizationAttributes.setEnumType(enumList);
735 classMap = new LinkedHashMap<>();
736 classMap.put(className, optimizationAttributes);
740 PrintWriter out = response.getWriter();
742 response.setCharacterEncoding(UTF8);
743 response.setContentType(APPLICATIONJSON);
744 request.setCharacterEncoding(UTF8);
746 ObjectMapper mapper = new ObjectMapper();
747 JSONObject j = new JSONObject();
748 j.put("classListDatas", modelList);
749 j.put("modelDatas", mapper.writeValueAsString(classMap));
750 j.put("modelType", modelType);
751 j.put("dataOrderInfo", modelUtil.getDataOrderInfo());
753 out.write(j.toString());
757 * Unzip file and store in the model directory for processing
759 @SuppressWarnings("rawtypes")
760 private void extractFolder(String zipFile) {
762 File file = new File(zipFile);
764 try (ZipFile zip = new ZipFile(file)) {
765 String newPath = MODEL + File.separator + zipFile.substring(0, zipFile.length() - 4);
766 this.directory = MODEL + File.separator + zipFile.substring(0, zipFile.length() - 4);
767 msController.checkZipDirectory(this.directory);
768 new File(newPath).mkdir();
769 Enumeration zipFileEntries = zip.entries();
771 // Process each entry
772 while (zipFileEntries.hasMoreElements()) {
773 // grab a zip file entry
774 ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
775 String currentEntry = entry.getName();
776 File destFile = new File(MODEL + File.separator + currentEntry);
777 File destinationParent = destFile.getParentFile();
779 destinationParent.mkdirs();
781 if (!entry.isDirectory()) {
782 BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
784 byte[] data = new byte[BUFFER];
785 try (FileOutputStream fos = new FileOutputStream(destFile);
786 BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) {
787 while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
788 dest.write(data, 0, currentByte);
791 } catch (IOException e) {
792 LOGGER.error("Failed to write zip contents to {}" + destFile + e);
794 // PLD should I throw e?
800 if (currentEntry.endsWith(".zip")) {
801 extractFolder(destFile.getAbsolutePath());
804 } catch (IOException e) {
805 LOGGER.error("Failed to unzip model file " + zipFile, e);
809 private void retrieveDependency(String workingFile) {
811 MSModelUtils utils = new MSModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName());
812 Map<String, MSAttributeObject> tempMap;
814 tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
816 classMap.putAll(tempMap);
817 LOGGER.info(tempMap);
823 private List<File> listModelFiles(String directoryName) {
824 File fileDirectory = new File(directoryName);
825 List<File> resultList = new ArrayList<>();
826 File[] fList = fileDirectory.listFiles();
827 for (File file : fList) {
829 resultList.add(file);
830 } else if (file.isDirectory()) {
831 dirDependencyList.add(file.getName());
832 resultList.addAll(listModelFiles(file.getAbsolutePath()));
838 private List<String> createList() {
839 List<String> list = new ArrayList<>();
840 for (Entry<String, MSAttributeObject> cMap : classMap.entrySet()) {
841 if (cMap.getValue().isPolicyTempalate()) {
842 list.add(cMap.getKey());
847 if (list.isEmpty()) {
848 if (classMap.containsKey(this.newModel.getModelName())) {
849 list.add(this.newModel.getModelName());
857 public Map<String, String> getAttributesListRefMap() {
858 return attributesListRefMap;
861 public Map<String, LinkedList<String>> getArrayTextList() {
862 return arrayTextList;
865 private OptimizationObject setOptimizationObjectValues(PolicyRestAdapter policyAdapter) {
866 OptimizationObject optimizationObject = new OptimizationObject();
867 optimizationObject.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_OOF));
869 if (policyAdapter.getServiceType() != null) {
870 optimizationObject.setService(policyAdapter.getServiceType());
871 optimizationObject.setVersion(policyAdapter.getVersion());
873 if (policyAdapter.getPolicyName() != null) {
874 optimizationObject.setPolicyName(policyAdapter.getPolicyName());
876 if (policyAdapter.getPolicyDescription() != null) {
877 optimizationObject.setDescription(policyAdapter.getPolicyDescription());
879 if (policyAdapter.getPriority() != null) {
880 optimizationObject.setPriority(policyAdapter.getPriority());
882 optimizationObject.setPriority("9999");
884 if (policyAdapter.getRiskLevel() != null) {
885 optimizationObject.setRiskLevel(policyAdapter.getRiskLevel());
887 if (policyAdapter.getRiskType() != null) {
888 optimizationObject.setRiskType(policyAdapter.getRiskType());
890 if (policyAdapter.getGuard() != null) {
891 optimizationObject.setGuard(policyAdapter.getGuard());
893 return optimizationObject;
899 class OptimizationObject {
901 private String service;
902 private String policyName;
903 private String description;
904 private String templateVersion;
905 private String version;
906 private String priority;
907 private String policyScope;
908 private String riskType;
909 private String riskLevel;
910 private String guard = null;
911 private Object content;