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