bdb45b53aa73d84ab4a311337b12a74a41100295
[aai/babel.git] / src / main / java / org / onap / aai / babel / service / GenerateArtifactsServiceImpl.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.babel.service;
24
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;
49
50
51 /**
52  * Generate SDC Artifacts by passing in a CSAR payload, Artifact Name and Artifact version
53  */
54 public class GenerateArtifactsServiceImpl implements GenerateArtifactsService {
55     private static Logger applicationLogger = LoggerFactory.getInstance().getLogger(GenerateArtifactsServiceImpl.class);
56
57     private AAIMicroServiceAuth aaiMicroServiceAuth;
58
59     /**
60      * @param authorization
61      */
62     @Inject
63     public GenerateArtifactsServiceImpl(final AAIMicroServiceAuth authorization) {
64         this.aaiMicroServiceAuth = authorization;
65     }
66
67     /*
68      * (non-Javadoc)
69      * 
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)
72      */
73     @Override
74     public Response generateArtifacts(UriInfo uriInfo, HttpHeaders headers, HttpServletRequest servletRequest,
75             String requestBody) throws AAIAuthException {
76         applicationLogger.debug("Received request: " + requestBody);
77
78         Response response;
79         try {
80             boolean authorized = aaiMicroServiceAuth.validateRequest(headers, servletRequest,
81                     AAIMicroServiceAuthCore.HTTP_METHODS.POST, uriInfo.getPath(false));
82
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);
87             throw e;
88         }
89
90         applicationLogger.debug("Sending response: " + response.getStatus() + " " + response.getEntity().toString());
91         return response;
92     }
93
94
95     /**
96      * Generate XML model artifacts from request body.
97      * 
98      * @param requestBody the request body in JSON format
99      * @return response object containing the generated XML models
100      */
101     protected Response generateArtifacts(String requestBody) {
102         Response response;
103
104         try {
105             Gson gson = new GsonBuilder().disableHtmlEscaping().create();
106
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));
113
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");
127         }
128
129         return response;
130     }
131
132     /**
133      * Helper method to create a REST response object.
134      * 
135      * @param status response status code
136      * @param entity response payload
137      * @return
138      */
139     private Response buildResponse(Status status, String entity) {
140         //@formatter:off
141         return Response
142                 .status(status)
143                 .entity(entity)
144                 .type(MediaType.TEXT_PLAIN)
145                 .build();
146         //@formatter:on
147     }
148 }