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;
24 import com.att.research.xacml.util.XACMLProperties;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.DeserializationFeature;
27 import com.fasterxml.jackson.databind.JsonNode;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.databind.ObjectWriter;
30 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;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
54 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
55 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
56 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
57 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
58 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
59 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
60 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
61 import org.apache.commons.compress.utils.IOUtils;
62 import org.apache.commons.fileupload.FileItem;
63 import org.apache.commons.fileupload.FileUploadException;
64 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
65 import org.apache.commons.fileupload.servlet.ServletFileUpload;
66 import org.apache.commons.lang.StringUtils;
67 import org.json.JSONArray;
68 import org.json.JSONObject;
69 import org.onap.policy.common.logging.flexlogger.FlexLogger;
70 import org.onap.policy.common.logging.flexlogger.Logger;
71 import org.onap.policy.rest.XACMLRestProperties;
72 import org.onap.policy.rest.adapter.PolicyRestAdapter;
73 import org.onap.policy.rest.dao.CommonClassDao;
74 import org.onap.policy.rest.jpa.MicroserviceHeaderdeFaults;
75 import org.onap.policy.rest.jpa.OptimizationModels;
76 import org.onap.policy.rest.jpa.PolicyEntity;
77 import org.onap.policy.rest.util.MSAttributeObject;
78 import org.onap.policy.rest.util.MSModelUtils;
79 import org.onap.policy.rest.util.MSModelUtils.MODEL_TYPE;
80 import org.onap.portalsdk.core.controller.RestrictedBaseController;
81 import org.springframework.beans.factory.annotation.Autowired;
82 import org.springframework.stereotype.Controller;
83 import org.springframework.web.bind.annotation.RequestMapping;
84 import org.springframework.web.servlet.ModelAndView;
88 public class CreateOptimizationController extends RestrictedBaseController {
89 private static final Logger LOGGER = FlexLogger.getLogger(CreateOptimizationController.class);
90 private static CommonClassDao commonClassDao;
92 public static CommonClassDao getCommonClassDao() {
93 return commonClassDao;
96 private OptimizationModels newModel;
97 private String newFile;
98 private String directory;
99 private List<String> modelList = new ArrayList<>();
100 private List<String> dirDependencyList = new ArrayList<>();
101 private LinkedHashMap<String, MSAttributeObject> classMap = new LinkedHashMap<>();
102 String referenceAttributes;
103 String attributeString;
104 Set<String> allManyTrueKeys = new HashSet<>();
106 public static final String DATATYPE = "data_types.policy.data.";
107 public static final String PROPERTIES = ".properties.";
108 public static final String TYPE = ".type";
109 public static final String STRING = "string";
110 public static final String INTEGER = "integer";
111 public static final String LIST = "list";
112 public static final String DEFAULT = ".default";
113 public static final String REQUIRED = ".required";
114 public static final String MATCHABLE = ".matchable";
115 public static final String MANYFALSE = ":MANY-false";
116 public static final String MODEL = "model";
117 public static final String MANY = "MANY-";
118 public static final String UTF8 = "UTF-8";
119 public static final String MODELNAME = "modelName";
120 public static final String APPLICATIONJSON = "application / json";
124 private CreateOptimizationController(CommonClassDao commonClassDao) {
125 setCommonClassDao(commonClassDao);
128 public static void setCommonClassDao(CommonClassDao commonClassDao) {
129 CreateOptimizationController.commonClassDao = commonClassDao;
132 public CreateOptimizationController() {
136 protected PolicyRestAdapter policyAdapter = null;
137 private Map<String, String> attributesListRefMap = new HashMap<>();
138 private Map<String, LinkedList<String>> arrayTextList = new HashMap<>();
139 CreateDcaeMicroServiceController msController = new CreateDcaeMicroServiceController();
141 public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
142 String jsonContent = null;
144 LOGGER.info("policyJSON :" + (root.get("policyJSON")).toString());
146 String tempJson = root.get("policyJSON").toString();
148 // ---replace empty value with the value below before calling decodeContent method.
149 String dummyValue = "*empty-value*" + UUID.randomUUID().toString();
150 LOGGER.info("dummyValue:" + dummyValue);
152 StringUtils.replaceEach(tempJson, new String[] {"\"\""}, new String[] {"\"" + dummyValue + "\""});
153 ObjectMapper mapper = new ObjectMapper();
154 JsonNode tempJsonNode = mapper.readTree(tempJson);
155 jsonContent = msController.decodeContent(tempJsonNode).toString();
156 constructJson(policyData, jsonContent, dummyValue);
157 } catch (Exception e) {
158 LOGGER.error("Error while decoding microservice content", e);
164 private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue) {
165 ObjectWriter om = new ObjectMapper().writer();
167 OptimizationObject optimizationObject = setOptimizationObjectValues(policyAdapter);
169 optimizationObject.setContent(jsonContent);
172 json = om.writeValueAsString(optimizationObject);
173 } catch (JsonProcessingException e) {
174 LOGGER.error("Error writing out the object", e);
176 LOGGER.info("input json: " + json);
177 LOGGER.info("input jsonContent: " + jsonContent);
178 String cleanJson = msController.cleanUPJson(json);
180 // --- reset empty value back after called cleanUPJson method and before calling removeNullAttributes
182 StringUtils.replaceEach(cleanJson, new String[] {"\"" + dummyValue + "\""}, new String[] {"\"\""});
183 LOGGER.info("tempJson: " + tempJson);
184 cleanJson = msController.removeNullAttributes(tempJson);
185 policyAdapter.setJsonBody(cleanJson);
186 return policyAdapter;
189 @RequestMapping(value = {"/policyController/getOptimizationTemplateData.htm"},
190 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
191 public ModelAndView getOptimizationTemplateData(HttpServletRequest request, HttpServletResponse response)
193 CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController();
194 ObjectMapper mapper = new ObjectMapper();
195 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
196 JsonNode root = mapper.readTree(request.getReader());
198 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
199 String servicename = value.toString().split("-v")[0];
200 String version = null;
201 if (value.toString().contains("-v")) {
202 version = value.toString().split("-v")[1];
205 OptimizationModels returnModel = getAttributeObject(servicename, version);
207 MicroserviceHeaderdeFaults returnHeaderDefauls = getHeaderDefaultsObject(value);
208 JSONObject jsonHdDefaultObj = null;
209 if (returnHeaderDefauls != null) {
210 jsonHdDefaultObj = new JSONObject();
211 jsonHdDefaultObj.put("onapName", returnHeaderDefauls.getOnapName());
212 jsonHdDefaultObj.put("guard", returnHeaderDefauls.getGuard());
213 jsonHdDefaultObj.put("riskLevel", returnHeaderDefauls.getRiskLevel());
214 jsonHdDefaultObj.put("riskType", returnHeaderDefauls.getRiskType());
215 jsonHdDefaultObj.put("priority", returnHeaderDefauls.getPriority());
218 String headDefautlsData = "";
219 if (jsonHdDefaultObj != null) {
220 headDefautlsData = jsonHdDefaultObj.toString();
221 LOGGER.info("returnHeaderDefauls headDefautlsData: " + headDefautlsData);
223 headDefautlsData = "null";
226 // Get all keys with "MANY-true" defined in their value from subAttribute
227 Set<String> allkeys = null;
228 if (returnModel.getSubattributes() != null && !returnModel.getSubattributes().isEmpty()) {
229 JSONObject json = new JSONObject(returnModel.getSubattributes());
231 allkeys = allManyTrueKeys;
232 allManyTrueKeys = new HashSet<>();
233 LOGGER.info("allkeys : " + allkeys);
236 // Get element order info
237 String dataOrderInfo = returnModel.getDataOrderInfo();
238 if (dataOrderInfo != null && !dataOrderInfo.startsWith("\"")) {
239 dataOrderInfo = "\"" + dataOrderInfo + "\"";
242 String nameOfTrueKeys = "";
243 if (allkeys != null) {
244 nameOfTrueKeys = allkeys.toString();
247 String jsonModel = createOptimizationJson(returnModel);
249 JSONObject jsonObject = new JSONObject(jsonModel);
251 JSONObject finalJsonObject = null;
252 if (allkeys != null) {
253 Iterator<String> iter = allkeys.iterator();
254 while (iter.hasNext()) {
255 // Convert to array values for MANY-true keys
256 finalJsonObject = controller.convertToArrayElement(jsonObject, iter.next());
260 if (finalJsonObject != null) {
261 LOGGER.info(finalJsonObject.toString());
262 jsonModel = finalJsonObject.toString();
265 // get all properties with "MANY-true" defined in Ref_attributes
266 Set<String> manyTrueProperties = controller.getManyTrueProperties(returnModel.getRefattributes());
267 JSONObject jsonObj = new JSONObject(jsonModel);
268 for (String s : manyTrueProperties) {
270 // convert to array element for MANY-true properties
271 finalJsonObject = controller.convertToArrayElement(jsonObj, s.trim());
274 if (finalJsonObject != null) {
275 LOGGER.info(finalJsonObject.toString());
276 jsonModel = finalJsonObject.toString();
279 response.setCharacterEncoding(UTF8);
280 response.setContentType(APPLICATIONJSON);
281 request.setCharacterEncoding(UTF8);
282 List<Object> list = new ArrayList<>();
283 PrintWriter out = response.getWriter();
284 String responseString = mapper.writeValueAsString(returnModel);
286 if ("".equals(nameOfTrueKeys)) {
287 j = new JSONObject("{optimizationModelData: " + responseString + ",jsonValue: " + jsonModel
288 + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:" + headDefautlsData + "}");
290 j = new JSONObject("{optimizationModelData: " + responseString + ",jsonValue: " + jsonModel
291 + ",allManyTrueKeys: " + allManyTrueKeys + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:"
292 + headDefautlsData + "}");
295 out.write(list.toString());
299 @SuppressWarnings({"rawtypes", "unchecked"})
300 private String createOptimizationJson(OptimizationModels returnModel) {
301 Map<String, String> attributeMap = new HashMap<>();
302 Map<String, String> refAttributeMap = new HashMap<>();
304 String attribute = returnModel.getAttributes();
305 if (attribute != null) {
306 attribute = attribute.trim();
308 String refAttribute = returnModel.getRefattributes();
309 if (refAttribute != null) {
310 refAttribute = refAttribute.trim();
313 String enumAttribute = returnModel.getEnumValues();
314 if (enumAttribute != null) {
315 enumAttribute = enumAttribute.trim();
318 CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController();
319 if (!StringUtils.isEmpty(attribute)) {
320 attributeMap = controller.convert(attribute, ",");
323 if (!StringUtils.isEmpty(refAttribute)) {
324 refAttributeMap = controller.convert(refAttribute, ",");
327 Gson gson = new Gson();
329 String subAttributes = returnModel.getSubattributes();
330 if (subAttributes != null) {
331 subAttributes = subAttributes.trim();
336 Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
338 JSONObject object = new JSONObject();
341 for (Entry<String, String> keySet : attributeMap.entrySet()) {
342 array = new JSONArray();
343 String value = keySet.getValue();
344 if ("true".equalsIgnoreCase(keySet.getValue().split(MANY)[1])) {
346 object.put(keySet.getKey().trim(), array);
348 object.put(keySet.getKey().trim(), value.trim());
352 for (Entry<String, String> keySet : refAttributeMap.entrySet()) {
353 array = new JSONArray();
354 String value = keySet.getValue().split(":")[0];
355 if (gsonObject.containsKey(value)) {
356 if ("true".equalsIgnoreCase(keySet.getValue().split(MANY)[1])) {
357 array.put(recursiveReference(value, gsonObject, enumAttribute));
358 object.put(keySet.getKey().trim(), array);
360 object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
363 if ("true".equalsIgnoreCase(keySet.getValue().split(MANY)[1])) {
364 array.put(value.trim());
365 object.put(keySet.getKey().trim(), array);
367 object.put(keySet.getKey().trim(), value.trim());
372 return object.toString();
375 @SuppressWarnings("unchecked")
376 private JSONObject recursiveReference(String name, Map<String, String> subAttributeMap, String enumAttribute) {
377 JSONObject object = new JSONObject();
378 Map<String, String> map;
379 Object returnClass = subAttributeMap.get(name);
380 map = (Map<String, String>) returnClass;
383 for (Entry<String, String> m : map.entrySet()) {
384 String[] splitValue = m.getValue().split(":");
385 array = new JSONArray();
386 if (subAttributeMap.containsKey(splitValue[0])) {
387 if ("true".equalsIgnoreCase(m.getValue().split(MANY)[1])) {
388 array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
389 object.put(m.getKey().trim(), array);
391 object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
394 if ("true".equalsIgnoreCase(m.getValue().split(MANY)[1])) {
395 array.put(splitValue[0].trim());
396 object.put(m.getKey().trim(), array);
398 object.put(m.getKey().trim(), splitValue[0].trim());
406 // call this method to start the recursive
407 private Set<String> getAllKeys(JSONObject json) {
408 return getAllKeys(json, new HashSet<>());
411 private Set<String> getAllKeys(JSONArray arr) {
412 return getAllKeys(arr, new HashSet<>());
415 private Set<String> getAllKeys(JSONArray arr, Set<String> keys) {
416 for (int i = 0; i < arr.length(); i++) {
417 Object obj = arr.get(i);
418 if (obj instanceof JSONObject)
419 keys.addAll(getAllKeys(arr.getJSONObject(i)));
420 if (obj instanceof JSONArray)
421 keys.addAll(getAllKeys(arr.getJSONArray(i)));
427 // this method returns a set of keys with "MANY-true" defined in their value.
428 private Set<String> getAllKeys(JSONObject json, Set<String> keys) {
429 for (String key : json.keySet()) {
430 Object obj = json.get(key);
431 if (obj instanceof String && ((String) obj).contains("MANY-true")) {
432 LOGGER.info("key : " + key);
433 LOGGER.info("obj : " + obj);
434 allManyTrueKeys.add(key);
436 if (obj instanceof JSONObject)
437 keys.addAll(getAllKeys(json.getJSONObject(key)));
438 if (obj instanceof JSONArray)
439 keys.addAll(getAllKeys(json.getJSONArray(key)));
445 @RequestMapping(value = {"/policyController/getModelServiceVersionData.htm"},
446 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
447 public ModelAndView getModelServiceVersionData(HttpServletRequest request, HttpServletResponse response)
449 ObjectMapper mapper = new ObjectMapper();
450 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
451 JsonNode root = mapper.readTree(request.getReader());
453 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
454 String servicename = value.split("-v")[0];
455 Set<String> returnList = getVersionList(servicename);
457 response.setCharacterEncoding(UTF8);
458 response.setContentType(APPLICATIONJSON);
459 request.setCharacterEncoding(UTF8);
460 List<Object> list = new ArrayList<>();
461 PrintWriter out = response.getWriter();
462 String responseString = mapper.writeValueAsString(returnList);
463 JSONObject j = new JSONObject("{optimizationModelVersionData: " + responseString + "}");
465 out.write(list.toString());
469 private Set<String> getVersionList(String name) {
470 OptimizationModels workingModel;
471 Set<String> list = new HashSet<>();
472 List<Object> optimizationModelsData = commonClassDao.getDataById(OptimizationModels.class, MODELNAME, name);
473 for (int i = 0; i < optimizationModelsData.size(); i++) {
474 workingModel = (OptimizationModels) optimizationModelsData.get(i);
475 if (workingModel.getVersion() != null) {
476 list.add(workingModel.getVersion());
484 private OptimizationModels getAttributeObject(String name, String version) {
485 OptimizationModels workingModel = new OptimizationModels();
486 List<Object> optimizationModelsData = commonClassDao.getDataById(OptimizationModels.class, MODELNAME, name);
487 for (int i = 0; i < optimizationModelsData.size(); i++) {
488 workingModel = (OptimizationModels) optimizationModelsData.get(i);
489 if (version != null) {
490 if (workingModel.getVersion() != null) {
491 if (workingModel.getVersion().equals(version)) {
505 private MicroserviceHeaderdeFaults getHeaderDefaultsObject(String modelName) {
506 return (MicroserviceHeaderdeFaults) commonClassDao.getEntityItem(MicroserviceHeaderdeFaults.class, MODELNAME,
510 public void prePopulatePolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
511 if (policyAdapter.getPolicyData() instanceof PolicyType) {
512 Object policyData = policyAdapter.getPolicyData();
513 PolicyType policy = (PolicyType) policyData;
514 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
515 String policyNameValue =
516 policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("OOF_") + 4);
517 policyAdapter.setPolicyName(policyNameValue);
518 String description = "";
520 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
521 } catch (Exception e) {
522 LOGGER.error("Error while collecting the description tag in " + policyNameValue, e);
523 description = policy.getDescription();
525 policyAdapter.setPolicyDescription(description);
526 // Get the target data under policy.
527 TargetType target = policy.getTarget();
528 if (target != null) {
529 // Under target we have AnyOFType
530 List<AnyOfType> anyOfList = target.getAnyOf();
531 if (anyOfList != null) {
532 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
533 while (iterAnyOf.hasNext()) {
534 AnyOfType anyOf = iterAnyOf.next();
535 // Under AnyOFType we have AllOFType
536 List<AllOfType> allOfList = anyOf.getAllOf();
537 if (allOfList != null) {
538 Iterator<AllOfType> iterAllOf = allOfList.iterator();
539 while (iterAllOf.hasNext()) {
540 AllOfType allOf = iterAllOf.next();
541 // Under AllOFType we have Match
542 List<MatchType> matchList = allOf.getMatch();
543 if (matchList != null) {
544 Iterator<MatchType> iterMatch = matchList.iterator();
545 while (matchList.size() > 1 && iterMatch.hasNext()) {
546 MatchType match = iterMatch.next();
548 // Under the match we have attribute value and
549 // attributeDesignator. So,finally down to the actual attribute.
551 AttributeValueType attributeValue = match.getAttributeValue();
552 String value = (String) attributeValue.getContent().get(0);
553 AttributeDesignatorType designator = match.getAttributeDesignator();
554 String attributeId = designator.getAttributeId();
555 // First match in the target is OnapName, so set that value.
556 if ("ONAPName".equals(attributeId)) {
557 policyAdapter.setOnapName(value);
559 if ("RiskType".equals(attributeId)) {
560 policyAdapter.setRiskType(value);
562 if ("RiskLevel".equals(attributeId)) {
563 policyAdapter.setRiskLevel(value);
565 if ("guard".equals(attributeId)) {
566 policyAdapter.setGuard(value);
568 if ("TTLDate".equals(attributeId) && !value.contains("NA")) {
569 PolicyController controller = new PolicyController();
570 String newDate = controller.convertDate(value);
571 policyAdapter.setTtlDate(newDate);
574 readFile(policyAdapter, entity);
584 @SuppressWarnings("unchecked")
585 private void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
586 String policyScopeName = null;
587 ObjectMapper mapper = new ObjectMapper();
589 OptimizationObject optimizationBody =
590 mapper.readValue(entity.getConfigurationData().getConfigBody(), OptimizationObject.class);
591 policyScopeName = msController.getPolicyScope(optimizationBody.getPolicyScope());
592 policyAdapter.setPolicyScope(policyScopeName);
594 policyAdapter.setPriority(optimizationBody.getPriority());
596 if (optimizationBody.getVersion() != null) {
597 policyAdapter.setServiceType(optimizationBody.getService());
598 policyAdapter.setVersion(optimizationBody.getVersion());
600 policyAdapter.setServiceType(optimizationBody.getService());
602 if (optimizationBody.getContent() != null) {
603 LinkedHashMap<String, Object> data = new LinkedHashMap<>();
604 LinkedHashMap<String, ?> map = (LinkedHashMap<String, ?>) optimizationBody.getContent();
605 msController.readRecursivlyJSONContent(map, data);
606 policyAdapter.setRuleData(data);
609 } catch (Exception e) {
615 @RequestMapping(value = {"/oof_dictionary/set_ModelData"},
616 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
617 public void setModelData(HttpServletRequest request, HttpServletResponse response)
618 throws IOException, FileUploadException {
619 modelList = new ArrayList<>();
620 dirDependencyList = new ArrayList<>();
621 classMap = new LinkedHashMap<>();
622 List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
626 String errorMsg = "";
627 for (FileItem item : items) {
628 if (item.getName().endsWith(".zip") || item.getName().endsWith(".xmi") || item.getName().endsWith(".yml")) {
629 this.newModel = new OptimizationModels();
631 File file = new File(item.getName());
632 OutputStream outputStream = new FileOutputStream(file);
633 IOUtils.copy(item.getInputStream(), outputStream);
634 outputStream.close();
635 this.newFile = file.toString();
636 this.newModel.setModelName(this.newFile.split("-v")[0]);
638 if (this.newFile.contains("-v")) {
639 if (item.getName().endsWith(".zip")) {
640 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".zip", ""));
642 } else if (item.getName().endsWith(".yml")) {
643 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".yml", ""));
646 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".xmi", ""));
649 } catch (Exception e) {
650 LOGGER.error("Upload error : ", e);
651 errorMsg = "Upload error:" + e.getMessage();
657 if (!errorMsg.isEmpty()) {
659 PrintWriter out = response.getWriter();
661 response.setCharacterEncoding(UTF8);
662 response.setContentType(APPLICATIONJSON);
663 request.setCharacterEncoding(UTF8);
665 JSONObject j = new JSONObject();
666 j.put("errorMsg", errorMsg);
667 out.write(j.toString());
671 List<File> fileList = new ArrayList<>();
672 MSModelUtils modelUtil = new MSModelUtils();
673 this.directory = MODEL;
675 extractFolder(this.newFile);
676 fileList = listModelFiles(this.directory);
678 modelUtil.parseTosca(this.newFile);
680 File file = new File(this.newFile);
686 // Process Main Model file first
687 classMap = new LinkedHashMap<>();
688 for (File file : fileList) {
689 if (!file.isDirectory() && file.getName().endsWith(".xmi")) {
690 retrieveDependency(file.toString());
694 modelList = createList();
696 msController.cleanUp(this.newFile);
697 msController.cleanUp(directory);
700 modelList.add(this.newModel.getModelName());
701 String className = this.newModel.getModelName();
702 MSAttributeObject optimizationAttributes = new MSAttributeObject();
703 optimizationAttributes.setClassName(className);
705 LinkedHashMap<String, String> returnAttributeList = new LinkedHashMap<>();
706 returnAttributeList.put(className, modelUtil.getAttributeString());
707 optimizationAttributes.setAttribute(returnAttributeList);
709 optimizationAttributes.setSubClass(modelUtil.getRetmap());
711 optimizationAttributes.setMatchingSet(modelUtil.getMatchableValues());
713 LinkedHashMap<String, String> returnReferenceList = new LinkedHashMap<>();
714 returnReferenceList.put(className, modelUtil.getReferenceAttributes());
715 optimizationAttributes.setRefAttribute(returnReferenceList);
717 if (!"".equals(modelUtil.getListConstraints())) {
718 LinkedHashMap<String, String> enumList = new LinkedHashMap<>();
719 String[] listArray = modelUtil.getListConstraints().split("#");
720 for (String str : listArray) {
721 String[] strArr = str.split("=");
722 if (strArr.length > 1) {
723 enumList.put(strArr[0], strArr[1]);
726 optimizationAttributes.setEnumType(enumList);
729 classMap = new LinkedHashMap<>();
730 classMap.put(className, optimizationAttributes);
734 PrintWriter out = response.getWriter();
736 response.setCharacterEncoding(UTF8);
737 response.setContentType(APPLICATIONJSON);
738 request.setCharacterEncoding(UTF8);
740 ObjectMapper mapper = new ObjectMapper();
741 JSONObject j = new JSONObject();
742 j.put("classListDatas", modelList);
743 j.put("modelDatas", mapper.writeValueAsString(classMap));
744 j.put("modelType", modelType);
745 j.put("dataOrderInfo", modelUtil.getDataOrderInfo());
747 out.write(j.toString());
751 * Unzip file and store in the model directory for processing
753 @SuppressWarnings("rawtypes")
754 private void extractFolder(String zipFile) {
756 File file = new File(zipFile);
758 try (ZipFile zip = new ZipFile(file)) {
759 String newPath = MODEL + File.separator + zipFile.substring(0, zipFile.length() - 4);
760 this.directory = MODEL + File.separator + zipFile.substring(0, zipFile.length() - 4);
761 msController.checkZipDirectory(this.directory);
762 new File(newPath).mkdir();
763 Enumeration zipFileEntries = zip.entries();
765 // Process each entry
766 while (zipFileEntries.hasMoreElements()) {
767 // grab a zip file entry
768 ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
769 String currentEntry = entry.getName();
770 File destFile = new File(MODEL + File.separator + currentEntry);
771 File destinationParent = destFile.getParentFile();
773 destinationParent.mkdirs();
775 if (!entry.isDirectory()) {
776 BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
778 byte[] data = new byte[BUFFER];
779 try (FileOutputStream fos = new FileOutputStream(destFile);
780 BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) {
781 while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
782 dest.write(data, 0, currentByte);
785 } catch (IOException e) {
786 LOGGER.error("Failed to write zip contents to {}" + destFile + e);
788 // PLD should I throw e?
794 if (currentEntry.endsWith(".zip")) {
795 extractFolder(destFile.getAbsolutePath());
798 } catch (IOException e) {
799 LOGGER.error("Failed to unzip model file " + zipFile, e);
803 private void retrieveDependency(String workingFile) {
805 MSModelUtils utils = new MSModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName());
806 Map<String, MSAttributeObject> tempMap;
808 tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
810 classMap.putAll(tempMap);
811 LOGGER.info(tempMap);
817 private List<File> listModelFiles(String directoryName) {
818 File fileDirectory = new File(directoryName);
819 List<File> resultList = new ArrayList<>();
820 File[] fList = fileDirectory.listFiles();
821 for (File file : fList) {
823 resultList.add(file);
824 } else if (file.isDirectory()) {
825 dirDependencyList.add(file.getName());
826 resultList.addAll(listModelFiles(file.getAbsolutePath()));
832 private List<String> createList() {
833 List<String> list = new ArrayList<>();
834 for (Entry<String, MSAttributeObject> cMap : classMap.entrySet()) {
835 if (cMap.getValue().isPolicyTempalate()) {
836 list.add(cMap.getKey());
841 if (list.isEmpty()) {
842 if (classMap.containsKey(this.newModel.getModelName())) {
843 list.add(this.newModel.getModelName());
851 public Map<String, String> getAttributesListRefMap() {
852 return attributesListRefMap;
855 public Map<String, LinkedList<String>> getArrayTextList() {
856 return arrayTextList;
859 private OptimizationObject setOptimizationObjectValues(PolicyRestAdapter policyAdapter) {
860 OptimizationObject optimizationObject = new OptimizationObject();
861 optimizationObject.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_OOF));
863 if (policyAdapter.getServiceType() != null) {
864 optimizationObject.setService(policyAdapter.getServiceType());
865 optimizationObject.setVersion(policyAdapter.getVersion());
867 if (policyAdapter.getPolicyName() != null) {
868 optimizationObject.setPolicyName(policyAdapter.getPolicyName());
870 if (policyAdapter.getPolicyDescription() != null) {
871 optimizationObject.setDescription(policyAdapter.getPolicyDescription());
873 if (policyAdapter.getPriority() != null) {
874 optimizationObject.setPriority(policyAdapter.getPriority());
876 optimizationObject.setPriority("9999");
878 if (policyAdapter.getRiskLevel() != null) {
879 optimizationObject.setRiskLevel(policyAdapter.getRiskLevel());
881 if (policyAdapter.getRiskType() != null) {
882 optimizationObject.setRiskType(policyAdapter.getRiskType());
884 if (policyAdapter.getGuard() != null) {
885 optimizationObject.setGuard(policyAdapter.getGuard());
887 return optimizationObject;
892 class OptimizationObject {
894 private String service;
895 private String policyName;
896 private String description;
897 private String templateVersion;
898 private String version;
899 private String priority;
900 private String policyScope;
901 private String riskType;
902 private String riskLevel;
903 private String guard = null;
905 public String getGuard() {
909 public void setGuard(String guard) {
913 public String getRiskType() {
917 public void setRiskType(String riskType) {
918 this.riskType = riskType;
921 public String getRiskLevel() {
925 public void setRiskLevel(String riskLevel) {
926 this.riskLevel = riskLevel;
929 public String getPriority() {
933 public void setPriority(String priority) {
934 this.priority = priority;
937 public String getPolicyScope() {
941 public void setPolicyScope(String policyScope) {
942 this.policyScope = policyScope;
945 public String getVersion() {
949 public void setVersion(String version) {
950 this.version = version;
953 private Object content;
955 public String getPolicyName() {
959 public void setPolicyName(String policyName) {
960 this.policyName = policyName;
963 public String getDescription() {
967 public void setDescription(String description) {
968 this.description = description;
971 public Object getContent() {
975 public void setContent(Object content) {
976 this.content = content;
979 public String getService() {
983 public void setService(String service) {
984 this.service = service;
987 public String getTemplateVersion() {
988 return templateVersion;
991 public void setTemplateVersion(String templateVersion) {
992 this.templateVersion = templateVersion;