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