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