f233de2baa7a3df08ed3aff15e868e32bf5c9e67
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / workflow / tasks / OrchestrationStatusValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Modifications Copyright (c) 2020 Nokia
10  * ================================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.so.bpmn.infrastructure.workflow.tasks;
26
27 import org.onap.so.bpmn.common.BuildingBlockExecution;
28 import org.onap.so.bpmn.servicedecomposition.bbobjects.Collection;
29 import org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
33 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
34 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
35 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
36 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
37 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
38 import org.onap.so.client.exception.BBObjectNotFoundException;
39 import org.onap.so.client.exception.ExceptionBuilder;
40 import org.onap.so.client.exception.OrchestrationStatusValidationException;
41 import org.onap.so.db.catalog.beans.BuildingBlockDetail;
42 import org.onap.so.db.catalog.beans.OrchestrationStatus;
43 import org.onap.so.db.catalog.beans.OrchestrationStatusStateTransitionDirective;
44 import org.onap.so.db.catalog.beans.OrchestrationStatusValidationDirective;
45 import org.onap.so.db.catalog.beans.ResourceType;
46 import org.onap.so.db.catalog.client.CatalogDbClient;
47 import org.onap.so.db.request.beans.InfraActiveRequests;
48 import org.onap.so.db.request.client.RequestsDbClient;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.stereotype.Component;
53 import org.springframework.web.client.HttpClientErrorException;
54 import java.util.EnumSet;
55 import java.util.Set;
56
57 @Component
58 public class OrchestrationStatusValidator {
59     private static final Logger logger = LoggerFactory.getLogger(OrchestrationStatusValidator.class);
60
61     private static final String BUILDING_BLOCK_DETAIL_NOT_FOUND =
62             "Building Block (%s) not set up in Orchestration_Status_Validation table in CatalogDB.";
63     private static final String UNKNOWN_RESOURCE_TYPE =
64             "Building Block (%s) not set up correctly in Orchestration_Status_Validation table in CatalogDB. ResourceType=(%s), TargetAction=(%s)";
65     private static final String ORCHESTRATION_VALIDATION_FAIL =
66             "Orchestration Status Validation failed. ResourceType=(%s), TargetAction=(%s), OrchestrationStatus=(%s)";
67     private static final String ORCHESTRATION_STATUS_VALIDATION_RESULT = "orchestrationStatusValidationResult";
68     private static final String RESOURCE_EXIST_STATUS_MESSAGE =
69             "The %s was found to already exist, thus no new %s was created in the cloud via this request";
70     private static final String RESOURCE_NOT_EXIST_STATUS_MESSAGE =
71             "The %s was not found, thus no %s was deleted in the cloud via this request";
72     private static final Set<ResourceType> cloudResources =
73             EnumSet.of(ResourceType.VF_MODULE, ResourceType.VOLUME_GROUP, ResourceType.NETWORK);
74
75     @Autowired
76     private ExtractPojosForBB extractPojosForBB;
77     @Autowired
78     private ExceptionBuilder exceptionBuilder;
79     @Autowired
80     private CatalogDbClient catalogDbClient;
81     @Autowired
82     RequestsDbClient requestDBClient;
83
84
85     /**
86      * This method validate's the status of the OrchestrationStatus against the buildingBlockDetail ResourceType
87      */
88     public void validateOrchestrationStatus(BuildingBlockExecution execution) {
89         try {
90             execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT, null);
91             String buildingBlockFlowName = execution.getFlowToBeCalled();
92             BuildingBlockDetail buildingBlockDetail = catalogDbClient.getBuildingBlockDetail(buildingBlockFlowName);
93
94             if (buildingBlockDetail == null) {
95                 throw new OrchestrationStatusValidationException(
96                         String.format(BUILDING_BLOCK_DETAIL_NOT_FOUND, buildingBlockFlowName));
97             }
98
99             OrchestrationStatus orchestrationStatus =
100                     getOrchestrationStatus(execution, buildingBlockFlowName, buildingBlockDetail);
101             if (buildingBlockDetail.getResourceType().equals(ResourceType.NO_VALIDATE)) {
102                 return;
103             }
104
105             if (orchestrationStatus == null) {
106                 throw new OrchestrationStatusValidationException(
107                         "The resource's orchstration status is null. Cannot perform task on a null orchestration status");
108             }
109             OrchestrationStatusStateTransitionDirective orchestrationStatusStateTransitionDirective = catalogDbClient
110                     .getOrchestrationStatusStateTransitionDirective(buildingBlockDetail.getResourceType(),
111                             orchestrationStatus, buildingBlockDetail.getTargetAction());
112
113             if (orchestrationStatusStateTransitionDirective
114                     .getFlowDirective() == OrchestrationStatusValidationDirective.FAIL) {
115                 throw new OrchestrationStatusValidationException(
116                         String.format(ORCHESTRATION_VALIDATION_FAIL, buildingBlockDetail.getResourceType(),
117                                 buildingBlockDetail.getTargetAction(), orchestrationStatus));
118             }
119
120             execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT,
121                     orchestrationStatusStateTransitionDirective.getFlowDirective());
122
123             if (buildingBlockFlowName.matches("Create(.*)|Delete(.*)") && orchestrationStatusStateTransitionDirective
124                     .getFlowDirective() == OrchestrationStatusValidationDirective.SILENT_SUCCESS) {
125
126                 updatedResourceStatus(execution, buildingBlockDetail);
127             }
128
129         } catch (BBObjectNotFoundException ex) {
130             logger.error(
131                     "Error occurred for bb object notfound in OrchestrationStatusValidator validateOrchestrationStatus ",
132                     ex);
133             if (execution.getFlowToBeCalled().contains("Unassign")) {
134                 execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT,
135                         OrchestrationStatusValidationDirective.SILENT_SUCCESS);
136             } else {
137                 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, ex);
138             }
139         } catch (Exception e) {
140             logger.error("Exception occurred", e);
141             exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, e);
142         }
143     }
144
145     private OrchestrationStatus getOrchestrationStatus(BuildingBlockExecution execution, String buildingBlockFlowName,
146             BuildingBlockDetail buildingBlockDetail)
147             throws BBObjectNotFoundException, OrchestrationStatusValidationException {
148         OrchestrationStatus orchestrationStatus = null;
149
150         switch (buildingBlockDetail.getResourceType()) {
151             case SERVICE:
152                 ServiceInstance serviceInstance =
153                         extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
154                 orchestrationStatus = serviceInstance.getOrchestrationStatus();
155                 break;
156             case VNF:
157                 GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
158                 orchestrationStatus = genericVnf.getOrchestrationStatus();
159                 break;
160             case VF_MODULE:
161                 VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
162                 orchestrationStatus = vfModule.getOrchestrationStatus();
163                 break;
164             case VOLUME_GROUP:
165                 VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID);
166                 orchestrationStatus = volumeGroup.getOrchestrationStatus();
167                 break;
168             case NETWORK:
169                 L3Network network = extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID);
170                 orchestrationStatus = network.getOrchestrationStatus();
171                 break;
172             case NETWORK_COLLECTION:
173                 Collection networkCollection = getNetworkCollection(execution);
174                 orchestrationStatus = networkCollection.getOrchestrationStatus();
175                 break;
176             case CONFIGURATION:
177                 Configuration configuration = extractPojosForBB.extractByKey(execution, ResourceKey.CONFIGURATION_ID);
178                 orchestrationStatus = configuration.getOrchestrationStatus();
179                 break;
180             case INSTANCE_GROUP:
181                 InstanceGroup instanceGroup = extractPojosForBB.extractByKey(execution, ResourceKey.INSTANCE_GROUP_ID);
182                 orchestrationStatus = instanceGroup.getOrchestrationStatus();
183                 break;
184             case NO_VALIDATE:
185                 // short circuit and exit method
186                 execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT,
187                         OrchestrationStatusValidationDirective.VALIDATION_SKIPPED);
188                 break;
189             default:
190                 // can't currently get here, so not tested. Added in case enum is expanded
191                 // without a change to this
192                 // code
193                 throw new OrchestrationStatusValidationException(
194                         String.format(UNKNOWN_RESOURCE_TYPE, buildingBlockFlowName,
195                                 buildingBlockDetail.getResourceType(), buildingBlockDetail.getTargetAction()));
196         }
197         return orchestrationStatus;
198     }
199
200     private Collection getNetworkCollection(BuildingBlockExecution execution) throws BBObjectNotFoundException {
201         ServiceInstance serviceInst = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
202         return serviceInst.getCollection();
203     }
204
205     private void updatedResourceStatus(BuildingBlockExecution execution, BuildingBlockDetail buildingBlockDetail) {
206
207         if (cloudResources.contains(buildingBlockDetail.getResourceType())) {
208             String resource = buildingBlockDetail.getResourceType().toString();
209
210             String resourceId = execution.getLookupMap()
211                     .get(ResourceKey.valueOf(buildingBlockDetail.getResourceType().getResourceKey()));
212
213             String resourceStatusMessage = RESOURCE_NOT_EXIST_STATUS_MESSAGE;
214             if (execution.getFlowToBeCalled().matches("Create(.*)")) {
215                 resourceStatusMessage = RESOURCE_EXIST_STATUS_MESSAGE;
216             }
217
218             updateRequestsDb(resourceId, String.format(resourceStatusMessage, resource, resource));
219         }
220
221     }
222
223     private void updateRequestsDb(String requestId, String resourceStatusMessage) {
224         InfraActiveRequests request = new InfraActiveRequests();
225         request.setRequestId(requestId);
226         request.setResourceStatusMessage(resourceStatusMessage);
227         try {
228             requestDBClient.patchInfraActiveRequests(request);
229         } catch (HttpClientErrorException e) {
230             logger.warn("Unable to update active request resource status");
231         }
232     }
233 }