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