0851dc9d951e5e004dfe2921ba8054a2a24c3b03
[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.adapter.vnf.tasks;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.so.adapters.vnfrest.CreateVfModuleResponse;
25 import org.onap.so.adapters.vnfrest.CreateVolumeGroupResponse;
26 import org.onap.so.adapters.vnfrest.DeleteVfModuleResponse;
27 import org.onap.so.adapters.vnfrest.DeleteVolumeGroupResponse;
28 import org.onap.so.bpmn.common.BuildingBlockExecution;
29 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
33 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
34 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
35 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
36 import org.onap.so.client.exception.ExceptionBuilder;
37 import org.onap.so.exceptions.MarshallerException;
38 import org.onap.so.logger.MessageEnum;
39 import org.onap.so.logger.MsoLogger;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Component;
42 import org.xml.sax.InputSource;
43 import org.xml.sax.XMLReader;
44
45 import javax.xml.XMLConstants;
46 import javax.xml.bind.JAXBContext;
47 import javax.xml.bind.Unmarshaller;
48 import javax.xml.parsers.SAXParserFactory;
49 import javax.xml.transform.sax.SAXSource;
50 import java.io.StringReader;
51 import java.util.ArrayList;
52 import java.util.Iterator;
53 import java.util.List;
54 import java.util.Map;
55
56 @Component
57 public class VnfAdapterImpl {
58         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, VnfAdapterImpl.class);
59         private static final String CONTRAIL_SERVICE_INSTANCE_FQDN = "contrailServiceInstanceFqdn";
60         private static final String OAM_MANAGEMENT_V4_ADDRESS = "oamManagementV4Address";
61         private static final String OAM_MANAGEMENT_V6_ADDRESS = "oamManagementV6Address";
62         private static final String CONTRAIL_NETWORK_POLICY_FQDN_LIST = "contrailNetworkPolicyFqdnList";
63         
64         @Autowired
65         private ExtractPojosForBB extractPojosForBB;
66         
67         @Autowired
68         private ExceptionBuilder exceptionUtil;
69
70         public void preProcessVnfAdapter(BuildingBlockExecution execution) {
71                 try {
72                         GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
73                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
74                         execution.setVariable("mso-request-id", gBBInput.getRequestContext().getMsoRequestId());
75                         execution.setVariable("mso-service-instance-id", serviceInstance.getServiceInstanceId());
76                         execution.setVariable("heatStackId", null);
77                         execution.setVariable(CONTRAIL_SERVICE_INSTANCE_FQDN, null);
78                         execution.setVariable(OAM_MANAGEMENT_V4_ADDRESS, null);
79                         execution.setVariable(OAM_MANAGEMENT_V6_ADDRESS, null);
80                         execution.setVariable(CONTRAIL_NETWORK_POLICY_FQDN_LIST, null);                 
81                 } catch (Exception ex) {
82                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
83                 }
84         }
85         
86         public void postProcessVnfAdapter(BuildingBlockExecution execution) {
87                 try {                   
88             String vnfAdapterResponse = execution.getVariable("vnfAdapterRestV1Response");           
89             if (!StringUtils.isEmpty( vnfAdapterResponse)) {
90                 Object vnfRestResponse = unMarshal(vnfAdapterResponse);
91                 if(vnfRestResponse instanceof CreateVfModuleResponse) {
92                     VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
93                     String heatStackId = ((CreateVfModuleResponse) vnfRestResponse).getVfModuleStackId();
94                     if(!StringUtils.isEmpty(heatStackId)) {
95                         vfModule.setHeatStackId(heatStackId);                        
96                         execution.setVariable("heatStackId", heatStackId);
97                     }
98                     Map<String,String> vfModuleOutputs = ((CreateVfModuleResponse) vnfRestResponse).getVfModuleOutputs();
99                     if (vfModuleOutputs != null) {
100                         processVfModuleOutputs(execution, vfModuleOutputs);
101                     }
102                 } else if(vnfRestResponse instanceof DeleteVfModuleResponse) {
103                     VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
104                     GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
105                     Boolean vfModuleDelete = ((DeleteVfModuleResponse) vnfRestResponse).getVfModuleDeleted();
106                     if(null!= vfModuleDelete && vfModuleDelete) {
107                         vfModule.setHeatStackId(null);
108                         execution.setVariable("heatStackId", null);
109                         Map<String,String> vfModuleOutputs = ((DeleteVfModuleResponse) vnfRestResponse).getVfModuleOutputs();
110                         if (vfModuleOutputs != null) {
111                                 processVfModuleOutputs(execution, vfModuleOutputs);
112                                 if (execution.getVariable(OAM_MANAGEMENT_V4_ADDRESS) != null) {
113                                         genericVnf.setIpv4OamAddress("");
114                                         execution.setVariable(OAM_MANAGEMENT_V4_ADDRESS, "");                                   
115                                 }
116                                 if (execution.getVariable(OAM_MANAGEMENT_V6_ADDRESS) != null) {
117                                         genericVnf.setManagementV6Address("");
118                                         execution.setVariable(OAM_MANAGEMENT_V6_ADDRESS, "");                                   
119                                 }
120                                 if (execution.getVariable(CONTRAIL_SERVICE_INSTANCE_FQDN) != null) {
121                                         vfModule.setContrailServiceInstanceFqdn("");
122                                         execution.setVariable(CONTRAIL_SERVICE_INSTANCE_FQDN, "");
123                                 }                               
124                         }
125                     }
126                 } else if(vnfRestResponse instanceof CreateVolumeGroupResponse) {
127                     VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID, execution.getLookupMap().get(ResourceKey.VOLUME_GROUP_ID));
128                     String heatStackId = ((CreateVolumeGroupResponse) vnfRestResponse).getVolumeGroupStackId();
129                     if(!StringUtils.isEmpty(heatStackId)) {
130                         volumeGroup.setHeatStackId(heatStackId);
131                         execution.setVariable("heatStackId", heatStackId);
132                     }else{
133                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "HeatStackId is missing from create VolumeGroup Vnf Adapter response.");
134                     }                    
135                 } else if(vnfRestResponse instanceof DeleteVolumeGroupResponse) {                       
136                         VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID, execution.getLookupMap().get(ResourceKey.VOLUME_GROUP_ID));
137                         Boolean volumeGroupDelete = ((DeleteVolumeGroupResponse) vnfRestResponse).getVolumeGroupDeleted();
138                         if(null!= volumeGroupDelete && volumeGroupDelete) {                             
139                                 volumeGroup.setHeatStackId(null);
140                                 execution.setVariable("heatStackId", null);
141                         }
142                 }
143             }
144                 } catch (Exception ex) {
145                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);                      
146                 }
147         }
148
149     private Object unMarshal(String input) throws MarshallerException {
150         try {
151             SAXParserFactory spf = SAXParserFactory.newInstance();
152             spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
153             spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
154             spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
155             spf.setNamespaceAware(true);
156             XMLReader xmlReader = spf.newSAXParser().getXMLReader();
157
158             JAXBContext jaxbContext = JAXBContext.newInstance(CreateVfModuleResponse.class,
159                     CreateVolumeGroupResponse.class,DeleteVfModuleResponse.class,DeleteVolumeGroupResponse.class);
160             Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
161
162             InputSource inputSource = new InputSource(new StringReader(input));
163             SAXSource source = new SAXSource(xmlReader, inputSource);
164             return jaxbUnmarshaller.unmarshal(source);
165         } catch (Exception e) {
166             msoLogger.error(MessageEnum.GENERAL_EXCEPTION, "", "", "", MsoLogger.ErrorCode.SchemaError, e.getMessage(), e);
167             throw new MarshallerException("Error parsing VNF Adapter response. " + e.getMessage(), MsoLogger.ErrorCode.SchemaError.getValue(), e);
168         }
169     }
170     
171     private void processVfModuleOutputs(BuildingBlockExecution execution, Map<String,String> vfModuleOutputs) {
172         if (vfModuleOutputs == null) {
173                 return;
174         }
175         try {
176                 VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
177                 GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
178                 List<String> contrailNetworkPolicyFqdnList = new ArrayList<String>();
179                 Iterator<String> keys = vfModuleOutputs.keySet().iterator();
180                 while (keys.hasNext()) {
181                         String key = keys.next();                       
182                         if (key.equals("contrail-service-instance-fqdn")) {
183                                         String contrailServiceInstanceFqdn = vfModuleOutputs.get(key);                          
184                                         msoLogger.debug("Obtained contrailServiceInstanceFqdn: " + contrailServiceInstanceFqdn);
185                                         vfModule.setContrailServiceInstanceFqdn(contrailServiceInstanceFqdn);
186                                         execution.setVariable(CONTRAIL_SERVICE_INSTANCE_FQDN, contrailServiceInstanceFqdn);
187                                 }
188                                 else if (key.endsWith("contrail_network_policy_fqdn")) {
189                                         String contrailNetworkPolicyFqdn = vfModuleOutputs.get(key);
190                                         msoLogger.debug("Obtained contrailNetworkPolicyFqdn: " + contrailNetworkPolicyFqdn);
191                                         contrailNetworkPolicyFqdnList.add(contrailNetworkPolicyFqdn);
192                                 }
193                                 else if (key.equals("oam_management_v4_address")) {
194                                         String oamManagementV4Address = vfModuleOutputs.get(key);
195                                         msoLogger.debug("Obtained oamManagementV4Address: " + oamManagementV4Address);
196                                         genericVnf.setIpv4OamAddress(oamManagementV4Address);
197                                         execution.setVariable(OAM_MANAGEMENT_V4_ADDRESS, oamManagementV4Address);
198                                 }
199                                 else if (key.equals("oam_management_v6_address")) {
200                                         String oamManagementV6Address = vfModuleOutputs.get(key);
201                                         msoLogger.debug("Obtained oamManagementV6Address: " + oamManagementV6Address);
202                                         genericVnf.setManagementV6Address(oamManagementV6Address);
203                                         execution.setVariable(OAM_MANAGEMENT_V6_ADDRESS, oamManagementV6Address);
204                                 }
205                         
206                         if (!contrailNetworkPolicyFqdnList.isEmpty()) {
207                                         execution.setVariable(CONTRAIL_NETWORK_POLICY_FQDN_LIST, String.join(",", contrailNetworkPolicyFqdnList));
208                                 }
209                 }
210         } catch (Exception ex) {
211                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
212                 }
213         
214     }
215 }