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.openecomp.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.Enumeration;
39 import java.util.HashMap;
40 import java.util.HashSet;
41 import java.util.Iterator;
42 import java.util.LinkedHashMap;
43 import java.util.LinkedList;
44 import java.util.List;
46 import java.util.Map.Entry;
48 import java.util.TreeMap;
49 import java.util.zip.ZipEntry;
50 import java.util.zip.ZipFile;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
55 import org.apache.commons.compress.utils.IOUtils;
56 import org.apache.commons.fileupload.FileItem;
57 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
58 import org.apache.commons.fileupload.servlet.ServletFileUpload;
59 import org.apache.commons.io.FileUtils;
60 import org.apache.commons.lang.StringUtils;
61 import org.json.JSONArray;
62 import org.json.JSONObject;
63 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
64 import org.openecomp.policy.common.logging.flexlogger.Logger;
65 import org.openecomp.policy.rest.XACMLRestProperties;
66 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
67 import org.openecomp.policy.rest.dao.CommonClassDao;
68 import org.openecomp.policy.rest.jpa.GroupPolicyScopeList;
69 import org.openecomp.policy.rest.jpa.MicroServiceModels;
70 import org.openecomp.policy.rest.jpa.PolicyEntity;
71 import org.openecomp.policy.rest.util.MSAttributeObject;
72 import org.openecomp.policy.rest.util.MSModelUtils;
73 import org.openecomp.policy.rest.util.MSModelUtils.MODEL_TYPE;
74 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
75 import org.openecomp.portalsdk.core.web.support.JsonMessage;
76 import org.springframework.beans.factory.annotation.Autowired;
77 import org.springframework.http.MediaType;
78 import org.springframework.stereotype.Controller;
79 import org.springframework.web.bind.annotation.RequestMapping;
80 import org.springframework.web.servlet.ModelAndView;
81 import org.yaml.snakeyaml.Yaml;
83 import com.att.research.xacml.util.XACMLProperties;
84 import com.fasterxml.jackson.core.JsonProcessingException;
85 import com.fasterxml.jackson.databind.DeserializationFeature;
86 import com.fasterxml.jackson.databind.JsonNode;
87 import com.fasterxml.jackson.databind.ObjectMapper;
88 import com.fasterxml.jackson.databind.ObjectWriter;
89 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
90 import com.fasterxml.jackson.databind.node.ObjectNode;
91 import com.google.gson.Gson;
93 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
94 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
95 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
96 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
97 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
98 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
99 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
103 public class CreateDcaeMicroServiceController extends RestrictedBaseController {
104 private static final Logger LOGGER = FlexLogger.getLogger(CreateDcaeMicroServiceController.class);
106 private static CommonClassDao commonClassDao;
108 public static CommonClassDao getCommonClassDao() {
109 return commonClassDao;
112 public static void setCommonClassDao(CommonClassDao commonClassDao) {
113 CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
116 private MicroServiceModels newModel;
117 private String newFile;
118 private String directory;
119 private List<String> modelList = new ArrayList<>();
120 private List<String> dirDependencyList = new ArrayList<>();
121 private HashMap<String,MSAttributeObject > classMap = new HashMap<>();
122 //Tosca Model related Datastructure.
123 String referenceAttributes;
124 String attributeString;
125 String listConstraints;
126 String subAttributeString;
127 HashMap<String, Object> retmap = new HashMap<>();
128 Set<String> uniqueKeys= new HashSet<>();
129 Set<String> uniqueDataKeys= new HashSet<>();
130 StringBuilder dataListBuffer=new StringBuilder();
131 List<String> dataConstraints= new ArrayList <>();
133 public static final String DATATYPE = "data_types.policy.data.";
134 public static final String PROPERTIES=".properties.";
135 public static final String TYPE=".type";
136 public static final String STRING="string";
137 public static final String INTEGER="integer";
138 public static final String LIST="list";
139 public static final String DEFAULT=".default";
140 public static final String REQUIRED=".required";
141 public static final String MANYFALSE=":MANY-false";
145 private CreateDcaeMicroServiceController(CommonClassDao commonClassDao){
146 CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
149 public CreateDcaeMicroServiceController(){}
151 protected PolicyRestAdapter policyAdapter = null;
152 private int priorityCount;
153 private Map<String, String> attributesListRefMap = new HashMap<>();
154 private Map<String, LinkedList<String>> arrayTextList = new HashMap<>();
156 public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
158 String jsonContent = null;
160 jsonContent = decodeContent(root.get("policyJSON")).toString();
161 constructJson(policyData, jsonContent);
163 LOGGER.error("Error while decoding microservice content");
169 private GroupPolicyScopeList getPolicyObject(String policyScope) {
170 GroupPolicyScopeList groupList= (GroupPolicyScopeList) commonClassDao.getEntityItem(GroupPolicyScopeList.class, "name", policyScope);
174 private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent) {
175 ObjectWriter om = new ObjectMapper().writer();
177 DCAEMicroServiceObject microServiceObject = new DCAEMicroServiceObject();
178 MicroServiceModels returnModel = new MicroServiceModels();
179 microServiceObject.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_MS));
180 if(policyAdapter.getServiceType() !=null){
181 microServiceObject.setService(policyAdapter.getServiceType());
182 microServiceObject.setVersion(policyAdapter.getVersion());
183 returnModel = getAttributeObject(microServiceObject.getService(), microServiceObject.getVersion());
185 if (returnModel.getAnnotation()==null || returnModel.getAnnotation().isEmpty()){
186 if(policyAdapter.getUuid()!=null){
187 microServiceObject.setUuid(policyAdapter.getUuid());
189 if(policyAdapter.getLocation()!=null){
190 microServiceObject.setLocation(policyAdapter.getLocation());
192 if(policyAdapter.getConfigName()!=null){
193 microServiceObject.setConfigName(policyAdapter.getConfigName());
195 GroupPolicyScopeList policyScopeValue = getPolicyObject(policyAdapter.getPolicyScope());
196 if(policyScopeValue!=null){
197 microServiceObject.setPolicyScope(policyScopeValue.getGroupList());
201 if(policyAdapter.getPolicyName()!=null){
202 microServiceObject.setPolicyName(policyAdapter.getPolicyName());
204 if(policyAdapter.getPolicyDescription()!=null){
205 microServiceObject.setDescription(policyAdapter.getPolicyDescription());
207 if (policyAdapter.getPriority()!=null){
208 microServiceObject.setPriority(policyAdapter.getPriority());
210 microServiceObject.setPriority("9999");
213 if (policyAdapter.getRiskLevel()!=null){
214 microServiceObject.setRiskLevel(policyAdapter.getRiskLevel());
216 if (policyAdapter.getRiskType()!=null){
217 microServiceObject.setRiskType(policyAdapter.getRiskType());
219 if (policyAdapter.getGuard()!=null){
220 microServiceObject.setGuard(policyAdapter.getGuard());
222 microServiceObject.setContent(jsonContent);
225 json = om.writeValueAsString(microServiceObject);
226 } catch (JsonProcessingException e) {
227 LOGGER.error("Error writing out the object");
230 String cleanJson = cleanUPJson(json);
231 cleanJson = removeNullAttributes(cleanJson);
232 policyAdapter.setJsonBody(cleanJson);
233 return policyAdapter;
236 private String removeNullAttributes(String cleanJson) {
237 ObjectMapper mapper = new ObjectMapper();
240 JsonNode rootNode = mapper.readTree(cleanJson);
241 JsonNode returnNode = mapper.readTree(cleanJson);
242 Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
243 boolean remove = false;
244 while (fieldsIterator.hasNext()) {
245 Map.Entry<String, JsonNode> field = fieldsIterator.next();
246 final String key = field.getKey();
247 final JsonNode value = field.getValue();
248 if (value==null || value.isNull()){
249 ((ObjectNode) returnNode).remove(key);
254 cleanJson = returnNode.toString();
256 } catch (IOException e) {
257 LOGGER.error("Error writing out the JsonNode");
262 // Second index of dot should be returned.
263 public int stringBetweenDots(String str){
264 String stringToSearch=str;
265 String[]ss=stringToSearch.split("\\.");
269 uniqueKeys.add(ss[2]);
273 return uniqueKeys.size();
276 public void stringBetweenDotsForDataFields(String str){
277 String stringToSearch=str;
278 String[]ss=stringToSearch.split("\\.");
283 uniqueDataKeys.add(ss[0]+"%"+ss[2]);
289 public Map<String, String> load(String fileName) throws IOException {
290 File newConfiguration = new File(fileName);
291 InputStream is = null;
293 is = new FileInputStream(newConfiguration);
294 } catch (FileNotFoundException e) {
298 Yaml yaml = new Yaml();
299 @SuppressWarnings("unchecked")
300 Map<Object, Object> yamlMap = (Map<Object, Object>) yaml.load(is);
301 StringBuilder sb = new StringBuilder();
302 Map<String, String> settings = new HashMap<>();
303 if (yamlMap == null) {
306 List<String> path = new ArrayList <>();
307 serializeMap(settings, sb, path, yamlMap);
311 public Map<String, String> load(byte[] source) throws IOException {
312 Yaml yaml = new Yaml();
313 @SuppressWarnings("unchecked")
314 Map<Object, Object> yamlMap = (Map<Object, Object>) yaml.load(source.toString());
315 StringBuilder sb = new StringBuilder();
316 Map<String, String> settings = new HashMap <>();
317 if (yamlMap == null) {
320 List<String> path = new ArrayList <>();
321 serializeMap(settings, sb, path, yamlMap);
325 @SuppressWarnings({ "unchecked", "rawtypes" })
326 private void serializeMap(Map<String, String> settings, StringBuilder sb, List<String> path, Map<Object, Object> yamlMap) {
327 for (Map.Entry<Object, Object> entry : yamlMap.entrySet()) {
328 if (entry.getValue() instanceof Map) {
329 path.add((String) entry.getKey());
330 serializeMap(settings, sb, path, (Map<Object, Object>) entry.getValue());
331 path.remove(path.size() - 1);
332 } else if (entry.getValue() instanceof List) {
333 path.add((String) entry.getKey());
334 serializeList(settings, sb, path, (List) entry.getValue());
335 path.remove(path.size() - 1);
337 serializeValue(settings, sb, path, (String) entry.getKey(), entry.getValue());
342 @SuppressWarnings("unchecked")
343 private void serializeList(Map<String, String> settings, StringBuilder sb, List<String> path, List<String> yamlList) {
345 for (Object listEle : yamlList) {
346 if (listEle instanceof Map) {
347 path.add(Integer.toString(counter));
348 serializeMap(settings, sb, path, (Map<Object, Object>) listEle);
349 path.remove(path.size() - 1);
350 } else if (listEle instanceof List) {
351 path.add(Integer.toString(counter));
352 serializeList(settings, sb, path, (List<String>) listEle);
353 path.remove(path.size() - 1);
355 serializeValue(settings, sb, path, Integer.toString(counter), listEle);
361 private void serializeValue(Map<String, String> settings, StringBuilder sb, List<String> path, String name, Object value) {
366 for (String pathEle : path) {
367 sb.append(pathEle).append('.');
370 settings.put(sb.toString(), value.toString());
373 void parseDataAndPolicyNodes(Map<String,String> map){
374 for(String key:map.keySet()){
375 if(key.contains("policy.nodes.Root"))
379 else if(key.contains("policy.nodes")){
380 String wordToFind = "policy.nodes.";
381 int indexForPolicyNode=key.indexOf(wordToFind);
382 String subNodeString= key.substring(indexForPolicyNode+13, key.length());
384 stringBetweenDots(subNodeString);
386 else if(key.contains("policy.data")){
387 String wordToFind="policy.data.";
388 int indexForPolicyNode=key.indexOf(wordToFind);
389 String subNodeString= key.substring(indexForPolicyNode+12, key.length());
391 stringBetweenDotsForDataFields(subNodeString);
396 HashMap<String,String> parseDataNodes(Map<String,String> map){
397 HashMap<String,String> dataMapForJson=new HashMap <>();
398 for(String uniqueDataKey: uniqueDataKeys){
399 if(uniqueDataKey.contains("%")){
400 String[] uniqueDataKeySplit= uniqueDataKey.split("%",2);
401 if (uniqueDataKeySplit.length < 2) {
404 String findType=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+TYPE;
405 String typeValue=map.get(findType);
406 LOGGER.info(typeValue);
407 if(typeValue.equalsIgnoreCase(STRING)||
408 typeValue.equalsIgnoreCase(INTEGER)
411 String findDefault=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+DEFAULT;
412 String defaultValue= map.get(findDefault);
413 LOGGER.info("defaultValue is:"+ defaultValue);
415 String findRequired=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+REQUIRED;
416 String requiredValue= map.get(findRequired);
417 LOGGER.info("requiredValue is:"+ requiredValue);
419 StringBuilder attributeIndividualStringBuilder= new StringBuilder();
420 attributeIndividualStringBuilder.append(typeValue+":defaultValue-");
421 attributeIndividualStringBuilder.append(defaultValue+":required-");
422 attributeIndividualStringBuilder.append(requiredValue+MANYFALSE);
423 dataMapForJson.put(uniqueDataKey, attributeIndividualStringBuilder.toString());
425 else if(typeValue.equalsIgnoreCase(LIST)){
426 String findList= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.type";
427 String listValue=map.get(findList);
429 LOGGER.info("Type of list is:"+ listValue);
431 if(listValue.contains(".")){
432 String trimValue=listValue.substring(listValue.lastIndexOf('.')+1);
433 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
434 referenceIndividualStringBuilder.append(trimValue+":MANY-true");
435 dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
438 StringBuilder stringListItems= new StringBuilder();
439 stringListItems.append(uniqueDataKeySplit[1].toUpperCase()+":MANY-false");
440 dataMapForJson.put(uniqueDataKey, stringListItems.toString());
441 dataListBuffer.append(uniqueDataKeySplit[1].toUpperCase()+"=[");
442 for(int i=0;i<10;i++){
443 String findConstraints= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.constraints.0.valid_values."+i;
444 String constraintsValue=map.get(findConstraints);
445 LOGGER.info(constraintsValue);
446 if(constraintsValue==null){
450 dataConstraints.add(constraintsValue);
451 dataListBuffer.append(constraintsValue+",");
454 dataListBuffer.append("]#");
456 LOGGER.info(dataListBuffer);
461 String findUserDefined="data_types.policy.data."+uniqueDataKeySplit[0]+"."+"properties"+"."+uniqueDataKeySplit[1]+".type";
462 String userDefinedValue=map.get(findUserDefined);
463 String trimValue=userDefinedValue.substring(userDefinedValue.lastIndexOf('.')+1);
464 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
465 referenceIndividualStringBuilder.append(trimValue+":MANY-false");
466 dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
471 return dataMapForJson;
474 void constructJsonForDataFields(HashMap<String,String> dataMapForJson){
475 HashMap<String,HashMap<String,String>> dataMapKey= new HashMap <>();
476 HashMap<String,String> hmSub;
477 for(Map.Entry<String, String> entry: dataMapForJson.entrySet()){
478 String uniqueDataKey= entry.getKey();
479 String[] uniqueDataKeySplit=uniqueDataKey.split("%",2);
480 if (uniqueDataKeySplit.length < 2) {
483 String value= dataMapForJson.get(uniqueDataKey);
484 if(dataMapKey.containsKey(uniqueDataKeySplit[0])){
485 hmSub = dataMapKey.get(uniqueDataKeySplit[0]);
486 hmSub.put(uniqueDataKeySplit[1], value);
489 hmSub=new HashMap <>();
490 hmSub.put(uniqueDataKeySplit[1], value);
493 dataMapKey.put(uniqueDataKeySplit[0], hmSub);
496 JSONObject mainObject= new JSONObject();
498 for(Map.Entry<String,HashMap<String,String>> entry: dataMapKey.entrySet()){
499 String s=entry.getKey();
500 json= new JSONObject();
501 HashMap<String,String> jsonHm=dataMapKey.get(s);
502 for(Map.Entry<String,String> entryMap:jsonHm.entrySet()){
503 String key=entryMap.getKey();
504 json.put(key, jsonHm.get(key));
506 mainObject.put(s,json);
508 Iterator<String> keysItr = mainObject.keys();
509 while(keysItr.hasNext()) {
510 String key = keysItr.next();
511 String value = mainObject.get(key).toString();
512 retmap.put(key, value);
515 LOGGER.info("#############################################################################");
516 LOGGER.info(mainObject);
517 LOGGER.info("###############################################################################");
521 HashMap<String,HashMap<String,String>> parsePolicyNodes(Map<String,String> map){
522 HashMap<String,HashMap<String,String>> mapKey= new HashMap <>();
523 for(String uniqueKey: uniqueKeys){
524 HashMap<String,String> hm;
526 for(Map.Entry<String,String> entry:map.entrySet()){
527 String key=entry.getKey();
528 if(key.contains(uniqueKey) && key.contains("policy.nodes")){
529 if(mapKey.containsKey(uniqueKey)){
530 hm = mapKey.get(uniqueKey);
531 String keyStr= key.substring(key.lastIndexOf('.')+1);
532 String valueStr= map.get(key);
533 if(("type").equals(keyStr)){
534 if(!key.contains("entry_schema"))
536 hm.put(keyStr,valueStr);
539 hm.put(keyStr,valueStr);
543 hm = new HashMap <>();
544 String keyStr= key.substring(key.lastIndexOf('.')+1);
545 String valueStr= map.get(key);
546 if(("type").equals(keyStr)){
547 if(!key.contains("entry_schema"))
549 hm.put(keyStr,valueStr);
552 hm.put(keyStr,valueStr);
554 mapKey.put(uniqueKey, hm);
562 void createAttributes(HashMap<String,HashMap<String,String>> mapKey){
563 StringBuilder attributeStringBuilder= new StringBuilder();
564 StringBuilder referenceStringBuilder= new StringBuilder();
565 StringBuilder listBuffer= new StringBuilder();
566 List<String> constraints= new ArrayList<>();
567 for(Map.Entry<String,HashMap<String,String>> entry: mapKey.entrySet()){
568 String keySetString= entry.getKey();
569 HashMap<String,String> keyValues=mapKey.get(keySetString);
570 if(keyValues.get("type").equalsIgnoreCase(STRING)||
571 keyValues.get("type").equalsIgnoreCase(INTEGER)
573 StringBuilder attributeIndividualStringBuilder= new StringBuilder();
574 attributeIndividualStringBuilder.append(keySetString+"=");
575 attributeIndividualStringBuilder.append(keyValues.get("type")+":defaultValue-");
576 attributeIndividualStringBuilder.append(keyValues.get("default")+":required-");
577 attributeIndividualStringBuilder.append(keyValues.get("required")+":MANY-false");
578 attributeStringBuilder.append(attributeIndividualStringBuilder+",");
581 else if(keyValues.get("type").equalsIgnoreCase(LIST)){
583 Set<String> keys= keyValues.keySet();
584 Iterator<String> itr=keys.iterator();
585 while(itr.hasNext()){
586 String key= itr.next();
587 if((!("type").equals(key) ||("required").equals(key)))
589 String value= keyValues.get(key);
590 //The "." in the value determines if its a string or a user defined type.
591 if (!value.contains(".")){
593 constraints.add(keyValues.get(key));
595 //This is userdefined string
596 String trimValue=value.substring(value.lastIndexOf('.')+1);
597 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
598 referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-true");
599 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
605 //User defined Datatype.
606 String value=keyValues.get("type");
607 String trimValue=value.substring(value.lastIndexOf('.')+1);
608 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
609 referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-false");
610 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
613 if(constraints!=null &&constraints.isEmpty()==false){
615 listBuffer.append(keySetString.toUpperCase()+"=[");
616 for(String str:constraints){
617 listBuffer.append(str+",");
619 listBuffer.append("]#");
620 LOGGER.info(listBuffer);
623 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
624 referenceIndividualStringBuilder.append(keySetString+"="+keySetString.toUpperCase()+":MANY-false");
625 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
630 dataListBuffer.append(listBuffer);
633 LOGGER.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
634 LOGGER.info("Whole attribute String is:"+attributeStringBuilder);
635 LOGGER.info("Whole reference String is:"+referenceStringBuilder);
636 LOGGER.info("List String is:"+listBuffer);
637 LOGGER.info("Data list buffer is:"+dataListBuffer);
638 LOGGER.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
640 this.listConstraints=dataListBuffer.toString();
641 this.referenceAttributes=referenceStringBuilder.toString();
642 this.attributeString=attributeStringBuilder.toString();
647 public void parseTosca (String fileName){
648 Map<String,String> map= new HashMap<>();
653 parseDataAndPolicyNodes(map);
655 HashMap<String,String> dataMapForJson=parseDataNodes(map);
657 constructJsonForDataFields(dataMapForJson);
659 HashMap<String,HashMap<String,String>> mapKey= parsePolicyNodes(map);
661 createAttributes(mapKey);
663 } catch (IOException e) {
669 private String cleanUPJson(String json) {
670 String cleanJson = StringUtils.replaceEach(json, new String[]{"\\\\", "\\\\\\", "\\\\\\\\"}, new String[]{"\\", "\\", "\\"});
671 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\"}, new String[]{"\\"});
672 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[]{""});
680 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\""}, new String[]{"\""});
681 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\\\\\\"}, new String[]{"\""});
682 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\\""}, new String[]{"\""});
683 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[", "]\""}, new String[]{"[", "]"});
687 private JSONObject decodeContent(JsonNode jsonNode){
688 Iterator<JsonNode> jsonElements = jsonNode.elements();
689 Iterator<String> jsonKeys = jsonNode.fieldNames();
690 Map<String,String> element = new TreeMap<>();
691 while(jsonElements.hasNext() && jsonKeys.hasNext()){
692 element.put(jsonKeys.next(), jsonElements.next().toString());
694 JSONObject jsonResult = new JSONObject();
695 JSONArray jsonArray = null;
696 String oldValue = null;
697 String nodeKey = null;
698 String arryKey = null;
699 Boolean isArray = false;
700 JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
701 ObjectNode node = nodeFactory.objectNode();
702 String prevKey = null;
703 String presKey = null;
704 for(String key: element.keySet()){
705 if(key.contains(".")){
706 presKey = key.substring(0,key.indexOf("."));
707 }else if(key.contains("@")){
708 presKey = key.substring(0,key.indexOf("@"));
712 // first check if we are different from old.
713 LOGGER.info(key+"\n");
714 if(jsonArray!=null && jsonArray.length()>0 && key.contains("@") && !key.contains(".") && oldValue!=null){
715 if(!oldValue.equals(key.substring(0,key.indexOf("@")))){
716 jsonResult.put(oldValue, jsonArray);
717 jsonArray = new JSONArray();
719 }else if(jsonArray!=null && jsonArray.length()>0 && !presKey.equals(prevKey) && oldValue!=null){
720 jsonResult.put(oldValue, jsonArray);
722 jsonArray = new JSONArray();
727 if(key.contains(".")){
729 nodeKey = key.substring(0,key.indexOf("."));
731 if(nodeKey.equals(key.substring(0,key.indexOf(".")))){
732 node.put(key.substring(key.indexOf(".")+1), element.get(key));
735 if(nodeKey.contains("@")){
737 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
739 if(nodeKey.endsWith("@0")){
741 jsonArray = new JSONArray();
743 if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
744 jsonArray.put(decodeContent(node));
746 if(key.contains("@") && !arryKey.equals(key.substring(0,nodeKey.indexOf("@")))){
747 jsonResult.put(arryKey, jsonArray);
748 jsonArray = new JSONArray();
749 }else if(!key.contains("@")){
750 jsonResult.put(arryKey, jsonArray);
751 jsonArray = new JSONArray();
753 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
756 jsonResult.put(nodeKey, decodeContent(node));
758 node = nodeFactory.objectNode();
760 nodeKey = key.substring(0,key.indexOf("."));
761 if(nodeKey.contains("@")){
762 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
764 node.put(key.substring(key.indexOf(".")+1), element.get(key));
766 }else if(node.size()!=0){
767 if(nodeKey.contains("@")){
769 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
771 if(nodeKey.endsWith("@0")){
773 jsonArray = new JSONArray();
775 if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
776 jsonArray.put(decodeContent(node));
778 jsonResult.put(arryKey, jsonArray);
779 jsonArray = new JSONArray();
780 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
783 jsonResult.put(nodeKey, decodeContent(node));
785 node = nodeFactory.objectNode();
786 if(key.contains("@")){
788 if(key.endsWith("@0")|| jsonArray==null){
789 jsonArray = new JSONArray();
791 }else if(!key.contains("@")){
796 oldValue = key.substring(0,key.indexOf("@"));
798 if(oldValue!=prevKey){
799 oldValue = key.substring(0,key.indexOf("@"));
801 if(oldValue.equals(key.substring(0,key.indexOf("@")))){
802 jsonArray.put(element.get(key));
804 jsonResult.put(oldValue, jsonArray);
805 jsonArray = new JSONArray();
807 oldValue = key.substring(0,key.indexOf("@"));
809 jsonResult.put(key, element.get(key));
812 if(key.contains("@")){
814 if(key.endsWith("@0")|| jsonArray==null){
815 jsonArray = new JSONArray();
817 }else if(!key.contains("@")){
822 oldValue = key.substring(0,key.indexOf("@"));
824 if(oldValue!=prevKey){
825 oldValue = key.substring(0,key.indexOf("@"));
827 if(oldValue.equals(key.substring(0,key.indexOf("@")))){
828 jsonArray.put(element.get(key));
830 jsonResult.put(oldValue, jsonArray);
831 jsonArray = new JSONArray();
833 oldValue = key.substring(0,key.indexOf("@"));
835 jsonResult.put(key, element.get(key));
840 if(nodeKey.contains("@")){
842 jsonArray = new JSONArray();
845 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
847 jsonArray.put(decodeContent(node));
848 jsonResult.put(arryKey, jsonArray);
851 jsonResult.put(nodeKey, decodeContent(node));
854 if(isArray && jsonArray.length() > 0){
855 jsonResult.put(oldValue, jsonArray);
860 @RequestMapping(value={"/policyController/getDCAEMSTemplateData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
861 public ModelAndView getDCAEMSTemplateData(HttpServletRequest request, HttpServletResponse response) throws Exception{
862 ObjectMapper mapper = new ObjectMapper();
863 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
864 JsonNode root = mapper.readTree(request.getReader());
866 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
867 String servicename = value.toString().split("-v")[0];
868 String version = null;
869 if (value.toString().contains("-v")){
870 version = value.toString().split("-v")[1];
872 MicroServiceModels returnModel = getAttributeObject(servicename, version);
874 String jsonModel = createMicroSeriveJson(returnModel);
876 response.setCharacterEncoding("UTF-8");
877 response.setContentType("application / json");
878 request.setCharacterEncoding("UTF-8");
879 List<Object> list = new ArrayList<>();
880 PrintWriter out = response.getWriter();
881 String responseString = mapper.writeValueAsString(returnModel);
882 JSONObject j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + "}");
884 out.write(list.toString());
888 @SuppressWarnings({ "unchecked", "rawtypes" })
889 private String createMicroSeriveJson(MicroServiceModels returnModel) {
890 Map<String, String> attributeMap = new HashMap<>();
891 Map<String, String> refAttributeMap = new HashMap<>();
892 String attribute = returnModel.getAttributes();
893 if(attribute != null){
894 attribute = attribute.trim();
896 String refAttribute = returnModel.getRef_attributes();
897 if(refAttribute != null){
898 refAttribute = refAttribute.trim();
900 String enumAttribute = returnModel.getEnumValues();
901 if(enumAttribute != null){
902 enumAttribute = enumAttribute.trim();
904 if (!StringUtils.isEmpty(attribute)){
905 attributeMap = convert(attribute, ",");
907 if (!StringUtils.isEmpty(refAttribute)){
908 refAttributeMap = convert(refAttribute, ",");
911 Gson gson = new Gson();
913 String subAttributes = returnModel.getSub_attributes();
914 if(subAttributes != null){
915 subAttributes = subAttributes.trim();
919 Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
921 JSONObject object = new JSONObject();
922 JSONArray array = new JSONArray();
924 for (Entry<String, String> keySet : attributeMap.entrySet()){
925 array = new JSONArray();
926 String value = keySet.getValue();
927 if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
929 object.put(keySet.getKey().trim(), array);
931 object.put(keySet.getKey().trim(), value.trim());
935 for (Entry<String, String> keySet : refAttributeMap.entrySet()){
936 array = new JSONArray();
937 String value = keySet.getValue().split(":")[0];
938 if (gsonObject.containsKey(value)){
939 if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
940 array.put(recursiveReference(value, gsonObject, enumAttribute));
941 object.put(keySet.getKey().trim(), array);
943 object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
946 if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
947 array.put(value.trim());
948 object.put(keySet.getKey().trim(), array);
950 object.put(keySet.getKey().trim(), value.trim());
955 return object.toString();
958 @SuppressWarnings("unchecked")
959 private JSONObject recursiveReference(String name, Map<String,String> subAttributeMap, String enumAttribute) {
960 JSONObject object = new JSONObject();
961 Map<String, String> map = new HashMap<>();
962 Object returnClass = subAttributeMap.get(name);
963 map = (Map<String, String>) returnClass;
964 JSONArray array = new JSONArray();
966 for( Entry<String, String> m:map.entrySet()){
967 String[] splitValue = m.getValue().split(":");
968 array = new JSONArray();
969 if (subAttributeMap.containsKey(splitValue[0])){
970 if (m.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
971 array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
972 object.put(m.getKey().trim(), array);
974 object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
977 if (m.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
978 array.put(splitValue[0].trim());
979 object.put(m.getKey().trim(), array);
981 object.put(m.getKey().trim(), splitValue[0].trim());
990 @RequestMapping(value={"/policyController/getModelServiceVersioneData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
991 public ModelAndView getModelServiceVersionData(HttpServletRequest request, HttpServletResponse response) throws Exception{
992 ObjectMapper mapper = new ObjectMapper();
993 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
994 JsonNode root = mapper.readTree(request.getReader());
996 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
997 String servicename = value.toString().split("-v")[0];
998 Set<String> returnList = getVersionList(servicename);
1000 response.setCharacterEncoding("UTF-8");
1001 response.setContentType("application / json");
1002 request.setCharacterEncoding("UTF-8");
1003 List<Object> list = new ArrayList<>();
1004 PrintWriter out = response.getWriter();
1005 String responseString = mapper.writeValueAsString(returnList);
1006 JSONObject j = new JSONObject("{dcaeModelVersionData: " + responseString +"}");
1008 out.write(list.toString());
1012 private Set<String> getVersionList(String name) {
1013 MicroServiceModels workingModel = new MicroServiceModels();
1014 Set<String> list = new HashSet<>();
1015 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
1016 for (int i = 0; i < microServiceModelsData.size(); i++) {
1017 workingModel = (MicroServiceModels) microServiceModelsData.get(i);
1018 if (workingModel.getVersion()!=null){
1019 list.add(workingModel.getVersion());
1021 list.add("Default");
1027 private MicroServiceModels getAttributeObject(String name, String version) {
1028 MicroServiceModels workingModel = new MicroServiceModels();
1029 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
1030 for (int i = 0; i < microServiceModelsData.size(); i++) {
1031 workingModel = (MicroServiceModels) microServiceModelsData.get(i);
1032 if(version != null){
1033 if (workingModel.getVersion()!=null){
1034 if (workingModel.getVersion().equals(version)){
1035 return workingModel;
1038 return workingModel;
1041 return workingModel;
1045 return workingModel;
1048 @RequestMapping(value={"/get_DCAEPriorityValues"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
1049 public void getDCAEPriorityValuesData(HttpServletRequest request, HttpServletResponse response){
1051 Map<String, Object> model = new HashMap<>();
1052 ObjectMapper mapper = new ObjectMapper();
1053 List<String> priorityList = new ArrayList<>();
1055 for (int i = 1; i < priorityCount; i++) {
1056 priorityList.add(String.valueOf(i));
1058 model.put("priorityDatas", mapper.writeValueAsString(priorityList));
1059 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
1060 JSONObject j = new JSONObject(msg);
1061 response.getWriter().write(j.toString());
1063 catch (Exception e){
1068 public void prePopulateDCAEMSPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1069 if (policyAdapter.getPolicyData() instanceof PolicyType) {
1070 Object policyData = policyAdapter.getPolicyData();
1071 PolicyType policy = (PolicyType) policyData;
1072 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
1073 String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("MS_") +3);
1074 policyAdapter.setPolicyName(policyNameValue);
1075 String description = "";
1077 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
1078 }catch(Exception e){
1079 description = policy.getDescription();
1081 policyAdapter.setPolicyDescription(description);
1082 // Get the target data under policy.
1083 TargetType target = policy.getTarget();
1084 if (target != null) {
1085 // Under target we have AnyOFType
1086 List<AnyOfType> anyOfList = target.getAnyOf();
1087 if (anyOfList != null) {
1088 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
1089 while (iterAnyOf.hasNext()) {
1090 AnyOfType anyOf = iterAnyOf.next();
1091 // Under AnyOFType we have AllOFType
1092 List<AllOfType> allOfList = anyOf.getAllOf();
1093 if (allOfList != null) {
1094 Iterator<AllOfType> iterAllOf = allOfList.iterator();
1095 while (iterAllOf.hasNext()) {
1096 AllOfType allOf = iterAllOf.next();
1097 // Under AllOFType we have Match
1098 List<MatchType> matchList = allOf.getMatch();
1099 if (matchList != null) {
1100 Iterator<MatchType> iterMatch = matchList.iterator();
1101 while (matchList.size()>1 && iterMatch.hasNext()) {
1102 MatchType match = iterMatch.next();
1104 // Under the match we have attribute value and
1105 // attributeDesignator. So,finally down to the actual attribute.
1107 AttributeValueType attributeValue = match.getAttributeValue();
1108 String value = (String) attributeValue.getContent().get(0);
1109 AttributeDesignatorType designator = match.getAttributeDesignator();
1110 String attributeId = designator.getAttributeId();
1111 // First match in the target is EcompName, so set that value.
1112 if (attributeId.equals("ECOMPName")) {
1113 policyAdapter.setEcompName(value);
1115 if (attributeId.equals("ConfigName")){
1116 policyAdapter.setConfigName(value);
1118 if (attributeId.equals("uuid")){
1119 policyAdapter.setUuid(value);
1121 if (attributeId.equals("location")){
1122 policyAdapter.setLocation(value);
1124 if (attributeId.equals("RiskType")){
1125 policyAdapter.setRiskType(value);
1127 if (attributeId.equals("RiskLevel")){
1128 policyAdapter.setRiskLevel(value);
1130 if (attributeId.equals("guard")){
1131 policyAdapter.setGuard(value);
1133 if (attributeId.equals("TTLDate") && !value.contains("NA")){
1134 String newDate = convertDate(value, true);
1135 policyAdapter.setTtlDate(newDate);
1138 readFile(policyAdapter, entity);
1148 private String convertDate(String dateTTL, boolean portalType) {
1149 String formateDate = null;
1150 String[] date = dateTTL.split("T");
1151 String[] parts = date[0].split("-");
1153 formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];
1157 public static Map<String, String> convert(String str, String split) {
1158 Map<String, String> map = new HashMap<>();
1159 for(final String entry : str.split(split)) {
1160 String[] parts = entry.split("=");
1161 map.put(parts[0], parts[1]);
1167 @SuppressWarnings("unchecked")
1168 private void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1169 String policyScopeName = null;
1170 ObjectMapper mapper = new ObjectMapper();
1172 DCAEMicroServiceObject msBody = (DCAEMicroServiceObject) mapper.readValue(entity.getConfigurationData().getConfigBody(), DCAEMicroServiceObject.class);
1173 policyScopeName = getPolicyScope(msBody.getPolicyScope());
1174 policyAdapter.setPolicyScope(policyScopeName);
1176 policyAdapter.setPriority(msBody.getPriority());
1178 if (msBody.getVersion()!= null){
1179 policyAdapter.setServiceType(msBody.getService());
1180 policyAdapter.setVersion(msBody.getVersion());
1182 policyAdapter.setServiceType(msBody.getService());
1184 if(msBody.getContent() != null){
1185 LinkedHashMap<String, Object> data = new LinkedHashMap<>();
1186 LinkedHashMap<String, ?> map = (LinkedHashMap<String, ?>) msBody.getContent();
1187 readRecursivlyJSONContent(map, data);
1188 policyAdapter.setRuleData(data);
1191 } catch (Exception e) {
1197 @SuppressWarnings({ "rawtypes", "unchecked" })
1198 private void readRecursivlyJSONContent(LinkedHashMap<String, ?> map, LinkedHashMap<String, Object> data){
1199 for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
1200 Object key = iterator.next();
1201 Object value = map.get(key);
1202 if(value instanceof LinkedHashMap<?, ?>){
1203 LinkedHashMap<String, Object> secondObjec = new LinkedHashMap<>();
1204 readRecursivlyJSONContent((LinkedHashMap<String, ?>) value, secondObjec);
1205 for(String objKey: secondObjec.keySet()){
1206 data.put(key+"." +objKey, secondObjec.get(objKey));
1208 }else if(value instanceof ArrayList){
1209 ArrayList<?> jsonArrayVal = (ArrayList<?>)value;
1210 for(int i = 0; i < jsonArrayVal.size(); i++){
1211 Object arrayvalue = jsonArrayVal.get(i);
1212 if(arrayvalue instanceof LinkedHashMap<?, ?>){
1213 LinkedHashMap<String, Object> newData = new LinkedHashMap<>();
1214 readRecursivlyJSONContent((LinkedHashMap<String, ?>) arrayvalue, newData);
1215 for(String objKey: newData.keySet()){
1216 data.put(key+"@"+i+"." +objKey, newData.get(objKey));
1218 }else if(arrayvalue instanceof ArrayList){
1219 ArrayList<?> jsonArrayVal1 = (ArrayList<?>)value;
1220 for(int j = 0; j < jsonArrayVal1.size(); j++){
1221 Object arrayvalue1 = jsonArrayVal1.get(i);
1222 data.put(key+"@"+j, arrayvalue1.toString());
1225 data.put(key+"@"+i, arrayvalue.toString());
1229 data.put(key.toString(), value.toString());
1234 private String getPolicyScope(String value) {
1235 GroupPolicyScopeList pScope = new GroupPolicyScopeList();
1236 List<Object> groupList= commonClassDao.getData(GroupPolicyScopeList.class);
1237 if(groupList.size() > 0){
1238 for(int i = 0 ; i < groupList.size() ; i ++){
1239 pScope = (GroupPolicyScopeList) groupList.get(i);
1240 if (pScope.getGroupList().equals(value)){
1245 return pScope.getGroupName();
1248 //Convert the map values and set into JSON body
1249 public Map<String, String> convertMap(Map<String, String> attributesMap, Map<String, String> attributesRefMap) {
1250 Map<String, String> attribute = new HashMap<>();
1254 for (Entry<String, String> entry : attributesMap.entrySet()) {
1255 key = entry.getKey();
1256 value = entry.getValue();
1257 attribute.put(key, value);
1259 for (Entry<String, String> entryRef : attributesRefMap.entrySet()) {
1260 key = entryRef.getKey();
1261 value = entryRef.getValue().toString();
1262 attribute.put(key, value);
1264 for (Entry<String, String> entryList : attributesListRefMap.entrySet()) {
1265 key = entryList.getKey();
1266 value = entryList.getValue().toString();
1267 attribute.put(key, value);
1269 for (Entry<String, LinkedList<String>> arrayList : arrayTextList.entrySet()){
1270 key = arrayList.getKey();
1272 for (Object textList : arrayList.getValue()){
1274 temp = "[" + textList;
1276 temp = temp + "," + textList;
1279 attribute.put(key, temp+ "]");
1285 @RequestMapping(value={"/ms_dictionary/set_MSModelData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
1286 public void SetMSModelData(HttpServletRequest request, HttpServletResponse response) throws Exception{
1287 List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
1288 boolean zip = false;
1290 for (FileItem item : items) {
1291 if(item.getName().endsWith(".zip") || item.getName().endsWith(".xmi")||item.getName().endsWith(".yml")){
1292 this.newModel = new MicroServiceModels();
1294 File file = new File(item.getName());
1295 OutputStream outputStream = new FileOutputStream(file);
1296 IOUtils.copy(item.getInputStream(), outputStream);
1297 outputStream.close();
1298 this.newFile = file.toString();
1299 this.newModel.setModelName(this.newFile.toString().split("-v")[0]);
1301 if (this.newFile.toString().contains("-v")){
1302 if (item.getName().endsWith(".zip")){
1303 this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".zip", ""));
1305 }else if(item.getName().endsWith(".yml")){
1306 this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".yml", ""));
1310 this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".xmi", ""));
1314 }catch(Exception e){
1315 LOGGER.error("Upload error : " + e);
1320 List<File> fileList = new ArrayList<>();;
1321 this.directory = "model";
1323 extractFolder(this.newFile);
1324 fileList = listModelFiles(this.directory);
1325 }else if (yml==true){
1326 parseTosca(this.newFile);
1328 File file = new File(this.newFile);
1331 String modelType= "";
1334 //Process Main Model file first
1335 classMap = new HashMap<>();
1336 for (File file : fileList) {
1337 if(!file.isDirectory() && file.getName().endsWith(".xmi")){
1338 retreiveDependency(file.toString(), true);
1342 modelList = createList();
1344 cleanUp(this.newFile);
1348 modelList.add(this.newModel.getModelName());
1349 String className=this.newModel.getModelName();
1350 MSAttributeObject msAttributes= new MSAttributeObject();
1351 msAttributes.setClassName(className);
1353 HashMap<String, String> returnAttributeList =new HashMap<>();
1354 returnAttributeList.put(className, this.attributeString);
1355 msAttributes.setAttribute(returnAttributeList);
1357 msAttributes.setSubClass(this.retmap);
1359 HashMap<String, String> returnReferenceList =new HashMap<>();
1360 //String[] referenceArray=this.referenceAttributes.split("=");
1361 returnReferenceList.put(className, this.referenceAttributes);
1362 msAttributes.setRefAttribute(returnReferenceList);
1364 if(this.listConstraints!=""){
1365 HashMap<String, String> enumList =new HashMap<>();
1366 String[] listArray=this.listConstraints.split("#");
1367 for(String str:listArray){
1368 String[] strArr= str.split("=");
1369 if(strArr.length>1){
1370 enumList.put(strArr[0], strArr[1]);
1373 msAttributes.setEnumType(enumList);
1376 classMap=new HashMap<>();
1377 classMap.put(className, msAttributes);
1381 PrintWriter out = response.getWriter();
1383 response.setCharacterEncoding("UTF-8");
1384 response.setContentType("application / json");
1385 request.setCharacterEncoding("UTF-8");
1387 ObjectMapper mapper = new ObjectMapper();
1388 JSONObject j = new JSONObject();
1389 j.put("classListDatas", modelList);
1390 j.put("modelDatas", mapper.writeValueAsString(classMap));
1391 j.put("modelType", modelType);
1392 out.write(j.toString());
1396 * Unzip file and store in the model directory for processing
1398 @SuppressWarnings("rawtypes")
1399 private void extractFolder(String zipFile ) {
1401 File file = new File(zipFile);
1405 zip = new ZipFile(file);
1406 String newPath = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
1407 this.directory = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
1408 checkZipDirectory(this.directory);
1409 new File(newPath).mkdir();
1410 Enumeration zipFileEntries = zip.entries();
1412 // Process each entry
1413 while (zipFileEntries.hasMoreElements()){
1414 // grab a zip file entry
1415 ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
1416 String currentEntry = entry.getName();
1417 File destFile = new File("model" + File.separator + currentEntry);
1418 File destinationParent = destFile.getParentFile();
1420 destinationParent.mkdirs();
1422 if (!entry.isDirectory()){
1423 BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
1425 byte data[] = new byte[BUFFER];
1426 FileOutputStream fos = new FileOutputStream(destFile);
1427 BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
1428 while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
1429 dest.write(data, 0, currentByte);
1436 if (currentEntry.endsWith(".zip")){
1437 extractFolder(destFile.getAbsolutePath());
1440 } catch (IOException e) {
1441 LOGGER.error("Failed to unzip model file " + zipFile);
1446 } catch (IOException e) {
1447 LOGGER.error("Exception Occured While closing zipfile " + e);
1452 private void retreiveDependency(String workingFile, Boolean modelClass) {
1454 MSModelUtils utils = new MSModelUtils(PolicyController.getMsEcompName(), PolicyController.getMsPolicyName());
1455 HashMap<String, MSAttributeObject> tempMap = new HashMap<>();
1457 tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
1459 classMap.putAll(tempMap);
1460 LOGGER.info(tempMap);
1466 private List<File> listModelFiles(String directoryName) {
1467 File directory = new File(directoryName);
1468 List<File> resultList = new ArrayList<>();
1469 File[] fList = directory.listFiles();
1470 for (File file : fList) {
1471 if (file.isFile()) {
1472 resultList.add(file);
1473 } else if (file.isDirectory()) {
1474 dirDependencyList.add(file.getName());
1475 resultList.addAll(listModelFiles(file.getAbsolutePath()));
1481 private void cleanUp(String path) {
1484 FileUtils.forceDelete(new File(path));
1485 } catch (IOException e) {
1486 LOGGER.error("Failed to delete folder " + path);
1491 private void checkZipDirectory(String zipDirectory) {
1492 Path path = Paths.get(zipDirectory);
1494 if (Files.exists(path)) {
1495 cleanUp(zipDirectory);
1499 private List<String> createList() {
1500 List<String> list = new ArrayList<>();
1501 for (Entry<String, MSAttributeObject> cMap : classMap.entrySet()){
1502 if (cMap.getValue().isPolicyTempalate()){
1503 list.add(cMap.getKey());
1508 if (list.isEmpty()){
1509 if (classMap.containsKey(this.newModel.getModelName())){
1510 list.add(this.newModel.getModelName());
1518 public Map<String, String> getAttributesListRefMap() {
1519 return attributesListRefMap;
1522 public Map<String, LinkedList<String>> getArrayTextList() {
1523 return arrayTextList;
1528 class DCAEMicroServiceObject {
1530 private String service;
1531 private String location;
1532 private String uuid;
1533 private String policyName;
1534 private String description;
1535 private String configName;
1536 private String templateVersion;
1537 private String version;
1538 private String priority;
1539 private String policyScope;
1540 private String riskType;
1541 private String riskLevel;
1542 private String guard = null;
1544 public String getGuard() {
1547 public void setGuard(String guard) {
1550 public String getRiskType() {
1553 public void setRiskType(String riskType) {
1554 this.riskType = riskType;
1556 public String getRiskLevel() {
1559 public void setRiskLevel(String riskLevel) {
1560 this.riskLevel = riskLevel;
1562 public String getPolicyScope() {
1565 public void setPolicyScope(String policyScope) {
1566 this.policyScope = policyScope;
1569 public String getPriority() {
1572 public void setPriority(String priority) {
1573 this.priority = priority;
1575 public String getVersion() {
1578 public void setVersion(String version) {
1579 this.version = version;
1581 private Object content;
1584 public String getPolicyName() {
1587 public void setPolicyName(String policyName) {
1588 this.policyName = policyName;
1590 public String getDescription() {
1593 public void setDescription(String description) {
1594 this.description = description;
1596 public String getConfigName() {
1599 public void setConfigName(String configName) {
1600 this.configName = configName;
1602 public Object getContent() {
1605 public void setContent(Object content) {
1606 this.content = content;
1609 public String getService() {
1612 public void setService(String service) {
1613 this.service = service;
1615 public String getLocation() {
1618 public void setLocation(String location) {
1619 this.location = location;
1622 public String getUuid() {
1625 public void setUuid(String uuid) {
1628 public String getTemplateVersion() {
1629 return templateVersion;
1631 public void setTemplateVersion(String templateVersion) {
1632 this.templateVersion = templateVersion;