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