b77f6a7d20673046e08722bd6a929fe0c85bf6bd
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / CreateOptimizationController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2018-2019 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.onap.policy.controller;
22
23 import com.att.research.xacml.util.XACMLProperties;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.DeserializationFeature;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.fasterxml.jackson.databind.ObjectWriter;
29 import com.google.gson.Gson;
30
31 import java.io.BufferedInputStream;
32 import java.io.BufferedOutputStream;
33 import java.io.File;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.OutputStream;
37 import java.io.PrintWriter;
38 import java.util.ArrayList;
39 import java.util.Enumeration;
40 import java.util.HashMap;
41 import java.util.HashSet;
42 import java.util.Iterator;
43 import java.util.LinkedHashMap;
44 import java.util.LinkedList;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.Map.Entry;
48 import java.util.Set;
49 import java.util.UUID;
50 import java.util.zip.ZipEntry;
51 import java.util.zip.ZipFile;
52
53 import javax.servlet.http.HttpServletRequest;
54 import javax.servlet.http.HttpServletResponse;
55 import lombok.Getter;
56 import lombok.Setter;
57 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
58 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
59 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
60 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
61 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
62 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
63 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
64
65 import org.apache.commons.compress.utils.IOUtils;
66 import org.apache.commons.fileupload.FileItem;
67 import org.apache.commons.fileupload.FileUploadException;
68 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
69 import org.apache.commons.fileupload.servlet.ServletFileUpload;
70 import org.apache.commons.lang.StringUtils;
71 import org.json.JSONArray;
72 import org.json.JSONObject;
73 import org.onap.policy.common.logging.flexlogger.FlexLogger;
74 import org.onap.policy.common.logging.flexlogger.Logger;
75 import org.onap.policy.rest.XACMLRestProperties;
76 import org.onap.policy.rest.adapter.PolicyRestAdapter;
77 import org.onap.policy.rest.dao.CommonClassDao;
78 import org.onap.policy.rest.jpa.MicroserviceHeaderdeFaults;
79 import org.onap.policy.rest.jpa.OptimizationModels;
80 import org.onap.policy.rest.jpa.PolicyEntity;
81 import org.onap.policy.rest.util.MSAttributeObject;
82 import org.onap.policy.rest.util.MSModelUtils;
83 import org.onap.policy.rest.util.MSModelUtils.MODEL_TYPE;
84 import org.onap.portalsdk.core.controller.RestrictedBaseController;
85 import org.springframework.beans.factory.annotation.Autowired;
86 import org.springframework.stereotype.Controller;
87 import org.springframework.web.bind.annotation.RequestMapping;
88 import org.springframework.web.servlet.ModelAndView;
89
90 @Controller
91 @RequestMapping("/")
92 public class CreateOptimizationController extends RestrictedBaseController {
93     private static final Logger LOGGER = FlexLogger.getLogger(CreateOptimizationController.class);
94     private static CommonClassDao commonClassDao;
95
96     public static CommonClassDao getCommonClassDao() {
97         return commonClassDao;
98     }
99
100     private OptimizationModels newModel;
101     private String newFile;
102     private String directory;
103     private List<String> modelList = new ArrayList<>();
104     private List<String> dirDependencyList = new ArrayList<>();
105     private LinkedHashMap<String, MSAttributeObject> classMap = new LinkedHashMap<>();
106     String referenceAttributes;
107     String attributeString;
108     Set<String> allManyTrueKeys = new HashSet<>();
109
110     public static final String DATATYPE = "data_types.policy.data.";
111     public static final String PROPERTIES = ".properties.";
112     public static final String TYPE = ".type";
113     public static final String STRING = "string";
114     public static final String INTEGER = "integer";
115     public static final String LIST = "list";
116     public static final String DEFAULT = ".default";
117     public static final String REQUIRED = ".required";
118     public static final String MATCHABLE = ".matchable";
119     public static final String MANYFALSE = ":MANY-false";
120     public static final String MODEL = "model";
121     public static final String MANY = "MANY-";
122     public static final String UTF8 = "UTF-8";
123     public static final String MODELNAME = "modelName";
124     public static final String APPLICATIONJSON = "application / json";
125
126     @Autowired
127     private CreateOptimizationController(CommonClassDao commonClassDao) {
128         setCommonClassDao(commonClassDao);
129     }
130
131     public static void setCommonClassDao(CommonClassDao commonClassDao) {
132         CreateOptimizationController.commonClassDao = commonClassDao;
133     }
134
135     public CreateOptimizationController() {
136         // Empty Constructor
137     }
138
139     protected PolicyRestAdapter policyAdapter = null;
140     private Map<String, String> attributesListRefMap = new HashMap<>();
141     private Map<String, LinkedList<String>> arrayTextList = new HashMap<>();
142     CreateDcaeMicroServiceController msController = new CreateDcaeMicroServiceController();
143
144     public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
145         String jsonContent = null;
146         try {
147             LOGGER.info("policyJSON :" + (root.get("policyJSON")).toString());
148
149             String tempJson = root.get("policyJSON").toString();
150
151             // ---replace empty value with the value below before calling decodeContent method.
152             String dummyValue = "*empty-value*" + UUID.randomUUID().toString();
153             LOGGER.info("dummyValue:" + dummyValue);
154             tempJson =
155                     StringUtils.replaceEach(tempJson, new String[] {"\"\""}, new String[] {"\"" + dummyValue + "\""});
156             ObjectMapper mapper = new ObjectMapper();
157             JsonNode tempJsonNode = mapper.readTree(tempJson);
158             jsonContent = msController.decodeContent(tempJsonNode).toString();
159             constructJson(policyData, jsonContent, dummyValue);
160         } catch (Exception e) {
161             LOGGER.error("Error while decoding microservice content", e);
162         }
163
164         return policyData;
165     }
166
167     private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue) {
168         ObjectWriter om = new ObjectMapper().writer();
169         String json = "";
170         OptimizationObject optimizationObject = setOptimizationObjectValues(policyAdapter);
171
172         optimizationObject.setContent(jsonContent);
173
174         try {
175             json = om.writeValueAsString(optimizationObject);
176         } catch (JsonProcessingException e) {
177             LOGGER.error("Error writing out the object", e);
178         }
179         LOGGER.info("input json: " + json);
180         LOGGER.info("input jsonContent: " + jsonContent);
181         String cleanJson = msController.cleanUPJson(json);
182
183         // --- reset empty value back after called cleanUPJson method and before calling removeNullAttributes
184         String tempJson =
185                 StringUtils.replaceEach(cleanJson, new String[] {"\"" + dummyValue + "\""}, new String[] {"\"\""});
186         LOGGER.info("tempJson: " + tempJson);
187         cleanJson = msController.removeNullAttributes(tempJson);
188         policyAdapter.setJsonBody(cleanJson);
189         return policyAdapter;
190     }
191
192     @RequestMapping(
193             value = {"/policyController/getOptimizationTemplateData.htm"},
194             method = {org.springframework.web.bind.annotation.RequestMethod.POST})
195     public ModelAndView getOptimizationTemplateData(HttpServletRequest request, HttpServletResponse response)
196             throws IOException {
197         CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController();
198         ObjectMapper mapper = new ObjectMapper();
199         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
200         JsonNode root = mapper.readTree(request.getReader());
201
202         String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
203         String servicename = value.toString().split("-v")[0];
204         String version = null;
205         if (value.toString().contains("-v")) {
206             version = value.toString().split("-v")[1];
207         }
208
209         OptimizationModels returnModel = getAttributeObject(servicename, version);
210
211         MicroserviceHeaderdeFaults returnHeaderDefauls = getHeaderDefaultsObject(value);
212         JSONObject jsonHdDefaultObj = null;
213         if (returnHeaderDefauls != null) {
214             jsonHdDefaultObj = new JSONObject();
215             jsonHdDefaultObj.put("onapName", returnHeaderDefauls.getOnapName());
216             jsonHdDefaultObj.put("guard", returnHeaderDefauls.getGuard());
217             jsonHdDefaultObj.put("riskLevel", returnHeaderDefauls.getRiskLevel());
218             jsonHdDefaultObj.put("riskType", returnHeaderDefauls.getRiskType());
219             jsonHdDefaultObj.put("priority", returnHeaderDefauls.getPriority());
220         }
221
222         String headDefautlsData = "";
223         if (jsonHdDefaultObj != null) {
224             headDefautlsData = jsonHdDefaultObj.toString();
225             LOGGER.info("returnHeaderDefauls headDefautlsData: " + headDefautlsData);
226         } else {
227             headDefautlsData = "null";
228         }
229
230         // Get all keys with "MANY-true" defined in their value from subAttribute
231         Set<String> allkeys = null;
232         if (returnModel.getSubattributes() != null && !returnModel.getSubattributes().isEmpty()) {
233             JSONObject json = new JSONObject(returnModel.getSubattributes());
234             getAllKeys(json);
235             allkeys = allManyTrueKeys;
236             allManyTrueKeys = new HashSet<>();
237             LOGGER.info("allkeys : " + allkeys);
238         }
239
240         // Get element order info
241         String dataOrderInfo = returnModel.getDataOrderInfo();
242         if (dataOrderInfo != null && !dataOrderInfo.startsWith("\"")) {
243             dataOrderInfo = "\"" + dataOrderInfo + "\"";
244         }
245
246         String nameOfTrueKeys = "";
247         if (allkeys != null) {
248             nameOfTrueKeys = allkeys.toString();
249         }
250
251         String jsonModel = createOptimizationJson(returnModel);
252
253         JSONObject jsonObject = new JSONObject(jsonModel);
254
255         JSONObject finalJsonObject = null;
256         if (allkeys != null) {
257             Iterator<String> iter = allkeys.iterator();
258             while (iter.hasNext()) {
259                 // Convert to array values for MANY-true keys
260                 finalJsonObject = controller.convertToArrayElement(jsonObject, iter.next());
261             }
262         }
263
264         if (finalJsonObject != null) {
265             LOGGER.info(finalJsonObject.toString());
266             jsonModel = finalJsonObject.toString();
267         }
268
269         // get all properties with "MANY-true" defined in Ref_attributes
270         Set<String> manyTrueProperties = controller.getManyTrueProperties(returnModel.getRefattributes());
271         JSONObject jsonObj = new JSONObject(jsonModel);
272         for (String s : manyTrueProperties) {
273             LOGGER.info(s);
274             // convert to array element for MANY-true properties
275             finalJsonObject = controller.convertToArrayElement(jsonObj, s.trim());
276         }
277
278         if (finalJsonObject != null) {
279             LOGGER.info(finalJsonObject.toString());
280             jsonModel = finalJsonObject.toString();
281         }
282
283         response.setCharacterEncoding(UTF8);
284         response.setContentType(APPLICATIONJSON);
285         request.setCharacterEncoding(UTF8);
286         List<Object> list = new ArrayList<>();
287         PrintWriter out = response.getWriter();
288         String responseString = mapper.writeValueAsString(returnModel);
289         JSONObject j = null;
290         if ("".equals(nameOfTrueKeys)) {
291             j = new JSONObject("{optimizationModelData: " + responseString + ",jsonValue: " + jsonModel
292                     + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:" + headDefautlsData + "}");
293         } else {
294             j = new JSONObject("{optimizationModelData: " + responseString + ",jsonValue: " + jsonModel
295                     + ",allManyTrueKeys: " + allManyTrueKeys + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:"
296                     + headDefautlsData + "}");
297         }
298         list.add(j);
299         out.write(list.toString());
300         return null;
301     }
302
303     @SuppressWarnings({"rawtypes", "unchecked"})
304     private String createOptimizationJson(OptimizationModels returnModel) {
305         Map<String, String> attributeMap = new HashMap<>();
306         Map<String, String> refAttributeMap = new HashMap<>();
307
308         String attribute = returnModel.getAttributes();
309         if (attribute != null) {
310             attribute = attribute.trim();
311         }
312         String refAttribute = returnModel.getRefattributes();
313         if (refAttribute != null) {
314             refAttribute = refAttribute.trim();
315         }
316
317         String enumAttribute = returnModel.getEnumValues();
318         if (enumAttribute != null) {
319             enumAttribute = enumAttribute.trim();
320         }
321
322         CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController();
323         if (!StringUtils.isEmpty(attribute)) {
324             attributeMap = controller.convert(attribute, ",");
325         }
326
327         if (!StringUtils.isEmpty(refAttribute)) {
328             refAttributeMap = controller.convert(refAttribute, ",");
329         }
330
331         Gson gson = new Gson();
332
333         String subAttributes = returnModel.getSubattributes();
334         if (subAttributes != null) {
335             subAttributes = subAttributes.trim();
336         } else {
337             subAttributes = "";
338         }
339
340         Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
341
342         JSONObject object = new JSONObject();
343         JSONArray array;
344
345         for (Entry<String, String> keySet : attributeMap.entrySet()) {
346             array = new JSONArray();
347             String value = keySet.getValue();
348             if ("true".equalsIgnoreCase(keySet.getValue().split(MANY)[1])) {
349                 array.put(value);
350                 object.put(keySet.getKey().trim(), array);
351             } else {
352                 object.put(keySet.getKey().trim(), value.trim());
353             }
354         }
355
356         for (Entry<String, String> keySet : refAttributeMap.entrySet()) {
357             array = new JSONArray();
358             String value = keySet.getValue().split(":")[0];
359             if (gsonObject.containsKey(value)) {
360                 if ("true".equalsIgnoreCase(keySet.getValue().split(MANY)[1])) {
361                     array.put(recursiveReference(value, gsonObject, enumAttribute));
362                     object.put(keySet.getKey().trim(), array);
363                 } else {
364                     object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
365                 }
366             } else {
367                 if ("true".equalsIgnoreCase(keySet.getValue().split(MANY)[1])) {
368                     array.put(value.trim());
369                     object.put(keySet.getKey().trim(), array);
370                 } else {
371                     object.put(keySet.getKey().trim(), value.trim());
372                 }
373             }
374         }
375
376         return object.toString();
377     }
378
379     @SuppressWarnings("unchecked")
380     private JSONObject recursiveReference(String name, Map<String, String> subAttributeMap, String enumAttribute) {
381         JSONObject object = new JSONObject();
382         Map<String, String> map;
383         Object returnClass = subAttributeMap.get(name);
384         map = (Map<String, String>) returnClass;
385         JSONArray array;
386
387         for (Entry<String, String> m : map.entrySet()) {
388             String[] splitValue = m.getValue().split(":");
389             array = new JSONArray();
390             if (subAttributeMap.containsKey(splitValue[0])) {
391                 if ("true".equalsIgnoreCase(m.getValue().split(MANY)[1])) {
392                     array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
393                     object.put(m.getKey().trim(), array);
394                 } else {
395                     object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
396                 }
397             } else {
398                 if ("true".equalsIgnoreCase(m.getValue().split(MANY)[1])) {
399                     array.put(splitValue[0].trim());
400                     object.put(m.getKey().trim(), array);
401                 } else {
402                     object.put(m.getKey().trim(), splitValue[0].trim());
403                 }
404             }
405         }
406
407         return object;
408     }
409
410     // call this method to start the recursive
411     private Set<String> getAllKeys(JSONObject json) {
412         return getAllKeys(json, new HashSet<>());
413     }
414
415     private Set<String> getAllKeys(JSONArray arr) {
416         return getAllKeys(arr, new HashSet<>());
417     }
418
419     private Set<String> getAllKeys(JSONArray arr, Set<String> keys) {
420         for (int i = 0; i < arr.length(); i++) {
421             Object obj = arr.get(i);
422             if (obj instanceof JSONObject)
423                 keys.addAll(getAllKeys(arr.getJSONObject(i)));
424             if (obj instanceof JSONArray)
425                 keys.addAll(getAllKeys(arr.getJSONArray(i)));
426         }
427
428         return keys;
429     }
430
431     // this method returns a set of keys with "MANY-true" defined in their value.
432     private Set<String> getAllKeys(JSONObject json, Set<String> keys) {
433         for (String key : json.keySet()) {
434             Object obj = json.get(key);
435             if (obj instanceof String && ((String) obj).contains("MANY-true")) {
436                 LOGGER.info("key : " + key);
437                 LOGGER.info("obj : " + obj);
438                 allManyTrueKeys.add(key);
439             }
440             if (obj instanceof JSONObject)
441                 keys.addAll(getAllKeys(json.getJSONObject(key)));
442             if (obj instanceof JSONArray)
443                 keys.addAll(getAllKeys(json.getJSONArray(key)));
444         }
445
446         return keys;
447     }
448
449     @RequestMapping(
450             value = {"/policyController/getModelServiceVersionData.htm"},
451             method = {org.springframework.web.bind.annotation.RequestMethod.POST})
452     public ModelAndView getModelServiceVersionData(HttpServletRequest request, HttpServletResponse response)
453             throws IOException {
454         ObjectMapper mapper = new ObjectMapper();
455         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
456         JsonNode root = mapper.readTree(request.getReader());
457
458         String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
459         String servicename = value.split("-v")[0];
460         Set<String> returnList = getVersionList(servicename);
461
462         response.setCharacterEncoding(UTF8);
463         response.setContentType(APPLICATIONJSON);
464         request.setCharacterEncoding(UTF8);
465         List<Object> list = new ArrayList<>();
466         PrintWriter out = response.getWriter();
467         String responseString = mapper.writeValueAsString(returnList);
468         JSONObject j = new JSONObject("{optimizationModelVersionData: " + responseString + "}");
469         list.add(j);
470         out.write(list.toString());
471         return null;
472     }
473
474     private Set<String> getVersionList(String name) {
475         OptimizationModels workingModel;
476         Set<String> list = new HashSet<>();
477         List<Object> optimizationModelsData = commonClassDao.getDataById(OptimizationModels.class, MODELNAME, name);
478         for (int i = 0; i < optimizationModelsData.size(); i++) {
479             workingModel = (OptimizationModels) optimizationModelsData.get(i);
480             if (workingModel.getVersion() != null) {
481                 list.add(workingModel.getVersion());
482             } else {
483                 list.add("Default");
484             }
485         }
486         return list;
487     }
488
489     private OptimizationModels getAttributeObject(String name, String version) {
490         OptimizationModels workingModel = new OptimizationModels();
491         List<Object> optimizationModelsData = commonClassDao.getDataById(OptimizationModels.class, MODELNAME, name);
492         for (int i = 0; i < optimizationModelsData.size(); i++) {
493             workingModel = (OptimizationModels) optimizationModelsData.get(i);
494             if (version != null) {
495                 if (workingModel.getVersion() != null) {
496                     if (workingModel.getVersion().equals(version)) {
497                         return workingModel;
498                     }
499                 } else {
500                     return workingModel;
501                 }
502             } else {
503                 return workingModel;
504             }
505
506         }
507         return workingModel;
508     }
509
510     private MicroserviceHeaderdeFaults getHeaderDefaultsObject(String modelName) {
511         return (MicroserviceHeaderdeFaults) commonClassDao.getEntityItem(MicroserviceHeaderdeFaults.class, MODELNAME,
512                 modelName);
513     }
514
515     public void prePopulatePolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
516         if (policyAdapter.getPolicyData() instanceof PolicyType) {
517             Object policyData = policyAdapter.getPolicyData();
518             PolicyType policy = (PolicyType) policyData;
519             policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
520             String policyNameValue =
521                     policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("OOF_") + 4);
522             policyAdapter.setPolicyName(policyNameValue);
523             String description = "";
524             try {
525                 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
526             } catch (Exception e) {
527                 LOGGER.error("Error while collecting the description tag in " + policyNameValue, e);
528                 description = policy.getDescription();
529             }
530             policyAdapter.setPolicyDescription(description);
531             // Get the target data under policy.
532             TargetType target = policy.getTarget();
533             if (target != null) {
534                 // Under target we have AnyOFType
535                 List<AnyOfType> anyOfList = target.getAnyOf();
536                 if (anyOfList != null) {
537                     Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
538                     while (iterAnyOf.hasNext()) {
539                         AnyOfType anyOf = iterAnyOf.next();
540                         // Under AnyOFType we have AllOFType
541                         List<AllOfType> allOfList = anyOf.getAllOf();
542                         if (allOfList != null) {
543                             Iterator<AllOfType> iterAllOf = allOfList.iterator();
544                             while (iterAllOf.hasNext()) {
545                                 AllOfType allOf = iterAllOf.next();
546                                 // Under AllOFType we have Match
547                                 List<MatchType> matchList = allOf.getMatch();
548                                 if (matchList != null) {
549                                     Iterator<MatchType> iterMatch = matchList.iterator();
550                                     while (matchList.size() > 1 && iterMatch.hasNext()) {
551                                         MatchType match = iterMatch.next();
552                                         //
553                                         // Under the match we have attribute value and
554                                         // attributeDesignator. So,finally down to the actual attribute.
555                                         //
556                                         AttributeValueType attributeValue = match.getAttributeValue();
557                                         String value = (String) attributeValue.getContent().get(0);
558                                         AttributeDesignatorType designator = match.getAttributeDesignator();
559                                         String attributeId = designator.getAttributeId();
560                                         // First match in the target is OnapName, so set that value.
561                                         if ("ONAPName".equals(attributeId)) {
562                                             policyAdapter.setOnapName(value);
563                                         }
564                                         if ("RiskType".equals(attributeId)) {
565                                             policyAdapter.setRiskType(value);
566                                         }
567                                         if ("RiskLevel".equals(attributeId)) {
568                                             policyAdapter.setRiskLevel(value);
569                                         }
570                                         if ("guard".equals(attributeId)) {
571                                             policyAdapter.setGuard(value);
572                                         }
573                                         if ("TTLDate".equals(attributeId) && !value.contains("NA")) {
574                                             PolicyController controller = new PolicyController();
575                                             String newDate = controller.convertDate(value);
576                                             policyAdapter.setTtlDate(newDate);
577                                         }
578                                     }
579                                     readFile(policyAdapter, entity);
580                                 }
581                             }
582                         }
583                     }
584                 }
585             }
586         }
587     }
588
589     @SuppressWarnings("unchecked")
590     private void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
591         String policyScopeName = null;
592         ObjectMapper mapper = new ObjectMapper();
593         try {
594             OptimizationObject optimizationBody =
595                     mapper.readValue(entity.getConfigurationData().getConfigBody(), OptimizationObject.class);
596             policyScopeName = msController.getPolicyScope(optimizationBody.getPolicyScope());
597             policyAdapter.setPolicyScope(policyScopeName);
598
599             policyAdapter.setPriority(optimizationBody.getPriority());
600
601             if (optimizationBody.getVersion() != null) {
602                 policyAdapter.setServiceType(optimizationBody.getService());
603                 policyAdapter.setVersion(optimizationBody.getVersion());
604             } else {
605                 policyAdapter.setServiceType(optimizationBody.getService());
606             }
607             if (optimizationBody.getContent() != null) {
608                 LinkedHashMap<String, Object> data = new LinkedHashMap<>();
609                 LinkedHashMap<String, ?> map = (LinkedHashMap<String, ?>) optimizationBody.getContent();
610                 msController.readRecursivlyJSONContent(map, data);
611                 policyAdapter.setRuleData(data);
612             }
613
614         } catch (Exception e) {
615             LOGGER.error(e);
616         }
617
618     }
619
620     @RequestMapping(
621             value = {"/oof_dictionary/set_ModelData"},
622             method = {org.springframework.web.bind.annotation.RequestMethod.POST})
623     public void setModelData(HttpServletRequest request, HttpServletResponse response)
624             throws IOException, FileUploadException {
625         modelList = new ArrayList<>();
626         dirDependencyList = new ArrayList<>();
627         classMap = new LinkedHashMap<>();
628         List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
629
630         boolean zip = false;
631         boolean yml = false;
632         String errorMsg = "";
633         for (FileItem item : items) {
634             if (item.getName().endsWith(".zip") || item.getName().endsWith(".xmi") || item.getName().endsWith(".yml")) {
635                 this.newModel = new OptimizationModels();
636                 try {
637                     File file = new File(item.getName());
638                     OutputStream outputStream = new FileOutputStream(file);
639                     IOUtils.copy(item.getInputStream(), outputStream);
640                     outputStream.close();
641                     this.newFile = file.toString();
642                     this.newModel.setModelName(this.newFile.split("-v")[0]);
643
644                     if (this.newFile.contains("-v")) {
645                         if (item.getName().endsWith(".zip")) {
646                             this.newModel.setVersion(this.newFile.split("-v")[1].replace(".zip", ""));
647                             zip = true;
648                         } else if (item.getName().endsWith(".yml")) {
649                             this.newModel.setVersion(this.newFile.split("-v")[1].replace(".yml", ""));
650                             yml = true;
651                         } else {
652                             this.newModel.setVersion(this.newFile.split("-v")[1].replace(".xmi", ""));
653                         }
654                     }
655                 } catch (Exception e) {
656                     LOGGER.error("Upload error : ", e);
657                     errorMsg = "Upload error:" + e.getMessage();
658                 }
659             }
660
661         }
662
663         if (!errorMsg.isEmpty()) {
664
665             PrintWriter out = response.getWriter();
666
667             response.setCharacterEncoding(UTF8);
668             response.setContentType(APPLICATIONJSON);
669             request.setCharacterEncoding(UTF8);
670
671             JSONObject j = new JSONObject();
672             j.put("errorMsg", errorMsg);
673             out.write(j.toString());
674             return;
675         }
676
677         List<File> fileList = new ArrayList<>();
678         MSModelUtils modelUtil = new MSModelUtils();
679         this.directory = MODEL;
680         if (zip) {
681             extractFolder(this.newFile);
682             fileList = listModelFiles(this.directory);
683         } else if (yml) {
684             modelUtil.parseTosca(this.newFile);
685         } else {
686             File file = new File(this.newFile);
687             fileList.add(file);
688         }
689         String modelType;
690         if (!yml) {
691             modelType = "xmi";
692             // Process Main Model file first
693             classMap = new LinkedHashMap<>();
694             for (File file : fileList) {
695                 if (!file.isDirectory() && file.getName().endsWith(".xmi")) {
696                     retrieveDependency(file.toString());
697                 }
698             }
699
700             modelList = createList();
701
702             msController.cleanUp(this.newFile);
703             msController.cleanUp(directory);
704         } else {
705             modelType = "yml";
706             modelList.add(this.newModel.getModelName());
707             String className = this.newModel.getModelName();
708             MSAttributeObject optimizationAttributes = new MSAttributeObject();
709             optimizationAttributes.setClassName(className);
710
711             LinkedHashMap<String, String> returnAttributeList = new LinkedHashMap<>();
712             returnAttributeList.put(className, modelUtil.getAttributeString());
713             optimizationAttributes.setAttribute(returnAttributeList);
714
715             optimizationAttributes.setSubClass(modelUtil.getRetmap());
716
717             optimizationAttributes.setMatchingSet(modelUtil.getMatchableValues());
718
719             LinkedHashMap<String, String> returnReferenceList = new LinkedHashMap<>();
720             returnReferenceList.put(className, modelUtil.getReferenceAttributes());
721             optimizationAttributes.setRefAttribute(returnReferenceList);
722
723             if (!"".equals(modelUtil.getListConstraints())) {
724                 LinkedHashMap<String, String> enumList = new LinkedHashMap<>();
725                 String[] listArray = modelUtil.getListConstraints().split("#");
726                 for (String str : listArray) {
727                     String[] strArr = str.split("=");
728                     if (strArr.length > 1) {
729                         enumList.put(strArr[0], strArr[1]);
730                     }
731                 }
732                 optimizationAttributes.setEnumType(enumList);
733             }
734
735             classMap = new LinkedHashMap<>();
736             classMap.put(className, optimizationAttributes);
737
738         }
739
740         PrintWriter out = response.getWriter();
741
742         response.setCharacterEncoding(UTF8);
743         response.setContentType(APPLICATIONJSON);
744         request.setCharacterEncoding(UTF8);
745
746         ObjectMapper mapper = new ObjectMapper();
747         JSONObject j = new JSONObject();
748         j.put("classListDatas", modelList);
749         j.put("modelDatas", mapper.writeValueAsString(classMap));
750         j.put("modelType", modelType);
751         j.put("dataOrderInfo", modelUtil.getDataOrderInfo());
752
753         out.write(j.toString());
754     }
755
756     /*
757      * Unzip file and store in the model directory for processing
758      */
759     @SuppressWarnings("rawtypes")
760     private void extractFolder(String zipFile) {
761         int BUFFER = 2048;
762         File file = new File(zipFile);
763
764         try (ZipFile zip = new ZipFile(file)) {
765             String newPath = MODEL + File.separator + zipFile.substring(0, zipFile.length() - 4);
766             this.directory = MODEL + File.separator + zipFile.substring(0, zipFile.length() - 4);
767             msController.checkZipDirectory(this.directory);
768             new File(newPath).mkdir();
769             Enumeration zipFileEntries = zip.entries();
770
771             // Process each entry
772             while (zipFileEntries.hasMoreElements()) {
773                 // grab a zip file entry
774                 ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
775                 String currentEntry = entry.getName();
776                 File destFile = new File(MODEL + File.separator + currentEntry);
777                 File destinationParent = destFile.getParentFile();
778
779                 destinationParent.mkdirs();
780
781                 if (!entry.isDirectory()) {
782                     BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
783                     int currentByte;
784                     byte[] data = new byte[BUFFER];
785                     try (FileOutputStream fos = new FileOutputStream(destFile);
786                             BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) {
787                         while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
788                             dest.write(data, 0, currentByte);
789                         }
790                         dest.flush();
791                     } catch (IOException e) {
792                         LOGGER.error("Failed to write zip contents to {}" + destFile + e);
793                         //
794                         // PLD should I throw e?
795                         //
796                         throw e;
797                     }
798                 }
799
800                 if (currentEntry.endsWith(".zip")) {
801                     extractFolder(destFile.getAbsolutePath());
802                 }
803             }
804         } catch (IOException e) {
805             LOGGER.error("Failed to unzip model file " + zipFile, e);
806         }
807     }
808
809     private void retrieveDependency(String workingFile) {
810
811         MSModelUtils utils = new MSModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName());
812         Map<String, MSAttributeObject> tempMap;
813
814         tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
815
816         classMap.putAll(tempMap);
817         LOGGER.info(tempMap);
818
819         return;
820
821     }
822
823     private List<File> listModelFiles(String directoryName) {
824         File fileDirectory = new File(directoryName);
825         List<File> resultList = new ArrayList<>();
826         File[] fList = fileDirectory.listFiles();
827         for (File file : fList) {
828             if (file.isFile()) {
829                 resultList.add(file);
830             } else if (file.isDirectory()) {
831                 dirDependencyList.add(file.getName());
832                 resultList.addAll(listModelFiles(file.getAbsolutePath()));
833             }
834         }
835         return resultList;
836     }
837
838     private List<String> createList() {
839         List<String> list = new ArrayList<>();
840         for (Entry<String, MSAttributeObject> cMap : classMap.entrySet()) {
841             if (cMap.getValue().isPolicyTempalate()) {
842                 list.add(cMap.getKey());
843             }
844
845         }
846
847         if (list.isEmpty()) {
848             if (classMap.containsKey(this.newModel.getModelName())) {
849                 list.add(this.newModel.getModelName());
850             } else {
851                 list.add("EMPTY");
852             }
853         }
854         return list;
855     }
856
857     public Map<String, String> getAttributesListRefMap() {
858         return attributesListRefMap;
859     }
860
861     public Map<String, LinkedList<String>> getArrayTextList() {
862         return arrayTextList;
863     }
864
865     private OptimizationObject setOptimizationObjectValues(PolicyRestAdapter policyAdapter) {
866         OptimizationObject optimizationObject = new OptimizationObject();
867         optimizationObject.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_OOF));
868
869         if (policyAdapter.getServiceType() != null) {
870             optimizationObject.setService(policyAdapter.getServiceType());
871             optimizationObject.setVersion(policyAdapter.getVersion());
872         }
873         if (policyAdapter.getPolicyName() != null) {
874             optimizationObject.setPolicyName(policyAdapter.getPolicyName());
875         }
876         if (policyAdapter.getPolicyDescription() != null) {
877             optimizationObject.setDescription(policyAdapter.getPolicyDescription());
878         }
879         if (policyAdapter.getPriority() != null) {
880             optimizationObject.setPriority(policyAdapter.getPriority());
881         } else {
882             optimizationObject.setPriority("9999");
883         }
884         if (policyAdapter.getRiskLevel() != null) {
885             optimizationObject.setRiskLevel(policyAdapter.getRiskLevel());
886         }
887         if (policyAdapter.getRiskType() != null) {
888             optimizationObject.setRiskType(policyAdapter.getRiskType());
889         }
890         if (policyAdapter.getGuard() != null) {
891             optimizationObject.setGuard(policyAdapter.getGuard());
892         }
893         return optimizationObject;
894     }
895 }
896
897 @Getter
898 @Setter
899 class OptimizationObject {
900
901     private String service;
902     private String policyName;
903     private String description;
904     private String templateVersion;
905     private String version;
906     private String priority;
907     private String policyScope;
908     private String riskType;
909     private String riskLevel;
910     private String guard = null;
911     private Object content;
912 }