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