fix try block
[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
189                 try(ZipFile zip = new ZipFile("ExtractDir" + File.separator +file)) {
190                     String newPath =  zipFile.substring(0, zipFile.length() - 4);
191                     new File(newPath).mkdir();
192                     Enumeration zipFileEntries = zip.entries();
193         
194                     // Process each entry
195                     while (zipFileEntries.hasMoreElements()){
196                         // grab a zip file entry
197                         ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
198                         String currentEntry = entry.getName();
199                         File destFile = new File("ExtractDir" + File.separator + newPath + File.separator + currentEntry);
200                         File destinationParent = destFile.getParentFile();
201         
202                         destinationParent.mkdirs();
203         
204                         if (!entry.isDirectory()){
205                             BufferedInputStream is = new BufferedInputStream(zip
206                             .getInputStream(entry));
207                             int currentByte;
208
209                             byte data[] = new byte[BUFFER];
210                                         try(FileOutputStream fos = new FileOutputStream(destFile);
211                                                 BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) {
212
213                                                 while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
214                                                         dest.write(data, 0, currentByte);
215                                                 }
216                                                 dest.flush();
217                                         }
218                             is.close();
219                         }
220         
221                         if (currentEntry.endsWith(".zip")){
222                             extractFolder(destFile.getAbsolutePath());
223                         }
224                     }
225             } catch (IOException e) {
226                         logger.error("Failed to unzip model file " + zipFile + e);
227                 }
228         }
229
230         public Map<String, String> addValuesToNewModel(String type) {
231                 
232                 Map<String, String> successMap = new HashMap<>();
233                 MSAttributeObject mainClass  = null;
234                 List<String> dependency = null;
235                 String subAttribute = null;
236                 
237                 if (!classMap.containsKey(this.newModel.getModelName())){
238                         logger.error("Model Provided does not contain the service name provided in request. Unable to import new model");
239                         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "AddValuesToNewModel", "Unable to pull out required values, file missing service name provided in request");
240                         successMap.put("error", "MISSING");
241                         return successMap;
242                 }
243                 mainClass = classMap.get(this.newModel.getModelName());
244                 
245                 
246                 if(".yml".equalsIgnoreCase(type)){
247                         
248                         newModel.setDependency("[]");
249                         if(mainClass.getSubClass() != null){
250                            String value = new Gson().toJson(mainClass.getSubClass());
251                            newModel.setSub_attributes(value);
252                         }
253                         
254                         if(mainClass.getAttribute() != null){
255                                 String attributes= mainClass.getAttribute().toString().replace("{", "").replace("}", "");
256                                 int equalsIndexForAttributes= attributes.indexOf("=");
257                                 String atttributesAfterFirstEquals= attributes.substring(equalsIndexForAttributes+1);
258                                 this.newModel.setAttributes(atttributesAfterFirstEquals);
259                         }
260                         
261                         if(mainClass.getRefAttribute() != null){
262                                 String refAttributes= mainClass.getRefAttribute().toString().replace("{", "").replace("}", "");
263                                 int equalsIndex= refAttributes.indexOf("=");
264                                 String refAttributesAfterFirstEquals= refAttributes.substring(equalsIndex+1);
265                                 this.newModel.setRef_attributes(refAttributesAfterFirstEquals);
266                         }
267                         
268                         if(mainClass.getEnumType() != null){
269                             this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", ""));
270                         }
271                         
272                         if(mainClass.getMatchingSet() != null){
273                             this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", ""));
274                         }
275                         
276                 }else{
277                 
278                         String dependTemp = StringUtils.replaceEach(mainClass.getDependency(), new String[]{"[", "]", " "}, new String[]{"", "", ""});
279                         this.newModel.setDependency(dependTemp);
280                         if (this.newModel.getDependency() != null && !this.newModel.getDependency().isEmpty()){
281                                 dependency = new ArrayList<String>(Arrays.asList(dependTemp.split(",")));       
282                                 dependency = utils.getFullDependencyList(dependency, classMap);
283                                 if (!dependency.isEmpty()){
284                                         for (String element : dependency){
285                                                 MSAttributeObject temp = new MSAttributeObject();
286                                                 if (classMap.containsKey(element)){
287                                                         temp = classMap.get(element);
288                                                         mainClass.addAllRefAttribute(temp.getRefAttribute());
289                                                         mainClass.addAllAttribute(temp.getAttribute());
290                                                 }
291                                         }
292                                 }               
293                         }
294                         subAttribute = utils.createSubAttributes(dependency, classMap, this.newModel.getModelName());
295         
296                         this.newModel.setSub_attributes(subAttribute);
297                         if(mainClass.getAttribute() != null && !mainClass.getAttribute().isEmpty()){
298                             this.newModel.setAttributes(mainClass.getAttribute().toString().replace("{", "").replace("}", ""));
299                         }
300                         
301                         if(mainClass.getRefAttribute() != null && !mainClass.getRefAttribute().isEmpty()){
302                            this.newModel.setRef_attributes(mainClass.getRefAttribute().toString().replace("{", "").replace("}", ""));
303                         }
304                         
305                         if(mainClass.getEnumType() != null && !mainClass.getEnumType().isEmpty()){
306                         this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", ""));
307                         }
308                         
309                         if(mainClass.getMatchingSet() != null && !mainClass.getMatchingSet().isEmpty()){
310                     this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", ""));
311                         }
312                 }
313                 successMap.put("success", "success");
314                 return successMap;
315                 
316         }
317         
318         public Map<String, String> saveImportService(){
319                 String modelName = this.newModel.getModelName();
320                 String imported_by = "API";
321                 String version = this.newModel.getVersion();
322                 Map<String, String> successMap = new HashMap<>();
323                 CommonClassDaoImpl dbConnection = new CommonClassDaoImpl();
324                 List<Object> result = dbConnection.getDataById(MicroServiceModels.class, "modelName:version", modelName+":"+version);
325                 if(result.isEmpty()){
326                         MicroServiceModels model = new MicroServiceModels();
327                         model.setModelName(modelName);
328                         model.setVersion(version);
329                         model.setAttributes(this.newModel.getAttributes());
330                         model.setAnnotation(this.newModel.getAnnotation());
331                         model.setDependency(this.newModel.getDependency());
332                         model.setDescription(this.newModel.getDescription());
333                         model.setEnumValues(this.newModel.getEnumValues());
334                         model.setRef_attributes(this.newModel.getRef_attributes());
335                         model.setSub_attributes(this.newModel.getSub_attributes());
336                         model.setDataOrderInfo(this.newModel.getDataOrderInfo());
337                         UserInfo userInfo = new UserInfo();
338                         userInfo.setUserLoginId(imported_by);
339                         userInfo.setUserName(imported_by);
340                         model.setUserCreatedBy(userInfo);
341                         dbConnection.save(model);
342                         successMap.put("success", "success");
343                 }else{
344                         successMap.put("DBError", "EXISTS");
345                         logger.error("Import new service failed.  Service already exists");
346                 }               
347                 return successMap;
348         }
349 }