Check for existing VNF in VNFM
[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.client.aai.AAIObjectType;
32 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
33 import org.onap.so.client.graphinventory.entities.uri.Depth;
34 import org.onap.vnfmadapter.v1.model.CreateVnfRequest;
35 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 public class LifecycleManager {
43     private static final Logger logger = LoggerFactory.getLogger(LifecycleManager.class);
44     private final AaiClientProvider aaiClientProvider;
45     private final VnfmServiceProvider vnfmServiceProvider;
46     private final AaiHelper aaiHelper;
47
48     @Autowired
49     LifecycleManager(final AaiClientProvider aaiClientProvider, final AaiHelper aaiHelper,
50             final VnfmServiceProvider vnfmServiceProvider) {
51         this.aaiClientProvider = aaiClientProvider;
52         this.vnfmServiceProvider = vnfmServiceProvider;
53         this.aaiHelper = aaiHelper;
54     }
55
56     public CreateVnfResponse createVnf(final String vnfIdInAai, final CreateVnfRequest request) {
57         final GenericVnf genericVnf = getGenericVnfFromAai(vnfIdInAai);
58         checkIfVnfAlreadyExistsInVnfm(genericVnf);
59
60         EsrVnfm vnfm = aaiHelper.getAssignedVnfm(genericVnf);
61         if (vnfm == null) {
62             vnfm = aaiHelper.selectVnfm(genericVnf);
63             aaiHelper.addRelationshipFromGenericVnfToVnfm(genericVnf, vnfm.getVnfmId());
64         }
65
66         final CreateVnfResponse response = new CreateVnfResponse();
67         response.setJobId(UUID.randomUUID().toString());
68         return response;
69     }
70
71     private GenericVnf getGenericVnfFromAai(final String vnfIdInAai) {
72         final GenericVnf genericVnf = aaiClientProvider.getAaiClient()
73                 .get(GenericVnf.class,
74                         AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfIdInAai).depth(Depth.ONE))
75                 .orElseGet(() -> {
76                     logger.debug("No Generic Vnf matched by id");
77                     return null;
78                 });
79         logger.debug("Retrieved generic VNF from AAI: " + genericVnf);
80         return genericVnf;
81     }
82
83     private void checkIfVnfAlreadyExistsInVnfm(final GenericVnf genericVnf) {
84         if (genericVnf.getSelflink() != null && !genericVnf.getSelflink().isEmpty()) {
85             Optional<InlineResponse201> response = Optional.absent();
86             try {
87                 response = vnfmServiceProvider.getVnf(genericVnf.getSelflink());
88             } catch (final Exception exception) {
89                 logger.debug("Ignoring invalid self link in generic vnf", exception);
90             }
91             if (response.isPresent()) {
92                 throw new IllegalArgumentException("VNF " + genericVnf.getVnfId()
93                         + " is already defined on the VNFM, self link: " + genericVnf.getSelflink());
94             }
95         }
96     }
97 }