Policy 1707 commit to LF
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / openecomp / policy / controller / CreateDcaeMicroServiceController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.controller;
22
23
24 import java.io.BufferedInputStream;
25 import java.io.BufferedOutputStream;
26 import java.io.File;
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;
42 import java.util.Map;
43 import java.util.Map.Entry;
44 import java.util.Set;
45 import java.util.TreeMap;
46 import java.util.zip.ZipEntry;
47 import java.util.zip.ZipFile;
48
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51
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;
78
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;
88
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;
95
96 @Controller
97 @RequestMapping("/")
98 public class CreateDcaeMicroServiceController extends RestrictedBaseController {
99         private static final Logger logger = FlexLogger.getLogger(CreateDcaeMicroServiceController.class);
100
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>();
108         
109         @Autowired
110         private CreateDcaeMicroServiceController(CommonClassDao commonClassDao){
111                 CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
112         }
113
114         public CreateDcaeMicroServiceController(){}
115
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>>();
120
121         public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
122                 
123                 String jsonContent = null;
124                 try{
125                         jsonContent = decodeContent(root.get("policyJSON")).toString();
126                         constructJson(policyData, jsonContent);
127                 }catch(Exception e){
128                         logger.error("Error while decoding microservice content");
129                 }
130                 
131                 return policyData;
132         }
133         
134         private GroupPolicyScopeList getPolicyObject(String policyScope) {
135                 GroupPolicyScopeList groupList= (GroupPolicyScopeList) commonClassDao.getEntityItem(GroupPolicyScopeList.class, "name", policyScope);
136                 return groupList;
137         }
138         
139         private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent) {
140                 ObjectWriter om = new ObjectMapper().writer();
141                 String json="";
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());
149                 }
150                 if (returnModel.getAnnotation()==null || returnModel.getAnnotation().isEmpty()){
151                         if(policyAdapter.getUuid()!=null){
152                                 microServiceObject.setUuid(policyAdapter.getUuid());
153                         }
154                         if(policyAdapter.getLocation()!=null){
155                                 microServiceObject.setLocation(policyAdapter.getLocation());
156                         } 
157                         if(policyAdapter.getConfigName()!=null){
158                                 microServiceObject.setConfigName(policyAdapter.getConfigName());
159                         }
160                         GroupPolicyScopeList policyScopeValue = getPolicyObject(policyAdapter.getPolicyScope());
161                         if(policyScopeValue!=null){
162                                 microServiceObject.setPolicyScope(policyScopeValue.getGroupList());     
163                         }
164                 }
165                 
166                 if(policyAdapter.getPolicyName()!=null){
167                         microServiceObject.setPolicyName(policyAdapter.getPolicyName());
168                 }
169                 if(policyAdapter.getPolicyDescription()!=null){
170                         microServiceObject.setDescription(policyAdapter.getPolicyDescription());
171                 }
172                 if (policyAdapter.getPriority()!=null){
173                         microServiceObject.setPriority(policyAdapter.getPriority());
174                 }else {
175                         microServiceObject.setPriority("9999");
176                 }
177                 
178                 if (policyAdapter.getRiskLevel()!=null){
179                         microServiceObject.setRiskLevel(policyAdapter.getRiskLevel());
180                 }
181                 if (policyAdapter.getRiskType()!=null){
182                         microServiceObject.setRiskType(policyAdapter.getRiskType());
183                 }
184                 if (policyAdapter.getGuard()!=null){
185                         microServiceObject.setGuard(policyAdapter.getGuard());
186                 }
187                 microServiceObject.setContent(jsonContent);
188                 
189                 try {
190                         json = om.writeValueAsString(microServiceObject);
191                 } catch (JsonProcessingException e) {
192                         logger.error("Error writing out the object");
193                 }
194                 System.out.println(json);
195                 String cleanJson = cleanUPJson(json);
196                 cleanJson = removeNullAttributes(cleanJson);
197                 policyAdapter.setJsonBody(cleanJson);
198                 return policyAdapter;
199         }
200         
201         private String removeNullAttributes(String cleanJson) {
202                 ObjectMapper mapper = new ObjectMapper();
203
204                 try {
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);
215                                         remove = true;
216                                 }
217                         }
218                         if (remove){
219                                 cleanJson = returnNode.toString();
220                         }
221                 } catch (IOException e) {
222                         logger.error("Error writing out the JsonNode");
223                 }
224                 return cleanJson;
225         }
226
227         
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[]{"\\", "[", "]"});
232                 
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[]{"[", "]"});
243                 return cleanJson;
244         }
245         
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());
252                 }
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("@"));
268                         }else{
269                                 presKey = key;
270                         }
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();
277                                 }
278                         }else if(jsonArray!=null && jsonArray.length()>0 && !presKey.equals(prevKey) && oldValue!=null){ 
279                                 jsonResult.put(oldValue, jsonArray);
280                                 isArray = false;
281                                 jsonArray = new JSONArray();
282                         }
283         
284                         prevKey = presKey;
285                         // 
286                         if(key.contains(".")){
287                                 if(nodeKey==null){
288                                         nodeKey = key.substring(0,key.indexOf("."));
289                                 }
290                                 if(nodeKey.equals(key.substring(0,key.indexOf(".")))){
291                                         node.put(key.substring(key.indexOf(".")+1), element.get(key));
292                                 }else{
293                                         if(node.size()!=0){
294                                                 if(nodeKey.contains("@")){
295                                                         if(arryKey==null){
296                                                                 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
297                                                         }
298                                                         if(nodeKey.endsWith("@0")){
299                                                                 isArray = true;
300                                                                 jsonArray = new JSONArray();
301                                                         }
302                                                         if(arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
303                                                                 jsonArray.put(decodeContent(node));
304                                                         } 
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();
311                                                         }
312                                                         arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
313                                                 }else{
314                                                         isArray = false;
315                                                         jsonResult.put(nodeKey, decodeContent(node));
316                                                 }
317                                                 node = nodeFactory.objectNode();
318                                         }
319                                         nodeKey = key.substring(0,key.indexOf("."));
320                                         if(nodeKey.contains("@")){
321                                                 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
322                                         }
323                                         node.put(key.substring(key.indexOf(".")+1), element.get(key));
324                                 }
325                         }else if(node.size()!=0){
326                                 if(nodeKey.contains("@")){
327                                         if(arryKey==null){
328                                                 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
329                                         }
330                                         if(nodeKey.endsWith("@0")){
331                                                 isArray = true;
332                                                 jsonArray = new JSONArray();
333                                         }
334                                         if(arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
335                                                 jsonArray.put(decodeContent(node));
336                                         }
337                                         jsonResult.put(arryKey, jsonArray);
338                                         jsonArray = new JSONArray();
339                                         arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
340                                 }else{
341                                         isArray = false;
342                                         jsonResult.put(nodeKey, decodeContent(node));
343                                 }
344                                 node = nodeFactory.objectNode();
345                                 if(key.contains("@")){
346                                         isArray = true;
347                                         if(key.endsWith("@0")|| jsonArray==null){
348                                                 jsonArray = new JSONArray();
349                                         }
350                                 }else if(!key.contains("@")){
351                                         isArray = false;
352                                 }
353                                 if(isArray){
354                                         if(oldValue==null){
355                                                 oldValue = key.substring(0,key.indexOf("@"));
356                                         }
357                                         if(oldValue!=prevKey){
358                                                 oldValue = key.substring(0,key.indexOf("@"));
359                                         }
360                                         if(oldValue.equals(key.substring(0,key.indexOf("@")))){
361                                                 jsonArray.put(element.get(key));
362                                         }else{
363                                                 jsonResult.put(oldValue, jsonArray);
364                                                 jsonArray = new JSONArray();
365                                         }
366                                         oldValue = key.substring(0,key.indexOf("@"));
367                                 }else{
368                                         jsonResult.put(key, element.get(key));
369                                 }
370                         }else{
371                                 if(key.contains("@")){
372                                         isArray = true;
373                                         if(key.endsWith("@0")|| jsonArray==null){
374                                                 jsonArray = new JSONArray();
375                                         }
376                                 }else if(!key.contains("@")){
377                                         isArray = false;
378                                 }
379                                 if(isArray){
380                                         if(oldValue==null){
381                                                 oldValue = key.substring(0,key.indexOf("@"));
382                                         }
383                                         if(oldValue!=prevKey){
384                                                 oldValue = key.substring(0,key.indexOf("@"));
385                                         }
386                                         if(oldValue.equals(key.substring(0,key.indexOf("@")))){
387                                                 jsonArray.put(element.get(key));
388                                         }else{
389                                                 jsonResult.put(oldValue, jsonArray);
390                                                 jsonArray = new JSONArray();
391                                         }
392                                         oldValue = key.substring(0,key.indexOf("@"));
393                                 }else{
394                                         jsonResult.put(key, element.get(key));
395                                 }
396                         }
397                 }
398                 if(node.size()>0){
399                         if(nodeKey.contains("@")){
400                                 if(jsonArray==null){
401                                         jsonArray = new JSONArray();
402                                 }
403                                 if(arryKey==null){
404                                         arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
405                                 }
406                                 jsonArray.put(decodeContent(node));
407                                 jsonResult.put(arryKey, jsonArray);
408                                 isArray = false;;
409                         }else{
410                                 jsonResult.put(nodeKey, decodeContent(node));
411                         }
412                 }
413                 if(isArray && jsonArray.length() > 0){
414                         jsonResult.put(oldValue, jsonArray);
415                 }
416                 return jsonResult;
417         }
418         
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());
424
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];
430                 }
431                 MicroServiceModels returnModel = getAttributeObject(servicename, version);
432                 
433                 String jsonModel = createMicroSeriveJson(returnModel);
434                 
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 + "}");
442                 list.add(j);
443                 out.write(list.toString());
444                 return null;
445         }
446         
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();
454                 }
455                 String refAttribute = returnModel.getRef_attributes();
456                 if(refAttribute != null){
457                         refAttribute = refAttribute.trim();
458                 }
459                 String enumAttribute = returnModel.getEnumValues();
460                 if(enumAttribute != null){
461                         enumAttribute = enumAttribute.trim();
462                 }
463                 if (!StringUtils.isEmpty(attribute)){
464                         attributeMap = convert(attribute, ",");
465                 }
466                 if (!StringUtils.isEmpty(refAttribute)){
467                         refAttributeMap = convert(refAttribute, ",");
468                 }
469
470                 Gson gson = new Gson();
471                 
472                 String subAttributes = returnModel.getSub_attributes();
473                 if(subAttributes != null){
474                         subAttributes = subAttributes.trim();
475                 }else{
476                         subAttributes = "";
477                 }
478                 Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
479                 
480                 JSONObject object = new JSONObject();
481                 JSONArray array = new JSONArray();
482                 
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")){
487                                 array.put(value);
488                                 object.put(keySet.getKey().trim(), array);
489                         }else {
490                                 object.put(keySet.getKey().trim(), value.trim());
491                         }
492                 }
493                 
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);
501                                 }else {
502                                         object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
503                                 }
504                         }else {
505                                 if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
506                                         array.put(value.trim());
507                                         object.put(keySet.getKey().trim(), array);
508                                 }else {
509                                         object.put(keySet.getKey().trim(), value.trim()); 
510                                 }
511                         }
512                 }
513
514                 return object.toString();
515         }
516
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();
524                 
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);
532                                 }else {
533                                         object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
534                                 }
535                         } else{
536                                 if (m.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
537                                         array.put(splitValue[0].trim());
538                                         object.put(m.getKey().trim(), array);
539                                 }else {
540                                         object.put(m.getKey().trim(), splitValue[0].trim());
541                                 }
542                         }
543                   }  
544                 
545                 return object;
546         }
547
548         
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());
554
555                 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
556                 String  servicename = value.toString().split("-v")[0];
557                 Set<String> returnList = getVersionList(servicename);
558                 
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 +"}");
566                 list.add(j);
567                 out.write(list.toString());
568                 return null;
569         }
570
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());
579                         }else{
580                                 list.add("Default");
581                         }
582                 }
583                 return list;
584         }
585         
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);
591                         if(version != null){
592                                 if (workingModel.getVersion()!=null){
593                                         if (workingModel.getVersion().equals(version)){
594                                                 return workingModel;
595                                         }
596                                 }else{
597                                         return workingModel;
598                                 }
599                         }else{
600                                 return workingModel;
601                         }
602                         
603                 }
604                 return workingModel;
605         }
606
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){
609                 try{
610                         Map<String, Object> model = new HashMap<String, Object>();
611                         ObjectMapper mapper = new ObjectMapper();
612                         List<String> priorityList = new ArrayList<String>();
613                         priorityCount = 10;
614                         for (int i = 1; i < priorityCount; i++) {
615                                 priorityList.add(String.valueOf(i));
616                         }
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());
621                 }
622                 catch (Exception e){
623                         e.printStackTrace();
624                 }
625         }
626
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 = "";
635                         try{
636                                 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
637                         }catch(Exception e){
638                                 description = policy.getDescription();
639                         }
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) {
659                                                                         int index = 0;
660                                                                         Iterator<MatchType> iterMatch = matchList.iterator();
661                                                                         while (matchList.size()>1 && iterMatch.hasNext()) {
662                                                                                 MatchType match = iterMatch.next();
663                                                                                 //
664                                                                                 // Under the match we have attributevalue and
665                                                                                 // attributeDesignator. So,finally down to the actual attribute.
666                                                                                 //
667                                                                                 AttributeValueType attributeValue = match.getAttributeValue();
668                                                                                 String value = (String) attributeValue.getContent().get(0);
669
670                                                                                 // First match in the target is EcompName, so set that value.
671                                                                                 if (index == 0) {
672                                                                                         policyAdapter.setEcompName(value);
673                                                                                 }
674                                                                                 if (index == 1){
675                                                                                         policyAdapter.setConfigName(value);
676                                                                                 }
677                                                                                 if (index == 2){
678                                                                                         if(value != null){
679                                                                                                 readFile(policyAdapter, entity);
680                                                                                         }
681                                                                                 }
682                                                                                 if (index == 3){
683                                                                                         policyAdapter.setUuid(value);
684                                                                                 }
685                                                                                 if (index == 4){
686                                                                                         policyAdapter.setLocation(value);
687                                                                                 }
688                                                                                 if (index ==  5){
689                                                                                         policyAdapter.setRiskType(value);
690                                                                                 }
691
692                                                                                 if (index ==  6){
693                                                                                         policyAdapter.setRiskLevel(value);
694                                                                                 }
695
696                                                                                 if (index ==  7){
697                                                                                         policyAdapter.setGuard(value);
698                                                                                 }
699                                                                                 if (index == 8 && !value.contains("NA")){
700                                                                                         String newDate = convertDate(value, true);
701                                                                                         policyAdapter.setTtlDate(newDate);
702                                                                                 }
703                                                                                 index++;
704                                                                         }
705                                                                 }
706                                                         }
707                                                 }
708                                         }
709                                 }
710                         }
711                 }
712         }
713
714         private String convertDate(String dateTTL, boolean portalType) {
715                 String formateDate = null;
716                 String[] date  = dateTTL.split("T");
717                 String[] parts = date[0].split("-");
718                         
719                 formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];
720                 return formateDate;
721         }
722         
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]);
728                 }
729                 return map;
730         }
731
732
733         @SuppressWarnings("unchecked")
734         private void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
735                 String policyScopeName = null;
736                 ObjectMapper mapper = new ObjectMapper();
737                 try {
738                         DCAEMicroServiceObject msBody = (DCAEMicroServiceObject) mapper.readValue(entity.getConfigurationData().getConfigBody(), DCAEMicroServiceObject.class);
739                         policyScopeName = getPolicyScope(msBody.getPolicyScope());
740                         policyAdapter.setPolicyScope(policyScopeName);
741
742                         policyAdapter.setPriority(msBody.getPriority());
743
744                         if (msBody.getVersion()!= null){
745                                 policyAdapter.setServiceType(msBody.getService());
746                                 policyAdapter.setVersion(msBody.getVersion());
747                         }else{
748                                 policyAdapter.setServiceType(msBody.getService());
749                         }
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);
755                         }
756
757                 } catch (Exception e) {
758                         e.printStackTrace();
759                 }
760
761         }
762
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));
773                                 }
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));
783                                                 }
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());
789                                                 }       
790                                         }else{
791                                                 data.put(key+"@"+i, arrayvalue.toString());
792                                         }       
793                                 }
794                         }else{
795                                 data.put(key.toString(), value.toString());
796                         }       
797                 }
798         }
799
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)){
807                                         break;
808                                 }               
809                         }
810                 }
811                 return pScope.getGroupName();
812         }
813
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>();
817                 String temp = null;
818                 String key;
819                 String value;
820                 for (Entry<String, String> entry : attributesMap.entrySet()) {
821                         key = entry.getKey();
822                         value = entry.getValue();
823                         attribute.put(key, value);
824                 }
825                 for (Entry<String, String> entryRef : attributesRefMap.entrySet()) {
826                         key = entryRef.getKey();
827                         value = entryRef.getValue().toString();
828                         attribute.put(key, value);
829                 }
830                 for (Entry<String, String> entryList : attributesListRefMap.entrySet()) {
831                         key = entryList.getKey();
832                         value = entryList.getValue().toString();
833                         attribute.put(key, value);
834                 }
835                 for (Entry<String, LinkedList<String>> arrayList : arrayTextList.entrySet()){
836                         key = arrayList.getKey();
837                         temp = null;
838                         for (Object textList : arrayList.getValue()){
839                                 if (temp == null){
840                                         temp = "[" + textList;
841                                 }else{
842                                         temp = temp + "," + textList;
843                                 }
844                         }
845                         attribute.put(key, temp+ "]");                  
846                 }
847
848                 return  attribute;
849         }
850         
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);
854                 boolean zip = false;
855                 for (FileItem item : items) {
856                         if(item.getName().endsWith(".zip") || item.getName().endsWith(".xmi")){
857                                 this.newModel = new MicroServiceModels();
858                                 try{
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", ""));
868                                                         zip = true;
869                                                 }else {
870                                                         this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".xmi", ""));
871                                                 }
872                                         }
873                                 }catch(Exception e){
874                                         logger.error("Upload error : " + e);
875                                 }
876                         }
877                 }
878                 List<File> fileList = new ArrayList<File>();;
879                 this.directory = "model";
880                 if (zip){
881                         extractFolder(this.newFile);
882                         fileList = listModelFiles(this.directory);
883                 }else {
884                         File file = new File(this.newFile);
885                         fileList.add(file);
886                 }
887                 
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);
893             }   
894                 }
895         
896                 modelList = createList();
897                 
898                 cleanUp(this.newFile);
899                 cleanUp(directory);
900                 
901                 PrintWriter out = response.getWriter();
902                 
903                 response.setCharacterEncoding("UTF-8");
904                 response.setContentType("application / json");
905                 request.setCharacterEncoding("UTF-8");
906                 
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());
912         }
913         
914         /*
915          * Unzip file and store in the model directory for processing
916          */
917         @SuppressWarnings("rawtypes")
918         private void extractFolder(String zipFile )  {
919             int BUFFER = 2048;
920             File file = new File(zipFile);
921
922             ZipFile zip;
923                 try {
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();
930         
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();
938                         
939                         destinationParent.mkdirs();
940         
941                         if (!entry.isDirectory()){
942                             BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
943                             int currentByte;
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);
949                             }
950                             dest.flush();
951                             dest.close();
952                             is.close();
953                         }
954         
955                         if (currentEntry.endsWith(".zip")){
956                             extractFolder(destFile.getAbsolutePath());
957                         }
958                     }
959             } catch (IOException e) {
960                 logger.error("Failed to unzip model file " + zipFile);
961                 }
962         }
963         
964         private void retreiveDependency(String workingFile, Boolean modelClass) {
965                 
966                 MSModelUtils utils = new MSModelUtils(PolicyController.msEcompName, PolicyController.msPolicyName);
967             HashMap<String, MSAttributeObject> tempMap = new HashMap<String, MSAttributeObject>();
968             
969             tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
970             
971             classMap.putAll(tempMap);
972             System.out.println(tempMap);
973             
974             return;     
975             
976         }
977                 
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) {
983                         if (file.isFile()) {
984                                 resultList.add(file);
985                         } else if (file.isDirectory()) {
986                                 dirDependencyList.add(file.getName());
987                                 resultList.addAll(listModelFiles(file.getAbsolutePath()));
988                         }
989                 }
990                 return resultList;
991         }
992         
993     private void cleanUp(String path) {
994         if (path!=null){
995             try {
996                 FileUtils.forceDelete(new File(path));
997             } catch (IOException e) {
998                 logger.error("Failed to delete folder " + path);
999             }  
1000         }
1001     }
1002  
1003     private void checkZipDirectory(String zipDirectory) {
1004         Path path = Paths.get(zipDirectory);
1005  
1006         if (Files.exists(path)) {
1007             cleanUp(zipDirectory);
1008         }
1009     }
1010         
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());
1016             }
1017             
1018         }
1019         
1020         if (list.isEmpty()){
1021             if (classMap.containsKey(this.newModel.getModelName())){
1022                 list.add(this.newModel.getModelName());
1023             }else {
1024                 list.add("EMPTY");
1025             }
1026         }
1027         return list;
1028     }
1029
1030 }
1031
1032 class DCAEMicroServiceObject {
1033
1034         public String service;
1035         public String location;
1036         public String uuid;
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;
1047
1048         public String getGuard() {
1049                 return guard;
1050         }
1051         public void setGuard(String guard) {
1052                 this.guard = guard;
1053         }
1054         public String getRiskType() {
1055                 return riskType;
1056         }
1057         public void setRiskType(String riskType) {
1058                 this.riskType = riskType;
1059         }
1060         public String getRiskLevel() {
1061                 return riskLevel;
1062         }
1063         public void setRiskLevel(String riskLevel) {
1064                 this.riskLevel = riskLevel;
1065         }
1066         public String getPolicyScope() {
1067                 return policyScope;
1068         }
1069         public void setPolicyScope(String policyScope) {
1070                 this.policyScope = policyScope;
1071         }
1072
1073         public String getPriority() {
1074                 return priority;
1075         }
1076         public void setPriority(String priority) {
1077                 this.priority = priority;
1078         }
1079         public String getVersion() {
1080                 return version;
1081         }
1082         public void setVersion(String version) {
1083                 this.version = version;
1084         }
1085         private Object content;
1086
1087
1088         public String getPolicyName() {
1089                 return policyName;
1090         }
1091         public void setPolicyName(String policyName) {
1092                 this.policyName = policyName;
1093         }
1094         public String getDescription() {
1095                 return description;
1096         }
1097         public void setDescription(String description) {
1098                 this.description = description;
1099         }
1100         public String getConfigName() {
1101                 return configName;
1102         }
1103         public void setConfigName(String configName) {
1104                 this.configName = configName;
1105         }
1106         public Object getContent() {
1107                 return content;
1108         }
1109         public void setContent(Object content) {
1110                 this.content = content;
1111         }
1112
1113         public String getService() {
1114                 return service;
1115         }
1116         public void setService(String service) {
1117                 this.service = service;
1118         }
1119         public String getLocation() {
1120                 return location;
1121         }
1122         public void setLocation(String location) {
1123                 this.location = location;
1124         }
1125
1126         public String getUuid() {
1127                 return uuid;
1128         }
1129         public void setUuid(String uuid) {
1130                 this.uuid = uuid;
1131         }
1132         public String getTemplateVersion() {
1133                 return templateVersion;
1134         }
1135         public void setTemplateVersion(String templateVersion) {
1136                 this.templateVersion = templateVersion;
1137         }
1138
1139 }