39a0485e8e170a20f55887721569c62fb1803808
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / service / ImportService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-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 package org.openecomp.policy.pap.xacml.rest.service;
21
22 import java.io.BufferedWriter;
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStreamWriter;
28 import java.io.Writer;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.UUID;
32
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35
36 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
37 import org.openecomp.policy.pap.xacml.rest.components.CreateBRMSRuleTemplate;
38 import org.openecomp.policy.pap.xacml.rest.components.CreateNewMicroSerivceModel;
39
40 public class ImportService {
41
42         public void doImportMicroServicePut(HttpServletRequest request, HttpServletResponse response) {
43                 String importServiceCreation = request.getParameter("importService");;
44                 String fileName = request.getParameter("fileName");
45                 String version = request.getParameter("version");
46                 String serviceName = request.getParameter("serviceName");
47                 String description = request.getParameter("description");
48                 Map<String, String> successMap = new HashMap<String, String>();
49                 switch(importServiceCreation){
50                 case "BRMSPARAM":
51                         StringBuilder builder = new StringBuilder();
52                         int ch;
53                         try {
54                                 while((ch = request.getInputStream().read()) != -1){
55                                     builder.append((char)ch);
56                                 }
57                         } catch (IOException e) {
58                                 PolicyLogger.error("Error in reading in file from API call");
59                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
60                                 response.addHeader("error", "missing"); 
61                                 response.addHeader("operation", "import");
62                                 response.addHeader("service", serviceName);
63                         }
64                         CreateBRMSRuleTemplate brmsRuleTemplate = new CreateBRMSRuleTemplate();
65                         successMap = brmsRuleTemplate.addRule(builder.toString(), serviceName, description, "API");
66                         break;
67                 case "MICROSERVICE":
68                         CreateNewMicroSerivceModel newMS = null;
69                         String randomID = UUID.randomUUID().toString();
70                         if ( fileName != null) {
71                                 File extracDir = new File("ExtractDir");
72                                 if (!extracDir.exists()){
73                                         extracDir.mkdirs();
74                                 }
75                                 if (fileName.contains(".xmi")){
76                                         // get the request content into a String
77                                         String xmi = null;
78                                         java.util.Scanner scanner;
79                                         try {
80                                                 scanner = new java.util.Scanner(request.getInputStream());
81                                                 scanner.useDelimiter("\\A");
82                                                 xmi =  scanner.hasNext() ? scanner.next() : "";
83                                                 scanner.close();
84                                         } catch (IOException e1) {
85                                                 PolicyLogger.error("Error in reading in file from API call");
86                                                 return;
87                                         }
88                                         PolicyLogger.info("XML request from API for import new Service"); 
89                                         try (Writer writer = new BufferedWriter(new OutputStreamWriter(
90                                                         new FileOutputStream("ExtractDir" + File.separator + randomID+".xmi"), "utf-8"))) {
91                                                 writer.write(xmi);
92                                         } catch (IOException e) {
93                                                 PolicyLogger.error("Error in reading in file from API call");
94                                                 return;
95                                         }
96                                 }else{ 
97                                         try {   
98                                                 InputStream inputStream = request.getInputStream();
99                                                 FileOutputStream outputStream = new FileOutputStream("ExtractDir" + File.separator + randomID+".zip"); 
100                                                 byte[] buffer = new byte[4096];
101                                                 int bytesRead = -1 ; 
102                                                 while ((bytesRead = inputStream.read(buffer)) != -1) { 
103                                                         outputStream.write(buffer, 0, bytesRead) ; 
104                                                 }
105                                                 outputStream.close() ; 
106                                                 inputStream.close() ;
107                                         } catch (IOException e) {
108                                                 PolicyLogger.error("Error in reading in Zip File from API call");
109                                                 return;
110                                         }
111                                 }
112                                 newMS =  new CreateNewMicroSerivceModel(fileName, serviceName, "API", version, randomID);
113                                 successMap = newMS.addValuesToNewModel();
114                                 if (successMap.containsKey("success")) {
115                                         successMap.clear();
116                                         successMap = newMS.saveImportService();
117                                 }
118                         }
119                         break;
120                 }
121                 // return a response to the PAP             
122                 if (successMap.containsKey("success")) {                                                        
123                         response.setStatus(HttpServletResponse.SC_OK);                                                          
124                         response.addHeader("successMapKey", "success");                                                         
125                         response.addHeader("operation", "import");
126                         response.addHeader("service", serviceName);
127                 } else if (successMap.containsKey("DBError")) {
128                         if (successMap.get("DBError").contains("EXISTS")){
129                                 response.setStatus(HttpServletResponse.SC_CONFLICT);
130                                 response.addHeader("service", serviceName);
131                                 response.addHeader("error", "modelExistsDB");
132                         }else{
133                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
134                                 response.addHeader("error", "importDB");
135                         }
136                         response.addHeader("operation", "import");
137                         response.addHeader("service", serviceName);
138                 }else if (successMap.get("error").contains("MISSING")){
139                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
140                         response.addHeader("error", "missing"); 
141                         response.addHeader("operation", "import");
142                         response.addHeader("service", serviceName);
143                 }else if (successMap.get("error").contains("VALIDATION")){
144                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
145                         response.addHeader("error", "validation");      
146                         response.addHeader("operation", "import");
147                         response.addHeader("service", serviceName);
148                 }
149         }
150
151 }