Merge "updated bbinput setup to pull vf if no vnf found"
[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 java.util.EnumSet;
28 import java.util.Set;
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;
48
49 @Component
50 public class OrchestrationStatusValidator {
51     private static final Logger logger = LoggerFactory.getLogger(OrchestrationStatusValidator.class);
52
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);
69
70     @Autowired
71     private ExtractPojosForBB extractPojosForBB;
72     @Autowired
73     private ExceptionBuilder exceptionBuilder;
74     @Autowired
75     private CatalogDbClient catalogDbClient;
76     @Autowired
77     RequestsDbClient requestDBClient;
78
79
80     /**
81      * This method validate's the status of the OrchestrationStatus against the buildingBlockDetail ResourceType
82      *
83      * @param execution
84      */
85     public void validateOrchestrationStatus(BuildingBlockExecution execution) {
86         try {
87             OrchestrationStatusValidationDirective previousOrchestrationStatusValidationResult =
88                     execution.getVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT);
89
90             execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT, null);
91
92             boolean aLaCarte = (boolean) execution.getVariable(ALACARTE);
93
94             String buildingBlockFlowName = execution.getFlowToBeCalled();
95
96             BuildingBlockDetail buildingBlockDetail = catalogDbClient.getBuildingBlockDetail(buildingBlockFlowName);
97
98             if (buildingBlockDetail == null) {
99                 throw new OrchestrationStatusValidationException(
100                         String.format(BUILDING_BLOCK_DETAIL_NOT_FOUND, buildingBlockFlowName));
101             }
102
103             OrchestrationStatus orchestrationStatus;
104
105             switch (buildingBlockDetail.getResourceType()) {
106                 case SERVICE:
107                     org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInstance =
108                             extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
109                     orchestrationStatus = serviceInstance.getOrchestrationStatus();
110                     break;
111                 case VNF:
112                     org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf genericVnf =
113                             extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
114                     orchestrationStatus = genericVnf.getOrchestrationStatus();
115                     break;
116                 case VF_MODULE:
117                     org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule vfModule =
118                             extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
119                     orchestrationStatus = vfModule.getOrchestrationStatus();
120                     break;
121                 case VOLUME_GROUP:
122                     org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup volumeGroup =
123                             extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID);
124                     orchestrationStatus = volumeGroup.getOrchestrationStatus();
125                     break;
126                 case NETWORK:
127                     org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network network =
128                             extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID);
129                     orchestrationStatus = network.getOrchestrationStatus();
130                     break;
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();
137                     break;
138                 case CONFIGURATION:
139                     org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration configuration =
140                             extractPojosForBB.extractByKey(execution, ResourceKey.CONFIGURATION_ID);
141                     orchestrationStatus = configuration.getOrchestrationStatus();
142                     break;
143                 case INSTANCE_GROUP:
144                     org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup instanceGroup =
145                             extractPojosForBB.extractByKey(execution, ResourceKey.INSTANCE_GROUP_ID);
146                     orchestrationStatus = instanceGroup.getOrchestrationStatus();
147                     break;
148                 case NO_VALIDATE:
149                     // short circuit and exit method
150                     execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT,
151                             OrchestrationStatusValidationDirective.VALIDATION_SKIPPED);
152                     return;
153                 default:
154                     // can't currently get here, so not tested. Added in case enum is expanded
155                     // without a change to this
156                     // code
157                     throw new OrchestrationStatusValidationException(
158                             String.format(UNKNOWN_RESOURCE_TYPE, buildingBlockFlowName,
159                                     buildingBlockDetail.getResourceType(), buildingBlockDetail.getTargetAction()));
160             }
161
162             if (orchestrationStatus == null) {
163                 throw new OrchestrationStatusValidationException(
164                         "The resource's orchstration status is null. Cannot perform task on a null orchestration status");
165             }
166             OrchestrationStatusStateTransitionDirective orchestrationStatusStateTransitionDirective = catalogDbClient
167                     .getOrchestrationStatusStateTransitionDirective(buildingBlockDetail.getResourceType(),
168                             orchestrationStatus, buildingBlockDetail.getTargetAction());
169
170             if (orchestrationStatusStateTransitionDirective
171                     .getFlowDirective() == OrchestrationStatusValidationDirective.FAIL) {
172                 throw new OrchestrationStatusValidationException(
173                         String.format(ORCHESTRATION_VALIDATION_FAIL, buildingBlockDetail.getResourceType(),
174                                 buildingBlockDetail.getTargetAction(), orchestrationStatus));
175             }
176
177             execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT,
178                     orchestrationStatusStateTransitionDirective.getFlowDirective());
179
180             if (buildingBlockFlowName.matches("Create(.*)|Delete(.*)") && orchestrationStatusStateTransitionDirective
181                     .getFlowDirective() == OrchestrationStatusValidationDirective.SILENT_SUCCESS) {
182
183                 updatedResourceStatus(execution, buildingBlockDetail);
184             }
185
186         } catch (BBObjectNotFoundException ex) {
187             logger.error(
188                     "Error occurred for bb object notfound in OrchestrationStatusValidator validateOrchestrationStatus ",
189                     ex);
190             if (execution.getFlowToBeCalled().contains("Unassign")) {
191                 execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT,
192                         OrchestrationStatusValidationDirective.SILENT_SUCCESS);
193             } else {
194                 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, ex);
195             }
196         } catch (Exception e) {
197             logger.error("Exception occurred", e);
198             exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, e);
199         }
200     }
201
202     private void updatedResourceStatus(BuildingBlockExecution execution, BuildingBlockDetail buildingBlockDetail) {
203
204         if (cloudResources.contains(buildingBlockDetail.getResourceType())) {
205             String resource = buildingBlockDetail.getResourceType().toString();
206
207             String resourceId = execution.getLookupMap()
208                     .get(ResourceKey.valueOf(buildingBlockDetail.getResourceType().getResourceKey()));
209
210             String resourceStatusMessage = RESOURCE_NOT_EXIST_STATUS_MESSAGE;
211             if (execution.getFlowToBeCalled().matches("Create(.*)")) {
212                 resourceStatusMessage = RESOURCE_EXIST_STATUS_MESSAGE;
213             }
214
215             updateRequestsDb(resourceId, String.format(resourceStatusMessage, resource, resource));
216         }
217
218     }
219
220     private void updateRequestsDb(String requestId, String resourceStatusMessage) {
221         InfraActiveRequests request = new InfraActiveRequests();
222         request.setRequestId(requestId);
223         request.setResourceStatusMessage(resourceStatusMessage);
224         try {
225             requestDBClient.patchInfraActiveRequests(request);
226         } catch (HttpClientErrorException e) {
227             logger.warn("Unable to update active request resource status");
228         }
229     }
230 }