Merge "Fix sdc-tosca version" into casablanca
[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  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.workflow.tasks;
22
23 import org.onap.so.bpmn.common.BuildingBlockExecution;
24 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
25 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
26 import org.onap.so.client.exception.BBObjectNotFoundException;
27 import org.onap.so.client.exception.ExceptionBuilder;
28 import org.onap.so.client.exception.OrchestrationStatusValidationException;
29 import org.onap.so.db.catalog.beans.BuildingBlockDetail;
30 import org.onap.so.db.catalog.beans.OrchestrationAction;
31 import org.onap.so.db.catalog.beans.OrchestrationStatus;
32 import org.onap.so.db.catalog.beans.OrchestrationStatusStateTransitionDirective;
33 import org.onap.so.db.catalog.beans.OrchestrationStatusValidationDirective;
34 import org.onap.so.db.catalog.beans.ResourceType;
35 import org.onap.so.db.catalog.client.CatalogDbClient;
36 import org.onap.so.logger.MsoLogger;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
39
40 @Component
41 public class OrchestrationStatusValidator {
42         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, OrchestrationStatusValidator.class);
43         
44         private static final String BUILDING_BLOCK_DETAIL_NOT_FOUND = "Building Block (%s) not set up in Orchestration_Status_Validation table in CatalogDB.";
45         private static final String UNKNOWN_RESOURCE_TYPE = "Building Block (%s) not set up correctly in Orchestration_Status_Validation table in CatalogDB. ResourceType=(%s), TargetAction=(%s)";
46         private static final String ORCHESTRATION_VALIDATION_FAIL = "Orchestration Status Validation failed. ResourceType=(%s), TargetAction=(%s), OrchestrationStatus=(%s)";
47         private static final String ORCHESTRATION_STATUS_VALIDATION_RESULT = "orchestrationStatusValidationResult";
48         private static final String ALACARTE = "aLaCarte";
49         private static final String MULTI_STAGE_DESIGN_OFF = "false";
50         private static final String MULTI_STAGE_DESIGN_ON = "true";
51         
52         
53         @Autowired
54         private ExtractPojosForBB extractPojosForBB;
55         @Autowired
56         private ExceptionBuilder exceptionBuilder;
57         @Autowired
58         private CatalogDbClient catalogDbClient;
59         
60         public void validateOrchestrationStatus(BuildingBlockExecution execution) {
61                 try {
62                         OrchestrationStatusValidationDirective previousOrchestrationStatusValidationResult = execution.getVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT);
63                         
64                         execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT, null);
65                         
66                         boolean aLaCarte = (boolean) execution.getVariable(ALACARTE);
67                         
68                         String buildingBlockFlowName = execution.getFlowToBeCalled();                   
69                                         
70                         BuildingBlockDetail buildingBlockDetail = catalogDbClient.getBuildingBlockDetail(buildingBlockFlowName);
71                         
72                         if (buildingBlockDetail == null) {
73                                 throw new OrchestrationStatusValidationException(String.format(BUILDING_BLOCK_DETAIL_NOT_FOUND, buildingBlockFlowName));
74                         }
75                         
76                         OrchestrationStatus orchestrationStatus = null;
77                         
78                         switch(buildingBlockDetail.getResourceType()) {
79                         case SERVICE:
80                                 org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
81                                 orchestrationStatus = serviceInstance.getOrchestrationStatus();
82                                 break;
83                         case VNF:
84                                 org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
85                                 orchestrationStatus = genericVnf.getOrchestrationStatus();
86                                 break;
87                         case VF_MODULE:
88                                 org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
89                                 orchestrationStatus = vfModule.getOrchestrationStatus();
90                                 break;
91                         case VOLUME_GROUP:
92                                 org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID, execution.getLookupMap().get(ResourceKey.VOLUME_GROUP_ID));
93                                 orchestrationStatus = volumeGroup.getOrchestrationStatus();
94                                 break;
95                         case NETWORK:
96                                 org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network network = extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID, execution.getLookupMap().get(ResourceKey.NETWORK_ID));
97                                 orchestrationStatus = network.getOrchestrationStatus();
98                                 break;
99                         case NETWORK_COLLECTION:
100                                 org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInst = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
101                                 org.onap.so.bpmn.servicedecomposition.bbobjects.Collection networkCollection = serviceInst.getCollection();
102                                 orchestrationStatus = networkCollection.getOrchestrationStatus();
103                                 break;
104                         case CONFIGURATION:
105                                 org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration configuration = extractPojosForBB.extractByKey(execution, ResourceKey.CONFIGURATION_ID, execution.getLookupMap().get(ResourceKey.CONFIGURATION_ID));
106                                 orchestrationStatus = configuration.getOrchestrationStatus();
107                                 break;
108                         case NO_VALIDATE:
109                                 //short circuit and exit method
110                                 execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT, OrchestrationStatusValidationDirective.VALIDATION_SKIPPED);
111                                 return;
112                         default:
113                                 // can't currently get here, so not tested. Added in case enum is expanded without a change to this code
114                                 throw new OrchestrationStatusValidationException(String.format(UNKNOWN_RESOURCE_TYPE, buildingBlockFlowName, buildingBlockDetail.getResourceType(), buildingBlockDetail.getTargetAction()));
115                         }
116                         
117                         if(orchestrationStatus==null){
118                                 throw new OrchestrationStatusValidationException("The resource's orchstration status is null. Cannot perform task on a null orchestration status");
119                         }
120                         OrchestrationStatusStateTransitionDirective orchestrationStatusStateTransitionDirective = catalogDbClient.getOrchestrationStatusStateTransitionDirective(buildingBlockDetail.getResourceType(), orchestrationStatus, buildingBlockDetail.getTargetAction());
121                         
122                         if(aLaCarte && ResourceType.VF_MODULE.equals(buildingBlockDetail.getResourceType()) && OrchestrationAction.CREATE.equals(buildingBlockDetail.getTargetAction()) &&
123                                         OrchestrationStatus.PENDING_ACTIVATION.equals(orchestrationStatus)) {                           
124                                 org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
125                                 orchestrationStatusStateTransitionDirective = processPossibleSecondStageofVfModuleCreate(execution, previousOrchestrationStatusValidationResult,
126                                                 genericVnf, orchestrationStatusStateTransitionDirective);       
127                         }
128                         
129                         if (orchestrationStatusStateTransitionDirective.getFlowDirective() == OrchestrationStatusValidationDirective.FAIL) {
130                                 throw new OrchestrationStatusValidationException(String.format(ORCHESTRATION_VALIDATION_FAIL, buildingBlockDetail.getResourceType(), buildingBlockDetail.getTargetAction(), orchestrationStatus));
131                         }
132                         
133                         execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT, orchestrationStatusStateTransitionDirective.getFlowDirective());
134                 }catch(BBObjectNotFoundException ex){
135                         if(execution.getFlowToBeCalled().contains("Unassign")){
136                                 execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT, OrchestrationStatusValidationDirective.SILENT_SUCCESS);
137                         }else{
138                                 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, ex);
139                         }
140                 }catch (Exception e) {
141                         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, e);
142                 }
143         }
144         
145         private OrchestrationStatusStateTransitionDirective processPossibleSecondStageofVfModuleCreate(BuildingBlockExecution execution, OrchestrationStatusValidationDirective previousOrchestrationStatusValidationResult,
146                         org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf genericVnf, OrchestrationStatusStateTransitionDirective orchestrationStatusStateTransitionDirective) {               
147                 if (previousOrchestrationStatusValidationResult != null && previousOrchestrationStatusValidationResult.equals(OrchestrationStatusValidationDirective.SILENT_SUCCESS)) {                 
148                         String multiStageDesign = MULTI_STAGE_DESIGN_OFF;                       
149                         if (genericVnf.getModelInfoGenericVnf() != null) {
150                                 multiStageDesign = genericVnf.getModelInfoGenericVnf().getMultiStageDesign();
151                         }
152                         if (multiStageDesign != null && multiStageDesign.equalsIgnoreCase(MULTI_STAGE_DESIGN_ON)) {                             
153                                 orchestrationStatusStateTransitionDirective.setFlowDirective(OrchestrationStatusValidationDirective.CONTINUE);                                          
154                         }                                       
155                 }
156                 return orchestrationStatusStateTransitionDirective;
157         }
158 }