b57bb5d1d836f080052683bcde0464e3fc7cb347
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.so.apihandlerinfra;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.stream.Collectors;
29 import javax.transaction.Transactional;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.QueryParam;
34 import javax.ws.rs.core.Response;
35 import org.apache.http.HttpStatus;
36 import org.onap.so.apihandler.common.ErrorNumbers;
37 import org.onap.so.apihandler.common.ResponseBuilder;
38 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
39 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
40 import org.onap.so.apihandlerinfra.workflowspecificationbeans.ActivitySequence;
41 import org.onap.so.apihandlerinfra.workflowspecificationbeans.ArtifactInfo;
42 import org.onap.so.apihandlerinfra.workflowspecificationbeans.Validation;
43 import org.onap.so.apihandlerinfra.workflowspecificationbeans.WorkflowInputParameter;
44 import org.onap.so.apihandlerinfra.workflowspecificationbeans.WorkflowSpecification;
45 import org.onap.so.apihandlerinfra.workflowspecificationbeans.WorkflowSpecificationList;
46 import org.onap.so.apihandlerinfra.workflowspecificationbeans.WorkflowSpecifications;
47 import org.onap.so.db.catalog.beans.ActivitySpec;
48 import org.onap.so.db.catalog.beans.ActivitySpecUserParameters;
49 import org.onap.so.db.catalog.beans.UserParameters;
50 import org.onap.so.db.catalog.beans.Workflow;
51 import org.onap.so.db.catalog.beans.WorkflowActivitySpecSequence;
52 import org.onap.so.db.catalog.client.CatalogDbClient;
53 import org.onap.so.logger.ErrorCode;
54 import org.onap.so.logger.MessageEnum;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.stereotype.Component;
59 import com.fasterxml.jackson.core.JsonProcessingException;
60 import com.fasterxml.jackson.databind.DeserializationFeature;
61 import com.fasterxml.jackson.databind.ObjectMapper;
62 import io.swagger.annotations.Api;
63 import io.swagger.annotations.ApiOperation;
64
65 @Path("onap/so/infra/workflowSpecifications")
66 @Api(value = "onap/so/infra/workflowSpecifications", description = "Queries of Workflow Specifications")
67 @Component
68 public class WorkflowSpecificationsHandler {
69
70     @Autowired
71     private ResponseBuilder builder;
72
73     @Autowired
74     private CatalogDbClient catalogDbClient;
75
76     private static Logger logger = LoggerFactory.getLogger(WorkflowSpecificationsHandler.class);
77     private static final String ARTIFACT_TYPE_WORKFLOW = "workflow";
78     private static final String NATIVE_WORKFLOW = "native";
79
80     @Path("/{version:[vV]1}/workflows")
81     @GET
82     @ApiOperation(value = "Finds Workflow Specifications", response = Response.class)
83     @Transactional
84
85     public Response queryWorkflowSpecifications(@QueryParam("vnfModelVersionId") String vnfModelVersionId,
86             @PathParam("version") String version) throws Exception {
87
88         String apiVersion = version.substring(1);
89
90         ObjectMapper mapper1 = new ObjectMapper();
91         mapper1.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
92
93         List<Workflow> workflows = catalogDbClient.findWorkflowByModelUUID(vnfModelVersionId);
94
95         List<Workflow> nativeWorkflows = catalogDbClient.findWorkflowBySource(NATIVE_WORKFLOW);
96         if (nativeWorkflows != null && !nativeWorkflows.isEmpty()) {
97             workflows.addAll(nativeWorkflows);
98         }
99
100         WorkflowSpecifications workflowSpecifications = mapWorkflowsToWorkflowSpecifications(workflows);
101
102         String jsonResponse;
103         try {
104             ObjectMapper mapper = new ObjectMapper();
105             jsonResponse = mapper.writeValueAsString(workflowSpecifications);
106         } catch (JsonProcessingException e) {
107             ErrorLoggerInfo errorLoggerInfo =
108                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
109                             .build();
110             ValidateException validateException =
111                     new ValidateException.Builder("Mapping of request to JSON object failed : " + e.getMessage(),
112                             HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
113                                     .errorInfo(errorLoggerInfo).build();
114             throw validateException;
115         }
116
117         return builder.buildResponse(HttpStatus.SC_OK, "", jsonResponse, apiVersion);
118     }
119
120     protected WorkflowSpecifications mapWorkflowsToWorkflowSpecifications(List<Workflow> workflows) {
121         if (workflows == null || workflows.isEmpty()) {
122             return null;
123         }
124         WorkflowSpecifications workflowSpecifications = new WorkflowSpecifications();
125         List<WorkflowSpecificationList> workflowSpecificationList = new ArrayList<>();
126
127         for (Workflow workflow : workflows) {
128             WorkflowSpecificationList workflowSpecificationListItem = new WorkflowSpecificationList();
129             WorkflowSpecification workflowSpecification = new WorkflowSpecification();
130             workflowSpecification.setArtifactInfo(buildArtifactInfo(workflow));
131             workflowSpecification.setActivitySequence(buildActivitySequence(workflow));
132             workflowSpecification.setWorkflowInputParameters(buildWorkflowInputParameters(workflow));
133             workflowSpecificationListItem.setWorkflowSpecification(workflowSpecification);
134             workflowSpecificationList.add(workflowSpecificationListItem);
135         }
136         workflowSpecifications.setWorkflowSpecificationList(workflowSpecificationList);
137         return workflowSpecifications;
138     }
139
140     private ArtifactInfo buildArtifactInfo(Workflow workflow) {
141         ArtifactInfo artifactInfo = new ArtifactInfo();
142         artifactInfo.setArtifactType(ARTIFACT_TYPE_WORKFLOW);
143         artifactInfo.setArtifactUuid(workflow.getArtifactUUID());
144         artifactInfo.setArtifactName(workflow.getArtifactName());
145         if (workflow.getVersion() != null) {
146             artifactInfo.setArtifactVersion(workflow.getVersion().toString());
147         }
148         artifactInfo.setArtifactDescription(workflow.getDescription());
149         artifactInfo.setWorkflowName(workflow.getName());
150         artifactInfo.setOperationName(workflow.getOperationName());
151         artifactInfo.setWorkflowSource(workflow.getSource());
152         artifactInfo.setWorkflowResourceTarget(workflow.getResourceTarget());
153         return artifactInfo;
154     }
155
156     private List<ActivitySequence> buildActivitySequence(Workflow workflow) {
157         List<WorkflowActivitySpecSequence> workflowActivitySpecSequences = workflow.getWorkflowActivitySpecSequence();
158         if (workflowActivitySpecSequences == null || workflowActivitySpecSequences.isEmpty()) {
159             return null;
160         }
161         List<ActivitySequence> activitySequences = new ArrayList<>();
162         for (WorkflowActivitySpecSequence workflowActivitySpecSequence : workflowActivitySpecSequences) {
163             if (workflowActivitySpecSequence != null) {
164                 ActivitySpec activitySpec = workflowActivitySpecSequence.getActivitySpec();
165                 if (activitySpec != null) {
166                     ActivitySequence activitySequence = new ActivitySequence();
167                     activitySequence.setName(activitySpec.getName());
168                     logger.debug("Adding activity: " + activitySpec.getName());
169                     activitySequence.setDescription(activitySpec.getDescription());
170                     activitySequences.add(activitySequence);
171                 }
172             }
173         }
174         return activitySequences;
175     }
176
177     private List<WorkflowInputParameter> buildWorkflowInputParameters(Workflow workflow) {
178         List<WorkflowActivitySpecSequence> workflowActivitySpecSequences = workflow.getWorkflowActivitySpecSequence();
179         if (workflowActivitySpecSequences == null || workflowActivitySpecSequences.isEmpty()) {
180             return new ArrayList<>();
181         }
182         Map<String, WorkflowInputParameter> workflowInputParameterMap = new HashMap<>();
183         for (WorkflowActivitySpecSequence workflowActivitySpecSequence : workflowActivitySpecSequences) {
184             if (workflowActivitySpecSequence != null) {
185                 ActivitySpec activitySpec = workflowActivitySpecSequence.getActivitySpec();
186                 if (activitySpec != null) {
187                     List<ActivitySpecUserParameters> activitySpecUserParameters =
188                             activitySpec.getActivitySpecUserParameters();
189                     if (activitySpecUserParameters != null && !activitySpecUserParameters.isEmpty()) {
190                         for (ActivitySpecUserParameters activitySpecUserParameter : activitySpecUserParameters) {
191                             UserParameters userParameter = activitySpecUserParameter.getUserParameters();
192                             if (userParameter != null) {
193                                 WorkflowInputParameter workflowInputParameter =
194                                         buildWorkflowInputParameter(userParameter);
195                                 workflowInputParameterMap.put(userParameter.getName(), workflowInputParameter);
196                             }
197                         }
198                     }
199                 }
200             }
201         }
202
203         if (workflowInputParameterMap.size() == 0) {
204             return new ArrayList<>();
205         }
206         List<WorkflowInputParameter> workflowInputParameterList =
207                 workflowInputParameterMap.values().stream().collect(Collectors.toList());
208         return workflowInputParameterList;
209     }
210
211     private WorkflowInputParameter buildWorkflowInputParameter(UserParameters userParameter) {
212         WorkflowInputParameter workflowInputParameter = new WorkflowInputParameter();
213         workflowInputParameter.setLabel(userParameter.getLabel());
214         workflowInputParameter.setInputType(userParameter.getType());
215         workflowInputParameter.setRequired(userParameter.getIsRequried());
216         workflowInputParameter.setSoFieldName(userParameter.getName());
217         workflowInputParameter.setSoPayloadLocation(userParameter.getPayloadLocation());
218         workflowInputParameter.setValidation(buildValidationList(userParameter));
219         return workflowInputParameter;
220     }
221
222     private List<Validation> buildValidationList(UserParameters userParameter) {
223         List<Validation> validationList = null;
224         if (userParameter.getMaxLength() != null || userParameter.getAllowableChars() != null) {
225             validationList = new ArrayList<>();
226             Validation validation = new Validation();
227             if (userParameter.getMaxLength() != null) {
228                 validation.setMaxLength(userParameter.getMaxLength().toString());
229             }
230             validation.setAllowableChars(userParameter.getAllowableChars());
231             validationList.add(validation);
232         }
233         return validationList;
234     }
235 }