2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
25 package org.onap.so.bpmn.infrastructure.workflow.tasks;
27 import java.util.EnumSet;
29 import org.onap.so.bpmn.common.BuildingBlockExecution;
30 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
31 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
32 import org.onap.so.client.exception.BBObjectNotFoundException;
33 import org.onap.so.client.exception.ExceptionBuilder;
34 import org.onap.so.client.exception.OrchestrationStatusValidationException;
35 import org.onap.so.db.catalog.beans.BuildingBlockDetail;
36 import org.onap.so.db.catalog.beans.OrchestrationStatus;
37 import org.onap.so.db.catalog.beans.OrchestrationStatusStateTransitionDirective;
38 import org.onap.so.db.catalog.beans.OrchestrationStatusValidationDirective;
39 import org.onap.so.db.catalog.beans.ResourceType;
40 import org.onap.so.db.catalog.client.CatalogDbClient;
41 import org.onap.so.db.request.beans.InfraActiveRequests;
42 import org.onap.so.db.request.client.RequestsDbClient;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Component;
47 import org.springframework.web.client.HttpClientErrorException;
50 public class OrchestrationStatusValidator {
51 private static final Logger logger = LoggerFactory.getLogger(OrchestrationStatusValidator.class);
53 private static final String BUILDING_BLOCK_DETAIL_NOT_FOUND =
54 "Building Block (%s) not set up in Orchestration_Status_Validation table in CatalogDB.";
55 private static final String UNKNOWN_RESOURCE_TYPE =
56 "Building Block (%s) not set up correctly in Orchestration_Status_Validation table in CatalogDB. ResourceType=(%s), TargetAction=(%s)";
57 private static final String ORCHESTRATION_VALIDATION_FAIL =
58 "Orchestration Status Validation failed. ResourceType=(%s), TargetAction=(%s), OrchestrationStatus=(%s)";
59 private static final String ORCHESTRATION_STATUS_VALIDATION_RESULT = "orchestrationStatusValidationResult";
60 private static final String ALACARTE = "aLaCarte";
61 private static final String MULTI_STAGE_DESIGN_OFF = "false";
62 private static final String MULTI_STAGE_DESIGN_ON = "true";
63 private static final String RESOURCE_EXIST_STATUS_MESSAGE =
64 "The %s was found to already exist, thus no new %s was created in the cloud via this request";
65 private static final String RESOURCE_NOT_EXIST_STATUS_MESSAGE =
66 "The %s was not found, thus no %s was deleted in the cloud via this request";
67 private static final Set<ResourceType> cloudResources =
68 EnumSet.of(ResourceType.VF_MODULE, ResourceType.VOLUME_GROUP, ResourceType.NETWORK);
71 private ExtractPojosForBB extractPojosForBB;
73 private ExceptionBuilder exceptionBuilder;
75 private CatalogDbClient catalogDbClient;
77 RequestsDbClient requestDBClient;
81 * This method validate's the status of the OrchestrationStatus against the buildingBlockDetail ResourceType
85 public void validateOrchestrationStatus(BuildingBlockExecution execution) {
87 OrchestrationStatusValidationDirective previousOrchestrationStatusValidationResult =
88 execution.getVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT);
90 execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT, null);
92 boolean aLaCarte = (boolean) execution.getVariable(ALACARTE);
94 String buildingBlockFlowName = execution.getFlowToBeCalled();
96 BuildingBlockDetail buildingBlockDetail = catalogDbClient.getBuildingBlockDetail(buildingBlockFlowName);
98 if (buildingBlockDetail == null) {
99 throw new OrchestrationStatusValidationException(
100 String.format(BUILDING_BLOCK_DETAIL_NOT_FOUND, buildingBlockFlowName));
103 OrchestrationStatus orchestrationStatus;
105 switch (buildingBlockDetail.getResourceType()) {
107 org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInstance =
108 extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
109 orchestrationStatus = serviceInstance.getOrchestrationStatus();
112 org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf genericVnf =
113 extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
114 orchestrationStatus = genericVnf.getOrchestrationStatus();
117 org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule vfModule =
118 extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
119 orchestrationStatus = vfModule.getOrchestrationStatus();
122 org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup volumeGroup =
123 extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID);
124 orchestrationStatus = volumeGroup.getOrchestrationStatus();
127 org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network network =
128 extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID);
129 orchestrationStatus = network.getOrchestrationStatus();
131 case NETWORK_COLLECTION:
132 org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInst =
133 extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
134 org.onap.so.bpmn.servicedecomposition.bbobjects.Collection networkCollection =
135 serviceInst.getCollection();
136 orchestrationStatus = networkCollection.getOrchestrationStatus();
139 org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration configuration =
140 extractPojosForBB.extractByKey(execution, ResourceKey.CONFIGURATION_ID);
141 orchestrationStatus = configuration.getOrchestrationStatus();
144 org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup instanceGroup =
145 extractPojosForBB.extractByKey(execution, ResourceKey.INSTANCE_GROUP_ID);
146 orchestrationStatus = instanceGroup.getOrchestrationStatus();
149 // short circuit and exit method
150 execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT,
151 OrchestrationStatusValidationDirective.VALIDATION_SKIPPED);
154 // can't currently get here, so not tested. Added in case enum is expanded
155 // without a change to this
157 throw new OrchestrationStatusValidationException(
158 String.format(UNKNOWN_RESOURCE_TYPE, buildingBlockFlowName,
159 buildingBlockDetail.getResourceType(), buildingBlockDetail.getTargetAction()));
162 if (orchestrationStatus == null) {
163 throw new OrchestrationStatusValidationException(
164 "The resource's orchstration status is null. Cannot perform task on a null orchestration status");
166 OrchestrationStatusStateTransitionDirective orchestrationStatusStateTransitionDirective = catalogDbClient
167 .getOrchestrationStatusStateTransitionDirective(buildingBlockDetail.getResourceType(),
168 orchestrationStatus, buildingBlockDetail.getTargetAction());
170 if (orchestrationStatusStateTransitionDirective
171 .getFlowDirective() == OrchestrationStatusValidationDirective.FAIL) {
172 throw new OrchestrationStatusValidationException(
173 String.format(ORCHESTRATION_VALIDATION_FAIL, buildingBlockDetail.getResourceType(),
174 buildingBlockDetail.getTargetAction(), orchestrationStatus));
177 execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT,
178 orchestrationStatusStateTransitionDirective.getFlowDirective());
180 if (buildingBlockFlowName.matches("Create(.*)|Delete(.*)") && orchestrationStatusStateTransitionDirective
181 .getFlowDirective() == OrchestrationStatusValidationDirective.SILENT_SUCCESS) {
183 updatedResourceStatus(execution, buildingBlockDetail);
186 } catch (BBObjectNotFoundException ex) {
188 "Error occurred for bb object notfound in OrchestrationStatusValidator validateOrchestrationStatus ",
190 if (execution.getFlowToBeCalled().contains("Unassign")) {
191 execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT,
192 OrchestrationStatusValidationDirective.SILENT_SUCCESS);
194 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, ex);
196 } catch (Exception e) {
197 logger.error("Exception occurred", e);
198 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, e);
202 private void updatedResourceStatus(BuildingBlockExecution execution, BuildingBlockDetail buildingBlockDetail) {
204 if (cloudResources.contains(buildingBlockDetail.getResourceType())) {
205 String resource = buildingBlockDetail.getResourceType().toString();
207 String resourceId = execution.getLookupMap()
208 .get(ResourceKey.valueOf(buildingBlockDetail.getResourceType().getResourceKey()));
210 String resourceStatusMessage = RESOURCE_NOT_EXIST_STATUS_MESSAGE;
211 if (execution.getFlowToBeCalled().matches("Create(.*)")) {
212 resourceStatusMessage = RESOURCE_EXIST_STATUS_MESSAGE;
215 updateRequestsDb(resourceId, String.format(resourceStatusMessage, resource, resource));
220 private void updateRequestsDb(String requestId, String resourceStatusMessage) {
221 InfraActiveRequests request = new InfraActiveRequests();
222 request.setRequestId(requestId);
223 request.setResourceStatusMessage(resourceStatusMessage);
225 requestDBClient.patchInfraActiveRequests(request);
226 } catch (HttpClientErrorException e) {
227 logger.warn("Unable to update active request resource status");