88ae3746ab1d84fdc97b297067f36fe61bdf9934
[so.git] /
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 MULTI_STAGE_DESIGN_OFF = "false";
49         private static final String MULTI_STAGE_DESIGN_ON = "true";
50         
51         
52         @Autowired
53         private ExtractPojosForBB extractPojosForBB;
54         @Autowired
55         private ExceptionBuilder exceptionBuilder;
56         @Autowired
57         private CatalogDbClient catalogDbClient;
58         
59         public void validateOrchestrationStatus(BuildingBlockExecution execution) {
60                 try {
61                         OrchestrationStatusValidationDirective previousOrchestrationStatusValidationResult = execution.getVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT);
62                         
63                         execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT, null);
64                         
65                         String buildingBlockFlowName = execution.getFlowToBeCalled();
66                         
67                         BuildingBlockDetail buildingBlockDetail = catalogDbClient.getBuildingBlockDetail(buildingBlockFlowName);
68                         
69                         if (buildingBlockDetail == null) {
70                                 throw new OrchestrationStatusValidationException(String.format(BUILDING_BLOCK_DETAIL_NOT_FOUND, buildingBlockFlowName));
71                         }
72                         
73                         OrchestrationStatus orchestrationStatus = null;
74                         
75                         switch(buildingBlockDetail.getResourceType()) {
76                         case SERVICE:
77                                 org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
78                                 orchestrationStatus = serviceInstance.getOrchestrationStatus();
79                                 break;
80                         case VNF:
81                                 org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
82                                 orchestrationStatus = genericVnf.getOrchestrationStatus();
83                                 break;
84                         case VF_MODULE:
85                                 org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
86                                 orchestrationStatus = vfModule.getOrchestrationStatus();
87                                 break;
88                         case VOLUME_GROUP:
89                                 org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID, execution.getLookupMap().get(ResourceKey.VOLUME_GROUP_ID));
90                                 orchestrationStatus = volumeGroup.getOrchestrationStatus();
91                                 break;
92                         case NETWORK:
93                                 org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network network = extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID, execution.getLookupMap().get(ResourceKey.NETWORK_ID));
94                                 orchestrationStatus = network.getOrchestrationStatus();
95                                 break;
96                         case NETWORK_COLLECTION:
97                                 org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInst = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
98                                 org.onap.so.bpmn.servicedecomposition.bbobjects.Collection networkCollection = serviceInst.getCollection();
99                                 orchestrationStatus = networkCollection.getOrchestrationStatus();
100                                 break;
101                         case CONFIGURATION:
102                                 org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration configuration = extractPojosForBB.extractByKey(execution, ResourceKey.CONFIGURATION_ID, execution.getLookupMap().get(ResourceKey.CONFIGURATION_ID));
103                                 orchestrationStatus = configuration.getOrchestrationStatus();
104                                 break;
105                         default:
106                                 // can't currently get here, so not tested. Added in case enum is expanded without a change to this code
107                                 throw new OrchestrationStatusValidationException(String.format(UNKNOWN_RESOURCE_TYPE, buildingBlockFlowName, buildingBlockDetail.getResourceType(), buildingBlockDetail.getTargetAction()));
108                         }
109                         
110                         if(orchestrationStatus==null){
111                                 throw new OrchestrationStatusValidationException("The resource's orchstration status is null. Cannot perform task on a null orchestration status");
112                         }
113                         OrchestrationStatusStateTransitionDirective orchestrationStatusStateTransitionDirective = catalogDbClient.getOrchestrationStatusStateTransitionDirective(buildingBlockDetail.getResourceType(), orchestrationStatus, buildingBlockDetail.getTargetAction());
114                         
115                         if(ResourceType.VF_MODULE.equals(buildingBlockDetail.getResourceType()) && OrchestrationAction.CREATE.equals(buildingBlockDetail.getTargetAction()) &&
116                                         OrchestrationStatus.PENDING_ACTIVATION.equals(orchestrationStatus)) {                           
117                                 org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
118                                 orchestrationStatusStateTransitionDirective = processPossibleSecondStageofVfModuleCreate(execution, previousOrchestrationStatusValidationResult,
119                                                 genericVnf, orchestrationStatusStateTransitionDirective);       
120                         }
121                         
122                         if (orchestrationStatusStateTransitionDirective.getFlowDirective() == OrchestrationStatusValidationDirective.FAIL) {
123                                 throw new OrchestrationStatusValidationException(String.format(ORCHESTRATION_VALIDATION_FAIL, buildingBlockDetail.getResourceType(), buildingBlockDetail.getTargetAction(), orchestrationStatus));
124                         }
125                         
126                         execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT, orchestrationStatusStateTransitionDirective.getFlowDirective());
127                 }catch(BBObjectNotFoundException ex){
128                         if(execution.getFlowToBeCalled().contains("Unassign")){
129                                 execution.setVariable(ORCHESTRATION_STATUS_VALIDATION_RESULT, OrchestrationStatusValidationDirective.SILENT_SUCCESS);
130                         }else{
131                                 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, ex);
132                         }
133                 }catch (Exception e) {
134                         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, e);
135                 }
136         }
137         
138         private OrchestrationStatusStateTransitionDirective processPossibleSecondStageofVfModuleCreate(BuildingBlockExecution execution, OrchestrationStatusValidationDirective previousOrchestrationStatusValidationResult,
139                         org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf genericVnf, OrchestrationStatusStateTransitionDirective orchestrationStatusStateTransitionDirective) {               
140                 if (previousOrchestrationStatusValidationResult != null && previousOrchestrationStatusValidationResult.equals(OrchestrationStatusValidationDirective.SILENT_SUCCESS)) {                 
141                         String multiStageDesign = "false";                      
142                         if (genericVnf.getModelInfoGenericVnf() != null) {
143                                 multiStageDesign = genericVnf.getModelInfoGenericVnf().getMultiStageDesign();
144                         }
145                         if (multiStageDesign != null && multiStageDesign.equalsIgnoreCase("true")) {                            
146                                 orchestrationStatusStateTransitionDirective.setFlowDirective(OrchestrationStatusValidationDirective.CONTINUE);                                          
147                         }                                       
148                 }
149                 return orchestrationStatusStateTransitionDirective;
150         }
151 }