Return empty WorkflowInputParameters
[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     private static final String NATIVE_WORKFLOW = "native";
77
78     @Path("/{version:[vV]1}/workflows")
79     @GET
80     @ApiOperation(value = "Finds Workflow Specifications", response = Response.class)
81     @Transactional
82
83     public Response queryWorkflowSpecifications(@QueryParam("vnfModelVersionId") String vnfModelVersionId,
84             @PathParam("version") String version) throws Exception {
85
86         String apiVersion = version.substring(1);
87
88         ObjectMapper mapper1 = new ObjectMapper();
89         mapper1.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
90
91         List<Workflow> workflows = catalogDbClient.findWorkflowByModelUUID(vnfModelVersionId);
92
93         List<Workflow> nativeWorkflows = catalogDbClient.findWorkflowBySource(NATIVE_WORKFLOW);
94         if (nativeWorkflows != null && nativeWorkflows.size() != 0) {
95             workflows.addAll(nativeWorkflows);
96         }
97
98         WorkflowSpecifications workflowSpecifications = mapWorkflowsToWorkflowSpecifications(workflows);
99
100         String jsonResponse = null;
101         try {
102             ObjectMapper mapper = new ObjectMapper();
103             jsonResponse = mapper.writeValueAsString(workflowSpecifications);
104         } catch (JsonProcessingException e) {
105             ErrorLoggerInfo errorLoggerInfo =
106                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
107                             .build();
108             ValidateException validateException =
109                     new ValidateException.Builder("Mapping of request to JSON object failed : " + e.getMessage(),
110                             HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
111                                     .errorInfo(errorLoggerInfo).build();
112             throw validateException;
113         }
114
115         return builder.buildResponse(HttpStatus.SC_OK, "", jsonResponse, apiVersion);
116     }
117
118     protected WorkflowSpecifications mapWorkflowsToWorkflowSpecifications(List<Workflow> workflows) {
119         if (workflows == null || workflows.size() == 0) {
120             return null;
121         }
122         WorkflowSpecifications workflowSpecifications = new WorkflowSpecifications();
123         List<WorkflowSpecificationList> workflowSpecificationList = new ArrayList<WorkflowSpecificationList>();
124
125         for (Workflow workflow : workflows) {
126             WorkflowSpecificationList workflowSpecificationListItem = new WorkflowSpecificationList();
127             WorkflowSpecification workflowSpecification = new WorkflowSpecification();
128             workflowSpecification.setArtifactInfo(buildArtifactInfo(workflow));
129             workflowSpecification.setActivitySequence(buildActivitySequence(workflow));
130             workflowSpecification.setWorkflowInputParameters(buildWorkflowInputParameters(workflow));
131             workflowSpecificationListItem.setWorkflowSpecification(workflowSpecification);
132             workflowSpecificationList.add(workflowSpecificationListItem);
133         }
134         workflowSpecifications.setWorkflowSpecificationList(workflowSpecificationList);
135         return workflowSpecifications;
136     }
137
138     private ArtifactInfo buildArtifactInfo(Workflow workflow) {
139         ArtifactInfo artifactInfo = new ArtifactInfo();
140         artifactInfo.setArtifactType(ARTIFACT_TYPE_WORKFLOW);
141         artifactInfo.setArtifactUuid(workflow.getArtifactUUID());
142         artifactInfo.setArtifactName(workflow.getArtifactName());
143         if (workflow.getVersion() != null) {
144             artifactInfo.setArtifactVersion(workflow.getVersion().toString());
145         }
146         artifactInfo.setArtifactDescription(workflow.getDescription());
147         artifactInfo.setWorkflowName(workflow.getName());
148         artifactInfo.setOperationName(workflow.getOperationName());
149         artifactInfo.setWorkflowSource(workflow.getSource());
150         artifactInfo.setWorkflowResourceTarget(workflow.getResourceTarget());
151         return artifactInfo;
152     }
153
154     private List<ActivitySequence> buildActivitySequence(Workflow workflow) {
155         List<WorkflowActivitySpecSequence> workflowActivitySpecSequences = workflow.getWorkflowActivitySpecSequence();
156         if (workflowActivitySpecSequences == null || workflowActivitySpecSequences.size() == 0) {
157             return null;
158         }
159         List<ActivitySequence> activitySequences = new ArrayList<ActivitySequence>();
160         for (WorkflowActivitySpecSequence workflowActivitySpecSequence : workflowActivitySpecSequences) {
161             if (workflowActivitySpecSequence != null) {
162                 ActivitySpec activitySpec = workflowActivitySpecSequence.getActivitySpec();
163                 if (activitySpec != null) {
164                     ActivitySequence activitySequence = new ActivitySequence();
165                     activitySequence.setName(activitySpec.getName());
166                     logger.debug("Adding activity: " + activitySpec.getName());
167                     activitySequence.setDescription(activitySpec.getDescription());
168                     activitySequences.add(activitySequence);
169                 }
170             }
171         }
172         return activitySequences;
173     }
174
175     private List<WorkflowInputParameter> buildWorkflowInputParameters(Workflow workflow) {
176         List<WorkflowActivitySpecSequence> workflowActivitySpecSequences = workflow.getWorkflowActivitySpecSequence();
177         if (workflowActivitySpecSequences == null || workflowActivitySpecSequences.size() == 0) {
178             return new ArrayList<WorkflowInputParameter>();
179         }
180         Map<String, WorkflowInputParameter> workflowInputParameterMap = new HashMap<String, WorkflowInputParameter>();
181         for (WorkflowActivitySpecSequence workflowActivitySpecSequence : workflowActivitySpecSequences) {
182             if (workflowActivitySpecSequence != null) {
183                 ActivitySpec activitySpec = workflowActivitySpecSequence.getActivitySpec();
184                 if (activitySpec != null) {
185                     List<ActivitySpecUserParameters> activitySpecUserParameters =
186                             activitySpec.getActivitySpecUserParameters();
187                     if (activitySpecUserParameters != null && activitySpecUserParameters.size() != 0) {
188                         for (ActivitySpecUserParameters activitySpecUserParameter : activitySpecUserParameters) {
189                             UserParameters userParameter = activitySpecUserParameter.getUserParameters();
190                             if (userParameter != null) {
191                                 WorkflowInputParameter workflowInputParameter =
192                                         buildWorkflowInputParameter(userParameter);
193                                 workflowInputParameterMap.put(userParameter.getName(), workflowInputParameter);
194                             }
195                         }
196                     }
197                 }
198             }
199         }
200
201         if (workflowInputParameterMap.size() == 0) {
202             return new ArrayList<WorkflowInputParameter>();
203         }
204         List<WorkflowInputParameter> workflowInputParameterList =
205                 workflowInputParameterMap.values().stream().collect(Collectors.toList());
206         return workflowInputParameterList;
207     }
208
209     private WorkflowInputParameter buildWorkflowInputParameter(UserParameters userParameter) {
210         WorkflowInputParameter workflowInputParameter = new WorkflowInputParameter();
211         workflowInputParameter.setLabel(userParameter.getLabel());
212         workflowInputParameter.setInputType(userParameter.getType());
213         workflowInputParameter.setRequired(userParameter.getIsRequried());
214         workflowInputParameter.setSoFieldName(userParameter.getName());
215         workflowInputParameter.setSoPayloadLocation(userParameter.getPayloadLocation());
216         workflowInputParameter.setValidation(buildValidationList(userParameter));
217         return workflowInputParameter;
218     }
219
220     private List<Validation> buildValidationList(UserParameters userParameter) {
221         List<Validation> validationList = null;
222         if (userParameter.getMaxLength() != null || userParameter.getAllowableChars() != null) {
223             validationList = new ArrayList<Validation>();
224             Validation validation = new Validation();
225             if (userParameter.getMaxLength() != null) {
226                 validation.setMaxLength(userParameter.getMaxLength().toString());
227             }
228             validation.setAllowableChars(userParameter.getAllowableChars());
229             validationList.add(validation);
230         }
231         return validationList;
232     }
233 }