bc4f389469df03a0beca07152c94553942a2e70a
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandlerinfra / WorkflowSpecificationsHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 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.onap.so.apihandlerinfra;
21
22 import java.nio.file.Files;
23 import java.nio.file.Paths;
24 import javax.transaction.Transactional;
25 import javax.ws.rs.GET;
26 import javax.ws.rs.Path;
27 import javax.ws.rs.PathParam;
28 import javax.ws.rs.QueryParam;
29 import javax.ws.rs.core.Response;
30 import org.apache.http.HttpStatus;
31 import org.onap.so.apihandler.common.ErrorNumbers;
32 import org.onap.so.apihandler.common.ResponseBuilder;
33 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
34 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
35 import org.onap.so.apihandlerinfra.workflowspecificationbeans.WorkflowSpecifications;
36 import org.onap.so.logger.ErrorCode;
37 import org.onap.so.logger.MessageEnum;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
40 import com.fasterxml.jackson.core.JsonProcessingException;
41 import com.fasterxml.jackson.databind.DeserializationFeature;
42 import com.fasterxml.jackson.databind.ObjectMapper;
43 import io.swagger.annotations.Api;
44 import io.swagger.annotations.ApiOperation;
45
46 @Path("onap/so/infra/workflowSpecifications")
47 @Api(value = "onap/so/infra/workflowSpecifications", description = "Queries of Workflow Specifications")
48 @Component
49 public class WorkflowSpecificationsHandler {
50
51     @Autowired
52     private ResponseBuilder builder;
53
54     @Path("/{version:[vV]1}/workflows")
55     @GET
56     @ApiOperation(value = "Finds Workflow Specifications", response = Response.class)
57     @Transactional
58     public Response queryFilters(@QueryParam("vnfModelVersionId") String vnfModelVersionId,
59             @PathParam("version") String version) throws Exception {
60
61         String apiVersion = version.substring(1);
62
63         ObjectMapper mapper1 = new ObjectMapper();
64         mapper1.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
65
66         // Replace with Catalog DB Query
67         WorkflowSpecifications workflowSpecifications = mapper1.readValue(
68                 new String(Files.readAllBytes(Paths.get("src/test/resources/__files/WorkflowSpecifications.json"))),
69                 WorkflowSpecifications.class);
70
71         String jsonResponse = null;
72         try {
73             ObjectMapper mapper = new ObjectMapper();
74             jsonResponse = mapper.writeValueAsString(workflowSpecifications);
75         } catch (JsonProcessingException e) {
76             ErrorLoggerInfo errorLoggerInfo =
77                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
78                             .build();
79             ValidateException validateException =
80                     new ValidateException.Builder("Mapping of request to JSON object failed : " + e.getMessage(),
81                             HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
82                                     .errorInfo(errorLoggerInfo).build();
83             throw validateException;
84         }
85
86         return builder.buildResponse(HttpStatus.SC_OK, "", jsonResponse, apiVersion);
87     }
88 }