890b54758e020f53835e2141caaa37f6680011ca
[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.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.stream.Collectors;
27 import javax.transaction.Transactional;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.PathParam;
31 import javax.ws.rs.QueryParam;
32 import javax.ws.rs.core.Response;
33 import org.apache.http.HttpStatus;
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 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
38 import org.onap.so.apihandlerinfra.workflowspecificationbeans.ActivitySequence;
39 import org.onap.so.apihandlerinfra.workflowspecificationbeans.ArtifactInfo;
40 import org.onap.so.apihandlerinfra.workflowspecificationbeans.Validation;
41 import org.onap.so.apihandlerinfra.workflowspecificationbeans.WorkflowInputParameter;
42 import org.onap.so.apihandlerinfra.workflowspecificationbeans.WorkflowSpecification;
43 import org.onap.so.apihandlerinfra.workflowspecificationbeans.WorkflowSpecificationList;
44 import org.onap.so.apihandlerinfra.workflowspecificationbeans.WorkflowSpecifications;
45 import org.onap.so.db.catalog.beans.ActivitySpec;
46 import org.onap.so.db.catalog.beans.ActivitySpecUserParameters;
47 import org.onap.so.db.catalog.beans.UserParameters;
48 import org.onap.so.db.catalog.beans.Workflow;
49 import org.onap.so.db.catalog.beans.WorkflowActivitySpecSequence;
50 import org.onap.so.db.catalog.client.CatalogDbClient;
51 import org.onap.so.logger.ErrorCode;
52 import org.onap.so.logger.MessageEnum;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.stereotype.Component;
57 import com.fasterxml.jackson.core.JsonProcessingException;
58 import com.fasterxml.jackson.databind.DeserializationFeature;
59 import com.fasterxml.jackson.databind.ObjectMapper;
60 import io.swagger.annotations.Api;
61 import io.swagger.annotations.ApiOperation;
62
63 @Path("onap/so/infra/workflowSpecifications")
64 @Api(value = "onap/so/infra/workflowSpecifications", description = "Queries of Workflow Specifications")
65 @Component
66 public class WorkflowSpecificationsHandler {
67
68     @Autowired
69     private ResponseBuilder builder;
70
71     @Autowired
72     private CatalogDbClient catalogDbClient;
73
74     private static Logger logger = LoggerFactory.getLogger(WorkflowSpecificationsHandler.class);
75     private static final String ARTIFACT_TYPE_WORKFLOW = "workflow";
76
77     @Path("/{version:[vV]1}/workflows")
78     @GET
79     @ApiOperation(value = "Finds Workflow Specifications", response = Response.class)
80     @Transactional
81
82     public Response queryWorkflowSpecifications(@QueryParam("vnfModelVersionId") String vnfModelVersionId,
83             @PathParam("version") String version) throws Exception {
84
85         String apiVersion = version.substring(1);
86
87         ObjectMapper mapper1 = new ObjectMapper();
88         mapper1.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
89
90         List<Workflow> workflows = catalogDbClient.findWorkflowByModelUUID(vnfModelVersionId);
91         WorkflowSpecifications workflowSpecifications = mapWorkflowsToWorkflowSpecifications(workflows);
92
93         String jsonResponse = null;
94         try {
95             ObjectMapper mapper = new ObjectMapper();
96             jsonResponse = mapper.writeValueAsString(workflowSpecifications);
97         } catch (JsonProcessingException e) {
98             ErrorLoggerInfo errorLoggerInfo =
99                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
100                             .build();
101             ValidateException validateException =
102                     new ValidateException.Builder("Mapping of request to JSON object failed : " + e.getMessage(),
103                             HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
104                                     .errorInfo(errorLoggerInfo).build();
105             throw validateException;
106         }
107
108         return builder.buildResponse(HttpStatus.SC_OK, "", jsonResponse, apiVersion);
109     }
110
111     protected WorkflowSpecifications mapWorkflowsToWorkflowSpecifications(List<Workflow> workflows) {
112         if (workflows == null || workflows.size() == 0) {
113             return null;
114         }
115         WorkflowSpecifications workflowSpecifications = new WorkflowSpecifications();
116         List<WorkflowSpecificationList> workflowSpecificationList = new ArrayList<WorkflowSpecificationList>();
117
118         for (Workflow workflow : workflows) {
119             WorkflowSpecificationList workflowSpecificationListItem = new WorkflowSpecificationList();
120             WorkflowSpecification workflowSpecification = new WorkflowSpecification();
121             workflowSpecification.setArtifactInfo(buildArtifactInfo(workflow));
122             workflowSpecification.setActivitySequence(buildActivitySequence(workflow));
123             workflowSpecification.setWorkflowInputParameters(buildWorkflowInputParameters(workflow));
124             workflowSpecificationListItem.setWorkflowSpecification(workflowSpecification);
125             workflowSpecificationList.add(workflowSpecificationListItem);
126         }
127         workflowSpecifications.setWorkflowSpecificationList(workflowSpecificationList);
128         return workflowSpecifications;
129     }
130
131     private ArtifactInfo buildArtifactInfo(Workflow workflow) {
132         ArtifactInfo artifactInfo = new ArtifactInfo();
133         artifactInfo.setArtifactType(ARTIFACT_TYPE_WORKFLOW);
134         artifactInfo.setArtifactUuid(workflow.getArtifactUUID());
135         artifactInfo.setArtifactName(workflow.getArtifactName());
136         if (workflow.getVersion() != null) {
137             artifactInfo.setArtifactVersion(workflow.getVersion().toString());
138         }
139         artifactInfo.setArtifactDescription(workflow.getDescription());
140         artifactInfo.setWorkflowName(workflow.getName());
141         artifactInfo.setOperationName(workflow.getOperationName());
142         artifactInfo.setWorkflowSource(workflow.getSource());
143         artifactInfo.setWorkflowResourceTarget(workflow.getResourceTarget());
144         return artifactInfo;
145     }
146
147     private List<ActivitySequence> buildActivitySequence(Workflow workflow) {
148         List<WorkflowActivitySpecSequence> workflowActivitySpecSequences = workflow.getWorkflowActivitySpecSequence();
149         if (workflowActivitySpecSequences == null || workflowActivitySpecSequences.size() == 0) {
150             return null;
151         }
152         List<ActivitySequence> activitySequences = new ArrayList<ActivitySequence>();
153         for (WorkflowActivitySpecSequence workflowActivitySpecSequence : workflowActivitySpecSequences) {
154             if (workflowActivitySpecSequence != null) {
155                 ActivitySpec activitySpec = workflowActivitySpecSequence.getActivitySpec();
156                 if (activitySpec != null) {
157                     ActivitySequence activitySequence = new ActivitySequence();
158                     activitySequence.setName(activitySpec.getName());
159                     logger.debug("Adding activity: " + activitySpec.getName());
160                     activitySequence.setDescription(activitySpec.getDescription());
161                     activitySequences.add(activitySequence);
162                 }
163             }
164         }
165         return activitySequences;
166     }
167
168     private List<WorkflowInputParameter> buildWorkflowInputParameters(Workflow workflow) {
169         List<WorkflowActivitySpecSequence> workflowActivitySpecSequences = workflow.getWorkflowActivitySpecSequence();
170         if (workflowActivitySpecSequences == null || workflowActivitySpecSequences.size() == 0) {
171             return null;
172         }
173         Map<String, WorkflowInputParameter> workflowInputParameterMap = new HashMap<String, WorkflowInputParameter>();
174         for (WorkflowActivitySpecSequence workflowActivitySpecSequence : workflowActivitySpecSequences) {
175             if (workflowActivitySpecSequence != null) {
176                 ActivitySpec activitySpec = workflowActivitySpecSequence.getActivitySpec();
177                 if (activitySpec != null) {
178                     List<ActivitySpecUserParameters> activitySpecUserParameters =
179                             activitySpec.getActivitySpecUserParameters();
180                     if (activitySpecUserParameters != null && activitySpecUserParameters.size() != 0) {
181                         for (ActivitySpecUserParameters activitySpecUserParameter : activitySpecUserParameters) {
182                             UserParameters userParameter = activitySpecUserParameter.getUserParameters();
183                             if (userParameter != null) {
184                                 WorkflowInputParameter workflowInputParameter =
185                                         buildWorkflowInputParameter(userParameter);
186                                 workflowInputParameterMap.put(userParameter.getName(), workflowInputParameter);
187                             }
188                         }
189                     }
190                 }
191             }
192         }
193
194         if (workflowInputParameterMap.size() == 0) {
195             return null;
196         }
197         List<WorkflowInputParameter> workflowInputParameterList =
198                 workflowInputParameterMap.values().stream().collect(Collectors.toList());
199         return workflowInputParameterList;
200     }
201
202     private WorkflowInputParameter buildWorkflowInputParameter(UserParameters userParameter) {
203         WorkflowInputParameter workflowInputParameter = new WorkflowInputParameter();
204         workflowInputParameter.setLabel(userParameter.getLabel());
205         workflowInputParameter.setInputType(userParameter.getType());
206         workflowInputParameter.setRequired(userParameter.getIsRequried());
207         workflowInputParameter.setSoFieldName(userParameter.getName());
208         workflowInputParameter.setSoPayloadLocation(userParameter.getPayloadLocation());
209         workflowInputParameter.setValidation(buildValidationList(userParameter));
210         return workflowInputParameter;
211     }
212
213     private List<Validation> buildValidationList(UserParameters userParameter) {
214         List<Validation> validationList = null;
215         if (userParameter.getMaxLength() != null || userParameter.getAllowableChars() != null) {
216             validationList = new ArrayList<Validation>();
217             Validation validation = new Validation();
218             if (userParameter.getMaxLength() != null) {
219                 validation.setMaxLength(userParameter.getMaxLength().toString());
220             }
221             validation.setAllowableChars(userParameter.getAllowableChars());
222             validationList.add(validation);
223         }
224         return validationList;
225     }
226 }