Add job handling
[so.git] / adapters / mso-vnfm-adapter / mso-vnfm-etsi-adapter / src / main / java / org / onap / so / adapters / vnfmadapter / lifecycle / LifecycleManager.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  *
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
21 package org.onap.so.adapters.vnfmadapter.lifecycle;
22
23 import com.google.common.base.Optional;
24 import java.util.UUID;
25 import org.onap.aai.domain.yang.EsrVnfm;
26 import org.onap.aai.domain.yang.GenericVnf;
27 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiClientProvider;
28 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiHelper;
29 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.VnfmServiceProvider;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
31 import org.onap.so.adapters.vnfmadapter.jobmanagement.JobManager;
32 import org.onap.so.client.aai.AAIObjectType;
33 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
34 import org.onap.so.client.graphinventory.entities.uri.Depth;
35 import org.onap.vnfmadapter.v1.model.CreateVnfRequest;
36 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
37 import org.onap.vnfmadapter.v1.model.DeleteVnfResponse;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Component;
42
43 /**
44  * Manages lifecycle operations towards the VNFMs.
45  */
46 @Component
47 public class LifecycleManager {
48     private static final Logger logger = LoggerFactory.getLogger(LifecycleManager.class);
49     private final AaiClientProvider aaiClientProvider;
50     private final VnfmServiceProvider vnfmServiceProvider;
51     private final AaiHelper aaiHelper;
52     private final JobManager jobManager;
53
54     @Autowired
55     LifecycleManager(final AaiClientProvider aaiClientProvider, final AaiHelper aaiHelper,
56             final VnfmServiceProvider vnfmServiceProvider, final JobManager jobManager) {
57         this.aaiClientProvider = aaiClientProvider;
58         this.vnfmServiceProvider = vnfmServiceProvider;
59         this.aaiHelper = aaiHelper;
60         this.jobManager = jobManager;
61     }
62
63     /**
64      * Create a VNF on a VNFM.
65      *
66      * @param vnfIdInAai the ID of the VNF in AAI
67      * @param request the create request
68      * @return the response to the request
69      */
70     public CreateVnfResponse createVnf(final String vnfIdInAai, final CreateVnfRequest request) {
71         final GenericVnf genericVnf = getGenericVnfFromAai(vnfIdInAai);
72         checkIfVnfAlreadyExistsInVnfm(genericVnf);
73
74         EsrVnfm vnfm = aaiHelper.getAssignedVnfm(genericVnf);
75         if (vnfm == null) {
76             vnfm = aaiHelper.selectVnfm(genericVnf);
77             aaiHelper.addRelationshipFromGenericVnfToVnfm(genericVnf, vnfm.getVnfmId());
78         }
79
80         // operation ID set to random value for now, will be set correctly once we implement instantiate
81         // call towards the VNFM
82         final String jobId = jobManager.createJob(vnfm.getVnfmId(), UUID.randomUUID().toString(), false);
83         final CreateVnfResponse response = new CreateVnfResponse();
84         response.setJobId(jobId);
85         return response;
86     }
87
88     private GenericVnf getGenericVnfFromAai(final String vnfIdInAai) {
89         final GenericVnf genericVnf = aaiClientProvider.getAaiClient()
90                 .get(GenericVnf.class,
91                         AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfIdInAai).depth(Depth.ONE))
92                 .orElseGet(() -> {
93                     logger.debug("No Generic Vnf matched by id");
94                     return null;
95                 });
96         logger.debug("Retrieved generic VNF from AAI: " + genericVnf);
97         return genericVnf;
98     }
99
100     private void checkIfVnfAlreadyExistsInVnfm(final GenericVnf genericVnf) {
101         if (genericVnf.getSelflink() != null && !genericVnf.getSelflink().isEmpty()) {
102             Optional<InlineResponse201> response = Optional.absent();
103             try {
104                 response = vnfmServiceProvider.getVnf(genericVnf.getSelflink());
105             } catch (final Exception exception) {
106                 logger.debug("Ignoring invalid self link in generic vnf", exception);
107             }
108             if (response.isPresent()) {
109                 throw new IllegalArgumentException("VNF " + genericVnf.getVnfId()
110                         + " is already defined on the VNFM, self link: " + genericVnf.getSelflink());
111             }
112         }
113     }
114
115     /**
116      * Delete a VNF on a VNFM.
117      *
118      * @param vnfIdInAai the ID of the VNF in AAI
119      * @return the response to the request
120      */
121     public DeleteVnfResponse deleteVnf(final String vnfIdInAai) {
122         // vnfm ID and operation ID set to random value for now, will be set correctly once we implement
123         // terminate call towards the VNFM
124         final String jobId = jobManager.createJob(UUID.randomUUID().toString(), UUID.randomUUID().toString(), true);
125         final DeleteVnfResponse response = new DeleteVnfResponse();
126         response.setJobId(jobId);
127         return response;
128     }
129 }