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