5e82b4f28484dd2b26c9ea07d89d20ead13cc845
[so.git] /
1 package org.onap.so.bpmn.infrastructure.workflow.tasks.validators;
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import java.io.IOException;
5 import java.util.List;
6 import java.util.Optional;
7 import java.util.Set;
8 import java.util.function.Function;
9 import java.util.function.Predicate;
10 import java.util.stream.Collectors;
11 import org.onap.so.bpmn.common.BBConstants;
12 import org.onap.so.bpmn.common.BuildingBlockExecution;
13 import org.onap.so.bpmn.common.listener.validation.PreWorkflowValidator;
14 import org.onap.so.bpmn.infrastructure.workflow.tasks.Resource;
15 import org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowType;
16 import org.onap.so.db.catalog.beans.Service;
17 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
18 import org.onap.so.db.catalog.client.CatalogDbClient;
19 import org.onap.so.serviceinstancebeans.RequestDetails;
20 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
21 import org.springframework.stereotype.Component;
22
23 @Component
24 public class UpgradePreWorkflowValidator implements PreWorkflowValidator {
25
26     private static final String ERR_MSG_INVARIANT_MISMATCH =
27             "Request service modelInvariantId: %s does not match AAI service modelInvariantId: %s";
28     private static final String ERR_MSG_EXISTING_VNFS_NOT_SUPPORTED =
29             "Existing vnfs in AAI are not supported by service model. Unsupported vnfCustomizationIds: %s";
30
31     private final CatalogDbClient catalogDbClient;
32
33     private static final Function<WorkflowType, Predicate<Resource>> resourceType =
34             workflowType -> resource -> resource.getResourceType() == workflowType;
35
36     public UpgradePreWorkflowValidator(CatalogDbClient catalogDbClient) {
37         this.catalogDbClient = catalogDbClient;
38     }
39
40     @Override
41     public boolean shouldRunFor(String requestAction) {
42         return "upgradeInstance".equalsIgnoreCase(requestAction);
43     }
44
45     @Override
46     public Optional<String> validate(BuildingBlockExecution execution) {
47         final String bpmnRequest = execution.getVariable(BBConstants.G_BPMN_REQUEST);
48         List<Resource> resources = execution.getVariable("resources");
49
50         Optional<ServiceInstancesRequest> sir = parseBpmnRequest(bpmnRequest);
51         if (sir.isEmpty()) {
52             return Optional.of("Failed to parse bpmnRequest");
53         }
54         var requestDetails = sir.get().getRequestDetails();
55         String requestModelInvariantId = requestDetails.getModelInfo().getModelInvariantId();
56
57         Optional<String> modelInvariantMismatch = validateInvariantId(resources, requestModelInvariantId);
58         if (modelInvariantMismatch.isPresent()) {
59             return modelInvariantMismatch;
60         }
61
62         List<Resource> aaiVnfResources = getVnfResources(resources);
63         if (aaiVnfResources.isEmpty()) {
64             return Optional.empty();
65         }
66
67         String serviceModelUuid = requestDetails.getModelInfo().getModelUuid();
68         Optional<List<VnfResourceCustomization>> vnfResourceCustomizations =
69                 getVnfResourceCustomizations(serviceModelUuid);
70         if (vnfResourceCustomizations.isEmpty()) {
71             return Optional.of(String.format("Service model: %s does not exist in catalog db.", serviceModelUuid));
72         }
73
74         return validateExistingVnfsSupported(aaiVnfResources, vnfResourceCustomizations.get());
75     }
76
77     private Optional<ServiceInstancesRequest> parseBpmnRequest(String bpmnRequest) {
78         try {
79             return Optional.of(new ObjectMapper().readValue(bpmnRequest, ServiceInstancesRequest.class));
80         } catch (IOException e) {
81             return Optional.empty();
82         }
83     }
84
85     private Optional<String> validateInvariantId(List<Resource> resources, String requestModelInvariantId) {
86         return resources.stream().filter(resourceType.apply(WorkflowType.SERVICE)).findFirst()
87                 .filter(r -> !r.getModelInvariantId().equals(requestModelInvariantId))
88                 .map(r -> String.format(ERR_MSG_INVARIANT_MISMATCH, requestModelInvariantId, r.getModelInvariantId()));
89     }
90
91     private Optional<List<VnfResourceCustomization>> getVnfResourceCustomizations(String serviceModelUuid) {
92         return Optional.ofNullable(catalogDbClient.getServiceByModelUUID(serviceModelUuid))
93                 .map(Service::getVnfCustomizations);
94     }
95
96     private List<Resource> getVnfResources(List<Resource> resources) {
97         return resources.stream().filter(resourceType.apply(WorkflowType.VNF)).collect(Collectors.toList());
98     }
99
100     private Optional<String> validateExistingVnfsSupported(List<Resource> vnfResources,
101             List<VnfResourceCustomization> vnfResourceCustomizations) {
102         Set<String> modeledVnfCustomizationIds = vnfResourceCustomizations.stream()
103                 .map(VnfResourceCustomization::getModelCustomizationUUID).collect(Collectors.toSet());
104
105         String unsupportedVnfCustomizationIds = vnfResources.stream().map(Resource::getVnfCustomizationId)
106                 .filter(id -> !modeledVnfCustomizationIds.contains(id)).collect(Collectors.joining(","));
107
108         if (unsupportedVnfCustomizationIds.isEmpty()) {
109             return Optional.empty();
110         }
111         return Optional.of(String.format(ERR_MSG_EXISTING_VNFS_NOT_SUPPORTED, unsupportedVnfCustomizationIds));
112     }
113
114 }