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.io.StringReader;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Enumeration;
41 import java.util.HashMap;
42 import java.util.HashSet;
43 import java.util.Iterator;
44 import java.util.LinkedHashMap;
45 import java.util.LinkedList;
46 import java.util.List;
48 import java.util.Map.Entry;
50 import java.util.TreeMap;
51 import java.util.UUID;
52 import java.util.zip.ZipEntry;
53 import java.util.zip.ZipFile;
55 import javax.json.Json;
56 import javax.json.JsonArray;
57 import javax.json.JsonArrayBuilder;
58 import javax.json.JsonObject;
59 import javax.json.JsonObjectBuilder;
60 import javax.json.JsonReader;
61 import javax.json.JsonValue;
62 import javax.servlet.http.HttpServletRequest;
63 import javax.servlet.http.HttpServletResponse;
65 import org.apache.commons.compress.utils.IOUtils;
66 import org.apache.commons.fileupload.FileItem;
67 import org.apache.commons.fileupload.FileUploadException;
68 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
69 import org.apache.commons.fileupload.servlet.ServletFileUpload;
70 import org.apache.commons.io.FileUtils;
71 import org.apache.commons.lang.StringUtils;
72 import org.json.JSONArray;
73 import org.json.JSONObject;
74 import org.onap.policy.common.logging.flexlogger.FlexLogger;
75 import org.onap.policy.common.logging.flexlogger.Logger;
76 import org.onap.policy.controller.PolicyController;
77 import org.onap.policy.rest.XACMLRestProperties;
78 import org.onap.policy.rest.adapter.PolicyRestAdapter;
79 import org.onap.policy.rest.dao.CommonClassDao;
80 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
81 import org.onap.policy.rest.jpa.MicroServiceModels;
82 import org.onap.policy.rest.jpa.PolicyEntity;
83 import org.onap.policy.rest.util.MSAttributeObject;
84 import org.onap.policy.rest.util.MSModelUtils;
85 import org.onap.policy.rest.util.MSModelUtils.MODEL_TYPE;
86 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
87 import org.openecomp.portalsdk.core.web.support.JsonMessage;
88 import org.springframework.beans.factory.annotation.Autowired;
89 import org.springframework.http.MediaType;
90 import org.springframework.stereotype.Controller;
91 import org.springframework.web.bind.annotation.RequestMapping;
92 import org.springframework.web.servlet.ModelAndView;
93 import org.yaml.snakeyaml.Yaml;
95 import com.att.research.xacml.util.XACMLProperties;
96 import com.fasterxml.jackson.core.JsonProcessingException;
97 import com.fasterxml.jackson.databind.DeserializationFeature;
98 import com.fasterxml.jackson.databind.JsonNode;
99 import com.fasterxml.jackson.databind.ObjectMapper;
100 import com.fasterxml.jackson.databind.ObjectWriter;
101 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
102 import com.fasterxml.jackson.databind.node.ObjectNode;
103 import com.google.gson.Gson;
105 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
106 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
107 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
108 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
109 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
110 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
111 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
115 public class CreateDcaeMicroServiceController extends RestrictedBaseController {
116 private static final Logger LOGGER = FlexLogger.getLogger(CreateDcaeMicroServiceController.class);
118 private static CommonClassDao commonClassDao;
120 public static CommonClassDao getCommonClassDao() {
121 return commonClassDao;
124 public static void setCommonClassDao(CommonClassDao commonClassDao) {
125 CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
128 private MicroServiceModels newModel;
129 private String newFile;
130 private String directory;
131 private List<String> modelList = new ArrayList<>();
132 private List<String> dirDependencyList = new ArrayList<>();
133 private HashMap<String,MSAttributeObject > classMap = new HashMap<>();
134 //Tosca Model related Datastructure.
135 String referenceAttributes;
136 String attributeString;
137 String listConstraints;
138 String subAttributeString;
139 HashMap<String, Object> retmap = new HashMap<>();
140 Set<String> uniqueKeys= new HashSet<>();
141 Set<String> uniqueDataKeys= new HashSet<>();
142 StringBuilder dataListBuffer=new StringBuilder();
143 List<String> dataConstraints= new ArrayList <>();
145 public static final String DATATYPE = "data_types.policy.data.";
146 public static final String PROPERTIES=".properties.";
147 public static final String TYPE=".type";
148 public static final String STRING="string";
149 public static final String INTEGER="integer";
150 public static final String LIST="list";
151 public static final String DEFAULT=".default";
152 public static final String REQUIRED=".required";
153 public static final String MANYFALSE=":MANY-false";
157 private CreateDcaeMicroServiceController(CommonClassDao commonClassDao){
158 CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
161 public CreateDcaeMicroServiceController(){}
163 protected PolicyRestAdapter policyAdapter = null;
164 private int priorityCount;
165 private Map<String, String> attributesListRefMap = new HashMap<>();
166 private Map<String, LinkedList<String>> arrayTextList = new HashMap<>();
168 public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
170 String jsonContent = null;
172 LOGGER.info("policyJSON :" + (root.get("policyJSON")).toString());
174 String tempJson = root.get("policyJSON").toString();
176 //---replace empty value with the value below before calling decodeContent method.
177 String dummyValue = "*empty-value*" + UUID.randomUUID().toString();
178 LOGGER.info("dummyValue:" + dummyValue);
179 tempJson = StringUtils.replaceEach(tempJson, new String[]{"\"\""}, new String[]{"\""+dummyValue+"\""});
180 ObjectMapper mapper = new ObjectMapper();
181 JsonNode tempJsonNode = mapper.readTree(tempJson);
182 jsonContent = decodeContent(tempJsonNode).toString();
183 constructJson(policyData, jsonContent, dummyValue);
185 LOGGER.error("Error while decoding microservice content", e);
191 private GroupPolicyScopeList getPolicyObject(String policyScope) {
192 GroupPolicyScopeList groupList= (GroupPolicyScopeList) commonClassDao.getEntityItem(GroupPolicyScopeList.class, "name", policyScope);
196 private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue) {
197 ObjectWriter om = new ObjectMapper().writer();
199 DCAEMicroServiceObject microServiceObject = new DCAEMicroServiceObject();
200 MicroServiceModels returnModel = new MicroServiceModels();
201 microServiceObject.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_MS));
202 if(policyAdapter.getServiceType() !=null){
203 microServiceObject.setService(policyAdapter.getServiceType());
204 microServiceObject.setVersion(policyAdapter.getVersion());
205 returnModel = getAttributeObject(microServiceObject.getService(), microServiceObject.getVersion());
207 if (returnModel.getAnnotation()==null || returnModel.getAnnotation().isEmpty()){
208 if(policyAdapter.getUuid()!=null){
209 microServiceObject.setUuid(policyAdapter.getUuid());
211 if(policyAdapter.getLocation()!=null){
212 microServiceObject.setLocation(policyAdapter.getLocation());
214 if(policyAdapter.getConfigName()!=null){
215 microServiceObject.setConfigName(policyAdapter.getConfigName());
217 GroupPolicyScopeList policyScopeValue = getPolicyObject(policyAdapter.getPolicyScope());
218 if(policyScopeValue!=null){
219 microServiceObject.setPolicyScope(policyScopeValue.getGroupList());
223 if(policyAdapter.getPolicyName()!=null){
224 microServiceObject.setPolicyName(policyAdapter.getPolicyName());
226 if(policyAdapter.getPolicyDescription()!=null){
227 microServiceObject.setDescription(policyAdapter.getPolicyDescription());
229 if (policyAdapter.getPriority()!=null){
230 microServiceObject.setPriority(policyAdapter.getPriority());
232 microServiceObject.setPriority("9999");
235 if (policyAdapter.getRiskLevel()!=null){
236 microServiceObject.setRiskLevel(policyAdapter.getRiskLevel());
238 if (policyAdapter.getRiskType()!=null){
239 microServiceObject.setRiskType(policyAdapter.getRiskType());
241 if (policyAdapter.getGuard()!=null){
242 microServiceObject.setGuard(policyAdapter.getGuard());
244 microServiceObject.setContent(jsonContent);
247 json = om.writeValueAsString(microServiceObject);
248 } catch (JsonProcessingException e) {
249 LOGGER.error("Error writing out the object", e);
251 LOGGER.info("input json: " + json);
252 LOGGER.info("input jsonContent: " + jsonContent);
253 String cleanJson = cleanUPJson(json);
254 //--- reset empty value back after called cleanUPJson method and before calling removeNullAttributes
255 String tempJson = StringUtils.replaceEach(cleanJson, new String[]{"\""+dummyValue+"\""}, new String[]{"\"\""});
256 LOGGER.info("tempJson: " + tempJson);
257 cleanJson = removeNullAttributes(tempJson);
258 policyAdapter.setJsonBody(cleanJson);
259 return policyAdapter;
262 private String removeNullAttributes(String cleanJson) {
263 ObjectMapper mapper = new ObjectMapper();
266 JsonNode rootNode = mapper.readTree(cleanJson);
267 JsonNode returnNode = mapper.readTree(cleanJson);
268 Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
269 boolean remove = false;
270 JsonObject removed = null;
271 boolean contentChanged = false;
272 while (fieldsIterator.hasNext()) {
273 Map.Entry<String, JsonNode> field = fieldsIterator.next();
274 final String key = field.getKey();
275 final JsonNode value = field.getValue();
276 if("content".equalsIgnoreCase(key)){
277 String contentStr = value.toString();
278 try(JsonReader reader = Json.createReader(new StringReader(contentStr))){
279 JsonObject jsonContent = reader.readObject();
280 removed = removeNull(jsonContent);
281 if(!jsonContent.toString().equals(removed.toString())){
282 contentChanged = true;
286 if (value==null || value.isNull()){
287 ((ObjectNode) returnNode).remove(key);
292 cleanJson = returnNode.toString();
294 if (value==null || value.isNull()){
295 ((ObjectNode) returnNode).remove(key);
300 cleanJson = returnNode.toString();
304 //set modified content to cleanJson
305 JSONObject jObject = new JSONObject(cleanJson);
306 jObject.put("content",removed.toString());
307 cleanJson = cleanUPJson(jObject.toString());
310 } catch (IOException e) {
311 LOGGER.error("Error writing out the JsonNode",e);
316 public static JsonArray removeNull(JsonArray array) {
317 JsonArrayBuilder builder = Json.createArrayBuilder();
319 for (Iterator<JsonValue> it = array.iterator(); it.hasNext(); ++i) {
320 JsonValue value = it.next();
321 switch (value.getValueType()) {
323 JsonArray a = removeNull(array.getJsonArray(i));
328 JsonObject object = removeNull(array.getJsonObject(i));
329 if (!object.isEmpty())
333 String s = array.getString(i);
334 if (s != null && !s.isEmpty())
338 builder.add(array.getJsonNumber(i));
342 builder.add(array.getBoolean(i));
348 return builder.build();
351 public static JsonObject removeNull(JsonObject obj) {
352 JsonObjectBuilder builder = Json.createObjectBuilder();
353 for (Iterator<Entry<String, JsonValue>> it = obj.entrySet().iterator(); it.hasNext();) {
354 Entry<String, JsonValue> e = it.next();
355 String key = e.getKey();
356 JsonValue value = e.getValue();
357 switch (value.getValueType()) {
359 JsonArray array = removeNull(obj.getJsonArray(key));
360 if (!array.isEmpty())
361 builder.add(key, array);
364 JsonObject object = removeNull(obj.getJsonObject(key));
365 if (!object.isEmpty())
366 builder.add(key, object);
369 String s = obj.getString(key);
370 if (s != null && !s.isEmpty())
374 builder.add(key, obj.getJsonNumber(key));
378 builder.add(key, obj.getBoolean(key));
384 return builder.build();
387 // Second index of dot should be returned.
388 public int stringBetweenDots(String str){
389 String stringToSearch=str;
390 String[]ss=stringToSearch.split("\\.");
394 uniqueKeys.add(ss[2]);
398 return uniqueKeys.size();
401 public void stringBetweenDotsForDataFields(String str){
402 String stringToSearch=str;
403 String[]ss=stringToSearch.split("\\.");
408 uniqueDataKeys.add(ss[0]+"%"+ss[2]);
414 public Map<String, String> load(String fileName) throws IOException {
415 File newConfiguration = new File(fileName);
416 InputStream is = null;
418 is = new FileInputStream(newConfiguration);
419 } catch (FileNotFoundException e) {
423 Yaml yaml = new Yaml();
425 Map<Object, Object> yamlMap = null;
427 yamlMap = (Map<Object, Object>) yaml.load(is);
429 LOGGER.error("load:", e);
431 StringBuilder sb = new StringBuilder();
432 Map<String, String> settings = new HashMap<>();
433 if (yamlMap == null) {
436 List<String> path = new ArrayList <>();
437 serializeMap(settings, sb, path, yamlMap);
441 public Map<String, String> load(byte[] source) throws IOException {
442 Yaml yaml = new Yaml();
443 @SuppressWarnings("unchecked")
444 Map<Object, Object> yamlMap = (Map<Object, Object>) yaml.load(Arrays.toString(source));
445 StringBuilder sb = new StringBuilder();
446 Map<String, String> settings = new HashMap <>();
447 if (yamlMap == null) {
450 List<String> path = new ArrayList <>();
451 serializeMap(settings, sb, path, yamlMap);
455 @SuppressWarnings({ "unchecked", "rawtypes" })
456 private void serializeMap(Map<String, String> settings, StringBuilder sb, List<String> path, Map<Object, Object> yamlMap) {
457 for (Map.Entry<Object, Object> entry : yamlMap.entrySet()) {
458 if (entry.getValue() instanceof Map) {
459 path.add((String) entry.getKey());
460 serializeMap(settings, sb, path, (Map<Object, Object>) entry.getValue());
461 path.remove(path.size() - 1);
462 } else if (entry.getValue() instanceof List) {
463 path.add((String) entry.getKey());
464 serializeList(settings, sb, path, (List) entry.getValue());
465 path.remove(path.size() - 1);
467 serializeValue(settings, sb, path, (String) entry.getKey(), entry.getValue());
472 @SuppressWarnings("unchecked")
473 private void serializeList(Map<String, String> settings, StringBuilder sb, List<String> path, List<String> yamlList) {
475 for (Object listEle : yamlList) {
476 if (listEle instanceof Map) {
477 path.add(Integer.toString(counter));
478 serializeMap(settings, sb, path, (Map<Object, Object>) listEle);
479 path.remove(path.size() - 1);
480 } else if (listEle instanceof List) {
481 path.add(Integer.toString(counter));
482 serializeList(settings, sb, path, (List<String>) listEle);
483 path.remove(path.size() - 1);
485 serializeValue(settings, sb, path, Integer.toString(counter), listEle);
491 private void serializeValue(Map<String, String> settings, StringBuilder sb, List<String> path, String name, Object value) {
496 for (String pathEle : path) {
497 sb.append(pathEle).append('.');
500 settings.put(sb.toString(), value.toString());
503 void parseDataAndPolicyNodes(Map<String,String> map){
504 for(String key:map.keySet()){
505 if(key.contains("policy.nodes.Root"))
509 else if(key.contains("policy.nodes")){
510 String wordToFind = "policy.nodes.";
511 int indexForPolicyNode=key.indexOf(wordToFind);
512 String subNodeString= key.substring(indexForPolicyNode+13, key.length());
514 stringBetweenDots(subNodeString);
516 else if(key.contains("policy.data")){
517 String wordToFind="policy.data.";
518 int indexForPolicyNode=key.indexOf(wordToFind);
519 String subNodeString= key.substring(indexForPolicyNode+12, key.length());
521 stringBetweenDotsForDataFields(subNodeString);
526 HashMap<String,String> parseDataNodes(Map<String,String> map){
527 HashMap<String,String> dataMapForJson=new HashMap <>();
528 for(String uniqueDataKey: uniqueDataKeys){
529 if(uniqueDataKey.contains("%")){
530 String[] uniqueDataKeySplit= uniqueDataKey.split("%");
531 String findType=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+TYPE;
532 String typeValue=map.get(findType);
533 LOGGER.info(typeValue);
534 if(typeValue != null && typeValue.equalsIgnoreCase(STRING)||
535 typeValue.equalsIgnoreCase(INTEGER)
538 String findDefault=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+DEFAULT;
539 String defaultValue= map.get(findDefault);
540 LOGGER.info("defaultValue is:"+ defaultValue);
542 String findRequired=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+REQUIRED;
543 String requiredValue= map.get(findRequired);
544 LOGGER.info("requiredValue is:"+ requiredValue);
546 StringBuilder attributeIndividualStringBuilder= new StringBuilder();
547 attributeIndividualStringBuilder.append(typeValue+":defaultValue-");
548 attributeIndividualStringBuilder.append(defaultValue+":required-");
549 attributeIndividualStringBuilder.append(requiredValue+MANYFALSE);
550 dataMapForJson.put(uniqueDataKey, attributeIndividualStringBuilder.toString());
552 else if(typeValue != null && typeValue.equalsIgnoreCase(LIST)){
553 String findList= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.type";
554 String listValue=map.get(findList);
556 LOGGER.info("Type of list is:"+ listValue);
558 if(listValue.contains(".")){
559 String trimValue=listValue.substring(listValue.lastIndexOf('.')+1);
560 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
561 referenceIndividualStringBuilder.append(trimValue+":MANY-true");
562 dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
565 StringBuilder stringListItems= new StringBuilder();
566 stringListItems.append(uniqueDataKeySplit[1].toUpperCase()+":MANY-false");
567 dataMapForJson.put(uniqueDataKey, stringListItems.toString());
568 dataListBuffer.append(uniqueDataKeySplit[1].toUpperCase()+"=[");
569 for(int i=0;i<10;i++){
570 String findConstraints= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.constraints.0.valid_values."+i;
571 String constraintsValue=map.get(findConstraints);
572 LOGGER.info(constraintsValue);
573 if(constraintsValue==null){
577 dataConstraints.add(constraintsValue);
578 dataListBuffer.append(constraintsValue+",");
581 dataListBuffer.append("]#");
583 LOGGER.info(dataListBuffer);
588 String findUserDefined="data_types.policy.data."+uniqueDataKeySplit[0]+"."+"properties"+"."+uniqueDataKeySplit[1]+".type";
589 String userDefinedValue=map.get(findUserDefined);
590 String trimValue=userDefinedValue.substring(userDefinedValue.lastIndexOf('.')+1);
591 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
592 referenceIndividualStringBuilder.append(trimValue+":MANY-false");
593 dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
598 return dataMapForJson;
601 void constructJsonForDataFields(HashMap<String,String> dataMapForJson){
602 HashMap<String,HashMap<String,String>> dataMapKey= new HashMap <>();
603 HashMap<String,String> hmSub;
604 for(Map.Entry<String, String> entry: dataMapForJson.entrySet()){
605 String uniqueDataKey= entry.getKey();
606 String[] uniqueDataKeySplit=uniqueDataKey.split("%");
607 String value= dataMapForJson.get(uniqueDataKey);
608 if(dataMapKey.containsKey(uniqueDataKeySplit[0])){
609 hmSub = dataMapKey.get(uniqueDataKeySplit[0]);
610 hmSub.put(uniqueDataKeySplit[1], value);
613 hmSub=new HashMap <>();
614 hmSub.put(uniqueDataKeySplit[1], value);
617 dataMapKey.put(uniqueDataKeySplit[0], hmSub);
620 JSONObject mainObject= new JSONObject();
622 for(Map.Entry<String,HashMap<String,String>> entry: dataMapKey.entrySet()){
623 String s=entry.getKey();
624 json= new JSONObject();
625 HashMap<String,String> jsonHm=dataMapKey.get(s);
626 for(Map.Entry<String,String> entryMap:jsonHm.entrySet()){
627 String key=entryMap.getKey();
628 json.put(key, jsonHm.get(key));
630 mainObject.put(s,json);
632 Iterator<String> keysItr = mainObject.keys();
633 while(keysItr.hasNext()) {
634 String key = keysItr.next();
635 String value = mainObject.get(key).toString();
636 retmap.put(key, value);
639 LOGGER.info("#############################################################################");
640 LOGGER.info(mainObject);
641 LOGGER.info("###############################################################################");
645 HashMap<String,HashMap<String,String>> parsePolicyNodes(Map<String,String> map){
646 HashMap<String,HashMap<String,String>> mapKey= new HashMap <>();
647 for(String uniqueKey: uniqueKeys){
648 HashMap<String,String> hm;
650 for(Map.Entry<String,String> entry:map.entrySet()){
651 String key=entry.getKey();
652 if(key.contains(uniqueKey) && key.contains("policy.nodes")){
653 if(mapKey.containsKey(uniqueKey)){
654 hm = mapKey.get(uniqueKey);
655 String keyStr= key.substring(key.lastIndexOf('.')+1);
656 String valueStr= map.get(key);
657 if(("type").equals(keyStr)){
658 if(!key.contains("entry_schema"))
660 hm.put(keyStr,valueStr);
663 hm.put(keyStr,valueStr);
667 hm = new HashMap <>();
668 String keyStr= key.substring(key.lastIndexOf('.')+1);
669 String valueStr= map.get(key);
670 if(("type").equals(keyStr)){
671 if(!key.contains("entry_schema"))
673 hm.put(keyStr,valueStr);
676 hm.put(keyStr,valueStr);
678 mapKey.put(uniqueKey, hm);
686 void createAttributes(HashMap<String,HashMap<String,String>> mapKey){
687 StringBuilder attributeStringBuilder= new StringBuilder();
688 StringBuilder referenceStringBuilder= new StringBuilder();
689 StringBuilder listBuffer= new StringBuilder();
690 List<String> constraints= new ArrayList<>();
691 for(Map.Entry<String,HashMap<String,String>> entry: mapKey.entrySet()){
692 String keySetString= entry.getKey();
693 HashMap<String,String> keyValues=mapKey.get(keySetString);
694 if(keyValues.get("type") != null && keyValues.get("type").equalsIgnoreCase(STRING)||
695 keyValues.get("type") != null && keyValues.get("type").equalsIgnoreCase(INTEGER)
697 StringBuilder attributeIndividualStringBuilder= new StringBuilder();
698 attributeIndividualStringBuilder.append(keySetString+"=");
699 attributeIndividualStringBuilder.append(keyValues.get("type")+":defaultValue-");
700 attributeIndividualStringBuilder.append(keyValues.get("default")+":required-");
701 attributeIndividualStringBuilder.append(keyValues.get("required")+":MANY-false");
702 attributeStringBuilder.append(attributeIndividualStringBuilder+",");
705 else if(keyValues.get("type") != null && keyValues.get("type").equalsIgnoreCase(LIST)){
707 Set<String> keys= keyValues.keySet();
708 Iterator<String> itr=keys.iterator();
709 while(itr.hasNext()){
710 String key= itr.next();
711 if((!("type").equals(key) ||("required").equals(key)))
713 String value= keyValues.get(key);
714 //The "." in the value determines if its a string or a user defined type.
715 if (!value.contains(".")){
717 constraints.add(keyValues.get(key));
719 //This is userdefined string
720 String trimValue=value.substring(value.lastIndexOf('.')+1);
721 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
722 referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-true");
723 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
729 //User defined Datatype.
730 String value=keyValues.get("type");
731 if(value != null && !value.isEmpty()){
732 String trimValue=value.substring(value.lastIndexOf('.')+1);
733 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
734 referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-false");
735 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
737 LOGGER.info("keyValues.get(type) is null/empty");
741 if(constraints!=null &&constraints.isEmpty()==false){
743 listBuffer.append(keySetString.toUpperCase()+"=[");
744 for(String str:constraints){
745 listBuffer.append(str+",");
747 listBuffer.append("]#");
748 LOGGER.info(listBuffer);
751 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
752 referenceIndividualStringBuilder.append(keySetString+"="+keySetString.toUpperCase()+":MANY-false");
753 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
758 dataListBuffer.append(listBuffer);
761 LOGGER.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
762 LOGGER.info("Whole attribute String is:"+attributeStringBuilder);
763 LOGGER.info("Whole reference String is:"+referenceStringBuilder);
764 LOGGER.info("List String is:"+listBuffer);
765 LOGGER.info("Data list buffer is:"+dataListBuffer);
766 LOGGER.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
768 this.listConstraints=dataListBuffer.toString();
769 this.referenceAttributes=referenceStringBuilder.toString();
770 this.attributeString=attributeStringBuilder.toString();
775 public void parseTosca (String fileName){
776 Map<String,String> map= new HashMap<>();
781 parseDataAndPolicyNodes(map);
783 HashMap<String,String> dataMapForJson=parseDataNodes(map);
785 constructJsonForDataFields(dataMapForJson);
787 HashMap<String,HashMap<String,String>> mapKey= parsePolicyNodes(map);
789 createAttributes(mapKey);
791 } catch (IOException e) {
797 private String cleanUPJson(String json) {
798 String cleanJson = StringUtils.replaceEach(json, new String[]{"\\\\", "\\\\\\", "\\\\\\\\"}, new String[]{"\\", "\\", "\\"});
799 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\"}, new String[]{"\\"});
800 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\", "[[", "]]"}, new String[]{"\\", "[", "]"});
802 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\"", "\\\"", "\"[{", "}]\""}, new String[]{"\"", "\"", "[{", "}]"});
803 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[{", "}]\""}, new String[]{"[{", "}]"});
804 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[", "]\""}, new String[]{"[", "]"});
805 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"{", "}\""}, new String[]{"{", "}"});
806 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\"\"", "\"\""}, new String[]{"\"", "\""});
807 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\""}, new String[]{""});
808 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\""}, new String[]{"\""});
809 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\\\\\\"}, new String[]{"\""});
810 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\\""}, new String[]{"\""});
811 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[", "]\""}, new String[]{"[", "]"});
815 private JSONObject decodeContent(JsonNode jsonNode){
816 Iterator<JsonNode> jsonElements = jsonNode.elements();
817 Iterator<String> jsonKeys = jsonNode.fieldNames();
818 Map<String,String> element = new TreeMap<>();
819 while(jsonElements.hasNext() && jsonKeys.hasNext()){
820 element.put(jsonKeys.next(), jsonElements.next().toString());
822 JSONObject jsonResult = new JSONObject();
823 JSONArray jsonArray = null;
824 String oldValue = null;
825 String nodeKey = null;
826 String arryKey = null;
827 Boolean isArray = false;
828 JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
829 ObjectNode node = nodeFactory.objectNode();
830 String prevKey = null;
831 String presKey = null;
832 for(String key: element.keySet()){
833 if(key.contains(".")){
834 presKey = key.substring(0,key.indexOf("."));
835 }else if(key.contains("@")){
836 presKey = key.substring(0,key.indexOf("@"));
840 // first check if we are different from old.
841 LOGGER.info(key+"\n");
842 if(jsonArray!=null && jsonArray.length()>0 && key.contains("@") && !key.contains(".") && oldValue!=null){
843 if(!oldValue.equals(key.substring(0,key.indexOf("@")))){
844 jsonResult.put(oldValue, jsonArray);
845 jsonArray = new JSONArray();
847 }else if(jsonArray!=null && jsonArray.length()>0 && !presKey.equals(prevKey) && oldValue!=null){
848 jsonResult.put(oldValue, jsonArray);
850 jsonArray = new JSONArray();
855 if(key.contains(".")){
857 nodeKey = key.substring(0,key.indexOf("."));
859 if(nodeKey.equals(key.substring(0,key.indexOf(".")))){
860 node.put(key.substring(key.indexOf(".")+1), element.get(key));
863 if(nodeKey.contains("@")){
865 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
867 if(nodeKey.endsWith("@0")){
869 jsonArray = new JSONArray();
871 if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
872 jsonArray.put(decodeContent(node));
874 if((key.contains("@") && !arryKey.equals(key.substring(0,nodeKey.indexOf("@")))) || !key.contains("@")){
875 jsonResult.put(arryKey, jsonArray);
876 jsonArray = new JSONArray();
878 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
881 jsonResult.put(nodeKey, decodeContent(node));
883 node = nodeFactory.objectNode();
885 nodeKey = key.substring(0,key.indexOf("."));
886 if(nodeKey.contains("@")){
887 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
889 node.put(key.substring(key.indexOf(".")+1), element.get(key));
891 }else if(node.size()!=0){
892 if(nodeKey.contains("@")){
894 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
896 if(nodeKey.endsWith("@0")){
898 jsonArray = new JSONArray();
900 if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
901 jsonArray.put(decodeContent(node));
903 jsonResult.put(arryKey, jsonArray);
904 jsonArray = new JSONArray();
905 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
908 jsonResult.put(nodeKey, decodeContent(node));
910 node = nodeFactory.objectNode();
911 if(key.contains("@")){
913 if(key.endsWith("@0")|| jsonArray==null){
914 jsonArray = new JSONArray();
916 }else if(!key.contains("@")){
921 oldValue = key.substring(0,key.indexOf("@"));
923 if(oldValue!=prevKey){
924 oldValue = key.substring(0,key.indexOf("@"));
926 if(oldValue.equals(key.substring(0,key.indexOf("@")))){
927 jsonArray.put(element.get(key));
929 jsonResult.put(oldValue, jsonArray);
930 jsonArray = new JSONArray();
932 oldValue = key.substring(0,key.indexOf("@"));
934 jsonResult.put(key, element.get(key));
937 if(key.contains("@")){
939 if(key.endsWith("@0")|| jsonArray==null){
940 jsonArray = new JSONArray();
942 }else if(!key.contains("@")){
947 oldValue = key.substring(0,key.indexOf("@"));
949 if(oldValue!=prevKey){
950 oldValue = key.substring(0,key.indexOf("@"));
952 if(oldValue.equals(key.substring(0,key.indexOf("@")))){
953 jsonArray.put(element.get(key));
955 jsonResult.put(oldValue, jsonArray);
956 jsonArray = new JSONArray();
958 oldValue = key.substring(0,key.indexOf("@"));
960 jsonResult.put(key, element.get(key));
965 if(nodeKey.contains("@")){
967 jsonArray = new JSONArray();
970 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
972 jsonArray.put(decodeContent(node));
973 jsonResult.put(arryKey, jsonArray);
976 jsonResult.put(nodeKey, decodeContent(node));
979 if(isArray && jsonArray.length() > 0){
980 jsonResult.put(oldValue, jsonArray);
985 @RequestMapping(value={"/policyController/getDCAEMSTemplateData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
986 public ModelAndView getDCAEMSTemplateData(HttpServletRequest request, HttpServletResponse response) throws IOException{
987 ObjectMapper mapper = new ObjectMapper();
988 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
989 JsonNode root = mapper.readTree(request.getReader());
991 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
992 String servicename = value.toString().split("-v")[0];
993 String version = null;
994 if (value.toString().contains("-v")){
995 version = value.toString().split("-v")[1];
997 MicroServiceModels returnModel = getAttributeObject(servicename, version);
1000 //get all keys with "MANY-true" defined in their value from subAttribute
1001 Set<String> allkeys = null;
1002 if(returnModel.getSub_attributes() != null && !returnModel.getSub_attributes().isEmpty()){
1003 JSONObject json = new JSONObject(returnModel.getSub_attributes());
1004 allkeys = getAllKeys(json);
1005 LOGGER.info("allkeys : " + allkeys);
1008 String allManyTrueKeys = "";
1009 if(allkeys != null){
1010 allManyTrueKeys = allkeys.toString();
1013 String jsonModel = createMicroSeriveJson(returnModel, allkeys);
1015 JSONObject jsonObject = new JSONObject(jsonModel);
1017 JSONObject finalJsonObject = null;
1018 if(allkeys != null){
1019 Iterator<String> iter = allkeys.iterator();
1020 while(iter.hasNext()){
1021 //convert to array values for MANY-true keys
1022 finalJsonObject = convertToArrayElement(jsonObject, iter.next());
1026 if(finalJsonObject != null){
1027 LOGGER.info(finalJsonObject.toString());
1028 jsonModel = finalJsonObject.toString();
1031 //get all properties with "MANY-true" defined in Ref_attributes
1032 Set<String> manyTrueProperties = getManyTrueProperties(returnModel.getRef_attributes());
1033 if(manyTrueProperties != null){
1034 JSONObject jsonObj = new JSONObject(jsonModel);
1035 for (String s : manyTrueProperties) {
1037 //convert to array element for MANY-true properties
1038 finalJsonObject = convertToArrayElement(jsonObj, s.trim());
1041 if(finalJsonObject != null){
1042 LOGGER.info(finalJsonObject.toString());
1043 jsonModel = finalJsonObject.toString();
1047 response.setCharacterEncoding("UTF-8");
1048 response.setContentType("application / json");
1049 request.setCharacterEncoding("UTF-8");
1050 List<Object> list = new ArrayList<>();
1051 PrintWriter out = response.getWriter();
1052 String responseString = mapper.writeValueAsString(returnModel);
1053 JSONObject j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",allManyTrueKeys: " + allManyTrueKeys+ "}");
1055 out.write(list.toString());
1059 @SuppressWarnings({ "unchecked", "rawtypes" })
1060 private String createMicroSeriveJson(MicroServiceModels returnModel, Set<String> allkeys) {
1061 Map<String, String> attributeMap = new HashMap<>();
1062 Map<String, String> refAttributeMap = new HashMap<>();
1063 String attribute = returnModel.getAttributes();
1064 if(attribute != null){
1065 attribute = attribute.trim();
1067 String refAttribute = returnModel.getRef_attributes();
1068 if(refAttribute != null){
1069 refAttribute = refAttribute.trim();
1071 String enumAttribute = returnModel.getEnumValues();
1072 if(enumAttribute != null){
1073 enumAttribute = enumAttribute.trim();
1075 if (!StringUtils.isEmpty(attribute)){
1076 attributeMap = convert(attribute, ",");
1078 if (!StringUtils.isEmpty(refAttribute)){
1079 refAttributeMap = convert(refAttribute, ",");
1082 Gson gson = new Gson();
1084 String subAttributes = returnModel.getSub_attributes();
1085 if(subAttributes != null){
1086 subAttributes = subAttributes.trim();
1090 Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
1092 JSONObject object = new JSONObject();
1093 JSONArray array = new JSONArray();
1095 for (Entry<String, String> keySet : attributeMap.entrySet()){
1096 array = new JSONArray();
1097 String value = keySet.getValue();
1098 if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
1100 object.put(keySet.getKey().trim(), array);
1102 object.put(keySet.getKey().trim(), value.trim());
1106 for (Entry<String, String> keySet : refAttributeMap.entrySet()){
1107 array = new JSONArray();
1108 String value = keySet.getValue().split(":")[0];
1109 if (gsonObject.containsKey(value)){
1110 if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
1111 array.put(recursiveReference(value, gsonObject, enumAttribute));
1112 object.put(keySet.getKey().trim(), array);
1114 object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
1117 if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
1118 array.put(value.trim());
1119 object.put(keySet.getKey().trim(), array);
1121 object.put(keySet.getKey().trim(), value.trim());
1128 return object.toString();
1131 @SuppressWarnings("unchecked")
1132 private JSONObject recursiveReference(String name, Map<String,String> subAttributeMap, String enumAttribute) {
1133 JSONObject object = new JSONObject();
1134 Map<String, String> map = new HashMap<>();
1135 Object returnClass = subAttributeMap.get(name);
1136 map = (Map<String, String>) returnClass;
1137 JSONArray array = new JSONArray();
1139 for( Entry<String, String> m:map.entrySet()){
1140 String[] splitValue = m.getValue().split(":");
1141 array = new JSONArray();
1142 if (subAttributeMap.containsKey(splitValue[0])){
1143 if (m.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
1144 array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
1145 object.put(m.getKey().trim(), array);
1147 object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
1150 if (m.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
1151 array.put(splitValue[0].trim());
1152 object.put(m.getKey().trim(), array);
1154 object.put(m.getKey().trim(), splitValue[0].trim());
1162 //call this method to check if the key is in the many-true key set
1163 private boolean isKeyFound(Set<String> allManyTruekeys, String key){
1165 if(allManyTruekeys != null && key != null){
1166 Iterator<String> iter = allManyTruekeys.iterator();
1167 while(iter.hasNext()){
1168 if(key.equals(iter.next())){
1176 public static JSONObject convertToArrayElement(JSONObject json, String keyValue) {
1177 return convertToArrayElement(json, new HashSet<>(), keyValue);
1180 private static JSONObject convertToArrayElement(JSONObject json, Set<String> keys, String keyValue) {
1181 for (String key : json.keySet()) {
1182 Object obj = json.get(key);
1183 if(key.equals(keyValue.trim())){
1184 if(!(obj instanceof JSONArray)){
1185 JSONArray newJsonArray = new JSONArray();
1186 newJsonArray.put(obj);
1187 json.put(key, newJsonArray);
1189 LOGGER.info("key : " + key);
1190 LOGGER.info("obj : " + obj);
1191 LOGGER.info("json.get(key) : " + json.get(key));
1192 LOGGER.info("keyValue : " + keyValue);
1193 keys.addAll(json.keySet());
1198 if (obj instanceof JSONObject) convertToArrayElement(json.getJSONObject(key), keyValue);
1204 // call this method to get all MANY-true properties
1205 public static Set<String> getManyTrueProperties(String referAttributes){
1206 LOGGER.info("referAttributes : " + referAttributes);
1207 Set<String> manyTrueProperties = new HashSet<>();
1209 if(referAttributes != null){
1210 String[] referAarray = referAttributes.split(",");
1211 String []element= null;
1212 for(int i=0; i<referAarray.length; i++){
1213 element = referAarray[i].split("=");
1214 if(element.length > 1 && element[1].contains("MANY-true")){
1215 manyTrueProperties.add(element[0]);
1220 return manyTrueProperties;
1223 //call this method to start the recursive
1224 private Set<String> getAllKeys(JSONObject json) {
1225 return getAllKeys(json, new HashSet<>());
1228 private Set<String> getAllKeys(JSONArray arr) {
1229 return getAllKeys(arr, new HashSet<>());
1232 private Set<String> getAllKeys(JSONArray arr, Set<String> keys) {
1233 for (int i = 0; i < arr.length(); i++) {
1234 Object obj = arr.get(i);
1235 if (obj instanceof JSONObject) keys.addAll(getAllKeys(arr.getJSONObject(i)));
1236 if (obj instanceof JSONArray) keys.addAll(getAllKeys(arr.getJSONArray(i)));
1241 // this method returns a set of keys with "MANY-true" defined in their value.
1242 private Set<String> getAllKeys(JSONObject json, Set<String> keys) {
1243 for (String key : json.keySet()) {
1244 Object obj = json.get(key);
1245 if(obj instanceof String && ((String) obj).contains("MANY-true")){
1246 LOGGER.info("key : " + key);
1247 LOGGER.info("obj : " + obj);
1248 keys.addAll(json.keySet());
1250 if (obj instanceof JSONObject) keys.addAll(getAllKeys(json.getJSONObject(key)));
1251 if (obj instanceof JSONArray) keys.addAll(getAllKeys(json.getJSONArray(key)));
1258 @RequestMapping(value={"/policyController/getModelServiceVersioneData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
1259 public ModelAndView getModelServiceVersionData(HttpServletRequest request, HttpServletResponse response) throws IOException{
1260 ObjectMapper mapper = new ObjectMapper();
1261 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
1262 JsonNode root = mapper.readTree(request.getReader());
1264 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
1265 String servicename = value.toString().split("-v")[0];
1266 Set<String> returnList = getVersionList(servicename);
1268 response.setCharacterEncoding("UTF-8");
1269 response.setContentType("application / json");
1270 request.setCharacterEncoding("UTF-8");
1271 List<Object> list = new ArrayList<>();
1272 PrintWriter out = response.getWriter();
1273 String responseString = mapper.writeValueAsString(returnList);
1274 JSONObject j = new JSONObject("{dcaeModelVersionData: " + responseString +"}");
1276 out.write(list.toString());
1280 private Set<String> getVersionList(String name) {
1281 MicroServiceModels workingModel = new MicroServiceModels();
1282 Set<String> list = new HashSet<>();
1283 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
1284 for (int i = 0; i < microServiceModelsData.size(); i++) {
1285 workingModel = (MicroServiceModels) microServiceModelsData.get(i);
1286 if (workingModel.getVersion()!=null){
1287 list.add(workingModel.getVersion());
1289 list.add("Default");
1295 private MicroServiceModels getAttributeObject(String name, String version) {
1296 MicroServiceModels workingModel = new MicroServiceModels();
1297 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
1298 for (int i = 0; i < microServiceModelsData.size(); i++) {
1299 workingModel = (MicroServiceModels) microServiceModelsData.get(i);
1300 if(version != null){
1301 if (workingModel.getVersion()!=null){
1302 if (workingModel.getVersion().equals(version)){
1303 return workingModel;
1306 return workingModel;
1309 return workingModel;
1313 return workingModel;
1316 @RequestMapping(value={"/get_DCAEPriorityValues"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
1317 public void getDCAEPriorityValuesData(HttpServletRequest request, HttpServletResponse response){
1319 Map<String, Object> model = new HashMap<>();
1320 ObjectMapper mapper = new ObjectMapper();
1321 List<String> priorityList = new ArrayList<>();
1323 for (int i = 1; i < priorityCount; i++) {
1324 priorityList.add(String.valueOf(i));
1326 model.put("priorityDatas", mapper.writeValueAsString(priorityList));
1327 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
1328 JSONObject j = new JSONObject(msg);
1329 response.getWriter().write(j.toString());
1331 catch (Exception e){
1336 public void prePopulateDCAEMSPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1337 if (policyAdapter.getPolicyData() instanceof PolicyType) {
1338 Object policyData = policyAdapter.getPolicyData();
1339 PolicyType policy = (PolicyType) policyData;
1340 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
1341 String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("MS_") +3);
1342 policyAdapter.setPolicyName(policyNameValue);
1343 String description = "";
1345 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
1346 }catch(Exception e){
1347 LOGGER.error("Error while collecting the desciption tag in ActionPolicy " + policyNameValue ,e);
1348 description = policy.getDescription();
1350 policyAdapter.setPolicyDescription(description);
1351 // Get the target data under policy.
1352 TargetType target = policy.getTarget();
1353 if (target != null) {
1354 // Under target we have AnyOFType
1355 List<AnyOfType> anyOfList = target.getAnyOf();
1356 if (anyOfList != null) {
1357 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
1358 while (iterAnyOf.hasNext()) {
1359 AnyOfType anyOf = iterAnyOf.next();
1360 // Under AnyOFType we have AllOFType
1361 List<AllOfType> allOfList = anyOf.getAllOf();
1362 if (allOfList != null) {
1363 Iterator<AllOfType> iterAllOf = allOfList.iterator();
1364 while (iterAllOf.hasNext()) {
1365 AllOfType allOf = iterAllOf.next();
1366 // Under AllOFType we have Match
1367 List<MatchType> matchList = allOf.getMatch();
1368 if (matchList != null) {
1369 Iterator<MatchType> iterMatch = matchList.iterator();
1370 while (matchList.size()>1 && iterMatch.hasNext()) {
1371 MatchType match = iterMatch.next();
1373 // Under the match we have attribute value and
1374 // attributeDesignator. So,finally down to the actual attribute.
1376 AttributeValueType attributeValue = match.getAttributeValue();
1377 String value = (String) attributeValue.getContent().get(0);
1378 AttributeDesignatorType designator = match.getAttributeDesignator();
1379 String attributeId = designator.getAttributeId();
1380 // First match in the target is OnapName, so set that value.
1381 if (attributeId.equals("ONAPName")) {
1382 policyAdapter.setOnapName(value);
1384 if (attributeId.equals("ConfigName")){
1385 policyAdapter.setConfigName(value);
1387 if (attributeId.equals("uuid")){
1388 policyAdapter.setUuid(value);
1390 if (attributeId.equals("location")){
1391 policyAdapter.setLocation(value);
1393 if (attributeId.equals("RiskType")){
1394 policyAdapter.setRiskType(value);
1396 if (attributeId.equals("RiskLevel")){
1397 policyAdapter.setRiskLevel(value);
1399 if (attributeId.equals("guard")){
1400 policyAdapter.setGuard(value);
1402 if (attributeId.equals("TTLDate") && !value.contains("NA")){
1403 PolicyController controller = new PolicyController();
1404 String newDate = controller.convertDate(value);
1405 policyAdapter.setTtlDate(newDate);
1408 readFile(policyAdapter, entity);
1418 public static Map<String, String> convert(String str, String split) {
1419 Map<String, String> map = new HashMap<>();
1420 for(final String entry : str.split(split)) {
1421 String[] parts = entry.split("=");
1422 map.put(parts[0], parts[1]);
1428 @SuppressWarnings("unchecked")
1429 private void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1430 String policyScopeName = null;
1431 ObjectMapper mapper = new ObjectMapper();
1433 DCAEMicroServiceObject msBody = (DCAEMicroServiceObject) mapper.readValue(entity.getConfigurationData().getConfigBody(), DCAEMicroServiceObject.class);
1434 policyScopeName = getPolicyScope(msBody.getPolicyScope());
1435 policyAdapter.setPolicyScope(policyScopeName);
1437 policyAdapter.setPriority(msBody.getPriority());
1439 if (msBody.getVersion()!= null){
1440 policyAdapter.setServiceType(msBody.getService());
1441 policyAdapter.setVersion(msBody.getVersion());
1443 policyAdapter.setServiceType(msBody.getService());
1445 if(msBody.getContent() != null){
1446 LinkedHashMap<String, Object> data = new LinkedHashMap<>();
1447 LinkedHashMap<String, ?> map = (LinkedHashMap<String, ?>) msBody.getContent();
1448 readRecursivlyJSONContent(map, data);
1449 policyAdapter.setRuleData(data);
1452 } catch (Exception e) {
1458 @SuppressWarnings({ "rawtypes", "unchecked" })
1459 private void readRecursivlyJSONContent(LinkedHashMap<String, ?> map, LinkedHashMap<String, Object> data){
1460 for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
1461 Object key = iterator.next();
1462 Object value = map.get(key);
1463 if(value instanceof LinkedHashMap<?, ?>){
1464 LinkedHashMap<String, Object> secondObjec = new LinkedHashMap<>();
1465 readRecursivlyJSONContent((LinkedHashMap<String, ?>) value, secondObjec);
1466 for(String objKey: secondObjec.keySet()){
1467 data.put(key+"." +objKey, secondObjec.get(objKey));
1469 }else if(value instanceof ArrayList){
1470 ArrayList<?> jsonArrayVal = (ArrayList<?>)value;
1471 for(int i = 0; i < jsonArrayVal.size(); i++){
1472 Object arrayvalue = jsonArrayVal.get(i);
1473 if(arrayvalue instanceof LinkedHashMap<?, ?>){
1474 LinkedHashMap<String, Object> newData = new LinkedHashMap<>();
1475 readRecursivlyJSONContent((LinkedHashMap<String, ?>) arrayvalue, newData);
1476 for(String objKey: newData.keySet()){
1477 data.put(key+"@"+i+"." +objKey, newData.get(objKey));
1479 }else if(arrayvalue instanceof ArrayList){
1480 ArrayList<?> jsonArrayVal1 = (ArrayList<?>)value;
1481 for(int j = 0; j < jsonArrayVal1.size(); j++){
1482 Object arrayvalue1 = jsonArrayVal1.get(i);
1483 data.put(key+"@"+j, arrayvalue1.toString());
1486 data.put(key+"@"+i, arrayvalue.toString());
1490 data.put(key.toString(), value.toString());
1495 private String getPolicyScope(String value) {
1496 List<Object> groupList= commonClassDao.getDataById(GroupPolicyScopeList.class, "groupList", value);
1497 if(groupList != null && !groupList.isEmpty()){
1498 GroupPolicyScopeList pScope = (GroupPolicyScopeList) groupList.get(0);
1499 return pScope.getGroupName();
1504 //Convert the map values and set into JSON body
1505 public Map<String, String> convertMap(Map<String, String> attributesMap, Map<String, String> attributesRefMap) {
1506 Map<String, String> attribute = new HashMap<>();
1510 for (Entry<String, String> entry : attributesMap.entrySet()) {
1511 key = entry.getKey();
1512 value = entry.getValue();
1513 attribute.put(key, value);
1515 for (Entry<String, String> entryRef : attributesRefMap.entrySet()) {
1516 key = entryRef.getKey();
1517 value = entryRef.getValue().toString();
1518 attribute.put(key, value);
1520 for (Entry<String, String> entryList : attributesListRefMap.entrySet()) {
1521 key = entryList.getKey();
1522 value = entryList.getValue().toString();
1523 attribute.put(key, value);
1525 for (Entry<String, LinkedList<String>> arrayList : arrayTextList.entrySet()){
1526 key = arrayList.getKey();
1528 for (Object textList : arrayList.getValue()){
1530 temp = "[" + textList;
1532 temp = temp + "," + textList;
1535 attribute.put(key, temp+ "]");
1541 @RequestMapping(value={"/ms_dictionary/set_MSModelData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
1542 public void SetMSModelData(HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException{
1543 modelList = new ArrayList<>();
1544 dirDependencyList = new ArrayList<>();
1545 classMap = new HashMap<>();
1546 retmap = new HashMap<>();
1547 uniqueKeys= new HashSet<>();
1548 uniqueDataKeys= new HashSet<>();
1549 dataListBuffer=new StringBuilder();
1550 dataConstraints= new ArrayList <>();
1551 List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
1552 boolean zip = false;
1554 String errorMsg = "";
1555 for (FileItem item : items) {
1556 if(item.getName().endsWith(".zip") || item.getName().endsWith(".xmi")||item.getName().endsWith(".yml")){
1557 this.newModel = new MicroServiceModels();
1559 File file = new File(item.getName());
1560 OutputStream outputStream = new FileOutputStream(file);
1561 IOUtils.copy(item.getInputStream(), outputStream);
1562 outputStream.close();
1563 this.newFile = file.toString();
1564 this.newModel.setModelName(this.newFile.toString().split("-v")[0]);
1566 if (this.newFile.toString().contains("-v")){
1567 if (item.getName().endsWith(".zip")){
1568 this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".zip", ""));
1570 }else if(item.getName().endsWith(".yml")){
1571 this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".yml", ""));
1575 this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".xmi", ""));
1578 errorMsg = "Upload error: The file name should contain '-v', such as xxx-v1802.yml";
1581 }catch(Exception e){
1582 LOGGER.error("Upload error : ", e);
1583 errorMsg = "Upload error:" + e.getMessage();
1589 if(!errorMsg.isEmpty()){
1591 PrintWriter out = response.getWriter();
1593 response.setCharacterEncoding("UTF-8");
1594 response.setContentType("application / json");
1595 request.setCharacterEncoding("UTF-8");
1597 ObjectMapper mapper = new ObjectMapper();
1598 JSONObject j = new JSONObject();
1599 j.put("errorMsg", errorMsg);
1600 out.write(j.toString());
1604 List<File> fileList = new ArrayList<>();;
1605 this.directory = "model";
1607 extractFolder(this.newFile);
1608 fileList = listModelFiles(this.directory);
1609 }else if (yml==true){
1610 parseTosca(this.newFile);
1612 File file = new File(this.newFile);
1615 String modelType= "";
1618 //Process Main Model file first
1619 classMap = new HashMap<>();
1620 for (File file : fileList) {
1621 if(!file.isDirectory() && file.getName().endsWith(".xmi")){
1622 retreiveDependency(file.toString(), true);
1626 modelList = createList();
1628 cleanUp(this.newFile);
1632 modelList.add(this.newModel.getModelName());
1633 String className=this.newModel.getModelName();
1634 MSAttributeObject msAttributes= new MSAttributeObject();
1635 msAttributes.setClassName(className);
1637 HashMap<String, String> returnAttributeList =new HashMap<>();
1638 returnAttributeList.put(className, this.attributeString);
1639 msAttributes.setAttribute(returnAttributeList);
1641 msAttributes.setSubClass(this.retmap);
1643 HashMap<String, String> returnReferenceList =new HashMap<>();
1644 //String[] referenceArray=this.referenceAttributes.split("=");
1645 returnReferenceList.put(className, this.referenceAttributes);
1646 msAttributes.setRefAttribute(returnReferenceList);
1648 if(this.listConstraints!=""){
1649 HashMap<String, String> enumList =new HashMap<>();
1650 String[] listArray=this.listConstraints.split("#");
1651 for(String str:listArray){
1652 String[] strArr= str.split("=");
1653 if(strArr.length>1){
1654 enumList.put(strArr[0], strArr[1]);
1657 msAttributes.setEnumType(enumList);
1660 classMap=new HashMap<>();
1661 classMap.put(className, msAttributes);
1665 PrintWriter out = response.getWriter();
1667 response.setCharacterEncoding("UTF-8");
1668 response.setContentType("application / json");
1669 request.setCharacterEncoding("UTF-8");
1671 ObjectMapper mapper = new ObjectMapper();
1672 JSONObject j = new JSONObject();
1673 j.put("classListDatas", modelList);
1674 j.put("modelDatas", mapper.writeValueAsString(classMap));
1675 j.put("modelType", modelType);
1676 out.write(j.toString());
1680 * Unzip file and store in the model directory for processing
1682 @SuppressWarnings("rawtypes")
1683 private void extractFolder(String zipFile ) {
1685 File file = new File(zipFile);
1689 zip = new ZipFile(file);
1690 String newPath = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
1691 this.directory = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
1692 checkZipDirectory(this.directory);
1693 new File(newPath).mkdir();
1694 Enumeration zipFileEntries = zip.entries();
1696 // Process each entry
1697 while (zipFileEntries.hasMoreElements()){
1698 // grab a zip file entry
1699 ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
1700 String currentEntry = entry.getName();
1701 File destFile = new File("model" + File.separator + currentEntry);
1702 File destinationParent = destFile.getParentFile();
1704 destinationParent.mkdirs();
1706 if (!entry.isDirectory()){
1707 BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
1709 byte data[] = new byte[BUFFER];
1710 FileOutputStream fos = new FileOutputStream(destFile);
1711 BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
1712 while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
1713 dest.write(data, 0, currentByte);
1720 if (currentEntry.endsWith(".zip")){
1721 extractFolder(destFile.getAbsolutePath());
1724 } catch (IOException e) {
1725 LOGGER.error("Failed to unzip model file " + zipFile, e);
1730 } catch (IOException e) {
1731 LOGGER.error("Exception Occured While closing zipfile " + e);
1736 private void retreiveDependency(String workingFile, Boolean modelClass) {
1738 MSModelUtils utils = new MSModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName());
1739 HashMap<String, MSAttributeObject> tempMap = new HashMap<>();
1741 tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
1743 classMap.putAll(tempMap);
1744 LOGGER.info(tempMap);
1750 private List<File> listModelFiles(String directoryName) {
1751 File directory = new File(directoryName);
1752 List<File> resultList = new ArrayList<>();
1753 File[] fList = directory.listFiles();
1754 for (File file : fList) {
1755 if (file.isFile()) {
1756 resultList.add(file);
1757 } else if (file.isDirectory()) {
1758 dirDependencyList.add(file.getName());
1759 resultList.addAll(listModelFiles(file.getAbsolutePath()));
1765 private void cleanUp(String path) {
1768 FileUtils.forceDelete(new File(path));
1769 } catch (IOException e) {
1770 LOGGER.error("Failed to delete folder " + path, e);
1775 private void checkZipDirectory(String zipDirectory) {
1776 Path path = Paths.get(zipDirectory);
1778 if (Files.exists(path)) {
1779 cleanUp(zipDirectory);
1783 private List<String> createList() {
1784 List<String> list = new ArrayList<>();
1785 for (Entry<String, MSAttributeObject> cMap : classMap.entrySet()){
1786 if (cMap.getValue().isPolicyTempalate()){
1787 list.add(cMap.getKey());
1792 if (list.isEmpty()){
1793 if (classMap.containsKey(this.newModel.getModelName())){
1794 list.add(this.newModel.getModelName());
1802 public Map<String, String> getAttributesListRefMap() {
1803 return attributesListRefMap;
1806 public Map<String, LinkedList<String>> getArrayTextList() {
1807 return arrayTextList;
1812 class DCAEMicroServiceObject {
1814 private String service;
1815 private String location;
1816 private String uuid;
1817 private String policyName;
1818 private String description;
1819 private String configName;
1820 private String templateVersion;
1821 private String version;
1822 private String priority;
1823 private String policyScope;
1824 private String riskType;
1825 private String riskLevel;
1826 private String guard = null;
1828 public String getGuard() {
1831 public void setGuard(String guard) {
1834 public String getRiskType() {
1837 public void setRiskType(String riskType) {
1838 this.riskType = riskType;
1840 public String getRiskLevel() {
1843 public void setRiskLevel(String riskLevel) {
1844 this.riskLevel = riskLevel;
1846 public String getPolicyScope() {
1849 public void setPolicyScope(String policyScope) {
1850 this.policyScope = policyScope;
1853 public String getPriority() {
1856 public void setPriority(String priority) {
1857 this.priority = priority;
1859 public String getVersion() {
1862 public void setVersion(String version) {
1863 this.version = version;
1865 private Object content;
1868 public String getPolicyName() {
1871 public void setPolicyName(String policyName) {
1872 this.policyName = policyName;
1874 public String getDescription() {
1877 public void setDescription(String description) {
1878 this.description = description;
1880 public String getConfigName() {
1883 public void setConfigName(String configName) {
1884 this.configName = configName;
1886 public Object getContent() {
1889 public void setContent(Object content) {
1890 this.content = content;
1893 public String getService() {
1896 public void setService(String service) {
1897 this.service = service;
1899 public String getLocation() {
1902 public void setLocation(String location) {
1903 this.location = location;
1906 public String getUuid() {
1909 public void setUuid(String uuid) {
1912 public String getTemplateVersion() {
1913 return templateVersion;
1915 public void setTemplateVersion(String templateVersion) {
1916 this.templateVersion = templateVersion;