Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / java / org / onap / so / bpmn / infrastructure / pnf / delegate / PrepareConfigDeployDelegate.java
1 /*
2  * ============LICENSE_START======================================================= Copyright (C) 2019 Nordix
3  * Foundation. ================================================================================ Licensed under the
4  * Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may
5  * obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
8  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
9  * either express or implied. See the License for the specific language governing permissions and limitations under the
10  * License.
11  *
12  * SPDX-License-Identifier: Apache-2.0 ============LICENSE_END=========================================================
13  */
14
15 package org.onap.so.bpmn.infrastructure.pnf.delegate;
16
17 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.MODEL_UUID;
18 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_CORRELATION_ID;
19 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_UUID;
20 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_CUSTOMIZATION_UUID;
21 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.SERVICE_INSTANCE_ID;
22 import java.io.IOException;
23 import java.util.Optional;
24 import org.camunda.bpm.engine.delegate.DelegateExecution;
25 import org.camunda.bpm.engine.delegate.JavaDelegate;
26 import org.onap.aai.domain.yang.Pnf;
27 import org.onap.so.bpmn.infrastructure.pnf.management.PnfManagement;
28 import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
29 import org.onap.so.client.cds.beans.ConfigDeployPropertiesForPnf;
30 import org.onap.so.client.cds.beans.ConfigDeployRequestPnf;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
33
34 /**
35  * This implementation of {@link JavaDelegate} is used to prepare for config Deploy.
36  *
37  * It queries the PNF resource customization table and construct the {@link AbstractCDSPropertiesBean} as
38  * executionObject.
39  */
40 @Component
41 public class PrepareConfigDeployDelegate extends PrepareCdsCallDelegate {
42
43     @Autowired
44     private PnfManagement pnfManagement;
45
46     public PrepareConfigDeployDelegate() {
47         this.actionName = "config-deploy";
48         this.mode = "async";
49     }
50
51     @Override
52     protected String getRequestObject(DelegateExecution delegateExecution) {
53
54         ConfigDeployPropertiesForPnf configDeployProperties = new ConfigDeployPropertiesForPnf();
55
56         configDeployProperties.setServiceInstanceId((String) delegateExecution.getVariable(SERVICE_INSTANCE_ID));
57
58         /**
59          * PNF Name matches the name in AAI, i.e., correlationID as in customized workflow.
60          */
61         configDeployProperties.setPnfName((String) delegateExecution.getVariable(PNF_CORRELATION_ID));
62
63         /**
64          * PNF id match AAI entry, i.e, PNF UUID.
65          */
66         configDeployProperties.setPnfId((String) delegateExecution.getVariable(PNF_UUID));
67         configDeployProperties.setPnfCustomizationUuid((String) delegateExecution.getVariable(PRC_CUSTOMIZATION_UUID));
68         configDeployProperties.setServiceModelUuid((String) delegateExecution.getVariable(MODEL_UUID));
69         setIpAddress(configDeployProperties, delegateExecution);
70
71         ConfigDeployRequestPnf configDeployRequest = new ConfigDeployRequestPnf();
72         configDeployRequest.setConfigDeployPropertiesForPnf(configDeployProperties);
73
74         /**
75          * Resolution key is the same as PNF name.
76          */
77         configDeployRequest.setResolutionKey((String) delegateExecution.getVariable(PNF_CORRELATION_ID));
78
79         return configDeployRequest.toString();
80     }
81
82     private void setIpAddress(ConfigDeployPropertiesForPnf configDeployProperties,
83             DelegateExecution delegateExecution) {
84
85         /**
86          * Retrieve PNF entry from AAI.
87          */
88         try {
89             String pnfName = (String) delegateExecution.getVariable(PNF_CORRELATION_ID);
90             Optional<Pnf> pnfOptional = pnfManagement.getEntryFor(pnfName);
91             if (pnfOptional.isPresent()) {
92                 Pnf pnf = pnfOptional.get();
93
94                 /**
95                  * PRH patches the AAI with oam address. Use ipaddress-v4-oam and ipaddress-v6-oam for the config deploy
96                  * request.
97                  */
98                 configDeployProperties.setPnfIpV4Address(pnf.getIpaddressV4Oam());
99                 configDeployProperties.setPnfIpV6Address(pnf.getIpaddressV6Oam());
100             } else {
101                 exceptionUtil.buildAndThrowWorkflowException(delegateExecution, ERROR_CODE,
102                         "AAI entry for PNF: " + pnfName + " does not exist");
103             }
104         } catch (IOException e) {
105             logger.warn(e.getMessage(), e);
106             exceptionUtil.buildAndThrowWorkflowException(delegateExecution, ERROR_CODE,
107                     "Unable to fetch from AAI" + e.getMessage());
108         }
109     }
110 }