2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.xacml.rest.components;
23 import java.io.BufferedInputStream;
24 import java.io.BufferedOutputStream;
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;
35 import java.util.zip.ZipEntry;
36 import java.util.zip.ZipFile;
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;
52 import com.google.gson.Gson;
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<>();
60 MSModelUtils utils = new MSModelUtils(XACMLPapServlet.getMsOnapName(), XACMLPapServlet.getMsPolicyName());
62 public CreateNewMicroServiceModel(String fileName, String serviceName, String string, String version) {
66 public CreateNewMicroServiceModel(String importFile, String modelName, String description, String version, String randomID) {
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;
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){
85 int i = file.getName().lastIndexOf('.');
86 String type = file.getName().substring(i+1);
88 if(type != null && "yml".equalsIgnoreCase(type)){
90 processYmlModel(file.toString(), modelName);
94 tempMap = utils.processEpackage(file.getAbsolutePath(), MODEL_TYPE.XMI);
95 classMap.putAll(tempMap);
99 cleanUpFile = "ExtractDir" + File.separator + randomID + ".zip";
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);
109 if(importFile.contains(".yml")){
111 processYmlModel("ExtractDir" + File.separator + randomID+".yml", modelName);
112 cleanUpFile = "ExtractDir" + File.separator + randomID+".yml";
115 tempMap = utils.processEpackage("ExtractDir" + File.separator + randomID+".xmi", MODEL_TYPE.XMI);
116 classMap.putAll(tempMap);
117 cleanUpFile = "ExtractDir" + File.separator + randomID+".xmi";
120 File deleteFile = new File(cleanUpFile);
125 private void processYmlModel(String fileName, String modelName){
130 utils.parseTosca(fileName);
132 MSAttributeObject msAttributes= new MSAttributeObject();
133 msAttributes.setClassName(modelName);
135 LinkedHashMap<String, String> returnAttributeList =new LinkedHashMap<>();
136 returnAttributeList.put(modelName, utils.getAttributeString());
137 msAttributes.setAttribute(returnAttributeList);
139 msAttributes.setSubClass(utils.getRetmap());
141 msAttributes.setMatchingSet(utils.getMatchableValues());
143 LinkedHashMap<String, String> returnReferenceList =new LinkedHashMap<>();
145 returnReferenceList.put(modelName, utils.getReferenceAttributes());
146 msAttributes.setRefAttribute(returnReferenceList);
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("=");
154 enumList.put(strArr[0], strArr[1]);
157 msAttributes.setEnumType(enumList);
160 classMap=new LinkedHashMap<>();
161 classMap.put(modelName, msAttributes);
163 } catch (Exception e) {
164 logger.error("Failed to process yml model" + e);
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) {
175 resultList.add(file);
176 } else if (file.isDirectory()) {
177 resultList.addAll(listModelFiles(file.getAbsolutePath()));
183 @SuppressWarnings("rawtypes")
184 private void extractFolder(String zipFile) {
186 File file = new File(zipFile);
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();
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();
203 destinationParent.mkdirs();
205 if (!entry.isDirectory()){
206 BufferedInputStream is = new BufferedInputStream(zip
207 .getInputStream(entry));
210 byte data[] = new byte[BUFFER];
211 try(FileOutputStream fos = new FileOutputStream(destFile);
212 BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) {
214 while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
215 dest.write(data, 0, currentByte);
222 if (currentEntry.endsWith(".zip")){
223 extractFolder(destFile.getAbsolutePath());
226 } catch (IOException e) {
227 logger.error("Failed to unzip model file " + zipFile + e);
232 } catch (Exception e) {
233 logger.error("Exception Occured while closing the zip file"+e);
239 public Map<String, String> addValuesToNewModel(String type) {
241 Map<String, String> successMap = new HashMap<>();
242 MSAttributeObject mainClass = null;
243 List<String> dependency = null;
244 String subAttribute = null;
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");
252 mainClass = classMap.get(this.newModel.getModelName());
255 if(".yml".equalsIgnoreCase(type)){
257 newModel.setDependency("[]");
258 if(mainClass.getSubClass() != null){
259 String value = new Gson().toJson(mainClass.getSubClass());
260 newModel.setSub_attributes(value);
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);
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);
277 if(mainClass.getEnumType() != null){
278 this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", ""));
281 if(mainClass.getMatchingSet() != null){
282 this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", ""));
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());
303 subAttribute = utils.createSubAttributes(dependency, classMap, this.newModel.getModelName());
305 this.newModel.setSub_attributes(subAttribute);
306 if(mainClass.getAttribute() != null && !mainClass.getAttribute().isEmpty()){
307 this.newModel.setAttributes(mainClass.getAttribute().toString().replace("{", "").replace("}", ""));
310 if(mainClass.getRefAttribute() != null && !mainClass.getRefAttribute().isEmpty()){
311 this.newModel.setRef_attributes(mainClass.getRefAttribute().toString().replace("{", "").replace("}", ""));
314 if(mainClass.getEnumType() != null && !mainClass.getEnumType().isEmpty()){
315 this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", ""));
318 if(mainClass.getMatchingSet() != null && !mainClass.getMatchingSet().isEmpty()){
319 this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", ""));
322 successMap.put("success", "success");
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");
353 successMap.put("DBError", "EXISTS");
354 logger.error("Import new service failed. Service already exists");