2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.io.PrintWriter;
34 import java.nio.file.Files;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
37 import java.util.ArrayList;
38 import java.util.Arrays;
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.TreeMap;
50 import java.util.zip.ZipEntry;
51 import java.util.zip.ZipFile;
53 import javax.servlet.http.HttpServletRequest;
54 import javax.servlet.http.HttpServletResponse;
56 import org.apache.commons.compress.utils.IOUtils;
57 import org.apache.commons.fileupload.FileItem;
58 import org.apache.commons.fileupload.FileUploadException;
59 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
60 import org.apache.commons.fileupload.servlet.ServletFileUpload;
61 import org.apache.commons.io.FileUtils;
62 import org.apache.commons.lang.StringUtils;
63 import org.json.JSONArray;
64 import org.json.JSONObject;
65 import org.onap.policy.common.logging.flexlogger.FlexLogger;
66 import org.onap.policy.common.logging.flexlogger.Logger;
67 import org.onap.policy.rest.XACMLRestProperties;
68 import org.onap.policy.rest.adapter.PolicyRestAdapter;
69 import org.onap.policy.rest.dao.CommonClassDao;
70 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
71 import org.onap.policy.rest.jpa.MicroServiceModels;
72 import org.onap.policy.rest.jpa.PolicyEntity;
73 import org.onap.policy.rest.util.MSAttributeObject;
74 import org.onap.policy.rest.util.MSModelUtils;
75 import org.onap.policy.rest.util.MSModelUtils.MODEL_TYPE;
76 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
77 import org.openecomp.portalsdk.core.web.support.JsonMessage;
78 import org.springframework.beans.factory.annotation.Autowired;
79 import org.springframework.http.MediaType;
80 import org.springframework.stereotype.Controller;
81 import org.springframework.web.bind.annotation.RequestMapping;
82 import org.springframework.web.servlet.ModelAndView;
83 import org.yaml.snakeyaml.Yaml;
85 import com.att.research.xacml.util.XACMLProperties;
86 import com.fasterxml.jackson.core.JsonProcessingException;
87 import com.fasterxml.jackson.databind.DeserializationFeature;
88 import com.fasterxml.jackson.databind.JsonNode;
89 import com.fasterxml.jackson.databind.ObjectMapper;
90 import com.fasterxml.jackson.databind.ObjectWriter;
91 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
92 import com.fasterxml.jackson.databind.node.ObjectNode;
93 import com.google.gson.Gson;
95 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
96 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
97 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
98 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
99 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
100 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
101 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
105 public class CreateDcaeMicroServiceController extends RestrictedBaseController {
106 private static final Logger LOGGER = FlexLogger.getLogger(CreateDcaeMicroServiceController.class);
108 private static CommonClassDao commonClassDao;
110 public static CommonClassDao getCommonClassDao() {
111 return commonClassDao;
114 public static void setCommonClassDao(CommonClassDao commonClassDao) {
115 CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
118 private MicroServiceModels newModel;
119 private String newFile;
120 private String directory;
121 private List<String> modelList = new ArrayList<>();
122 private List<String> dirDependencyList = new ArrayList<>();
123 private HashMap<String,MSAttributeObject > classMap = new HashMap<>();
124 //Tosca Model related Datastructure.
125 String referenceAttributes;
126 String attributeString;
127 String listConstraints;
128 String subAttributeString;
129 HashMap<String, Object> retmap = new HashMap<>();
130 Set<String> uniqueKeys= new HashSet<>();
131 Set<String> uniqueDataKeys= new HashSet<>();
132 StringBuilder dataListBuffer=new StringBuilder();
133 List<String> dataConstraints= new ArrayList <>();
135 public static final String DATATYPE = "data_types.policy.data.";
136 public static final String PROPERTIES=".properties.";
137 public static final String TYPE=".type";
138 public static final String STRING="string";
139 public static final String INTEGER="integer";
140 public static final String LIST="list";
141 public static final String DEFAULT=".default";
142 public static final String REQUIRED=".required";
143 public static final String MANYFALSE=":MANY-false";
147 private CreateDcaeMicroServiceController(CommonClassDao commonClassDao){
148 CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
151 public CreateDcaeMicroServiceController(){}
153 protected PolicyRestAdapter policyAdapter = null;
154 private int priorityCount;
155 private Map<String, String> attributesListRefMap = new HashMap<>();
156 private Map<String, LinkedList<String>> arrayTextList = new HashMap<>();
158 public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
160 String jsonContent = null;
162 jsonContent = decodeContent(root.get("policyJSON")).toString();
163 constructJson(policyData, jsonContent);
165 LOGGER.error("Error while decoding microservice content", e);
171 private GroupPolicyScopeList getPolicyObject(String policyScope) {
172 GroupPolicyScopeList groupList= (GroupPolicyScopeList) commonClassDao.getEntityItem(GroupPolicyScopeList.class, "name", policyScope);
176 private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent) {
177 ObjectWriter om = new ObjectMapper().writer();
179 DCAEMicroServiceObject microServiceObject = new DCAEMicroServiceObject();
180 MicroServiceModels returnModel = new MicroServiceModels();
181 microServiceObject.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_MS));
182 if(policyAdapter.getServiceType() !=null){
183 microServiceObject.setService(policyAdapter.getServiceType());
184 microServiceObject.setVersion(policyAdapter.getVersion());
185 returnModel = getAttributeObject(microServiceObject.getService(), microServiceObject.getVersion());
187 if (returnModel.getAnnotation()==null || returnModel.getAnnotation().isEmpty()){
188 if(policyAdapter.getUuid()!=null){
189 microServiceObject.setUuid(policyAdapter.getUuid());
191 if(policyAdapter.getLocation()!=null){
192 microServiceObject.setLocation(policyAdapter.getLocation());
194 if(policyAdapter.getConfigName()!=null){
195 microServiceObject.setConfigName(policyAdapter.getConfigName());
197 GroupPolicyScopeList policyScopeValue = getPolicyObject(policyAdapter.getPolicyScope());
198 if(policyScopeValue!=null){
199 microServiceObject.setPolicyScope(policyScopeValue.getGroupList());
203 if(policyAdapter.getPolicyName()!=null){
204 microServiceObject.setPolicyName(policyAdapter.getPolicyName());
206 if(policyAdapter.getPolicyDescription()!=null){
207 microServiceObject.setDescription(policyAdapter.getPolicyDescription());
209 if (policyAdapter.getPriority()!=null){
210 microServiceObject.setPriority(policyAdapter.getPriority());
212 microServiceObject.setPriority("9999");
215 if (policyAdapter.getRiskLevel()!=null){
216 microServiceObject.setRiskLevel(policyAdapter.getRiskLevel());
218 if (policyAdapter.getRiskType()!=null){
219 microServiceObject.setRiskType(policyAdapter.getRiskType());
221 if (policyAdapter.getGuard()!=null){
222 microServiceObject.setGuard(policyAdapter.getGuard());
224 microServiceObject.setContent(jsonContent);
227 json = om.writeValueAsString(microServiceObject);
228 } catch (JsonProcessingException e) {
229 LOGGER.error("Error writing out the object", e);
232 String cleanJson = cleanUPJson(json);
233 cleanJson = removeNullAttributes(cleanJson);
234 policyAdapter.setJsonBody(cleanJson);
235 return policyAdapter;
238 private String removeNullAttributes(String cleanJson) {
239 ObjectMapper mapper = new ObjectMapper();
242 JsonNode rootNode = mapper.readTree(cleanJson);
243 JsonNode returnNode = mapper.readTree(cleanJson);
244 Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
245 boolean remove = false;
246 while (fieldsIterator.hasNext()) {
247 Map.Entry<String, JsonNode> field = fieldsIterator.next();
248 final String key = field.getKey();
249 final JsonNode value = field.getValue();
250 if (value==null || value.isNull()){
251 ((ObjectNode) returnNode).remove(key);
256 cleanJson = returnNode.toString();
258 } catch (IOException e) {
259 LOGGER.error("Error writing out the JsonNode",e);
264 // Second index of dot should be returned.
265 public int stringBetweenDots(String str){
266 String stringToSearch=str;
267 String[]ss=stringToSearch.split("\\.");
271 uniqueKeys.add(ss[2]);
275 return uniqueKeys.size();
278 public void stringBetweenDotsForDataFields(String str){
279 String stringToSearch=str;
280 String[]ss=stringToSearch.split("\\.");
285 uniqueDataKeys.add(ss[0]+"%"+ss[2]);
291 public Map<String, String> load(String fileName) throws IOException {
292 File newConfiguration = new File(fileName);
293 InputStream is = null;
295 is = new FileInputStream(newConfiguration);
296 } catch (FileNotFoundException e) {
300 Yaml yaml = new Yaml();
301 @SuppressWarnings("unchecked")
302 Map<Object, Object> yamlMap = (Map<Object, Object>) yaml.load(is);
303 StringBuilder sb = new StringBuilder();
304 Map<String, String> settings = new HashMap<>();
305 if (yamlMap == null) {
308 List<String> path = new ArrayList <>();
309 serializeMap(settings, sb, path, yamlMap);
313 public Map<String, String> load(byte[] source) throws IOException {
314 Yaml yaml = new Yaml();
315 @SuppressWarnings("unchecked")
316 Map<Object, Object> yamlMap = (Map<Object, Object>) yaml.load(Arrays.toString(source));
317 StringBuilder sb = new StringBuilder();
318 Map<String, String> settings = new HashMap <>();
319 if (yamlMap == null) {
322 List<String> path = new ArrayList <>();
323 serializeMap(settings, sb, path, yamlMap);
327 @SuppressWarnings({ "unchecked", "rawtypes" })
328 private void serializeMap(Map<String, String> settings, StringBuilder sb, List<String> path, Map<Object, Object> yamlMap) {
329 for (Map.Entry<Object, Object> entry : yamlMap.entrySet()) {
330 if (entry.getValue() instanceof Map) {
331 path.add((String) entry.getKey());
332 serializeMap(settings, sb, path, (Map<Object, Object>) entry.getValue());
333 path.remove(path.size() - 1);
334 } else if (entry.getValue() instanceof List) {
335 path.add((String) entry.getKey());
336 serializeList(settings, sb, path, (List) entry.getValue());
337 path.remove(path.size() - 1);
339 serializeValue(settings, sb, path, (String) entry.getKey(), entry.getValue());
344 @SuppressWarnings("unchecked")
345 private void serializeList(Map<String, String> settings, StringBuilder sb, List<String> path, List<String> yamlList) {
347 for (Object listEle : yamlList) {
348 if (listEle instanceof Map) {
349 path.add(Integer.toString(counter));
350 serializeMap(settings, sb, path, (Map<Object, Object>) listEle);
351 path.remove(path.size() - 1);
352 } else if (listEle instanceof List) {
353 path.add(Integer.toString(counter));
354 serializeList(settings, sb, path, (List<String>) listEle);
355 path.remove(path.size() - 1);
357 serializeValue(settings, sb, path, Integer.toString(counter), listEle);
363 private void serializeValue(Map<String, String> settings, StringBuilder sb, List<String> path, String name, Object value) {
368 for (String pathEle : path) {
369 sb.append(pathEle).append('.');
372 settings.put(sb.toString(), value.toString());
375 void parseDataAndPolicyNodes(Map<String,String> map){
376 for(String key:map.keySet()){
377 if(key.contains("policy.nodes.Root"))
381 else if(key.contains("policy.nodes")){
382 String wordToFind = "policy.nodes.";
383 int indexForPolicyNode=key.indexOf(wordToFind);
384 String subNodeString= key.substring(indexForPolicyNode+13, key.length());
386 stringBetweenDots(subNodeString);
388 else if(key.contains("policy.data")){
389 String wordToFind="policy.data.";
390 int indexForPolicyNode=key.indexOf(wordToFind);
391 String subNodeString= key.substring(indexForPolicyNode+12, key.length());
393 stringBetweenDotsForDataFields(subNodeString);
398 HashMap<String,String> parseDataNodes(Map<String,String> map){
399 HashMap<String,String> dataMapForJson=new HashMap <>();
400 for(String uniqueDataKey: uniqueDataKeys){
401 if(uniqueDataKey.contains("%")){
402 String[] uniqueDataKeySplit= uniqueDataKey.split("%");
403 String findType=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+TYPE;
404 String typeValue=map.get(findType);
405 LOGGER.info(typeValue);
406 if(typeValue.equalsIgnoreCase(STRING)||
407 typeValue.equalsIgnoreCase(INTEGER)
410 String findDefault=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+DEFAULT;
411 String defaultValue= map.get(findDefault);
412 LOGGER.info("defaultValue is:"+ defaultValue);
414 String findRequired=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+REQUIRED;
415 String requiredValue= map.get(findRequired);
416 LOGGER.info("requiredValue is:"+ requiredValue);
418 StringBuilder attributeIndividualStringBuilder= new StringBuilder();
419 attributeIndividualStringBuilder.append(typeValue+":defaultValue-");
420 attributeIndividualStringBuilder.append(defaultValue+":required-");
421 attributeIndividualStringBuilder.append(requiredValue+MANYFALSE);
422 dataMapForJson.put(uniqueDataKey, attributeIndividualStringBuilder.toString());
424 else if(typeValue.equalsIgnoreCase(LIST)){
425 String findList= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.type";
426 String listValue=map.get(findList);
428 LOGGER.info("Type of list is:"+ listValue);
430 if(listValue.contains(".")){
431 String trimValue=listValue.substring(listValue.lastIndexOf('.')+1);
432 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
433 referenceIndividualStringBuilder.append(trimValue+":MANY-true");
434 dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
437 StringBuilder stringListItems= new StringBuilder();
438 stringListItems.append(uniqueDataKeySplit[1].toUpperCase()+":MANY-false");
439 dataMapForJson.put(uniqueDataKey, stringListItems.toString());
440 dataListBuffer.append(uniqueDataKeySplit[1].toUpperCase()+"=[");
441 for(int i=0;i<10;i++){
442 String findConstraints= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.constraints.0.valid_values."+i;
443 String constraintsValue=map.get(findConstraints);
444 LOGGER.info(constraintsValue);
445 if(constraintsValue==null){
449 dataConstraints.add(constraintsValue);
450 dataListBuffer.append(constraintsValue+",");
453 dataListBuffer.append("]#");
455 LOGGER.info(dataListBuffer);
460 String findUserDefined="data_types.policy.data."+uniqueDataKeySplit[0]+"."+"properties"+"."+uniqueDataKeySplit[1]+".type";
461 String userDefinedValue=map.get(findUserDefined);
462 String trimValue=userDefinedValue.substring(userDefinedValue.lastIndexOf('.')+1);
463 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
464 referenceIndividualStringBuilder.append(trimValue+":MANY-false");
465 dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
470 return dataMapForJson;
473 void constructJsonForDataFields(HashMap<String,String> dataMapForJson){
474 HashMap<String,HashMap<String,String>> dataMapKey= new HashMap <>();
475 HashMap<String,String> hmSub;
476 for(Map.Entry<String, String> entry: dataMapForJson.entrySet()){
477 String uniqueDataKey= entry.getKey();
478 String[] uniqueDataKeySplit=uniqueDataKey.split("%");
479 String value= dataMapForJson.get(uniqueDataKey);
480 if(dataMapKey.containsKey(uniqueDataKeySplit[0])){
481 hmSub = dataMapKey.get(uniqueDataKeySplit[0]);
482 hmSub.put(uniqueDataKeySplit[1], value);
485 hmSub=new HashMap <>();
486 hmSub.put(uniqueDataKeySplit[1], value);
489 dataMapKey.put(uniqueDataKeySplit[0], hmSub);
492 JSONObject mainObject= new JSONObject();
494 for(Map.Entry<String,HashMap<String,String>> entry: dataMapKey.entrySet()){
495 String s=entry.getKey();
496 json= new JSONObject();
497 HashMap<String,String> jsonHm=dataMapKey.get(s);
498 for(Map.Entry<String,String> entryMap:jsonHm.entrySet()){
499 String key=entryMap.getKey();
500 json.put(key, jsonHm.get(key));
502 mainObject.put(s,json);
504 Iterator<String> keysItr = mainObject.keys();
505 while(keysItr.hasNext()) {
506 String key = keysItr.next();
507 String value = mainObject.get(key).toString();
508 retmap.put(key, value);
511 LOGGER.info("#############################################################################");
512 LOGGER.info(mainObject);
513 LOGGER.info("###############################################################################");
517 HashMap<String,HashMap<String,String>> parsePolicyNodes(Map<String,String> map){
518 HashMap<String,HashMap<String,String>> mapKey= new HashMap <>();
519 for(String uniqueKey: uniqueKeys){
520 HashMap<String,String> hm;
522 for(Map.Entry<String,String> entry:map.entrySet()){
523 String key=entry.getKey();
524 if(key.contains(uniqueKey) && key.contains("policy.nodes")){
525 if(mapKey.containsKey(uniqueKey)){
526 hm = mapKey.get(uniqueKey);
527 String keyStr= key.substring(key.lastIndexOf('.')+1);
528 String valueStr= map.get(key);
529 if(("type").equals(keyStr)){
530 if(!key.contains("entry_schema"))
532 hm.put(keyStr,valueStr);
535 hm.put(keyStr,valueStr);
539 hm = new HashMap <>();
540 String keyStr= key.substring(key.lastIndexOf('.')+1);
541 String valueStr= map.get(key);
542 if(("type").equals(keyStr)){
543 if(!key.contains("entry_schema"))
545 hm.put(keyStr,valueStr);
548 hm.put(keyStr,valueStr);
550 mapKey.put(uniqueKey, hm);
558 void createAttributes(HashMap<String,HashMap<String,String>> mapKey){
559 StringBuilder attributeStringBuilder= new StringBuilder();
560 StringBuilder referenceStringBuilder= new StringBuilder();
561 StringBuilder listBuffer= new StringBuilder();
562 List<String> constraints= new ArrayList<>();
563 for(Map.Entry<String,HashMap<String,String>> entry: mapKey.entrySet()){
564 String keySetString= entry.getKey();
565 HashMap<String,String> keyValues=mapKey.get(keySetString);
566 if(keyValues.get("type").equalsIgnoreCase(STRING)||
567 keyValues.get("type").equalsIgnoreCase(INTEGER)
569 StringBuilder attributeIndividualStringBuilder= new StringBuilder();
570 attributeIndividualStringBuilder.append(keySetString+"=");
571 attributeIndividualStringBuilder.append(keyValues.get("type")+":defaultValue-");
572 attributeIndividualStringBuilder.append(keyValues.get("default")+":required-");
573 attributeIndividualStringBuilder.append(keyValues.get("required")+":MANY-false");
574 attributeStringBuilder.append(attributeIndividualStringBuilder+",");
577 else if(keyValues.get("type").equalsIgnoreCase(LIST)){
579 Set<String> keys= keyValues.keySet();
580 Iterator<String> itr=keys.iterator();
581 while(itr.hasNext()){
582 String key= itr.next();
583 if((!("type").equals(key) ||("required").equals(key)))
585 String value= keyValues.get(key);
586 //The "." in the value determines if its a string or a user defined type.
587 if (!value.contains(".")){
589 constraints.add(keyValues.get(key));
591 //This is userdefined string
592 String trimValue=value.substring(value.lastIndexOf('.')+1);
593 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
594 referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-true");
595 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
601 //User defined Datatype.
602 String value=keyValues.get("type");
603 String trimValue=value.substring(value.lastIndexOf('.')+1);
604 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
605 referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-false");
606 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
609 if(constraints!=null &&constraints.isEmpty()==false){
611 listBuffer.append(keySetString.toUpperCase()+"=[");
612 for(String str:constraints){
613 listBuffer.append(str+",");
615 listBuffer.append("]#");
616 LOGGER.info(listBuffer);
619 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
620 referenceIndividualStringBuilder.append(keySetString+"="+keySetString.toUpperCase()+":MANY-false");
621 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
626 dataListBuffer.append(listBuffer);
629 LOGGER.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
630 LOGGER.info("Whole attribute String is:"+attributeStringBuilder);
631 LOGGER.info("Whole reference String is:"+referenceStringBuilder);
632 LOGGER.info("List String is:"+listBuffer);
633 LOGGER.info("Data list buffer is:"+dataListBuffer);
634 LOGGER.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
636 this.listConstraints=dataListBuffer.toString();
637 this.referenceAttributes=referenceStringBuilder.toString();
638 this.attributeString=attributeStringBuilder.toString();
643 public void parseTosca (String fileName){
644 Map<String,String> map= new HashMap<>();
649 parseDataAndPolicyNodes(map);
651 HashMap<String,String> dataMapForJson=parseDataNodes(map);
653 constructJsonForDataFields(dataMapForJson);
655 HashMap<String,HashMap<String,String>> mapKey= parsePolicyNodes(map);
657 createAttributes(mapKey);
659 } catch (IOException e) {
665 private String cleanUPJson(String json) {
666 String cleanJson = StringUtils.replaceEach(json, new String[]{"\\\\", "\\\\\\", "\\\\\\\\"}, new String[]{"\\", "\\", "\\"});
667 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\"}, new String[]{"\\"});
668 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\", "[[", "]]"}, new String[]{"\\", "[", "]"});
670 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\"", "\\\"", "\"[{", "}]\""}, new String[]{"\"", "\"", "[{", "}]"});
671 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[{", "}]\""}, new String[]{"[{", "}]"});
672 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[", "]\""}, new String[]{"[", "]"});
673 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"{", "}\""}, new String[]{"{", "}"});
674 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\"\"", "\"\""}, new String[]{"\"", "\""});
675 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\""}, new String[]{""});
676 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\""}, new String[]{"\""});
677 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\\\\\\"}, new String[]{"\""});
678 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\\""}, new String[]{"\""});
679 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[", "]\""}, new String[]{"[", "]"});
683 private JSONObject decodeContent(JsonNode jsonNode){
684 Iterator<JsonNode> jsonElements = jsonNode.elements();
685 Iterator<String> jsonKeys = jsonNode.fieldNames();
686 Map<String,String> element = new TreeMap<>();
687 while(jsonElements.hasNext() && jsonKeys.hasNext()){
688 element.put(jsonKeys.next(), jsonElements.next().toString());
690 JSONObject jsonResult = new JSONObject();
691 JSONArray jsonArray = null;
692 String oldValue = null;
693 String nodeKey = null;
694 String arryKey = null;
695 Boolean isArray = false;
696 JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
697 ObjectNode node = nodeFactory.objectNode();
698 String prevKey = null;
699 String presKey = null;
700 for(String key: element.keySet()){
701 if(key.contains(".")){
702 presKey = key.substring(0,key.indexOf("."));
703 }else if(key.contains("@")){
704 presKey = key.substring(0,key.indexOf("@"));
708 // first check if we are different from old.
709 LOGGER.info(key+"\n");
710 if(jsonArray!=null && jsonArray.length()>0 && key.contains("@") && !key.contains(".") && oldValue!=null){
711 if(!oldValue.equals(key.substring(0,key.indexOf("@")))){
712 jsonResult.put(oldValue, jsonArray);
713 jsonArray = new JSONArray();
715 }else if(jsonArray!=null && jsonArray.length()>0 && !presKey.equals(prevKey) && oldValue!=null){
716 jsonResult.put(oldValue, jsonArray);
718 jsonArray = new JSONArray();
723 if(key.contains(".")){
725 nodeKey = key.substring(0,key.indexOf("."));
727 if(nodeKey.equals(key.substring(0,key.indexOf(".")))){
728 node.put(key.substring(key.indexOf(".")+1), element.get(key));
731 if(nodeKey.contains("@")){
733 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
735 if(nodeKey.endsWith("@0")){
737 jsonArray = new JSONArray();
739 if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
740 jsonArray.put(decodeContent(node));
742 if((key.contains("@") && !arryKey.equals(key.substring(0,nodeKey.indexOf("@")))) || !key.contains("@")){
743 jsonResult.put(arryKey, jsonArray);
744 jsonArray = new JSONArray();
746 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
749 jsonResult.put(nodeKey, decodeContent(node));
751 node = nodeFactory.objectNode();
753 nodeKey = key.substring(0,key.indexOf("."));
754 if(nodeKey.contains("@")){
755 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
757 node.put(key.substring(key.indexOf(".")+1), element.get(key));
759 }else if(node.size()!=0){
760 if(nodeKey.contains("@")){
762 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
764 if(nodeKey.endsWith("@0")){
766 jsonArray = new JSONArray();
768 if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
769 jsonArray.put(decodeContent(node));
771 jsonResult.put(arryKey, jsonArray);
772 jsonArray = new JSONArray();
773 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
776 jsonResult.put(nodeKey, decodeContent(node));
778 node = nodeFactory.objectNode();
779 if(key.contains("@")){
781 if(key.endsWith("@0")|| jsonArray==null){
782 jsonArray = new JSONArray();
784 }else if(!key.contains("@")){
789 oldValue = key.substring(0,key.indexOf("@"));
791 if(oldValue!=prevKey){
792 oldValue = key.substring(0,key.indexOf("@"));
794 if(oldValue.equals(key.substring(0,key.indexOf("@")))){
795 jsonArray.put(element.get(key));
797 jsonResult.put(oldValue, jsonArray);
798 jsonArray = new JSONArray();
800 oldValue = key.substring(0,key.indexOf("@"));
802 jsonResult.put(key, element.get(key));
805 if(key.contains("@")){
807 if(key.endsWith("@0")|| jsonArray==null){
808 jsonArray = new JSONArray();
810 }else if(!key.contains("@")){
815 oldValue = key.substring(0,key.indexOf("@"));
817 if(oldValue!=prevKey){
818 oldValue = key.substring(0,key.indexOf("@"));
820 if(oldValue.equals(key.substring(0,key.indexOf("@")))){
821 jsonArray.put(element.get(key));
823 jsonResult.put(oldValue, jsonArray);
824 jsonArray = new JSONArray();
826 oldValue = key.substring(0,key.indexOf("@"));
828 jsonResult.put(key, element.get(key));
833 if(nodeKey.contains("@")){
835 jsonArray = new JSONArray();
838 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
840 jsonArray.put(decodeContent(node));
841 jsonResult.put(arryKey, jsonArray);
844 jsonResult.put(nodeKey, decodeContent(node));
847 if(isArray && jsonArray.length() > 0){
848 jsonResult.put(oldValue, jsonArray);
853 @RequestMapping(value={"/policyController/getDCAEMSTemplateData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
854 public ModelAndView getDCAEMSTemplateData(HttpServletRequest request, HttpServletResponse response) throws IOException{
855 ObjectMapper mapper = new ObjectMapper();
856 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
857 JsonNode root = mapper.readTree(request.getReader());
859 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
860 String servicename = value.toString().split("-v")[0];
861 String version = null;
862 if (value.toString().contains("-v")){
863 version = value.toString().split("-v")[1];
865 MicroServiceModels returnModel = getAttributeObject(servicename, version);
867 String jsonModel = createMicroSeriveJson(returnModel);
869 response.setCharacterEncoding("UTF-8");
870 response.setContentType("application / json");
871 request.setCharacterEncoding("UTF-8");
872 List<Object> list = new ArrayList<>();
873 PrintWriter out = response.getWriter();
874 String responseString = mapper.writeValueAsString(returnModel);
875 JSONObject j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + "}");
877 out.write(list.toString());
881 @SuppressWarnings({ "unchecked", "rawtypes" })
882 private String createMicroSeriveJson(MicroServiceModels returnModel) {
883 Map<String, String> attributeMap = new HashMap<>();
884 Map<String, String> refAttributeMap = new HashMap<>();
885 String attribute = returnModel.getAttributes();
886 if(attribute != null){
887 attribute = attribute.trim();
889 String refAttribute = returnModel.getRef_attributes();
890 if(refAttribute != null){
891 refAttribute = refAttribute.trim();
893 String enumAttribute = returnModel.getEnumValues();
894 if(enumAttribute != null){
895 enumAttribute = enumAttribute.trim();
897 if (!StringUtils.isEmpty(attribute)){
898 attributeMap = convert(attribute, ",");
900 if (!StringUtils.isEmpty(refAttribute)){
901 refAttributeMap = convert(refAttribute, ",");
904 Gson gson = new Gson();
906 String subAttributes = returnModel.getSub_attributes();
907 if(subAttributes != null){
908 subAttributes = subAttributes.trim();
912 Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
914 JSONObject object = new JSONObject();
915 JSONArray array = new JSONArray();
917 for (Entry<String, String> keySet : attributeMap.entrySet()){
918 array = new JSONArray();
919 String value = keySet.getValue();
920 if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
922 object.put(keySet.getKey().trim(), array);
924 object.put(keySet.getKey().trim(), value.trim());
928 for (Entry<String, String> keySet : refAttributeMap.entrySet()){
929 array = new JSONArray();
930 String value = keySet.getValue().split(":")[0];
931 if (gsonObject.containsKey(value)){
932 if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
933 array.put(recursiveReference(value, gsonObject, enumAttribute));
934 object.put(keySet.getKey().trim(), array);
936 object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
939 if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
940 array.put(value.trim());
941 object.put(keySet.getKey().trim(), array);
943 object.put(keySet.getKey().trim(), value.trim());
948 return object.toString();
951 @SuppressWarnings("unchecked")
952 private JSONObject recursiveReference(String name, Map<String,String> subAttributeMap, String enumAttribute) {
953 JSONObject object = new JSONObject();
954 Map<String, String> map = new HashMap<>();
955 Object returnClass = subAttributeMap.get(name);
956 map = (Map<String, String>) returnClass;
957 JSONArray array = new JSONArray();
959 for( Entry<String, String> m:map.entrySet()){
960 String[] splitValue = m.getValue().split(":");
961 array = new JSONArray();
962 if (subAttributeMap.containsKey(splitValue[0])){
963 if (m.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
964 array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
965 object.put(m.getKey().trim(), array);
967 object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
970 if (m.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
971 array.put(splitValue[0].trim());
972 object.put(m.getKey().trim(), array);
974 object.put(m.getKey().trim(), splitValue[0].trim());
983 @RequestMapping(value={"/policyController/getModelServiceVersioneData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
984 public ModelAndView getModelServiceVersionData(HttpServletRequest request, HttpServletResponse response) throws IOException{
985 ObjectMapper mapper = new ObjectMapper();
986 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
987 JsonNode root = mapper.readTree(request.getReader());
989 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
990 String servicename = value.toString().split("-v")[0];
991 Set<String> returnList = getVersionList(servicename);
993 response.setCharacterEncoding("UTF-8");
994 response.setContentType("application / json");
995 request.setCharacterEncoding("UTF-8");
996 List<Object> list = new ArrayList<>();
997 PrintWriter out = response.getWriter();
998 String responseString = mapper.writeValueAsString(returnList);
999 JSONObject j = new JSONObject("{dcaeModelVersionData: " + responseString +"}");
1001 out.write(list.toString());
1005 private Set<String> getVersionList(String name) {
1006 MicroServiceModels workingModel = new MicroServiceModels();
1007 Set<String> list = new HashSet<>();
1008 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
1009 for (int i = 0; i < microServiceModelsData.size(); i++) {
1010 workingModel = (MicroServiceModels) microServiceModelsData.get(i);
1011 if (workingModel.getVersion()!=null){
1012 list.add(workingModel.getVersion());
1014 list.add("Default");
1020 private MicroServiceModels getAttributeObject(String name, String version) {
1021 MicroServiceModels workingModel = new MicroServiceModels();
1022 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
1023 for (int i = 0; i < microServiceModelsData.size(); i++) {
1024 workingModel = (MicroServiceModels) microServiceModelsData.get(i);
1025 if(version != null){
1026 if (workingModel.getVersion()!=null){
1027 if (workingModel.getVersion().equals(version)){
1028 return workingModel;
1031 return workingModel;
1034 return workingModel;
1038 return workingModel;
1041 @RequestMapping(value={"/get_DCAEPriorityValues"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
1042 public void getDCAEPriorityValuesData(HttpServletRequest request, HttpServletResponse response){
1044 Map<String, Object> model = new HashMap<>();
1045 ObjectMapper mapper = new ObjectMapper();
1046 List<String> priorityList = new ArrayList<>();
1048 for (int i = 1; i < priorityCount; i++) {
1049 priorityList.add(String.valueOf(i));
1051 model.put("priorityDatas", mapper.writeValueAsString(priorityList));
1052 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
1053 JSONObject j = new JSONObject(msg);
1054 response.getWriter().write(j.toString());
1056 catch (Exception e){
1061 public void prePopulateDCAEMSPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1062 if (policyAdapter.getPolicyData() instanceof PolicyType) {
1063 Object policyData = policyAdapter.getPolicyData();
1064 PolicyType policy = (PolicyType) policyData;
1065 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
1066 String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("MS_") +3);
1067 policyAdapter.setPolicyName(policyNameValue);
1068 String description = "";
1070 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
1071 }catch(Exception e){
1072 LOGGER.error("Error while collecting the desciption tag in ActionPolicy " + policyNameValue ,e);
1073 description = policy.getDescription();
1075 policyAdapter.setPolicyDescription(description);
1076 // Get the target data under policy.
1077 TargetType target = policy.getTarget();
1078 if (target != null) {
1079 // Under target we have AnyOFType
1080 List<AnyOfType> anyOfList = target.getAnyOf();
1081 if (anyOfList != null) {
1082 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
1083 while (iterAnyOf.hasNext()) {
1084 AnyOfType anyOf = iterAnyOf.next();
1085 // Under AnyOFType we have AllOFType
1086 List<AllOfType> allOfList = anyOf.getAllOf();
1087 if (allOfList != null) {
1088 Iterator<AllOfType> iterAllOf = allOfList.iterator();
1089 while (iterAllOf.hasNext()) {
1090 AllOfType allOf = iterAllOf.next();
1091 // Under AllOFType we have Match
1092 List<MatchType> matchList = allOf.getMatch();
1093 if (matchList != null) {
1094 Iterator<MatchType> iterMatch = matchList.iterator();
1095 while (matchList.size()>1 && iterMatch.hasNext()) {
1096 MatchType match = iterMatch.next();
1098 // Under the match we have attribute value and
1099 // attributeDesignator. So,finally down to the actual attribute.
1101 AttributeValueType attributeValue = match.getAttributeValue();
1102 String value = (String) attributeValue.getContent().get(0);
1103 AttributeDesignatorType designator = match.getAttributeDesignator();
1104 String attributeId = designator.getAttributeId();
1105 // First match in the target is OnapName, so set that value.
1106 if (attributeId.equals("ONAPName")) {
1107 policyAdapter.setOnapName(value);
1109 if (attributeId.equals("ConfigName")){
1110 policyAdapter.setConfigName(value);
1112 if (attributeId.equals("uuid")){
1113 policyAdapter.setUuid(value);
1115 if (attributeId.equals("location")){
1116 policyAdapter.setLocation(value);
1118 if (attributeId.equals("RiskType")){
1119 policyAdapter.setRiskType(value);
1121 if (attributeId.equals("RiskLevel")){
1122 policyAdapter.setRiskLevel(value);
1124 if (attributeId.equals("guard")){
1125 policyAdapter.setGuard(value);
1127 if (attributeId.equals("TTLDate") && !value.contains("NA")){
1128 String newDate = convertDate(value, true);
1129 policyAdapter.setTtlDate(newDate);
1132 readFile(policyAdapter, entity);
1142 private String convertDate(String dateTTL, boolean portalType) {
1143 String formateDate = null;
1144 String[] date = dateTTL.split("T");
1145 String[] parts = date[0].split("-");
1147 formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];
1151 public static Map<String, String> convert(String str, String split) {
1152 Map<String, String> map = new HashMap<>();
1153 for(final String entry : str.split(split)) {
1154 String[] parts = entry.split("=");
1155 map.put(parts[0], parts[1]);
1161 @SuppressWarnings("unchecked")
1162 private void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1163 String policyScopeName = null;
1164 ObjectMapper mapper = new ObjectMapper();
1166 DCAEMicroServiceObject msBody = (DCAEMicroServiceObject) mapper.readValue(entity.getConfigurationData().getConfigBody(), DCAEMicroServiceObject.class);
1167 policyScopeName = getPolicyScope(msBody.getPolicyScope());
1168 policyAdapter.setPolicyScope(policyScopeName);
1170 policyAdapter.setPriority(msBody.getPriority());
1172 if (msBody.getVersion()!= null){
1173 policyAdapter.setServiceType(msBody.getService());
1174 policyAdapter.setVersion(msBody.getVersion());
1176 policyAdapter.setServiceType(msBody.getService());
1178 if(msBody.getContent() != null){
1179 LinkedHashMap<String, Object> data = new LinkedHashMap<>();
1180 LinkedHashMap<String, ?> map = (LinkedHashMap<String, ?>) msBody.getContent();
1181 readRecursivlyJSONContent(map, data);
1182 policyAdapter.setRuleData(data);
1185 } catch (Exception e) {
1191 @SuppressWarnings({ "rawtypes", "unchecked" })
1192 private void readRecursivlyJSONContent(LinkedHashMap<String, ?> map, LinkedHashMap<String, Object> data){
1193 for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
1194 Object key = iterator.next();
1195 Object value = map.get(key);
1196 if(value instanceof LinkedHashMap<?, ?>){
1197 LinkedHashMap<String, Object> secondObjec = new LinkedHashMap<>();
1198 readRecursivlyJSONContent((LinkedHashMap<String, ?>) value, secondObjec);
1199 for(String objKey: secondObjec.keySet()){
1200 data.put(key+"." +objKey, secondObjec.get(objKey));
1202 }else if(value instanceof ArrayList){
1203 ArrayList<?> jsonArrayVal = (ArrayList<?>)value;
1204 for(int i = 0; i < jsonArrayVal.size(); i++){
1205 Object arrayvalue = jsonArrayVal.get(i);
1206 if(arrayvalue instanceof LinkedHashMap<?, ?>){
1207 LinkedHashMap<String, Object> newData = new LinkedHashMap<>();
1208 readRecursivlyJSONContent((LinkedHashMap<String, ?>) arrayvalue, newData);
1209 for(String objKey: newData.keySet()){
1210 data.put(key+"@"+i+"." +objKey, newData.get(objKey));
1212 }else if(arrayvalue instanceof ArrayList){
1213 ArrayList<?> jsonArrayVal1 = (ArrayList<?>)value;
1214 for(int j = 0; j < jsonArrayVal1.size(); j++){
1215 Object arrayvalue1 = jsonArrayVal1.get(i);
1216 data.put(key+"@"+j, arrayvalue1.toString());
1219 data.put(key+"@"+i, arrayvalue.toString());
1223 data.put(key.toString(), value.toString());
1228 private String getPolicyScope(String value) {
1229 List<Object> groupList= commonClassDao.getDataById(GroupPolicyScopeList.class, "groupList", value);
1230 if(groupList != null && !groupList.isEmpty()){
1231 GroupPolicyScopeList pScope = (GroupPolicyScopeList) groupList.get(0);
1232 return pScope.getGroupName();
1237 //Convert the map values and set into JSON body
1238 public Map<String, String> convertMap(Map<String, String> attributesMap, Map<String, String> attributesRefMap) {
1239 Map<String, String> attribute = new HashMap<>();
1243 for (Entry<String, String> entry : attributesMap.entrySet()) {
1244 key = entry.getKey();
1245 value = entry.getValue();
1246 attribute.put(key, value);
1248 for (Entry<String, String> entryRef : attributesRefMap.entrySet()) {
1249 key = entryRef.getKey();
1250 value = entryRef.getValue().toString();
1251 attribute.put(key, value);
1253 for (Entry<String, String> entryList : attributesListRefMap.entrySet()) {
1254 key = entryList.getKey();
1255 value = entryList.getValue().toString();
1256 attribute.put(key, value);
1258 for (Entry<String, LinkedList<String>> arrayList : arrayTextList.entrySet()){
1259 key = arrayList.getKey();
1261 for (Object textList : arrayList.getValue()){
1263 temp = "[" + textList;
1265 temp = temp + "," + textList;
1268 attribute.put(key, temp+ "]");
1274 @RequestMapping(value={"/ms_dictionary/set_MSModelData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
1275 public void SetMSModelData(HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException{
1276 List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
1277 boolean zip = false;
1279 for (FileItem item : items) {
1280 if(item.getName().endsWith(".zip") || item.getName().endsWith(".xmi")||item.getName().endsWith(".yml")){
1281 this.newModel = new MicroServiceModels();
1283 File file = new File(item.getName());
1284 OutputStream outputStream = new FileOutputStream(file);
1285 IOUtils.copy(item.getInputStream(), outputStream);
1286 outputStream.close();
1287 this.newFile = file.toString();
1288 this.newModel.setModelName(this.newFile.toString().split("-v")[0]);
1290 if (this.newFile.toString().contains("-v")){
1291 if (item.getName().endsWith(".zip")){
1292 this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".zip", ""));
1294 }else if(item.getName().endsWith(".yml")){
1295 this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".yml", ""));
1299 this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".xmi", ""));
1303 }catch(Exception e){
1304 LOGGER.error("Upload error : " + e);
1309 List<File> fileList = new ArrayList<>();;
1310 this.directory = "model";
1312 extractFolder(this.newFile);
1313 fileList = listModelFiles(this.directory);
1314 }else if (yml==true){
1315 parseTosca(this.newFile);
1317 File file = new File(this.newFile);
1320 String modelType= "";
1323 //Process Main Model file first
1324 classMap = new HashMap<>();
1325 for (File file : fileList) {
1326 if(!file.isDirectory() && file.getName().endsWith(".xmi")){
1327 retreiveDependency(file.toString(), true);
1331 modelList = createList();
1333 cleanUp(this.newFile);
1337 modelList.add(this.newModel.getModelName());
1338 String className=this.newModel.getModelName();
1339 MSAttributeObject msAttributes= new MSAttributeObject();
1340 msAttributes.setClassName(className);
1342 HashMap<String, String> returnAttributeList =new HashMap<>();
1343 returnAttributeList.put(className, this.attributeString);
1344 msAttributes.setAttribute(returnAttributeList);
1346 msAttributes.setSubClass(this.retmap);
1348 HashMap<String, String> returnReferenceList =new HashMap<>();
1349 //String[] referenceArray=this.referenceAttributes.split("=");
1350 returnReferenceList.put(className, this.referenceAttributes);
1351 msAttributes.setRefAttribute(returnReferenceList);
1353 if(this.listConstraints!=""){
1354 HashMap<String, String> enumList =new HashMap<>();
1355 String[] listArray=this.listConstraints.split("#");
1356 for(String str:listArray){
1357 String[] strArr= str.split("=");
1358 if(strArr.length>1){
1359 enumList.put(strArr[0], strArr[1]);
1362 msAttributes.setEnumType(enumList);
1365 classMap=new HashMap<>();
1366 classMap.put(className, msAttributes);
1370 PrintWriter out = response.getWriter();
1372 response.setCharacterEncoding("UTF-8");
1373 response.setContentType("application / json");
1374 request.setCharacterEncoding("UTF-8");
1376 ObjectMapper mapper = new ObjectMapper();
1377 JSONObject j = new JSONObject();
1378 j.put("classListDatas", modelList);
1379 j.put("modelDatas", mapper.writeValueAsString(classMap));
1380 j.put("modelType", modelType);
1381 out.write(j.toString());
1385 * Unzip file and store in the model directory for processing
1387 @SuppressWarnings("rawtypes")
1388 private void extractFolder(String zipFile ) {
1390 File file = new File(zipFile);
1394 zip = new ZipFile(file);
1395 String newPath = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
1396 this.directory = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
1397 checkZipDirectory(this.directory);
1398 new File(newPath).mkdir();
1399 Enumeration zipFileEntries = zip.entries();
1401 // Process each entry
1402 while (zipFileEntries.hasMoreElements()){
1403 // grab a zip file entry
1404 ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
1405 String currentEntry = entry.getName();
1406 File destFile = new File("model" + File.separator + currentEntry);
1407 File destinationParent = destFile.getParentFile();
1409 destinationParent.mkdirs();
1411 if (!entry.isDirectory()){
1412 BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
1414 byte data[] = new byte[BUFFER];
1415 FileOutputStream fos = new FileOutputStream(destFile);
1416 BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
1417 while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
1418 dest.write(data, 0, currentByte);
1425 if (currentEntry.endsWith(".zip")){
1426 extractFolder(destFile.getAbsolutePath());
1429 } catch (IOException e) {
1430 LOGGER.error("Failed to unzip model file " + zipFile, e);
1435 } catch (IOException e) {
1436 LOGGER.error("Exception Occured While closing zipfile " + e);
1441 private void retreiveDependency(String workingFile, Boolean modelClass) {
1443 MSModelUtils utils = new MSModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName());
1444 HashMap<String, MSAttributeObject> tempMap = new HashMap<>();
1446 tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
1448 classMap.putAll(tempMap);
1449 LOGGER.info(tempMap);
1455 private List<File> listModelFiles(String directoryName) {
1456 File directory = new File(directoryName);
1457 List<File> resultList = new ArrayList<>();
1458 File[] fList = directory.listFiles();
1459 for (File file : fList) {
1460 if (file.isFile()) {
1461 resultList.add(file);
1462 } else if (file.isDirectory()) {
1463 dirDependencyList.add(file.getName());
1464 resultList.addAll(listModelFiles(file.getAbsolutePath()));
1470 private void cleanUp(String path) {
1473 FileUtils.forceDelete(new File(path));
1474 } catch (IOException e) {
1475 LOGGER.error("Failed to delete folder " + path, e);
1480 private void checkZipDirectory(String zipDirectory) {
1481 Path path = Paths.get(zipDirectory);
1483 if (Files.exists(path)) {
1484 cleanUp(zipDirectory);
1488 private List<String> createList() {
1489 List<String> list = new ArrayList<>();
1490 for (Entry<String, MSAttributeObject> cMap : classMap.entrySet()){
1491 if (cMap.getValue().isPolicyTempalate()){
1492 list.add(cMap.getKey());
1497 if (list.isEmpty()){
1498 if (classMap.containsKey(this.newModel.getModelName())){
1499 list.add(this.newModel.getModelName());
1507 public Map<String, String> getAttributesListRefMap() {
1508 return attributesListRefMap;
1511 public Map<String, LinkedList<String>> getArrayTextList() {
1512 return arrayTextList;
1517 class DCAEMicroServiceObject {
1519 private String service;
1520 private String location;
1521 private String uuid;
1522 private String policyName;
1523 private String description;
1524 private String configName;
1525 private String templateVersion;
1526 private String version;
1527 private String priority;
1528 private String policyScope;
1529 private String riskType;
1530 private String riskLevel;
1531 private String guard = null;
1533 public String getGuard() {
1536 public void setGuard(String guard) {
1539 public String getRiskType() {
1542 public void setRiskType(String riskType) {
1543 this.riskType = riskType;
1545 public String getRiskLevel() {
1548 public void setRiskLevel(String riskLevel) {
1549 this.riskLevel = riskLevel;
1551 public String getPolicyScope() {
1554 public void setPolicyScope(String policyScope) {
1555 this.policyScope = policyScope;
1558 public String getPriority() {
1561 public void setPriority(String priority) {
1562 this.priority = priority;
1564 public String getVersion() {
1567 public void setVersion(String version) {
1568 this.version = version;
1570 private Object content;
1573 public String getPolicyName() {
1576 public void setPolicyName(String policyName) {
1577 this.policyName = policyName;
1579 public String getDescription() {
1582 public void setDescription(String description) {
1583 this.description = description;
1585 public String getConfigName() {
1588 public void setConfigName(String configName) {
1589 this.configName = configName;
1591 public Object getContent() {
1594 public void setContent(Object content) {
1595 this.content = content;
1598 public String getService() {
1601 public void setService(String service) {
1602 this.service = service;
1604 public String getLocation() {
1607 public void setLocation(String location) {
1608 this.location = location;
1611 public String getUuid() {
1614 public void setUuid(String uuid) {
1617 public String getTemplateVersion() {
1618 return templateVersion;
1620 public void setTemplateVersion(String templateVersion) {
1621 this.templateVersion = templateVersion;