2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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 java.io.BufferedInputStream;
25 import java.io.BufferedOutputStream;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.io.PrintWriter;
31 import java.util.ArrayList;
32 import java.util.Enumeration;
33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.Iterator;
36 import java.util.LinkedHashMap;
37 import java.util.LinkedList;
38 import java.util.List;
40 import java.util.Map.Entry;
42 import java.util.UUID;
43 import java.util.zip.ZipEntry;
44 import java.util.zip.ZipFile;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
49 import org.apache.commons.compress.utils.IOUtils;
50 import org.apache.commons.fileupload.FileItem;
51 import org.apache.commons.fileupload.FileUploadException;
52 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
53 import org.apache.commons.fileupload.servlet.ServletFileUpload;
54 import org.apache.commons.lang.StringUtils;
55 import org.json.JSONArray;
56 import org.json.JSONObject;
57 import org.onap.policy.common.logging.flexlogger.FlexLogger;
58 import org.onap.policy.common.logging.flexlogger.Logger;
59 import org.onap.policy.rest.XACMLRestProperties;
60 import org.onap.policy.rest.adapter.PolicyRestAdapter;
61 import org.onap.policy.rest.dao.CommonClassDao;
62 import org.onap.policy.rest.jpa.OptimizationModels;
63 import org.onap.policy.rest.jpa.PolicyEntity;
64 import org.onap.policy.rest.jpa.MicroserviceHeaderdeFaults;
65 import org.onap.policy.rest.util.MSAttributeObject;
66 import org.onap.policy.rest.util.MSModelUtils;
67 import org.onap.policy.rest.util.MSModelUtils.MODEL_TYPE;
68 import org.onap.portalsdk.core.controller.RestrictedBaseController;
69 import org.springframework.beans.factory.annotation.Autowired;
70 import org.springframework.stereotype.Controller;
71 import org.springframework.web.bind.annotation.RequestMapping;
72 import org.springframework.web.servlet.ModelAndView;
74 import com.att.research.xacml.util.XACMLProperties;
75 import com.fasterxml.jackson.core.JsonProcessingException;
76 import com.fasterxml.jackson.databind.DeserializationFeature;
77 import com.fasterxml.jackson.databind.JsonNode;
78 import com.fasterxml.jackson.databind.ObjectMapper;
79 import com.fasterxml.jackson.databind.ObjectWriter;
80 import com.google.gson.Gson;
82 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
83 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
84 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
85 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
86 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
87 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
88 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
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";
128 private CreateOptimizationController(CommonClassDao commonClassDao){
129 setCommonClassDao(commonClassDao);
132 public static void setCommonClassDao(CommonClassDao commonClassDao) {
133 CreateOptimizationController.commonClassDao = commonClassDao;
136 public CreateOptimizationController(){
140 protected PolicyRestAdapter policyAdapter = null;
141 private Map<String, String> attributesListRefMap = new HashMap<>();
142 private Map<String, LinkedList<String>> arrayTextList = new HashMap<>();
143 CreateDcaeMicroServiceController msController = new CreateDcaeMicroServiceController();
145 public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
146 String jsonContent = null;
148 LOGGER.info("policyJSON :" + (root.get("policyJSON")).toString());
150 String tempJson = root.get("policyJSON").toString();
152 //---replace empty value with the value below before calling decodeContent method.
153 String dummyValue = "*empty-value*" + UUID.randomUUID().toString();
154 LOGGER.info("dummyValue:" + dummyValue);
155 tempJson = 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);
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
184 String tempJson = StringUtils.replaceEach(cleanJson, new String[]{"\""+dummyValue+"\""}, new String[]{"\"\""});
185 LOGGER.info("tempJson: " + tempJson);
186 cleanJson = msController.removeNullAttributes(tempJson);
187 policyAdapter.setJsonBody(cleanJson);
188 return policyAdapter;
191 @RequestMapping(value={"/policyController/getOptimizationTemplateData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
192 public ModelAndView getOptimizationTemplateData(HttpServletRequest request, HttpServletResponse response) throws IOException{
193 ObjectMapper mapper = new ObjectMapper();
194 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
195 JsonNode root = mapper.readTree(request.getReader());
197 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
198 String servicename = value.toString().split("-v")[0];
199 String version = null;
200 if (value.toString().contains("-v")){
201 version = value.toString().split("-v")[1];
204 OptimizationModels returnModel = getAttributeObject(servicename, version);
206 MicroserviceHeaderdeFaults returnHeaderDefauls = getHeaderDefaultsObject(value);
207 JSONObject jsonHdDefaultObj = null;
208 if(returnHeaderDefauls != null){
209 jsonHdDefaultObj = new JSONObject();
210 jsonHdDefaultObj.put("onapName", returnHeaderDefauls.getOnapName());
211 jsonHdDefaultObj.put("guard", returnHeaderDefauls.getGuard());
212 jsonHdDefaultObj.put("riskLevel", returnHeaderDefauls.getRiskLevel());
213 jsonHdDefaultObj.put("riskType", returnHeaderDefauls.getRiskType());
214 jsonHdDefaultObj.put("priority", returnHeaderDefauls.getPriority());
217 String headDefautlsData = "";
218 if(jsonHdDefaultObj != null){
219 headDefautlsData = jsonHdDefaultObj.toString();
220 LOGGER.info("returnHeaderDefauls headDefautlsData: " + headDefautlsData);
222 headDefautlsData = "null";
225 //Get all keys with "MANY-true" defined in their value from subAttribute
226 Set<String> allkeys = null;
227 if(returnModel.getSubattributes() != null && !returnModel.getSubattributes().isEmpty()){
228 JSONObject json = new JSONObject(returnModel.getSubattributes());
230 allkeys = allManyTrueKeys;
231 allManyTrueKeys = new HashSet <>();
232 LOGGER.info("allkeys : " + allkeys);
235 //Get element order info
236 String dataOrderInfo = returnModel.getDataOrderInfo();
237 if(dataOrderInfo != null && !dataOrderInfo.startsWith("\"")){
238 dataOrderInfo = "\"" + dataOrderInfo + "\"";
241 String nameOfTrueKeys = "";
243 nameOfTrueKeys = allkeys.toString();
246 String jsonModel = createOptimizationJson(returnModel);
248 JSONObject jsonObject = new JSONObject(jsonModel);
250 JSONObject finalJsonObject = null;
252 Iterator<String> iter = allkeys.iterator();
253 while(iter.hasNext()){
254 //Convert to array values for MANY-true keys
255 finalJsonObject = CreateDcaeMicroServiceController.convertToArrayElement(jsonObject, iter.next());
259 if(finalJsonObject != null){
260 LOGGER.info(finalJsonObject.toString());
261 jsonModel = finalJsonObject.toString();
264 //get all properties with "MANY-true" defined in Ref_attributes
265 Set<String> manyTrueProperties = CreateDcaeMicroServiceController.getManyTrueProperties(returnModel.getRefattributes());
266 JSONObject jsonObj = new JSONObject(jsonModel);
267 for (String s : manyTrueProperties) {
269 //convert to array element for MANY-true properties
270 finalJsonObject = CreateDcaeMicroServiceController.convertToArrayElement(jsonObj, s.trim());
273 if(finalJsonObject != null){
274 LOGGER.info(finalJsonObject.toString());
275 jsonModel = finalJsonObject.toString();
278 response.setCharacterEncoding(UTF8);
279 response.setContentType(APPLICATIONJSON);
280 request.setCharacterEncoding(UTF8);
281 List<Object> list = new ArrayList<>();
282 PrintWriter out = response.getWriter();
283 String responseString = mapper.writeValueAsString(returnModel);
285 if("".equals(nameOfTrueKeys)){
286 j = new JSONObject("{optimizationModelData: " + responseString + ",jsonValue: " + jsonModel + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:" + headDefautlsData +"}");
288 j = new JSONObject("{optimizationModelData: " + responseString + ",jsonValue: " + jsonModel + ",allManyTrueKeys: " + allManyTrueKeys+",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:" + headDefautlsData+ "}");
291 out.write(list.toString());
295 @SuppressWarnings({ "rawtypes", "unchecked" })
296 private String createOptimizationJson(OptimizationModels returnModel) {
297 Map<String, String> attributeMap = new HashMap<>();
298 Map<String, String> refAttributeMap = new HashMap<>();
300 String attribute = returnModel.getAttributes();
301 if(attribute != null){
302 attribute = attribute.trim();
304 String refAttribute = returnModel.getRefattributes();
305 if(refAttribute != null){
306 refAttribute = refAttribute.trim();
309 String enumAttribute = returnModel.getEnumValues();
310 if(enumAttribute != null){
311 enumAttribute = enumAttribute.trim();
314 if (!StringUtils.isEmpty(attribute)){
315 attributeMap = CreateDcaeMicroServiceController.convert(attribute, ",");
318 if (!StringUtils.isEmpty(refAttribute)){
319 refAttributeMap = CreateDcaeMicroServiceController.convert(refAttribute, ",");
322 Gson gson = new Gson();
324 String subAttributes = returnModel.getSubattributes();
325 if(subAttributes != null){
326 subAttributes = subAttributes.trim();
331 Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
333 JSONObject object = new JSONObject();
336 for (Entry<String, String> keySet : attributeMap.entrySet()){
337 array = new JSONArray();
338 String value = keySet.getValue();
339 if ("true".equalsIgnoreCase(keySet.getValue().split(MANY)[1])){
341 object.put(keySet.getKey().trim(), array);
343 object.put(keySet.getKey().trim(), value.trim());
347 for (Entry<String, String> keySet : refAttributeMap.entrySet()){
348 array = new JSONArray();
349 String value = keySet.getValue().split(":")[0];
350 if (gsonObject.containsKey(value)){
351 if ("true".equalsIgnoreCase(keySet.getValue().split(MANY)[1])){
352 array.put(recursiveReference(value, gsonObject, enumAttribute));
353 object.put(keySet.getKey().trim(), array);
355 object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
358 if ("true".equalsIgnoreCase(keySet.getValue().split(MANY)[1])){
359 array.put(value.trim());
360 object.put(keySet.getKey().trim(), array);
362 object.put(keySet.getKey().trim(), value.trim());
367 return object.toString();
370 @SuppressWarnings("unchecked")
371 private JSONObject recursiveReference(String name, Map<String,String> subAttributeMap, String enumAttribute) {
372 JSONObject object = new JSONObject();
373 Map<String, String> map;
374 Object returnClass = subAttributeMap.get(name);
375 map = (Map<String, String>) returnClass;
378 for( Entry<String, String> m:map.entrySet()){
379 String[] splitValue = m.getValue().split(":");
380 array = new JSONArray();
381 if (subAttributeMap.containsKey(splitValue[0])){
382 if ("true".equalsIgnoreCase(m.getValue().split(MANY)[1])){
383 array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
384 object.put(m.getKey().trim(), array);
386 object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
389 if ("true".equalsIgnoreCase(m.getValue().split(MANY)[1])){
390 array.put(splitValue[0].trim());
391 object.put(m.getKey().trim(), array);
393 object.put(m.getKey().trim(), splitValue[0].trim());
401 //call this method to start the recursive
402 private Set<String> getAllKeys(JSONObject json) {
403 return getAllKeys(json, new HashSet<>());
406 private Set<String> getAllKeys(JSONArray arr) {
407 return getAllKeys(arr, new HashSet<>());
410 private Set<String> getAllKeys(JSONArray arr, Set<String> keys) {
411 for (int i = 0; i < arr.length(); i++) {
412 Object obj = arr.get(i);
413 if (obj instanceof JSONObject) keys.addAll(getAllKeys(arr.getJSONObject(i)));
414 if (obj instanceof JSONArray) keys.addAll(getAllKeys(arr.getJSONArray(i)));
420 // this method returns a set of keys with "MANY-true" defined in their value.
421 private Set<String> getAllKeys(JSONObject json, Set<String> keys) {
422 for (String key : json.keySet()) {
423 Object obj = json.get(key);
424 if(obj instanceof String && ((String) obj).contains("MANY-true")){
425 LOGGER.info("key : " + key);
426 LOGGER.info("obj : " + obj);
427 allManyTrueKeys.add(key);
429 if (obj instanceof JSONObject) keys.addAll(getAllKeys(json.getJSONObject(key)));
430 if (obj instanceof JSONArray) keys.addAll(getAllKeys(json.getJSONArray(key)));
436 @RequestMapping(value={"/policyController/getModelServiceVersionData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
437 public ModelAndView getModelServiceVersionData(HttpServletRequest request, HttpServletResponse response) throws IOException{
438 ObjectMapper mapper = new ObjectMapper();
439 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
440 JsonNode root = mapper.readTree(request.getReader());
442 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
443 String servicename = value.split("-v")[0];
444 Set<String> returnList = getVersionList(servicename);
446 response.setCharacterEncoding(UTF8);
447 response.setContentType(APPLICATIONJSON);
448 request.setCharacterEncoding(UTF8);
449 List<Object> list = new ArrayList<>();
450 PrintWriter out = response.getWriter();
451 String responseString = mapper.writeValueAsString(returnList);
452 JSONObject j = new JSONObject("{optimizationModelVersionData: " + responseString +"}");
454 out.write(list.toString());
458 private Set<String> getVersionList(String name) {
459 OptimizationModels workingModel;
460 Set<String> list = new HashSet<>();
461 List<Object> optimizationModelsData = commonClassDao.getDataById(OptimizationModels.class, MODELNAME, name);
462 for (int i = 0; i < optimizationModelsData.size(); i++) {
463 workingModel = (OptimizationModels) optimizationModelsData.get(i);
464 if (workingModel.getVersion()!=null){
465 list.add(workingModel.getVersion());
473 private OptimizationModels getAttributeObject(String name, String version) {
474 OptimizationModels workingModel = new OptimizationModels();
475 List<Object> optimizationModelsData = commonClassDao.getDataById(OptimizationModels.class, MODELNAME, name);
476 for (int i = 0; i < optimizationModelsData.size(); i++) {
477 workingModel = (OptimizationModels) optimizationModelsData.get(i);
479 if (workingModel.getVersion()!=null){
480 if (workingModel.getVersion().equals(version)){
494 private MicroserviceHeaderdeFaults getHeaderDefaultsObject(String modelName) {
495 return (MicroserviceHeaderdeFaults) commonClassDao.getEntityItem(MicroserviceHeaderdeFaults.class, MODELNAME, modelName);
498 public void prePopulatePolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
499 if (policyAdapter.getPolicyData() instanceof PolicyType) {
500 Object policyData = policyAdapter.getPolicyData();
501 PolicyType policy = (PolicyType) policyData;
502 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
503 String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("OOF_") +4);
504 policyAdapter.setPolicyName(policyNameValue);
505 String description = "";
507 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
509 LOGGER.error("Error while collecting the description tag in " + policyNameValue ,e);
510 description = policy.getDescription();
512 policyAdapter.setPolicyDescription(description);
513 // Get the target data under policy.
514 TargetType target = policy.getTarget();
515 if (target != null) {
516 // Under target we have AnyOFType
517 List<AnyOfType> anyOfList = target.getAnyOf();
518 if (anyOfList != null) {
519 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
520 while (iterAnyOf.hasNext()) {
521 AnyOfType anyOf = iterAnyOf.next();
522 // Under AnyOFType we have AllOFType
523 List<AllOfType> allOfList = anyOf.getAllOf();
524 if (allOfList != null) {
525 Iterator<AllOfType> iterAllOf = allOfList.iterator();
526 while (iterAllOf.hasNext()) {
527 AllOfType allOf = iterAllOf.next();
528 // Under AllOFType we have Match
529 List<MatchType> matchList = allOf.getMatch();
530 if (matchList != null) {
531 Iterator<MatchType> iterMatch = matchList.iterator();
532 while (matchList.size()>1 && iterMatch.hasNext()) {
533 MatchType match = iterMatch.next();
535 // Under the match we have attribute value and
536 // attributeDesignator. So,finally down to the actual attribute.
538 AttributeValueType attributeValue = match.getAttributeValue();
539 String value = (String) attributeValue.getContent().get(0);
540 AttributeDesignatorType designator = match.getAttributeDesignator();
541 String attributeId = designator.getAttributeId();
542 // First match in the target is OnapName, so set that value.
543 if ("ONAPName".equals(attributeId)) {
544 policyAdapter.setOnapName(value);
546 if ("RiskType".equals(attributeId)){
547 policyAdapter.setRiskType(value);
549 if ("RiskLevel".equals(attributeId)){
550 policyAdapter.setRiskLevel(value);
552 if ("guard".equals(attributeId)){
553 policyAdapter.setGuard(value);
555 if ("TTLDate".equals(attributeId) && !value.contains("NA")){
556 PolicyController controller = new PolicyController();
557 String newDate = controller.convertDate(value);
558 policyAdapter.setTtlDate(newDate);
561 readFile(policyAdapter, entity);
571 @SuppressWarnings("unchecked")
572 private void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
573 String policyScopeName = null;
574 ObjectMapper mapper = new ObjectMapper();
576 OptimizationObject optimizationBody = mapper.readValue(entity.getConfigurationData().getConfigBody(), OptimizationObject.class);
577 policyScopeName = msController.getPolicyScope(optimizationBody.getPolicyScope());
578 policyAdapter.setPolicyScope(policyScopeName);
580 policyAdapter.setPriority(optimizationBody.getPriority());
582 if (optimizationBody.getVersion()!= null){
583 policyAdapter.setServiceType(optimizationBody.getService());
584 policyAdapter.setVersion(optimizationBody.getVersion());
586 policyAdapter.setServiceType(optimizationBody.getService());
588 if(optimizationBody.getContent() != null){
589 LinkedHashMap<String, Object> data = new LinkedHashMap<>();
590 LinkedHashMap<String, ?> map = (LinkedHashMap<String, ?>) optimizationBody.getContent();
591 msController.readRecursivlyJSONContent(map, data);
592 policyAdapter.setRuleData(data);
595 } catch (Exception e) {
601 @RequestMapping(value={"/oof_dictionary/set_ModelData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
602 public void setModelData(HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException{
603 modelList = new ArrayList<>();
604 dirDependencyList = new ArrayList<>();
605 classMap = new LinkedHashMap<>();
606 List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
610 String errorMsg = "";
611 for (FileItem item : items) {
612 if(item.getName().endsWith(".zip") || item.getName().endsWith(".xmi")||item.getName().endsWith(".yml")){
613 this.newModel = new OptimizationModels();
615 File file = new File(item.getName());
616 OutputStream outputStream = new FileOutputStream(file);
617 IOUtils.copy(item.getInputStream(), outputStream);
618 outputStream.close();
619 this.newFile = file.toString();
620 this.newModel.setModelName(this.newFile.split("-v")[0]);
622 if (this.newFile.contains("-v")){
623 if (item.getName().endsWith(".zip")){
624 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".zip", ""));
626 }else if(item.getName().endsWith(".yml")){
627 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".yml", ""));
631 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".xmi", ""));
635 LOGGER.error("Upload error : ", e);
636 errorMsg = "Upload error:" + e.getMessage();
642 if(!errorMsg.isEmpty()){
644 PrintWriter out = response.getWriter();
646 response.setCharacterEncoding(UTF8);
647 response.setContentType(APPLICATIONJSON);
648 request.setCharacterEncoding(UTF8);
650 JSONObject j = new JSONObject();
651 j.put("errorMsg", errorMsg);
652 out.write(j.toString());
656 List<File> fileList = new ArrayList<>();
657 MSModelUtils modelUtil = new MSModelUtils();
658 this.directory = MODEL;
660 extractFolder(this.newFile);
661 fileList = listModelFiles(this.directory);
663 modelUtil.parseTosca(this.newFile);
665 File file = new File(this.newFile);
671 //Process Main Model file first
672 classMap = new LinkedHashMap<>();
673 for (File file : fileList) {
674 if(!file.isDirectory() && file.getName().endsWith(".xmi")){
675 retrieveDependency(file.toString());
679 modelList = createList();
681 msController.cleanUp(this.newFile);
682 msController.cleanUp(directory);
685 modelList.add(this.newModel.getModelName());
686 String className=this.newModel.getModelName();
687 MSAttributeObject optimizationAttributes= new MSAttributeObject();
688 optimizationAttributes.setClassName(className);
690 LinkedHashMap<String, String> returnAttributeList =new LinkedHashMap<>();
691 returnAttributeList.put(className, modelUtil.getAttributeString());
692 optimizationAttributes.setAttribute(returnAttributeList);
694 optimizationAttributes.setSubClass(modelUtil.getRetmap());
696 optimizationAttributes.setMatchingSet(modelUtil.getMatchableValues());
698 LinkedHashMap<String, String> returnReferenceList =new LinkedHashMap<>();
699 returnReferenceList.put(className, modelUtil.getReferenceAttributes());
700 optimizationAttributes.setRefAttribute(returnReferenceList);
702 if(!"".equals(modelUtil.getListConstraints())){
703 LinkedHashMap<String, String> enumList =new LinkedHashMap<>();
704 String[] listArray=modelUtil.getListConstraints().split("#");
705 for(String str:listArray){
706 String[] strArr= str.split("=");
708 enumList.put(strArr[0], strArr[1]);
711 optimizationAttributes.setEnumType(enumList);
714 classMap=new LinkedHashMap<>();
715 classMap.put(className, optimizationAttributes);
719 PrintWriter out = response.getWriter();
721 response.setCharacterEncoding(UTF8);
722 response.setContentType(APPLICATIONJSON);
723 request.setCharacterEncoding(UTF8);
725 ObjectMapper mapper = new ObjectMapper();
726 JSONObject j = new JSONObject();
727 j.put("classListDatas", modelList);
728 j.put("modelDatas", mapper.writeValueAsString(classMap));
729 j.put("modelType", modelType);
730 j.put("dataOrderInfo", modelUtil.getDataOrderInfo());
732 out.write(j.toString());
736 * Unzip file and store in the model directory for processing
738 @SuppressWarnings("rawtypes")
739 private void extractFolder(String zipFile ) {
741 File file = new File(zipFile);
743 try (ZipFile zip = new ZipFile(file)) {
744 String newPath = MODEL + File.separator + zipFile.substring(0, zipFile.length() - 4);
745 this.directory = MODEL + File.separator + zipFile.substring(0, zipFile.length() - 4);
746 msController.checkZipDirectory(this.directory);
747 new File(newPath).mkdir();
748 Enumeration zipFileEntries = zip.entries();
750 // Process each entry
751 while (zipFileEntries.hasMoreElements()){
752 // grab a zip file entry
753 ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
754 String currentEntry = entry.getName();
755 File destFile = new File(MODEL + File.separator + currentEntry);
756 File destinationParent = destFile.getParentFile();
758 destinationParent.mkdirs();
760 if (!entry.isDirectory()){
761 BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
763 byte[] data = new byte[BUFFER];
764 try (FileOutputStream fos = new FileOutputStream(destFile);
765 BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) {
766 while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
767 dest.write(data, 0, currentByte);
770 } catch (IOException e) {
771 LOGGER.error("Failed to write zip contents to {}" + destFile + e);
773 // PLD should I throw e?
779 if (currentEntry.endsWith(".zip")){
780 extractFolder(destFile.getAbsolutePath());
783 } catch (IOException e) {
784 LOGGER.error("Failed to unzip model file " + zipFile, e);
788 private void retrieveDependency(String workingFile) {
790 MSModelUtils utils = new MSModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName());
791 Map<String, MSAttributeObject> tempMap;
793 tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
795 classMap.putAll(tempMap);
796 LOGGER.info(tempMap);
802 private List<File> listModelFiles(String directoryName) {
803 File fileDirectory = new File(directoryName);
804 List<File> resultList = new ArrayList<>();
805 File[] fList = fileDirectory.listFiles();
806 for (File file : fList) {
808 resultList.add(file);
809 } else if (file.isDirectory()) {
810 dirDependencyList.add(file.getName());
811 resultList.addAll(listModelFiles(file.getAbsolutePath()));
817 private List<String> createList() {
818 List<String> list = new ArrayList<>();
819 for (Entry<String, MSAttributeObject> cMap : classMap.entrySet()){
820 if (cMap.getValue().isPolicyTempalate()){
821 list.add(cMap.getKey());
827 if (classMap.containsKey(this.newModel.getModelName())){
828 list.add(this.newModel.getModelName());
836 public Map<String, String> getAttributesListRefMap() {
837 return attributesListRefMap;
840 public Map<String, LinkedList<String>> getArrayTextList() {
841 return arrayTextList;
844 private OptimizationObject setOptimizationObjectValues(PolicyRestAdapter policyAdapter) {
845 OptimizationObject optimizationObject = new OptimizationObject();
846 optimizationObject.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_OOF));
848 if(policyAdapter.getServiceType() !=null){
849 optimizationObject.setService(policyAdapter.getServiceType());
850 optimizationObject.setVersion(policyAdapter.getVersion());
852 if(policyAdapter.getPolicyName()!=null){
853 optimizationObject.setPolicyName(policyAdapter.getPolicyName());
855 if(policyAdapter.getPolicyDescription()!=null){
856 optimizationObject.setDescription(policyAdapter.getPolicyDescription());
858 if (policyAdapter.getPriority()!=null){
859 optimizationObject.setPriority(policyAdapter.getPriority());
861 optimizationObject.setPriority("9999");
863 if (policyAdapter.getRiskLevel()!=null){
864 optimizationObject.setRiskLevel(policyAdapter.getRiskLevel());
866 if (policyAdapter.getRiskType()!=null){
867 optimizationObject.setRiskType(policyAdapter.getRiskType());
869 if (policyAdapter.getGuard()!=null){
870 optimizationObject.setGuard(policyAdapter.getGuard());
872 return optimizationObject;
876 class OptimizationObject {
878 private String service;
879 private String policyName;
880 private String description;
881 private String templateVersion;
882 private String version;
883 private String priority;
884 private String policyScope;
885 private String riskType;
886 private String riskLevel;
887 private String guard = null;
889 public String getGuard() {
892 public void setGuard(String guard) {
895 public String getRiskType() {
898 public void setRiskType(String riskType) {
899 this.riskType = riskType;
901 public String getRiskLevel() {
904 public void setRiskLevel(String riskLevel) {
905 this.riskLevel = riskLevel;
907 public String getPriority() {
910 public void setPriority(String priority) {
911 this.priority = priority;
913 public String getPolicyScope() {
916 public void setPolicyScope(String policyScope) {
917 this.policyScope = policyScope;
919 public String getVersion() {
922 public void setVersion(String version) {
923 this.version = version;
925 private Object content;
927 public String getPolicyName() {
930 public void setPolicyName(String policyName) {
931 this.policyName = policyName;
933 public String getDescription() {
936 public void setDescription(String description) {
937 this.description = description;
939 public Object getContent() {
942 public void setContent(Object content) {
943 this.content = content;
945 public String getService() {
948 public void setService(String service) {
949 this.service = service;
951 public String getTemplateVersion() {
952 return templateVersion;
954 public void setTemplateVersion(String templateVersion) {
955 this.templateVersion = templateVersion;