Implementing Create NS
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-bpmn-flows / src / main / java / org / onap / so / etsi / nfvo / ns / lcm / bpmn / flows / tasks / CreateNsTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.tasks;
21
22 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.CREATE_NS_REQUEST_PARAM_NAME;
23 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.CREATE_NS_RESPONSE_PARAM_NAME;
24 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.CREATE_NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME;
25 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.GLOBAL_CUSTOMER_ID_PARAM_NAME;
26 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.NS_INSTANCE_ID_PARAM_NAME;
27 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.NS_PACKAGE_MODEL_PARAM_NAME;
28 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.SERVICE_TYPE_PARAM_NAME;
29 import java.time.LocalDateTime;
30 import java.util.Optional;
31 import java.util.UUID;
32 import org.camunda.bpm.engine.delegate.DelegateExecution;
33 import org.onap.aai.domain.yang.ServiceInstance;
34 import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.NsdInfo;
35 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.exceptions.EtsiCatalogManagerRequestFailureException;
36 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.aai.AaiServiceProvider;
37 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.etsicatalog.EtsiCatalogPackageManagementServiceProvider;
38 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum;
39 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoNsInst;
40 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.State;
41 import org.onap.so.etsi.nfvo.ns.lcm.database.service.DatabaseServiceProvider;
42 import org.onap.so.etsi.nfvo.ns.lcm.model.CreateNsRequest;
43 import org.onap.so.etsi.nfvo.ns.lcm.model.InlineResponse400;
44 import org.onap.so.etsi.nfvo.ns.lcm.model.NsInstancesNsInstance;
45 import org.onap.so.etsi.nfvo.ns.lcm.model.NsInstancesNsInstance.NsStateEnum;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.stereotype.Component;
50
51 /**
52  * @author Waqas Ikram (waqas.ikram@est.tech)
53  *
54  */
55 @Component
56 public class CreateNsTask extends AbstractNetworkServiceTask {
57     private static final String NETWORK_SERVICE_NAME = "NetworkService";
58     private static final String NETWORK_SERVICE_ROLE = "NetworkService";
59     private static final String DOES_NS_PACKAGE_EXISTS_PARAM_NAME = "doesNsPackageExists";
60     private static final String DOES_NS_INSTANCE_EXISTS_PARAM_NAME = "doesNsInstanceExists";
61     private static final Logger logger = LoggerFactory.getLogger(CreateNsTask.class);
62     private final EtsiCatalogPackageManagementServiceProvider etsiCatalogPackageManagementServiceProvider;
63     private final AaiServiceProvider aaiServiceProvider;
64
65     @Autowired
66     public CreateNsTask(final DatabaseServiceProvider databaseServiceProvider,
67             final AaiServiceProvider aaiServiceProvider,
68             final EtsiCatalogPackageManagementServiceProvider etsiCatalogPackageManagementServiceProvider) {
69         super(databaseServiceProvider);
70         this.aaiServiceProvider = aaiServiceProvider;
71         this.etsiCatalogPackageManagementServiceProvider = etsiCatalogPackageManagementServiceProvider;
72     }
73
74     public void setJobStatusToStarted(final DelegateExecution execution) {
75         setJobStatus(execution, JobStatusEnum.STARTED, "Create NS workflow process started");
76     }
77
78     public void setJobStatusToFinished(final DelegateExecution execution) {
79         setJobStatus(execution, JobStatusEnum.FINISHED, "Create NS workflow process finished");
80     }
81
82     public void setJobStatusToError(final DelegateExecution execution) {
83         setJobStatusToError(execution, "Create NS workflow process failed");
84     }
85
86     public void getNsPackage(final DelegateExecution execution) {
87         logger.info("Retrieving NS package from ETSI Catalog Manager ...");
88         setJobStatus(execution, JobStatusEnum.IN_PROGRESS, "Retrieving NS package from ETSI Catalog Manager");
89
90         final CreateNsRequest createNsRequest = (CreateNsRequest) execution.getVariable(CREATE_NS_REQUEST_PARAM_NAME);
91
92         try {
93             final Optional<NsdInfo> optional =
94                     etsiCatalogPackageManagementServiceProvider.getNSPackageModel(createNsRequest.getNsdId());
95
96             if (optional.isPresent()) {
97                 final NsdInfo packageModel = optional.get();
98                 logger.info("NS Package exists {}", packageModel);
99                 execution.setVariable(NS_PACKAGE_MODEL_PARAM_NAME, packageModel);
100                 execution.setVariable(DOES_NS_PACKAGE_EXISTS_PARAM_NAME, true);
101             } else {
102                 final String message = "Unable to find NS package using NsdId: " + createNsRequest.getNsdId();
103                 logger.error(message);
104                 execution.setVariable(DOES_NS_PACKAGE_EXISTS_PARAM_NAME, false);
105                 execution.setVariable(CREATE_NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME,
106                         new InlineResponse400().detail(message));
107             }
108
109         } catch (final EtsiCatalogManagerRequestFailureException failureException) {
110             final String message =
111                     "Unexpected exception occured while getting ns package using nsdId: " + createNsRequest.getNsdId();
112             logger.error(message, failureException);
113
114             execution.setVariable(DOES_NS_PACKAGE_EXISTS_PARAM_NAME, false);
115
116             execution.setVariable(CREATE_NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME,
117                     new InlineResponse400().title(message).detail(message));
118         }
119
120     }
121
122     public void doesNsInstanceExistsInDb(final DelegateExecution execution) {
123         logger.info("Executing doesNsInstanceExistsInDb  ...");
124
125         setJobStatus(execution, JobStatusEnum.IN_PROGRESS, "Checking if NS package exists in database");
126
127         final CreateNsRequest createNsRequest =
128                 (CreateNsRequest) execution.getVariables().get(CREATE_NS_REQUEST_PARAM_NAME);
129
130         final boolean exists = databaseServiceProvider.isNsInstExists(createNsRequest.getNsName());
131         logger.info("Ns Instance entry {} exists in database", exists ? "does" : "doesn't");
132         execution.setVariable(DOES_NS_INSTANCE_EXISTS_PARAM_NAME, exists);
133
134         if (exists) {
135             final Optional<NfvoNsInst> optional =
136                     databaseServiceProvider.getNfvoNsInstByName(createNsRequest.getNsName());
137             final NfvoNsInst nfvoNsInst = optional.get();
138             execution.setVariable(CREATE_NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, new InlineResponse400()
139                     .detail("Ns Instance already exists in database : " + nfvoNsInst.toString()));
140         }
141
142         logger.info("Finished executing doesNsInstanceExistsInDb  ...");
143
144     }
145
146     public void createNsInstanceInDb(final DelegateExecution execution) {
147         logger.info("Executing createNsInstanceInDb  ...");
148
149         setJobStatus(execution, JobStatusEnum.IN_PROGRESS, "Checking if NS package exists");
150
151         final CreateNsRequest createNsRequest = (CreateNsRequest) execution.getVariable(CREATE_NS_REQUEST_PARAM_NAME);
152         final NsdInfo packageMode = (NsdInfo) execution.getVariable(NS_PACKAGE_MODEL_PARAM_NAME);
153
154         final String globalCustomerId = (String) execution.getVariable(GLOBAL_CUSTOMER_ID_PARAM_NAME);
155         final String serviceType = (String) execution.getVariable(SERVICE_TYPE_PARAM_NAME);
156
157         final String nsInstId = UUID.randomUUID().toString();
158         execution.setVariable(NS_INSTANCE_ID_PARAM_NAME, nsInstId);
159
160         databaseServiceProvider.saveNfvoNsInst(new NfvoNsInst().nsInstId(nsInstId).name(createNsRequest.getNsName())
161                 .nsPackageId(packageMode.getId()).nsdId(packageMode.getNsdId())
162                 .nsdInvariantId(packageMode.getNsdInvariantId()).description(createNsRequest.getNsDescription())
163                 .status(State.NOT_INSTANTIATED).statusUpdatedTime(LocalDateTime.now())
164                 .globalCustomerId(globalCustomerId).serviceType(serviceType));
165         logger.info("Finished executing createNsInstanceInDb  ...");
166
167     }
168
169
170     public void createNsInstanceInAai(final DelegateExecution execution) {
171         logger.info("Executing createNsInstanceInAai  ...");
172         try {
173             setJobStatus(execution, JobStatusEnum.IN_PROGRESS, "Creating NS Instance in AAI");
174
175             final CreateNsRequest createNsRequest =
176                     (CreateNsRequest) execution.getVariable(CREATE_NS_REQUEST_PARAM_NAME);
177             final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
178
179             final String globalCustomerId = (String) execution.getVariable(GLOBAL_CUSTOMER_ID_PARAM_NAME);
180             final String serviceType = (String) execution.getVariable(SERVICE_TYPE_PARAM_NAME);
181
182             final ServiceInstance aaiServiceInstance = new ServiceInstance();
183             aaiServiceInstance.setServiceInstanceId(nsInstId);
184             aaiServiceInstance.setServiceInstanceName(createNsRequest.getNsName());
185             aaiServiceInstance.setServiceType(NETWORK_SERVICE_NAME);
186             aaiServiceInstance.setServiceRole(NETWORK_SERVICE_ROLE);
187
188             aaiServiceProvider.createServiceInstance(globalCustomerId, serviceType, aaiServiceInstance);
189         } catch (final Exception exception) {
190             final String message = "Unable to Create Service Instance in AAI";
191             logger.error(message, exception);
192             abortOperation(execution, message, new InlineResponse400().detail(message));
193         }
194         logger.info("Finished executing createNsInstanceInAai  ...");
195
196     }
197
198     public void setCreateNsResponse(final DelegateExecution execution) {
199         logger.info("Executing setCreateNsResponse  ...");
200         final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
201         final Optional<NfvoNsInst> optional = databaseServiceProvider.getNfvoNsInstByNsInstId(nsInstId);
202
203         if (optional.isPresent()) {
204             final NfvoNsInst nfvoNsInst = optional.get();
205             final NsInstancesNsInstance response = new NsInstancesNsInstance().id(nfvoNsInst.getNsInstId())
206                     .nsInstanceName(nfvoNsInst.getName()).nsdId(nfvoNsInst.getNsdId())
207                     .nsdInfoId(nfvoNsInst.getNsPackageId()).nsInstanceDescription(nfvoNsInst.getDescription())
208                     .nsState(NsStateEnum.fromValue(nfvoNsInst.getStatus().toString()));
209             logger.info("Saving CreateNsResponse: {} in Execution ...", response);
210             execution.setVariable(CREATE_NS_RESPONSE_PARAM_NAME, response);
211         } else {
212             final String message = "Unable to find NS Instance in datababse using id: " + nsInstId;
213             logger.error(message);
214             abortOperation(execution, message);
215         }
216
217         logger.info("Finished executing setCreateNsResponse  ...");
218
219     }
220
221 }