Merge "Lower code duplication of AaiClient calls in BBInputSetupUtils"
[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.entities.ResourceKey;
29 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
30 import org.onap.so.client.exception.BBObjectNotFoundException;
31 import org.onap.so.client.exception.ExceptionBuilder;
32 import org.onap.so.client.exception.OrchestrationStatusValidationException;
33 import org.onap.so.db.catalog.beans.BuildingBlockDetail;
34 import org.onap.so.db.catalog.beans.OrchestrationStatus;
35 import org.onap.so.db.catalog.beans.OrchestrationStatusStateTransitionDirective;
36 import org.onap.so.db.catalog.beans.OrchestrationStatusValidationDirective;
37 import org.onap.so.db.catalog.client.CatalogDbClient;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Component;
42
43 @Component
44 public class OrchestrationStatusValidator {
45     private static final Logger logger = LoggerFactory.getLogger(OrchestrationStatusValidator.class);
46
47     private static final String BUILDING_BLOCK_DETAIL_NOT_FOUND =
48             "Building Block (%s) not set up in Orchestration_Status_Validation table in CatalogDB.";
49     private static final String UNKNOWN_RESOURCE_TYPE =
50             "Building Block (%s) not set up correctly in Orchestration_Status_Validation table in CatalogDB. ResourceType=(%s), TargetAction=(%s)";
51     private static final String ORCHESTRATION_VALIDATION_FAIL =
52             "Orchestration Status Validation failed. ResourceType=(%s), TargetAction=(%s), OrchestrationStatus=(%s)";
53     private static final String ORCHESTRATION_STATUS_VALIDATION_RESULT = "orchestrationStatusValidationResult";
54     private static final String ALACARTE = "aLaCarte";
55
56     @Autowired
57     private ExtractPojosForBB extractPojosForBB;
58     @Autowired
59     private ExceptionBuilder exceptionBuilder;
60     @Autowired
61     private CatalogDbClient catalogDbClient;
62
63
64     /**
65      * This method validate's the status of the OrchestrationStatus against the buildingBlockDetail ResourceType
66      *
67      * @param execution
68      */
69     public void validateOrchestrationStatus(BuildingBlockExecution execution) {
70         try {
71             OrchestrationStatusValidationDirective previousOrchestrationStatusValidationResult =
72                     execution.getVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT);
73
74             execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT, null);
75
76             boolean aLaCarte = (boolean) execution.getVariable(ALACARTE);
77
78             String buildingBlockFlowName = execution.getFlowToBeCalled();
79
80             BuildingBlockDetail buildingBlockDetail = catalogDbClient.getBuildingBlockDetail(buildingBlockFlowName);
81
82             if (buildingBlockDetail == null) {
83                 throw new OrchestrationStatusValidationException(
84                         String.format(BUILDING_BLOCK_DETAIL_NOT_FOUND, buildingBlockFlowName));
85             }
86
87             OrchestrationStatus orchestrationStatus;
88
89             switch (buildingBlockDetail.getResourceType()) {
90                 case SERVICE:
91                     org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInstance =
92                             extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
93                     orchestrationStatus = serviceInstance.getOrchestrationStatus();
94                     break;
95                 case VNF:
96                     org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf genericVnf =
97                             extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
98                     orchestrationStatus = genericVnf.getOrchestrationStatus();
99                     break;
100                 case VF_MODULE:
101                     org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule vfModule =
102                             extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
103                     orchestrationStatus = vfModule.getOrchestrationStatus();
104                     break;
105                 case VOLUME_GROUP:
106                     org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup volumeGroup =
107                             extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID);
108                     orchestrationStatus = volumeGroup.getOrchestrationStatus();
109                     break;
110                 case NETWORK:
111                     org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network network =
112                             extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID);
113                     orchestrationStatus = network.getOrchestrationStatus();
114                     break;
115                 case NETWORK_COLLECTION:
116                     org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInst =
117                             extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
118                     org.onap.so.bpmn.servicedecomposition.bbobjects.Collection networkCollection =
119                             serviceInst.getCollection();
120                     orchestrationStatus = networkCollection.getOrchestrationStatus();
121                     break;
122                 case CONFIGURATION:
123                     org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration configuration =
124                             extractPojosForBB.extractByKey(execution, ResourceKey.CONFIGURATION_ID);
125                     orchestrationStatus = configuration.getOrchestrationStatus();
126                     break;
127                 case INSTANCE_GROUP:
128                     org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup instanceGroup =
129                             extractPojosForBB.extractByKey(execution, ResourceKey.INSTANCE_GROUP_ID);
130                     orchestrationStatus = instanceGroup.getOrchestrationStatus();
131                     break;
132                 case NO_VALIDATE:
133                     // short circuit and exit method
134                     execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT,
135                             OrchestrationStatusValidationDirective.VALIDATION_SKIPPED);
136                     return;
137                 default:
138                     // can't currently get here, so not tested. Added in case enum is expanded
139                     // without a change to this
140                     // code
141                     throw new OrchestrationStatusValidationException(
142                             String.format(UNKNOWN_RESOURCE_TYPE, buildingBlockFlowName,
143                                     buildingBlockDetail.getResourceType(), buildingBlockDetail.getTargetAction()));
144             }
145
146             if (orchestrationStatus == null) {
147                 throw new OrchestrationStatusValidationException(
148                         "The resource's orchstration status is null. Cannot perform task on a null orchestration status");
149             }
150             OrchestrationStatusStateTransitionDirective orchestrationStatusStateTransitionDirective = catalogDbClient
151                     .getOrchestrationStatusStateTransitionDirective(buildingBlockDetail.getResourceType(),
152                             orchestrationStatus, buildingBlockDetail.getTargetAction());
153
154             if (orchestrationStatusStateTransitionDirective
155                     .getFlowDirective() == OrchestrationStatusValidationDirective.FAIL) {
156                 throw new OrchestrationStatusValidationException(
157                         String.format(ORCHESTRATION_VALIDATION_FAIL, buildingBlockDetail.getResourceType(),
158                                 buildingBlockDetail.getTargetAction(), orchestrationStatus));
159             }
160
161             execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT,
162                     orchestrationStatusStateTransitionDirective.getFlowDirective());
163         } catch (BBObjectNotFoundException ex) {
164             logger.error(
165                     "Error occurred for bb object notfound in OrchestrationStatusValidator validateOrchestrationStatus ",
166                     ex);
167             if (execution.getFlowToBeCalled().contains("Unassign")) {
168                 execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT,
169                         OrchestrationStatusValidationDirective.SILENT_SUCCESS);
170             } else {
171                 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, ex);
172             }
173         } catch (Exception e) {
174             logger.error("Exception occurred", e);
175             exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, e);
176         }
177     }
178 }