* ============LICENSE_START=======================================================
* ONAP-PAP-REST
* ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* limitations under the License.
* ============LICENSE_END=========================================================
*/
+
package org.onap.policy.pap.xacml.rest.service;
import java.io.BufferedWriter;
public class ImportService {
private static final Logger logger = FlexLogger.getLogger(ImportService.class);
private static String errorMessage = "Error in reading in file from API call";
- private static String errorMsg = "error";
+ private static String errorMsg = "error";
private static String operation = "operation";
private static String importHeader = "import";
private static String service = "service";
String version = request.getParameter("version");
String serviceName = request.getParameter("serviceName");
- if(serviceName == null || serviceName.isEmpty() || !serviceName.matches(REGEX)){
+ if (serviceName == null || serviceName.isEmpty() || !serviceName.matches(REGEX)) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.addHeader(errorMsg, MISSING);
response.addHeader(operation, importHeader);
String description = request.getParameter("description");
Map<String, String> successMap = new HashMap<>();
- if(("BRMSPARAM").equals(importServiceCreation)){
+ if (("BRMSPARAM").equals(importServiceCreation)) {
StringBuilder builder = new StringBuilder();
int ch;
try {
- while((ch = request.getInputStream().read()) != -1){
- builder.append((char)ch);
+ while ((ch = request.getInputStream().read()) != -1) {
+ builder.append((char) ch);
}
} catch (IOException e) {
logger.error(e);
}
CreateBRMSRuleTemplate brmsRuleTemplate = new CreateBRMSRuleTemplate();
successMap = brmsRuleTemplate.addRule(builder.toString(), serviceName, description, "API");
- }
- else if(("MICROSERVICE").equals(importServiceCreation)){
+ } else if (("MICROSERVICE").equals(importServiceCreation)) {
CreateNewMicroServiceModel newMS = null;
String randomID = UUID.randomUUID().toString();
String type = ".xmi";
- if ( fileName != null) {
+ if (fileName != null) {
File extracDir = new File(extractDir);
- if (!extracDir.exists()){
+ if (!extracDir.exists()) {
extracDir.mkdirs();
}
- if (fileName.contains(".xmi") || fileName.contains(".yml")){
- if(fileName.contains(".yml")){
+ if (fileName.contains(".xmi") || fileName.contains(".yml")) {
+ if (fileName.contains(".yml")) {
type = ".yml";
}
// get the request content into a String
try {
scanner = new java.util.Scanner(request.getInputStream());
scanner.useDelimiter("\\A");
- xmi = scanner.hasNext() ? scanner.next() : "";
+ xmi = scanner.hasNext() ? scanner.next() : "";
scanner.close();
} catch (IOException e1) {
logger.error(e1);
}
PolicyLogger.info("Request from API to import new Service");
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream(extractDir + File.separator + randomID+type), "utf-8"))) {
+ new FileOutputStream(extractDir + File.separator + randomID + type), "utf-8"))) {
writer.write(xmi);
} catch (IOException e) {
logger.error(e);
PolicyLogger.error(errorMessage);
return;
}
- }else{
+ } else {
InputStream inputStream = null;
- try(FileOutputStream outputStream = new FileOutputStream(extractDir + File.separator + randomID+".zip")) {
+ try (FileOutputStream outputStream =
+ new FileOutputStream(extractDir + File.separator + randomID + ".zip")) {
inputStream = request.getInputStream();
byte[] buffer = new byte[4096];
- int bytesRead = -1 ;
+ int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
- outputStream.write(buffer, 0, bytesRead) ;
+ outputStream.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
- PolicyLogger.error("Error in reading in Zip File from API call"+e);
+ PolicyLogger.error("Error in reading in Zip File from API call" + e);
return;
- }finally{
+ } finally {
try {
- if(inputStream != null){
+ if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
- PolicyLogger.error("Exception Occured while closing the input/output stream"+e);
+ PolicyLogger.error("Exception Occured while closing the input/output stream" + e);
}
}
}
- newMS = new CreateNewMicroServiceModel(fileName, serviceName, "API", version, randomID);
+ newMS = new CreateNewMicroServiceModel(fileName, serviceName, "API", version, randomID);
successMap = newMS.addValuesToNewModel(type);
successMap = newMS.saveImportService();
}
}
- } else if(("OPTIMIZATION").equals(importServiceCreation)){
+ } else if (("OPTIMIZATION").equals(importServiceCreation)) {
CreateNewOptimizationModel newOOF = null;
String randomID = UUID.randomUUID().toString();
- if ( fileName != null) {
+ if (fileName != null) {
File extracDir = new File(extractDir);
- if (!extracDir.exists()){
+ if (!extracDir.exists()) {
extracDir.mkdirs();
}
// get the request content into a String
String yml = null;
- try (java.util.Scanner scanner = new java.util.Scanner(request.getInputStream());){
+ try (java.util.Scanner scanner = new java.util.Scanner(request.getInputStream());) {
scanner.useDelimiter("\\A");
- yml = scanner.hasNext() ? scanner.next() : "";
+ yml = scanner.hasNext() ? scanner.next() : "";
} catch (IOException e1) {
logger.error(e1);
PolicyLogger.error(errorMessage);
}
PolicyLogger.info("Request from API to import new Optimization Service Model");
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream(extractDir + File.separator + randomID+type), "utf-8"))) {
+ new FileOutputStream(extractDir + File.separator + randomID + type), "utf-8"))) {
writer.write(yml);
} catch (IOException e) {
logger.error(e);
return;
}
- newOOF = new CreateNewOptimizationModel(fileName, serviceName, "API Import Service", version, randomID);
+ newOOF = new CreateNewOptimizationModel(fileName, serviceName, "API Import Service", version, randomID);
successMap = newOOF.addValuesToNewModel();
if (successMap.containsKey(successMessage)) {
successMap.clear();
response.addHeader(operation, importHeader);
response.addHeader(service, serviceName);
} else if (successMap.containsKey("DBError")) {
- if (successMap.get("DBError").contains("EXISTS")){
+ if (successMap.get("DBError").contains("EXISTS")) {
response.setStatus(HttpServletResponse.SC_CONFLICT);
response.addHeader(service, serviceName);
response.addHeader(errorMsg, "modelExistsDB");
- }else{
+ } else {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.addHeader(errorMsg, "importDB");
}
response.addHeader(operation, importHeader);
response.addHeader(service, serviceName);
- }else if (successMap.get(errorMsg).contains("MISSING")){
+ } else if (successMap.get(errorMsg).contains("MISSING")) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.addHeader(errorMsg, MISSING);
response.addHeader(operation, importHeader);
response.addHeader(service, serviceName);
- }else if (successMap.get(errorMsg).contains("VALIDATION")){
+ } else if (successMap.get(errorMsg).contains("VALIDATION")) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.addHeader(errorMsg, "validation");
response.addHeader(operation, importHeader);
}
}
-}
\ No newline at end of file
+}