Merge "New Optimization Policy"
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / components / CreateNewMicroServiceModel.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.components;
22
23 import java.io.BufferedInputStream;
24 import java.io.BufferedOutputStream;
25 import java.io.File;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Enumeration;
31 import java.util.HashMap;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.zip.ZipEntry;
36 import java.util.zip.ZipFile;
37
38 import org.apache.commons.io.FileUtils;
39 import org.apache.commons.lang.StringUtils;
40 import org.onap.policy.common.logging.eelf.MessageCodes;
41 import org.onap.policy.common.logging.eelf.PolicyLogger;
42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
43 import org.onap.policy.common.logging.flexlogger.Logger;
44 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
45 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
46 import org.onap.policy.rest.jpa.MicroServiceModels;
47 import org.onap.policy.rest.jpa.UserInfo;
48 import org.onap.policy.rest.util.MSAttributeObject;
49 import org.onap.policy.rest.util.MSModelUtils;
50 import org.onap.policy.rest.util.MSModelUtils.MODEL_TYPE;
51
52 import com.google.gson.Gson;
53
54 public class CreateNewMicroServiceModel {
55         private static final Logger logger = FlexLogger.getLogger(CreateNewMicroServiceModel.class);
56         private MicroServiceModels newModel = null;
57         private HashMap<String,MSAttributeObject > classMap = new HashMap<>();
58
59         
60         MSModelUtils utils = new MSModelUtils(XACMLPapServlet.getMsOnapName(), XACMLPapServlet.getMsPolicyName());
61
62         public CreateNewMicroServiceModel(String fileName, String serviceName, String string, String version) {
63                 super();
64         }
65
66         public CreateNewMicroServiceModel(String importFile, String  modelName, String description, String version, String randomID) {
67         
68                 this.newModel = new MicroServiceModels();
69                 this.newModel.setVersion(version);
70                 this.newModel.setModelName(modelName);
71                 UserInfo userInfo = new UserInfo();
72                 userInfo.setUserLoginId("API");
73                 this.newModel.setUserCreatedBy(userInfo);
74                 String cleanUpFile = null;
75         
76             Map<String, MSAttributeObject> tempMap = new HashMap<>();
77             //Need to delete the file
78             if (importFile.contains(".zip")){
79                 extractFolder(randomID + ".zip");
80                 File directory = new File("ExtractDir" + File.separator + randomID);
81                 List<File> fileList = listModelFiles(directory.toString());
82                 //get all the files from a director
83                 for (File file : fileList){
84                     if (file.isFile()){
85                         int i = file.getName().lastIndexOf('.');
86                         String type = file.getName().substring(i+1);
87                         
88                         if(type != null && "yml".equalsIgnoreCase(type)){
89                                 
90                                 processYmlModel(file.toString(), modelName);
91                                 
92                         }else{
93                                 
94                                         tempMap = utils.processEpackage(file.getAbsolutePath(), MODEL_TYPE.XMI);
95                                         classMap.putAll(tempMap);
96                         }
97                     }
98                 }
99                 cleanUpFile = "ExtractDir" + File.separator + randomID + ".zip";
100                 try {
101                                 FileUtils.deleteDirectory(new File("ExtractDir" + File.separator + randomID));
102                                 FileUtils.deleteDirectory(new File(randomID));
103                                 File deleteFile = new File(cleanUpFile); 
104                                 FileUtils.forceDelete(deleteFile);
105                         } catch (IOException e) {
106                                 logger.error("Failed to unzip model file " + randomID, e);
107                         }
108             }else {             
109                 if(importFile.contains(".yml")){
110                         
111                                 processYmlModel("ExtractDir" + File.separator + randomID+".yml", modelName);
112                             cleanUpFile = "ExtractDir" + File.separator + randomID+".yml";
113                         
114                 }else{
115                             tempMap = utils.processEpackage("ExtractDir" + File.separator + randomID+".xmi", MODEL_TYPE.XMI);
116                             classMap.putAll(tempMap);
117                             cleanUpFile = "ExtractDir" + File.separator + randomID+".xmi";
118                 }
119                 
120                     File deleteFile = new File(cleanUpFile); 
121                         deleteFile.delete();
122             }
123         }
124         
125         private void processYmlModel(String fileName, String  modelName){
126
127                 try {
128                         
129                         
130                         utils.parseTosca(fileName);
131                         
132                         MSAttributeObject msAttributes= new MSAttributeObject();
133                         msAttributes.setClassName(modelName);
134                         
135                         LinkedHashMap<String, String> returnAttributeList =new LinkedHashMap<>();
136                         returnAttributeList.put(modelName, utils.getAttributeString());
137                         msAttributes.setAttribute(returnAttributeList);
138                         
139                         msAttributes.setSubClass(utils.getRetmap());
140                         
141                         msAttributes.setMatchingSet(utils.getMatchableValues());
142                         
143                         LinkedHashMap<String, String> returnReferenceList =new LinkedHashMap<>();
144
145                         returnReferenceList.put(modelName, utils.getReferenceAttributes());
146                         msAttributes.setRefAttribute(returnReferenceList);
147                         
148                         if(utils.getListConstraints()!=""){
149                                 LinkedHashMap<String, String> enumList =new LinkedHashMap<>();
150                                 String[] listArray=utils.getListConstraints().split("#");
151                 for(String str:listArray){
152                     String[] strArr= str.split("=");
153                     if(strArr.length>1){
154                         enumList.put(strArr[0], strArr[1]);
155                     }
156                 }
157                                 msAttributes.setEnumType(enumList);
158                         }
159                         
160                         classMap=new LinkedHashMap<>();
161                         classMap.put(modelName, msAttributes);
162                         
163                 } catch (Exception e) {
164                         logger.error("Failed to process yml model" + e);
165                 }
166         
167         }
168         
169         private List<File> listModelFiles(String directoryName) {
170                 File directory = new File(directoryName);
171                 List<File> resultList = new ArrayList<>();
172                 File[] fList = directory.listFiles();
173                 for (File file : fList) {
174                         if (file.isFile()) {
175                                 resultList.add(file);
176                         } else if (file.isDirectory()) {
177                                 resultList.addAll(listModelFiles(file.getAbsolutePath()));
178                         }
179                 }
180                 return resultList;
181         }
182
183         @SuppressWarnings("rawtypes")
184         private void extractFolder(String zipFile) {
185             int BUFFER = 2048;
186             File file = new File(zipFile);
187
188             ZipFile zip = null;
189                 try {
190                         zip = new ZipFile("ExtractDir" + File.separator +file);
191                     String newPath =  zipFile.substring(0, zipFile.length() - 4);
192                     new File(newPath).mkdir();
193                     Enumeration zipFileEntries = zip.entries();
194         
195                     // Process each entry
196                     while (zipFileEntries.hasMoreElements()){
197                         // grab a zip file entry
198                         ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
199                         String currentEntry = entry.getName();
200                         File destFile = new File("ExtractDir" + File.separator + newPath + File.separator + currentEntry);
201                         File destinationParent = destFile.getParentFile();
202         
203                         destinationParent.mkdirs();
204         
205                         if (!entry.isDirectory()){
206                             BufferedInputStream is = new BufferedInputStream(zip
207                             .getInputStream(entry));
208                             int currentByte;
209
210                             byte data[] = new byte[BUFFER];
211                                         try(FileOutputStream fos = new FileOutputStream(destFile);
212                                                 BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) {
213
214                                                 while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
215                                                         dest.write(data, 0, currentByte);
216                                                 }
217                                                 dest.flush();
218                                         }
219                             is.close();
220                         }
221         
222                         if (currentEntry.endsWith(".zip")){
223                             extractFolder(destFile.getAbsolutePath());
224                         }
225                     }
226             } catch (IOException e) {
227                         logger.error("Failed to unzip model file " + zipFile + e);
228                 }finally{
229                         if(zip != null){
230                                 try {
231                                         zip.close();
232                                 } catch (Exception e) {
233                                         logger.error("Exception Occured while closing the zip file"+e);
234                                 }
235                         }
236                 }
237         }
238
239         public Map<String, String> addValuesToNewModel(String type) {
240                 
241                 Map<String, String> successMap = new HashMap<>();
242                 MSAttributeObject mainClass  = null;
243                 List<String> dependency = null;
244                 String subAttribute = null;
245                 
246                 if (!classMap.containsKey(this.newModel.getModelName())){
247                         logger.error("Model Provided does not contain the service name provided in request. Unable to import new model");
248                         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "AddValuesToNewModel", "Unable to pull out required values, file missing service name provided in request");
249                         successMap.put("error", "MISSING");
250                         return successMap;
251                 }
252                 mainClass = classMap.get(this.newModel.getModelName());
253                 
254                 
255                 if(".yml".equalsIgnoreCase(type)){
256                         
257                         newModel.setDependency("[]");
258                         if(mainClass.getSubClass() != null){
259                            String value = new Gson().toJson(mainClass.getSubClass());
260                            newModel.setSub_attributes(value);
261                         }
262                         
263                         if(mainClass.getAttribute() != null){
264                                 String attributes= mainClass.getAttribute().toString().replace("{", "").replace("}", "");
265                                 int equalsIndexForAttributes= attributes.indexOf("=");
266                                 String atttributesAfterFirstEquals= attributes.substring(equalsIndexForAttributes+1);
267                                 this.newModel.setAttributes(atttributesAfterFirstEquals);
268                         }
269                         
270                         if(mainClass.getRefAttribute() != null){
271                                 String refAttributes= mainClass.getRefAttribute().toString().replace("{", "").replace("}", "");
272                                 int equalsIndex= refAttributes.indexOf("=");
273                                 String refAttributesAfterFirstEquals= refAttributes.substring(equalsIndex+1);
274                                 this.newModel.setRef_attributes(refAttributesAfterFirstEquals);
275                         }
276                         
277                         if(mainClass.getEnumType() != null){
278                             this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", ""));
279                         }
280                         
281                         if(mainClass.getMatchingSet() != null){
282                             this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", ""));
283                         }
284                         
285                 }else{
286                 
287                         String dependTemp = StringUtils.replaceEach(mainClass.getDependency(), new String[]{"[", "]", " "}, new String[]{"", "", ""});
288                         this.newModel.setDependency(dependTemp);
289                         if (this.newModel.getDependency() != null && !this.newModel.getDependency().isEmpty()){
290                                 dependency = new ArrayList<String>(Arrays.asList(dependTemp.split(",")));       
291                                 dependency = utils.getFullDependencyList(dependency, classMap);
292                                 if (!dependency.isEmpty()){
293                                         for (String element : dependency){
294                                                 MSAttributeObject temp = new MSAttributeObject();
295                                                 if (classMap.containsKey(element)){
296                                                         temp = classMap.get(element);
297                                                         mainClass.addAllRefAttribute(temp.getRefAttribute());
298                                                         mainClass.addAllAttribute(temp.getAttribute());
299                                                 }
300                                         }
301                                 }               
302                         }
303                         subAttribute = utils.createSubAttributes(dependency, classMap, this.newModel.getModelName());
304         
305                         this.newModel.setSub_attributes(subAttribute);
306                         if(mainClass.getAttribute() != null && !mainClass.getAttribute().isEmpty()){
307                             this.newModel.setAttributes(mainClass.getAttribute().toString().replace("{", "").replace("}", ""));
308                         }
309                         
310                         if(mainClass.getRefAttribute() != null && !mainClass.getRefAttribute().isEmpty()){
311                            this.newModel.setRef_attributes(mainClass.getRefAttribute().toString().replace("{", "").replace("}", ""));
312                         }
313                         
314                         if(mainClass.getEnumType() != null && !mainClass.getEnumType().isEmpty()){
315                         this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", ""));
316                         }
317                         
318                         if(mainClass.getMatchingSet() != null && !mainClass.getMatchingSet().isEmpty()){
319                     this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", ""));
320                         }
321                 }
322                 successMap.put("success", "success");
323                 return successMap;
324                 
325         }
326         
327         public Map<String, String> saveImportService(){
328                 String modelName = this.newModel.getModelName();
329                 String imported_by = "API";
330                 String version = this.newModel.getVersion();
331                 Map<String, String> successMap = new HashMap<>();
332                 CommonClassDaoImpl dbConnection = new CommonClassDaoImpl();
333                 List<Object> result = dbConnection.getDataById(MicroServiceModels.class, "modelName:version", modelName+":"+version);
334                 if(result.isEmpty()){
335                         MicroServiceModels model = new MicroServiceModels();
336                         model.setModelName(modelName);
337                         model.setVersion(version);
338                         model.setAttributes(this.newModel.getAttributes());
339                         model.setAnnotation(this.newModel.getAnnotation());
340                         model.setDependency(this.newModel.getDependency());
341                         model.setDescription(this.newModel.getDescription());
342                         model.setEnumValues(this.newModel.getEnumValues());
343                         model.setRef_attributes(this.newModel.getRef_attributes());
344                         model.setSub_attributes(this.newModel.getSub_attributes());
345                         model.setDataOrderInfo(this.newModel.getDataOrderInfo());
346                         UserInfo userInfo = new UserInfo();
347                         userInfo.setUserLoginId(imported_by);
348                         userInfo.setUserName(imported_by);
349                         model.setUserCreatedBy(userInfo);
350                         dbConnection.save(model);
351                         successMap.put("success", "success");
352                 }else{
353                         successMap.put("DBError", "EXISTS");
354                         logger.error("Import new service failed.  Service already exists");
355                 }               
356                 return successMap;
357         }
358 }