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.FileOutputStream;
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.io.PrintWriter;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.Enumeration;
36 import java.util.HashMap;
37 import java.util.HashSet;
38 import java.util.Iterator;
39 import java.util.LinkedHashMap;
40 import java.util.LinkedList;
41 import java.util.List;
43 import java.util.Map.Entry;
45 import java.util.TreeMap;
46 import java.util.zip.ZipEntry;
47 import java.util.zip.ZipFile;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
52 import org.apache.commons.compress.utils.IOUtils;
53 import org.apache.commons.fileupload.FileItem;
54 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
55 import org.apache.commons.fileupload.servlet.ServletFileUpload;
56 import org.apache.commons.io.FileUtils;
57 import org.apache.commons.lang.StringUtils;
58 import org.json.JSONArray;
59 import org.json.JSONObject;
60 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
61 import org.openecomp.policy.common.logging.flexlogger.Logger;
62 import org.openecomp.policy.rest.XACMLRestProperties;
63 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
64 import org.openecomp.policy.rest.dao.CommonClassDao;
65 import org.openecomp.policy.rest.jpa.GroupPolicyScopeList;
66 import org.openecomp.policy.rest.jpa.MicroServiceModels;
67 import org.openecomp.policy.rest.jpa.PolicyEntity;
68 import org.openecomp.policy.rest.util.MSAttributeObject;
69 import org.openecomp.policy.rest.util.MSModelUtils;
70 import org.openecomp.policy.rest.util.MSModelUtils.MODEL_TYPE;
71 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
72 import org.openecomp.portalsdk.core.web.support.JsonMessage;
73 import org.springframework.beans.factory.annotation.Autowired;
74 import org.springframework.http.MediaType;
75 import org.springframework.stereotype.Controller;
76 import org.springframework.web.bind.annotation.RequestMapping;
77 import org.springframework.web.servlet.ModelAndView;
79 import com.att.research.xacml.util.XACMLProperties;
80 import com.fasterxml.jackson.core.JsonProcessingException;
81 import com.fasterxml.jackson.databind.DeserializationFeature;
82 import com.fasterxml.jackson.databind.JsonNode;
83 import com.fasterxml.jackson.databind.ObjectMapper;
84 import com.fasterxml.jackson.databind.ObjectWriter;
85 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
86 import com.fasterxml.jackson.databind.node.ObjectNode;
87 import com.google.gson.Gson;
89 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
90 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
91 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
92 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
93 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
94 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
98 public class CreateDcaeMicroServiceController extends RestrictedBaseController {
99 private static final Logger logger = FlexLogger.getLogger(CreateDcaeMicroServiceController.class);
101 private static CommonClassDao commonClassDao;
102 private MicroServiceModels newModel;
103 private String newFile;
104 private String directory;
105 private List<String> modelList = new ArrayList<String>();
106 private List<String> dirDependencyList = new ArrayList<String>();
107 private HashMap<String,MSAttributeObject > classMap = new HashMap<String,MSAttributeObject>();
110 private CreateDcaeMicroServiceController(CommonClassDao commonClassDao){
111 CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
114 public CreateDcaeMicroServiceController(){}
116 protected PolicyRestAdapter policyAdapter = null;
117 private int priorityCount;
118 private Map<String, String> attributesListRefMap = new HashMap<String, String>();
119 private Map<String, LinkedList<String>> arrayTextList = new HashMap<String, LinkedList<String>>();
121 public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
123 String jsonContent = null;
125 jsonContent = decodeContent(root.get("policyJSON")).toString();
126 constructJson(policyData, jsonContent);
128 logger.error("Error while decoding microservice content");
134 private GroupPolicyScopeList getPolicyObject(String policyScope) {
135 GroupPolicyScopeList groupList= (GroupPolicyScopeList) commonClassDao.getEntityItem(GroupPolicyScopeList.class, "name", policyScope);
139 private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent) {
140 ObjectWriter om = new ObjectMapper().writer();
142 DCAEMicroServiceObject microServiceObject = new DCAEMicroServiceObject();
143 MicroServiceModels returnModel = new MicroServiceModels();
144 microServiceObject.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_MS));
145 if(policyAdapter.getServiceType() !=null){
146 microServiceObject.setService(policyAdapter.getServiceType());
147 microServiceObject.setVersion(policyAdapter.getVersion());
148 returnModel = getAttributeObject(microServiceObject.getService(), microServiceObject.getVersion());
150 if (returnModel.getAnnotation()==null || returnModel.getAnnotation().isEmpty()){
151 if(policyAdapter.getUuid()!=null){
152 microServiceObject.setUuid(policyAdapter.getUuid());
154 if(policyAdapter.getLocation()!=null){
155 microServiceObject.setLocation(policyAdapter.getLocation());
157 if(policyAdapter.getConfigName()!=null){
158 microServiceObject.setConfigName(policyAdapter.getConfigName());
160 GroupPolicyScopeList policyScopeValue = getPolicyObject(policyAdapter.getPolicyScope());
161 if(policyScopeValue!=null){
162 microServiceObject.setPolicyScope(policyScopeValue.getGroupList());
166 if(policyAdapter.getPolicyName()!=null){
167 microServiceObject.setPolicyName(policyAdapter.getPolicyName());
169 if(policyAdapter.getPolicyDescription()!=null){
170 microServiceObject.setDescription(policyAdapter.getPolicyDescription());
172 if (policyAdapter.getPriority()!=null){
173 microServiceObject.setPriority(policyAdapter.getPriority());
175 microServiceObject.setPriority("9999");
178 if (policyAdapter.getRiskLevel()!=null){
179 microServiceObject.setRiskLevel(policyAdapter.getRiskLevel());
181 if (policyAdapter.getRiskType()!=null){
182 microServiceObject.setRiskType(policyAdapter.getRiskType());
184 if (policyAdapter.getGuard()!=null){
185 microServiceObject.setGuard(policyAdapter.getGuard());
187 microServiceObject.setContent(jsonContent);
190 json = om.writeValueAsString(microServiceObject);
191 } catch (JsonProcessingException e) {
192 logger.error("Error writing out the object");
194 System.out.println(json);
195 String cleanJson = cleanUPJson(json);
196 cleanJson = removeNullAttributes(cleanJson);
197 policyAdapter.setJsonBody(cleanJson);
198 return policyAdapter;
201 private String removeNullAttributes(String cleanJson) {
202 ObjectMapper mapper = new ObjectMapper();
205 JsonNode rootNode = mapper.readTree(cleanJson);
206 JsonNode returnNode = mapper.readTree(cleanJson);
207 Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
208 boolean remove = false;
209 while (fieldsIterator.hasNext()) {
210 Map.Entry<String, JsonNode> field = fieldsIterator.next();
211 final String key = field.getKey();
212 final JsonNode value = field.getValue();
213 if (value==null || value.isNull()){
214 ((ObjectNode) returnNode).remove(key);
219 cleanJson = returnNode.toString();
221 } catch (IOException e) {
222 logger.error("Error writing out the JsonNode");
228 private String cleanUPJson(String json) {
229 String cleanJson = StringUtils.replaceEach(json, new String[]{"\\\\", "\\\\\\", "\\\\\\\\"}, new String[]{"\\", "\\", "\\"});
230 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\"}, new String[]{"\\"});
231 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\", "[[", "]]"}, new String[]{"\\", "[", "]"});
233 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\"", "\\\"", "\"[{", "}]\""}, new String[]{"\"", "\"", "[{", "}]"});
234 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[{", "}]\""}, new String[]{"[{", "}]"});
235 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[", "]\""}, new String[]{"[", "]"});
236 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"{", "}\""}, new String[]{"{", "}"});
237 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\"\"", "\"\""}, new String[]{"\"", "\""});
238 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\""}, new String[]{""});
239 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\""}, new String[]{"\""});
240 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\\\\\\"}, new String[]{"\""});
241 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\\""}, new String[]{"\""});
242 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[", "]\""}, new String[]{"[", "]"});
246 private JSONObject decodeContent(JsonNode jsonNode){
247 Iterator<JsonNode> jsonElements = jsonNode.elements();
248 Iterator<String> jsonKeys = jsonNode.fieldNames();
249 Map<String,String> element = new TreeMap<String,String>();
250 while(jsonElements.hasNext() && jsonKeys.hasNext()){
251 element.put(jsonKeys.next(), jsonElements.next().toString());
253 JSONObject jsonResult = new JSONObject();
254 JSONArray jsonArray = null;
255 String oldValue = null;
256 String nodeKey = null;
257 String arryKey = null;
258 Boolean isArray = false;
259 JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
260 ObjectNode node = nodeFactory.objectNode();
261 String prevKey = null;
262 String presKey = null;
263 for(String key: element.keySet()){
264 if(key.contains(".")){
265 presKey = key.substring(0,key.indexOf("."));
266 }else if(key.contains("@")){
267 presKey = key.substring(0,key.indexOf("@"));
271 // first check if we are different from old.
272 System.out.println(key+"\n");
273 if(jsonArray!=null && jsonArray.length()>0 && key.contains("@") && !key.contains(".") && oldValue!=null){
274 if(!oldValue.equals(key.substring(0,key.indexOf("@")))){
275 jsonResult.put(oldValue, jsonArray);
276 jsonArray = new JSONArray();
278 }else if(jsonArray!=null && jsonArray.length()>0 && !presKey.equals(prevKey) && oldValue!=null){
279 jsonResult.put(oldValue, jsonArray);
281 jsonArray = new JSONArray();
286 if(key.contains(".")){
288 nodeKey = key.substring(0,key.indexOf("."));
290 if(nodeKey.equals(key.substring(0,key.indexOf(".")))){
291 node.put(key.substring(key.indexOf(".")+1), element.get(key));
294 if(nodeKey.contains("@")){
296 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
298 if(nodeKey.endsWith("@0")){
300 jsonArray = new JSONArray();
302 if(arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
303 jsonArray.put(decodeContent(node));
305 if(key.contains("@") && !arryKey.equals(key.substring(0,nodeKey.indexOf("@")))){
306 jsonResult.put(arryKey, jsonArray);
307 jsonArray = new JSONArray();
308 }else if(!key.contains("@")){
309 jsonResult.put(arryKey, jsonArray);
310 jsonArray = new JSONArray();
312 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
315 jsonResult.put(nodeKey, decodeContent(node));
317 node = nodeFactory.objectNode();
319 nodeKey = key.substring(0,key.indexOf("."));
320 if(nodeKey.contains("@")){
321 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
323 node.put(key.substring(key.indexOf(".")+1), element.get(key));
325 }else if(node.size()!=0){
326 if(nodeKey.contains("@")){
328 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
330 if(nodeKey.endsWith("@0")){
332 jsonArray = new JSONArray();
334 if(arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
335 jsonArray.put(decodeContent(node));
337 jsonResult.put(arryKey, jsonArray);
338 jsonArray = new JSONArray();
339 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
342 jsonResult.put(nodeKey, decodeContent(node));
344 node = nodeFactory.objectNode();
345 if(key.contains("@")){
347 if(key.endsWith("@0")|| jsonArray==null){
348 jsonArray = new JSONArray();
350 }else if(!key.contains("@")){
355 oldValue = key.substring(0,key.indexOf("@"));
357 if(oldValue!=prevKey){
358 oldValue = key.substring(0,key.indexOf("@"));
360 if(oldValue.equals(key.substring(0,key.indexOf("@")))){
361 jsonArray.put(element.get(key));
363 jsonResult.put(oldValue, jsonArray);
364 jsonArray = new JSONArray();
366 oldValue = key.substring(0,key.indexOf("@"));
368 jsonResult.put(key, element.get(key));
371 if(key.contains("@")){
373 if(key.endsWith("@0")|| jsonArray==null){
374 jsonArray = new JSONArray();
376 }else if(!key.contains("@")){
381 oldValue = key.substring(0,key.indexOf("@"));
383 if(oldValue!=prevKey){
384 oldValue = key.substring(0,key.indexOf("@"));
386 if(oldValue.equals(key.substring(0,key.indexOf("@")))){
387 jsonArray.put(element.get(key));
389 jsonResult.put(oldValue, jsonArray);
390 jsonArray = new JSONArray();
392 oldValue = key.substring(0,key.indexOf("@"));
394 jsonResult.put(key, element.get(key));
399 if(nodeKey.contains("@")){
401 jsonArray = new JSONArray();
404 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
406 jsonArray.put(decodeContent(node));
407 jsonResult.put(arryKey, jsonArray);
410 jsonResult.put(nodeKey, decodeContent(node));
413 if(isArray && jsonArray.length() > 0){
414 jsonResult.put(oldValue, jsonArray);
419 @RequestMapping(value={"/policyController/getDCAEMSTemplateData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
420 public ModelAndView getDCAEMSTemplateData(HttpServletRequest request, HttpServletResponse response) throws Exception{
421 ObjectMapper mapper = new ObjectMapper();
422 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
423 JsonNode root = mapper.readTree(request.getReader());
425 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
426 String servicename = value.toString().split("-v")[0];
427 String version = null;
428 if (value.toString().contains("-v")){
429 version = value.toString().split("-v")[1];
431 MicroServiceModels returnModel = getAttributeObject(servicename, version);
433 String jsonModel = createMicroSeriveJson(returnModel);
435 response.setCharacterEncoding("UTF-8");
436 response.setContentType("application / json");
437 request.setCharacterEncoding("UTF-8");
438 List<Object> list = new ArrayList<>();
439 PrintWriter out = response.getWriter();
440 String responseString = mapper.writeValueAsString(returnModel);
441 JSONObject j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + "}");
443 out.write(list.toString());
447 @SuppressWarnings("unchecked")
448 private String createMicroSeriveJson(MicroServiceModels returnModel) {
449 Map<String, String> attributeMap = new HashMap<String, String>();
450 Map<String, String> refAttributeMap = new HashMap<String, String>();
451 String attribute = returnModel.getAttributes();
452 if(attribute != null){
453 attribute = attribute.trim();
455 String refAttribute = returnModel.getRef_attributes();
456 if(refAttribute != null){
457 refAttribute = refAttribute.trim();
459 String enumAttribute = returnModel.getEnumValues();
460 if(enumAttribute != null){
461 enumAttribute = enumAttribute.trim();
463 if (!StringUtils.isEmpty(attribute)){
464 attributeMap = convert(attribute, ",");
466 if (!StringUtils.isEmpty(refAttribute)){
467 refAttributeMap = convert(refAttribute, ",");
470 Gson gson = new Gson();
472 String subAttributes = returnModel.getSub_attributes();
473 if(subAttributes != null){
474 subAttributes = subAttributes.trim();
478 Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
480 JSONObject object = new JSONObject();
481 JSONArray array = new JSONArray();
483 for (Entry<String, String> keySet : attributeMap.entrySet()){
484 array = new JSONArray();
485 String value = keySet.getValue();
486 if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
488 object.put(keySet.getKey().trim(), array);
490 object.put(keySet.getKey().trim(), value.trim());
494 for (Entry<String, String> keySet : refAttributeMap.entrySet()){
495 array = new JSONArray();
496 String value = keySet.getValue().split(":")[0];
497 if (gsonObject.containsKey(value)){
498 if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
499 array.put(recursiveReference(value, gsonObject, enumAttribute));
500 object.put(keySet.getKey().trim(), array);
502 object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
505 if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
506 array.put(value.trim());
507 object.put(keySet.getKey().trim(), array);
509 object.put(keySet.getKey().trim(), value.trim());
514 return object.toString();
517 @SuppressWarnings("unchecked")
518 private JSONObject recursiveReference(String name, Map<String,String> subAttributeMap, String enumAttribute) {
519 JSONObject object = new JSONObject();
520 Map<String, String> map = new HashMap<String, String>();
521 Object returnClass = subAttributeMap.get(name);
522 map = (Map<String, String>) returnClass;
523 JSONArray array = new JSONArray();
525 for( Entry<String, String> m:map.entrySet()){
526 String[] splitValue = m.getValue().split(":");
527 array = new JSONArray();
528 if (subAttributeMap.containsKey(splitValue[0])){
529 if (m.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
530 array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
531 object.put(m.getKey().trim(), array);
533 object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
536 if (m.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
537 array.put(splitValue[0].trim());
538 object.put(m.getKey().trim(), array);
540 object.put(m.getKey().trim(), splitValue[0].trim());
549 @RequestMapping(value={"/policyController/getModelServiceVersioneData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
550 public ModelAndView getModelServiceVersionData(HttpServletRequest request, HttpServletResponse response) throws Exception{
551 ObjectMapper mapper = new ObjectMapper();
552 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
553 JsonNode root = mapper.readTree(request.getReader());
555 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
556 String servicename = value.toString().split("-v")[0];
557 Set<String> returnList = getVersionList(servicename);
559 response.setCharacterEncoding("UTF-8");
560 response.setContentType("application / json");
561 request.setCharacterEncoding("UTF-8");
562 List<Object> list = new ArrayList<>();
563 PrintWriter out = response.getWriter();
564 String responseString = mapper.writeValueAsString(returnList);
565 JSONObject j = new JSONObject("{dcaeModelVersionData: " + responseString +"}");
567 out.write(list.toString());
571 private Set<String> getVersionList(String name) {
572 MicroServiceModels workingModel = new MicroServiceModels();
573 Set<String> list = new HashSet<String>();
574 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
575 for (int i = 0; i < microServiceModelsData.size(); i++) {
576 workingModel = (MicroServiceModels) microServiceModelsData.get(i);
577 if (workingModel.getVersion()!=null){
578 list.add(workingModel.getVersion());
586 private MicroServiceModels getAttributeObject(String name, String version) {
587 MicroServiceModels workingModel = new MicroServiceModels();
588 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
589 for (int i = 0; i < microServiceModelsData.size(); i++) {
590 workingModel = (MicroServiceModels) microServiceModelsData.get(i);
592 if (workingModel.getVersion()!=null){
593 if (workingModel.getVersion().equals(version)){
607 @RequestMapping(value={"/get_DCAEPriorityValues"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
608 public void getDCAEPriorityValuesData(HttpServletRequest request, HttpServletResponse response){
610 Map<String, Object> model = new HashMap<String, Object>();
611 ObjectMapper mapper = new ObjectMapper();
612 List<String> priorityList = new ArrayList<String>();
614 for (int i = 1; i < priorityCount; i++) {
615 priorityList.add(String.valueOf(i));
617 model.put("priorityDatas", mapper.writeValueAsString(priorityList));
618 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
619 JSONObject j = new JSONObject(msg);
620 response.getWriter().write(j.toString());
627 public void prePopulateDCAEMSPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
628 if (policyAdapter.getPolicyData() instanceof PolicyType) {
629 Object policyData = policyAdapter.getPolicyData();
630 PolicyType policy = (PolicyType) policyData;
631 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
632 String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("MS_") +3);
633 policyAdapter.setPolicyName(policyNameValue);
634 String description = "";
636 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
638 description = policy.getDescription();
640 policyAdapter.setPolicyDescription(description);
641 // Get the target data under policy.
642 TargetType target = policy.getTarget();
643 if (target != null) {
644 // Under target we have AnyOFType
645 List<AnyOfType> anyOfList = target.getAnyOf();
646 if (anyOfList != null) {
647 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
648 while (iterAnyOf.hasNext()) {
649 AnyOfType anyOf = iterAnyOf.next();
650 // Under AnyOFType we have AllOFType
651 List<AllOfType> allOfList = anyOf.getAllOf();
652 if (allOfList != null) {
653 Iterator<AllOfType> iterAllOf = allOfList.iterator();
654 while (iterAllOf.hasNext()) {
655 AllOfType allOf = iterAllOf.next();
656 // Under AllOFType we have Match
657 List<MatchType> matchList = allOf.getMatch();
658 if (matchList != null) {
660 Iterator<MatchType> iterMatch = matchList.iterator();
661 while (matchList.size()>1 && iterMatch.hasNext()) {
662 MatchType match = iterMatch.next();
664 // Under the match we have attributevalue and
665 // attributeDesignator. So,finally down to the actual attribute.
667 AttributeValueType attributeValue = match.getAttributeValue();
668 String value = (String) attributeValue.getContent().get(0);
670 // First match in the target is EcompName, so set that value.
672 policyAdapter.setEcompName(value);
675 policyAdapter.setConfigName(value);
679 readFile(policyAdapter, entity);
683 policyAdapter.setUuid(value);
686 policyAdapter.setLocation(value);
689 policyAdapter.setRiskType(value);
693 policyAdapter.setRiskLevel(value);
697 policyAdapter.setGuard(value);
699 if (index == 8 && !value.contains("NA")){
700 String newDate = convertDate(value, true);
701 policyAdapter.setTtlDate(newDate);
714 private String convertDate(String dateTTL, boolean portalType) {
715 String formateDate = null;
716 String[] date = dateTTL.split("T");
717 String[] parts = date[0].split("-");
719 formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];
723 public static Map<String, String> convert(String str, String split) {
724 Map<String, String> map = new HashMap<String, String>();
725 for(final String entry : str.split(split)) {
726 String[] parts = entry.split("=");
727 map.put(parts[0], parts[1]);
733 @SuppressWarnings("unchecked")
734 private void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
735 String policyScopeName = null;
736 ObjectMapper mapper = new ObjectMapper();
738 DCAEMicroServiceObject msBody = (DCAEMicroServiceObject) mapper.readValue(entity.getConfigurationData().getConfigBody(), DCAEMicroServiceObject.class);
739 policyScopeName = getPolicyScope(msBody.getPolicyScope());
740 policyAdapter.setPolicyScope(policyScopeName);
742 policyAdapter.setPriority(msBody.getPriority());
744 if (msBody.getVersion()!= null){
745 policyAdapter.setServiceType(msBody.getService());
746 policyAdapter.setVersion(msBody.getVersion());
748 policyAdapter.setServiceType(msBody.getService());
750 if(msBody.getContent() != null){
751 LinkedHashMap<String, Object> data = new LinkedHashMap<String, Object>();
752 LinkedHashMap<String, ?> map = (LinkedHashMap<String, ?>) msBody.getContent();
753 readRecursivlyJSONContent(map, data);
754 policyAdapter.setRuleData(data);
757 } catch (Exception e) {
763 @SuppressWarnings({ "rawtypes", "unchecked" })
764 private void readRecursivlyJSONContent(LinkedHashMap<String, ?> map, LinkedHashMap<String, Object> data){
765 for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
766 Object key = iterator.next();
767 Object value = map.get(key);
768 if(value instanceof LinkedHashMap<?, ?>){
769 LinkedHashMap<String, Object> secondObjec = new LinkedHashMap<String, Object>();
770 readRecursivlyJSONContent((LinkedHashMap<String, ?>) value, secondObjec);
771 for(String objKey: secondObjec.keySet()){
772 data.put(key+"." +objKey, secondObjec.get(objKey));
774 }else if(value instanceof ArrayList){
775 ArrayList<?> jsonArrayVal = (ArrayList<?>)value;
776 for(int i = 0; i < jsonArrayVal.size(); i++){
777 Object arrayvalue = jsonArrayVal.get(i);
778 if(arrayvalue instanceof LinkedHashMap<?, ?>){
779 LinkedHashMap<String, Object> newData = new LinkedHashMap<String, Object>();
780 readRecursivlyJSONContent((LinkedHashMap<String, ?>) arrayvalue, newData);
781 for(String objKey: newData.keySet()){
782 data.put(key+"@"+i+"." +objKey, newData.get(objKey));
784 }else if(arrayvalue instanceof ArrayList){
785 ArrayList<?> jsonArrayVal1 = (ArrayList<?>)value;
786 for(int j = 0; j < jsonArrayVal1.size(); j++){
787 Object arrayvalue1 = jsonArrayVal1.get(i);
788 data.put(key+"@"+j, arrayvalue1.toString());
791 data.put(key+"@"+i, arrayvalue.toString());
795 data.put(key.toString(), value.toString());
800 private String getPolicyScope(String value) {
801 GroupPolicyScopeList pScope = new GroupPolicyScopeList();
802 List<Object> groupList= commonClassDao.getData(GroupPolicyScopeList.class);
803 if(groupList.size() > 0){
804 for(int i = 0 ; i < groupList.size() ; i ++){
805 pScope = (GroupPolicyScopeList) groupList.get(i);
806 if (pScope.getGroupList().equals(value)){
811 return pScope.getGroupName();
814 //Convert the map values and set into JSON body
815 public Map<String, String> convertMap(Map<String, String> attributesMap, Map<String, String> attributesRefMap) {
816 Map<String, String> attribute = new HashMap<String, String>();
820 for (Entry<String, String> entry : attributesMap.entrySet()) {
821 key = entry.getKey();
822 value = entry.getValue();
823 attribute.put(key, value);
825 for (Entry<String, String> entryRef : attributesRefMap.entrySet()) {
826 key = entryRef.getKey();
827 value = entryRef.getValue().toString();
828 attribute.put(key, value);
830 for (Entry<String, String> entryList : attributesListRefMap.entrySet()) {
831 key = entryList.getKey();
832 value = entryList.getValue().toString();
833 attribute.put(key, value);
835 for (Entry<String, LinkedList<String>> arrayList : arrayTextList.entrySet()){
836 key = arrayList.getKey();
838 for (Object textList : arrayList.getValue()){
840 temp = "[" + textList;
842 temp = temp + "," + textList;
845 attribute.put(key, temp+ "]");
851 @RequestMapping(value={"/ms_dictionary/set_MSModelData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
852 public void SetMSModelData(HttpServletRequest request, HttpServletResponse response) throws Exception{
853 List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
855 for (FileItem item : items) {
856 if(item.getName().endsWith(".zip") || item.getName().endsWith(".xmi")){
857 this.newModel = new MicroServiceModels();
859 File file = new File(item.getName());
860 OutputStream outputStream = new FileOutputStream(file);
861 IOUtils.copy(item.getInputStream(), outputStream);
862 outputStream.close();
863 this.newFile = file.toString();
864 this.newModel.setModelName(this.newFile.toString().split("-v")[0]);
865 if (this.newFile.toString().contains("-v")){
866 if (item.getName().endsWith(".zip")){
867 this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".zip", ""));
870 this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".xmi", ""));
874 logger.error("Upload error : " + e);
878 List<File> fileList = new ArrayList<File>();;
879 this.directory = "model";
881 extractFolder(this.newFile);
882 fileList = listModelFiles(this.directory);
884 File file = new File(this.newFile);
888 //Process Main Model file first
889 classMap = new HashMap<String,MSAttributeObject>();
890 for (File file : fileList) {
891 if(!file.isDirectory() && file.getName().endsWith(".xmi")){
892 retreiveDependency(file.toString(), true);
896 modelList = createList();
898 cleanUp(this.newFile);
901 PrintWriter out = response.getWriter();
903 response.setCharacterEncoding("UTF-8");
904 response.setContentType("application / json");
905 request.setCharacterEncoding("UTF-8");
907 ObjectMapper mapper = new ObjectMapper();
908 JSONObject j = new JSONObject();
909 j.put("classListDatas", modelList);
910 j.put("modelDatas", mapper.writeValueAsString(classMap));
911 out.write(j.toString());
915 * Unzip file and store in the model directory for processing
917 @SuppressWarnings("rawtypes")
918 private void extractFolder(String zipFile ) {
920 File file = new File(zipFile);
924 zip = new ZipFile(file);
925 String newPath = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
926 this.directory = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
927 checkZipDirectory(this.directory);
928 new File(newPath).mkdir();
929 Enumeration zipFileEntries = zip.entries();
931 // Process each entry
932 while (zipFileEntries.hasMoreElements()){
933 // grab a zip file entry
934 ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
935 String currentEntry = entry.getName();
936 File destFile = new File("model" + File.separator + currentEntry);
937 File destinationParent = destFile.getParentFile();
939 destinationParent.mkdirs();
941 if (!entry.isDirectory()){
942 BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
944 byte data[] = new byte[BUFFER];
945 FileOutputStream fos = new FileOutputStream(destFile);
946 BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
947 while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
948 dest.write(data, 0, currentByte);
955 if (currentEntry.endsWith(".zip")){
956 extractFolder(destFile.getAbsolutePath());
959 } catch (IOException e) {
960 logger.error("Failed to unzip model file " + zipFile);
964 private void retreiveDependency(String workingFile, Boolean modelClass) {
966 MSModelUtils utils = new MSModelUtils(PolicyController.msEcompName, PolicyController.msPolicyName);
967 HashMap<String, MSAttributeObject> tempMap = new HashMap<String, MSAttributeObject>();
969 tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
971 classMap.putAll(tempMap);
972 System.out.println(tempMap);
978 private List<File> listModelFiles(String directoryName) {
979 File directory = new File(directoryName);
980 List<File> resultList = new ArrayList<File>();
981 File[] fList = directory.listFiles();
982 for (File file : fList) {
984 resultList.add(file);
985 } else if (file.isDirectory()) {
986 dirDependencyList.add(file.getName());
987 resultList.addAll(listModelFiles(file.getAbsolutePath()));
993 private void cleanUp(String path) {
996 FileUtils.forceDelete(new File(path));
997 } catch (IOException e) {
998 logger.error("Failed to delete folder " + path);
1003 private void checkZipDirectory(String zipDirectory) {
1004 Path path = Paths.get(zipDirectory);
1006 if (Files.exists(path)) {
1007 cleanUp(zipDirectory);
1011 private List<String> createList() {
1012 List<String> list = new ArrayList<String>();
1013 for (Entry<String, MSAttributeObject> cMap : classMap.entrySet()){
1014 if (cMap.getValue().isPolicyTempalate()){
1015 list.add(cMap.getKey());
1020 if (list.isEmpty()){
1021 if (classMap.containsKey(this.newModel.getModelName())){
1022 list.add(this.newModel.getModelName());
1032 class DCAEMicroServiceObject {
1034 public String service;
1035 public String location;
1037 public String policyName;
1038 public String description;
1039 public String configName;
1040 public String templateVersion;
1041 public String version;
1042 public String priority;
1043 public String policyScope;
1044 public String riskType;
1045 public String riskLevel;
1046 public String guard = null;
1048 public String getGuard() {
1051 public void setGuard(String guard) {
1054 public String getRiskType() {
1057 public void setRiskType(String riskType) {
1058 this.riskType = riskType;
1060 public String getRiskLevel() {
1063 public void setRiskLevel(String riskLevel) {
1064 this.riskLevel = riskLevel;
1066 public String getPolicyScope() {
1069 public void setPolicyScope(String policyScope) {
1070 this.policyScope = policyScope;
1073 public String getPriority() {
1076 public void setPriority(String priority) {
1077 this.priority = priority;
1079 public String getVersion() {
1082 public void setVersion(String version) {
1083 this.version = version;
1085 private Object content;
1088 public String getPolicyName() {
1091 public void setPolicyName(String policyName) {
1092 this.policyName = policyName;
1094 public String getDescription() {
1097 public void setDescription(String description) {
1098 this.description = description;
1100 public String getConfigName() {
1103 public void setConfigName(String configName) {
1104 this.configName = configName;
1106 public Object getContent() {
1109 public void setContent(Object content) {
1110 this.content = content;
1113 public String getService() {
1116 public void setService(String service) {
1117 this.service = service;
1119 public String getLocation() {
1122 public void setLocation(String location) {
1123 this.location = location;
1126 public String getUuid() {
1129 public void setUuid(String uuid) {
1132 public String getTemplateVersion() {
1133 return templateVersion;
1135 public void setTemplateVersion(String templateVersion) {
1136 this.templateVersion = templateVersion;