Bugfixes for December 2018
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / sdnc / tasks / SDNCQueryTasks.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.sdnc.tasks;
22
23 import org.onap.so.bpmn.common.BuildingBlockExecution;
24 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
25 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
26 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
27 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
28 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
29 import org.onap.so.client.exception.BBObjectNotFoundException;
30 import org.onap.so.client.exception.BadResponseException;
31 import org.onap.so.client.exception.ExceptionBuilder;
32 import org.onap.so.client.orchestration.SDNCVnfResources;
33 import org.onap.so.client.orchestration.SDNCVfModuleResources;
34 import org.onap.so.logger.MsoLogger;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
37
38 @Component
39 public class SDNCQueryTasks {
40         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, SDNCQueryTasks.class);        
41         @Autowired
42         private SDNCVnfResources sdncVnfResources;
43         @Autowired
44         private SDNCVfModuleResources sdncVfModuleResources;
45         @Autowired
46         private ExceptionBuilder exceptionUtil;
47         @Autowired
48         private ExtractPojosForBB extractPojosForBB;
49         
50         public void queryVnf(BuildingBlockExecution execution) throws Exception {       
51                 ServiceInstance serviceInstance =  extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
52                 GenericVnf genericVnf =  extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
53                                 
54                 String selfLink = "restconf/config/GENERIC-RESOURCE-API:services/service/"
55                                                         + serviceInstance.getServiceInstanceId() + "/service-data/vnfs/vnf/"
56                                                         + genericVnf.getVnfId() + "/vnf-data/vnf-topology/";
57                 try {
58                         if(genericVnf.getSelflink() == null) {
59                                 genericVnf.setSelflink(selfLink);
60                         }
61                         String response = sdncVnfResources.queryVnf(genericVnf);                
62                         execution.setVariable("SDNCQueryResponse_" + genericVnf.getVnfId(), response);                  
63                 } catch (Exception ex) {                        
64                     exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
65                 }
66         }
67
68         
69         public void queryVfModule(BuildingBlockExecution execution) throws Exception {          
70                 ServiceInstance serviceInstance =  extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
71                 GenericVnf genericVnf =  extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
72                 VfModule vfModule =  extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));               
73                 String selfLink = "restconf/config/GENERIC-RESOURCE-API:services/service/"
74                                 + serviceInstance.getServiceInstanceId() + "/service-data/vnfs/vnf/"
75                                 + genericVnf.getVnfId() + "/vnf-data/vf-modules/vf-module/"
76                                 + vfModule.getVfModuleId() + "/vf-module-data/vf-module-topology/";
77                 try {
78                         if(vfModule.getSelflink() == null || (vfModule.getSelflink() != null && vfModule.getSelflink().isEmpty())) {
79                                 vfModule.setSelflink(selfLink);
80                         }
81                         if(vfModule.getSelflink() != null && !vfModule.getSelflink().isEmpty()) {       
82                                 String response = sdncVfModuleResources.queryVfModule(vfModule);                
83                                 execution.setVariable("SDNCQueryResponse_" + vfModule.getVfModuleId(), response);                       
84                         }
85                         else {
86                                 throw new Exception("Vf Module " + vfModule.getVfModuleId() + " exists in gBuildingBlock but does not have a selflink value");
87                         }
88                 } catch (Exception ex) {                        
89                     exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
90                 }
91         }
92         
93         public void queryVfModuleForVolumeGroup(BuildingBlockExecution execution) {
94                 try {
95                         VfModule vfModule =  extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
96                         if(vfModule.getSelflink() != null && !vfModule.getSelflink().isEmpty()) {
97                                 String response = sdncVfModuleResources.queryVfModule(vfModule);
98                                 execution.setVariable("SDNCQueryResponse_" + vfModule.getVfModuleId(), response);
99                         }
100                         else {
101                                 throw new Exception("Vf Module " + vfModule.getVfModuleId() + " exists in gBuildingBlock but does not have a selflink value");
102                         }
103                 } catch(BBObjectNotFoundException bbException) {
104                         // If there is not a vf module in the general building block, we will not call SDNC and proceed as normal without throwing an error
105                         // If we see a bb object not found exception for something that is not a vf module id, then we should throw the error as normal
106                         if(!ResourceKey.VF_MODULE_ID.equals(bbException.getResourceKey())) {
107                                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, bbException);
108                         }
109                 } catch(Exception ex) {
110                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
111                 }
112         }
113 }