Merge "Added null check for injectedVariables to prevent crash at call of injectedVar...
[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  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.workflow.tasks;
24
25 import org.onap.so.bpmn.common.BuildingBlockExecution;
26 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
27 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
28 import org.onap.so.client.exception.BBObjectNotFoundException;
29 import org.onap.so.client.exception.ExceptionBuilder;
30 import org.onap.so.client.exception.OrchestrationStatusValidationException;
31 import org.onap.so.db.catalog.beans.BuildingBlockDetail;
32 import org.onap.so.db.catalog.beans.OrchestrationAction;
33 import org.onap.so.db.catalog.beans.OrchestrationStatus;
34 import org.onap.so.db.catalog.beans.OrchestrationStatusStateTransitionDirective;
35 import org.onap.so.db.catalog.beans.OrchestrationStatusValidationDirective;
36 import org.onap.so.db.catalog.beans.ResourceType;
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     private static final String MULTI_STAGE_DESIGN_OFF = "false";
56     private static final String MULTI_STAGE_DESIGN_ON = "true";
57
58
59     @Autowired
60     private ExtractPojosForBB extractPojosForBB;
61     @Autowired
62     private ExceptionBuilder exceptionBuilder;
63     @Autowired
64     private CatalogDbClient catalogDbClient;
65
66     /**
67      * This method validate's the status of the OrchestrationStatus against the buildingBlockDetail ResourceType
68      *
69      * @param execution
70      */
71     public void validateOrchestrationStatus(BuildingBlockExecution execution) {
72         try {
73             OrchestrationStatusValidationDirective previousOrchestrationStatusValidationResult =
74                     execution.getVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT);
75
76             execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT, null);
77
78             boolean aLaCarte = (boolean) execution.getVariable(ALACARTE);
79
80             String buildingBlockFlowName = execution.getFlowToBeCalled();
81
82             BuildingBlockDetail buildingBlockDetail = catalogDbClient.getBuildingBlockDetail(buildingBlockFlowName);
83
84             if (buildingBlockDetail == null) {
85                 throw new OrchestrationStatusValidationException(
86                         String.format(BUILDING_BLOCK_DETAIL_NOT_FOUND, buildingBlockFlowName));
87             }
88
89             OrchestrationStatus orchestrationStatus = null;
90
91             switch (buildingBlockDetail.getResourceType()) {
92                 case SERVICE:
93                     org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInstance =
94                             extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
95                     orchestrationStatus = serviceInstance.getOrchestrationStatus();
96                     break;
97                 case VNF:
98                     org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf genericVnf =
99                             extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
100                     orchestrationStatus = genericVnf.getOrchestrationStatus();
101                     break;
102                 case VF_MODULE:
103                     org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule vfModule =
104                             extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
105                     orchestrationStatus = vfModule.getOrchestrationStatus();
106                     break;
107                 case VOLUME_GROUP:
108                     org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup volumeGroup =
109                             extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID);
110                     orchestrationStatus = volumeGroup.getOrchestrationStatus();
111                     break;
112                 case NETWORK:
113                     org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network network =
114                             extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID);
115                     orchestrationStatus = network.getOrchestrationStatus();
116                     break;
117                 case NETWORK_COLLECTION:
118                     org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInst =
119                             extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
120                     org.onap.so.bpmn.servicedecomposition.bbobjects.Collection networkCollection =
121                             serviceInst.getCollection();
122                     orchestrationStatus = networkCollection.getOrchestrationStatus();
123                     break;
124                 case CONFIGURATION:
125                     org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration configuration =
126                             extractPojosForBB.extractByKey(execution, ResourceKey.CONFIGURATION_ID);
127                     orchestrationStatus = configuration.getOrchestrationStatus();
128                     break;
129                 case INSTANCE_GROUP:
130                     org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup instanceGroup =
131                             extractPojosForBB.extractByKey(execution, ResourceKey.INSTANCE_GROUP_ID);
132                     orchestrationStatus = instanceGroup.getOrchestrationStatus();
133                     break;
134                 case NO_VALIDATE:
135                     // short circuit and exit method
136                     execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT,
137                             OrchestrationStatusValidationDirective.VALIDATION_SKIPPED);
138                     return;
139                 default:
140                     // can't currently get here, so not tested. Added in case enum is expanded without a change to this
141                     // code
142                     throw new OrchestrationStatusValidationException(
143                             String.format(UNKNOWN_RESOURCE_TYPE, buildingBlockFlowName,
144                                     buildingBlockDetail.getResourceType(), buildingBlockDetail.getTargetAction()));
145             }
146
147             if (orchestrationStatus == null) {
148                 throw new OrchestrationStatusValidationException(
149                         "The resource's orchstration status is null. Cannot perform task on a null orchestration status");
150             }
151             OrchestrationStatusStateTransitionDirective orchestrationStatusStateTransitionDirective = catalogDbClient
152                     .getOrchestrationStatusStateTransitionDirective(buildingBlockDetail.getResourceType(),
153                             orchestrationStatus, buildingBlockDetail.getTargetAction());
154
155             if (aLaCarte && ResourceType.VF_MODULE.equals(buildingBlockDetail.getResourceType())
156                     && OrchestrationAction.CREATE.equals(buildingBlockDetail.getTargetAction())
157                     && OrchestrationStatus.PENDING_ACTIVATION.equals(orchestrationStatus)) {
158                 org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf genericVnf =
159                         extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
160                 orchestrationStatusStateTransitionDirective = processPossibleSecondStageofVfModuleCreate(execution,
161                         previousOrchestrationStatusValidationResult, genericVnf,
162                         orchestrationStatusStateTransitionDirective);
163             }
164
165             if (orchestrationStatusStateTransitionDirective
166                     .getFlowDirective() == OrchestrationStatusValidationDirective.FAIL) {
167                 throw new OrchestrationStatusValidationException(
168                         String.format(ORCHESTRATION_VALIDATION_FAIL, buildingBlockDetail.getResourceType(),
169                                 buildingBlockDetail.getTargetAction(), orchestrationStatus));
170             }
171
172             execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT,
173                     orchestrationStatusStateTransitionDirective.getFlowDirective());
174         } catch (BBObjectNotFoundException ex) {
175             if (execution.getFlowToBeCalled().contains("Unassign")) {
176                 execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT,
177                         OrchestrationStatusValidationDirective.SILENT_SUCCESS);
178             } else {
179                 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, ex);
180             }
181         } catch (Exception e) {
182             exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, e);
183         }
184     }
185
186     private OrchestrationStatusStateTransitionDirective processPossibleSecondStageofVfModuleCreate(
187             BuildingBlockExecution execution,
188             OrchestrationStatusValidationDirective previousOrchestrationStatusValidationResult,
189             org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf genericVnf,
190             OrchestrationStatusStateTransitionDirective orchestrationStatusStateTransitionDirective) {
191         if (previousOrchestrationStatusValidationResult != null && previousOrchestrationStatusValidationResult
192                 .equals(OrchestrationStatusValidationDirective.SILENT_SUCCESS)) {
193             String multiStageDesign = MULTI_STAGE_DESIGN_OFF;
194             if (genericVnf.getModelInfoGenericVnf() != null) {
195                 multiStageDesign = genericVnf.getModelInfoGenericVnf().getMultiStageDesign();
196             }
197             if (multiStageDesign != null && multiStageDesign.equalsIgnoreCase(MULTI_STAGE_DESIGN_ON)) {
198                 orchestrationStatusStateTransitionDirective
199                         .setFlowDirective(OrchestrationStatusValidationDirective.CONTINUE);
200             }
201         }
202         return orchestrationStatusStateTransitionDirective;
203     }
204 }