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