2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 European Software Marketing 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.babel.service;
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27 import com.google.gson.JsonSyntaxException;
28 import java.util.Base64;
29 import java.util.List;
30 import javax.inject.Inject;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.ws.rs.core.HttpHeaders;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 import javax.ws.rs.core.Response.Status;
36 import javax.ws.rs.core.UriInfo;
37 import org.onap.aai.auth.AAIAuthException;
38 import org.onap.aai.auth.AAIMicroServiceAuth;
39 import org.onap.aai.auth.AAIMicroServiceAuthCore;
40 import org.onap.aai.babel.csar.CsarConverterException;
41 import org.onap.aai.babel.csar.CsarToXmlConverter;
42 import org.onap.aai.babel.logging.ApplicationMsgs;
43 import org.onap.aai.babel.service.data.BabelArtifact;
44 import org.onap.aai.babel.service.data.BabelRequest;
45 import org.onap.aai.babel.util.RequestValidationException;
46 import org.onap.aai.babel.util.RequestValidator;
47 import org.onap.aai.cl.api.Logger;
48 import org.onap.aai.cl.eelf.LoggerFactory;
52 * Generate SDC Artifacts by passing in a CSAR payload, Artifact Name and Artifact version
54 public class GenerateArtifactsServiceImpl implements GenerateArtifactsService {
55 private static Logger applicationLogger = LoggerFactory.getInstance().getLogger(GenerateArtifactsServiceImpl.class);
57 private AAIMicroServiceAuth aaiMicroServiceAuth;
60 * @param authorization
63 public GenerateArtifactsServiceImpl(final AAIMicroServiceAuth authorization) {
64 this.aaiMicroServiceAuth = authorization;
70 * @see org.onap.aai.babel.service.GenerateArtifactsService#generateArtifacts(javax.ws.rs.core.UriInfo,
71 * javax.ws.rs.core.HttpHeaders, javax.servlet.http.HttpServletRequest, java.lang.String)
74 public Response generateArtifacts(UriInfo uriInfo, HttpHeaders headers, HttpServletRequest servletRequest,
75 String requestBody) throws AAIAuthException {
76 applicationLogger.debug("Received request: " + requestBody);
80 boolean authorized = aaiMicroServiceAuth.validateRequest(headers, servletRequest,
81 AAIMicroServiceAuthCore.HTTP_METHODS.POST, uriInfo.getPath(false));
83 response = authorized ? generateArtifacts(requestBody)
84 : buildResponse(Status.UNAUTHORIZED, "User not authorized to perform the operation.");
85 } catch (AAIAuthException e) {
86 applicationLogger.error(ApplicationMsgs.PROCESS_REQUEST_ERROR, e);
90 applicationLogger.debug("Sending response: " + response.getStatus() + " " + response.getEntity().toString());
96 * Generate XML model artifacts from request body.
98 * @param requestBody the request body in JSON format
99 * @return response object containing the generated XML models
101 protected Response generateArtifacts(String requestBody) {
105 Gson gson = new GsonBuilder().disableHtmlEscaping().create();
107 BabelRequest babelRequest = gson.fromJson(requestBody, BabelRequest.class);
108 RequestValidator.validateRequest(babelRequest);
109 byte[] csarFile = Base64.getDecoder().decode(babelRequest.getCsar());
110 List<BabelArtifact> xmlArtifacts = new CsarToXmlConverter().generateXmlFromCsar(csarFile,
111 babelRequest.getArtifactName(), babelRequest.getArtifactVersion());
112 response = buildResponse(Status.OK, gson.toJson(xmlArtifacts));
114 } catch (JsonSyntaxException e) {
115 applicationLogger.error(ApplicationMsgs.INVALID_REQUEST_JSON, e);
116 response = buildResponse(Status.BAD_REQUEST, "Malformed request.");
117 } catch (CsarConverterException e) {
118 applicationLogger.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
119 response = buildResponse(Status.INTERNAL_SERVER_ERROR, "Error converting CSAR artifact to XML model.");
120 } catch (RequestValidationException e) {
121 applicationLogger.error(ApplicationMsgs.PROCESS_REQUEST_ERROR, e);
122 response = buildResponse(Status.BAD_REQUEST, e.getLocalizedMessage());
123 } catch (Exception e) {
124 applicationLogger.error(ApplicationMsgs.PROCESS_REQUEST_ERROR, e);
125 response = buildResponse(Status.INTERNAL_SERVER_ERROR,
126 "Error while processing request. Please check the babel service logs for more details.\n");
133 * Helper method to create a REST response object.
135 * @param status response status code
136 * @param entity response payload
139 private Response buildResponse(Status status, String entity) {
144 .type(MediaType.TEXT_PLAIN)