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