4506699ce8d1e3e6beea60910354984bc7d5135e
[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                         vfModule.setSelflink(selfLink);
79                         if(vfModule.getSelflink() != null && !vfModule.getSelflink().isEmpty()) {       
80                                 String response = sdncVfModuleResources.queryVfModule(vfModule);                
81                                 execution.setVariable("SDNCQueryResponse_" + vfModule.getVfModuleId(), response);                       
82                         }
83                         else {
84                                 throw new Exception("Vf Module " + vfModule.getVfModuleId() + " exists in gBuildingBlock but does not have a selflink value");
85                         }
86                 } catch (Exception ex) {                        
87                     exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
88                 }
89         }
90         
91         public void queryVfModuleForVolumeGroup(BuildingBlockExecution execution) {
92                 try {
93                         VfModule vfModule =  extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
94                         if(vfModule.getSelflink() != null && !vfModule.getSelflink().isEmpty()) {
95                                 String response = sdncVfModuleResources.queryVfModule(vfModule);
96                                 execution.setVariable("SDNCQueryResponse_" + vfModule.getVfModuleId(), response);
97                         }
98                         else {
99                                 throw new Exception("Vf Module " + vfModule.getVfModuleId() + " exists in gBuildingBlock but does not have a selflink value");
100                         }
101                 } catch(BBObjectNotFoundException bbException) {
102                         // 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
103                         // 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
104                         if(!ResourceKey.VF_MODULE_ID.equals(bbException.getResourceKey())) {
105                                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, bbException);
106                         }
107                 } catch(Exception ex) {
108                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
109                 }
110         }
111 }