Check for existing VNF in VNFM
[so.git] / adapters / mso-vnfm-adapter / mso-vnfm-etsi-adapter / src / main / java / org / onap / so / adapters / vnfmadapter / extclients / aai / AaiHelper.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.extclients.aai;
22
23 import java.util.Collections;
24 import org.onap.aai.domain.yang.EsrSystemInfo;
25 import org.onap.aai.domain.yang.EsrSystemInfoList;
26 import org.onap.aai.domain.yang.EsrVnfm;
27 import org.onap.aai.domain.yang.EsrVnfmList;
28 import org.onap.aai.domain.yang.GenericVnf;
29 import org.onap.aai.domain.yang.Relationship;
30 import org.onap.aai.domain.yang.RelationshipData;
31 import org.onap.aai.domain.yang.RelationshipList;
32 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmNotFoundException;
33 import org.onap.so.client.aai.AAIObjectType;
34 import org.onap.so.client.aai.AAIVersion;
35 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Service;
40
41 /**
42  * Provides helper methods for interactions with AAI.
43  */
44 @Service
45 public class AaiHelper {
46
47     private static final Logger logger = LoggerFactory.getLogger(AaiHelper.class);
48     private final AaiServiceProvider aaiServiceProvider;
49
50     @Autowired
51     public AaiHelper(final AaiServiceProvider aaiServiceProvider) {
52         this.aaiServiceProvider = aaiServiceProvider;
53     }
54
55     /**
56      * Add a relationship to the given generic VNF to the given VNFM.
57      *
58      * @param vnf the generic VNF
59      * @param vnfmId the ID of the VNFM
60      */
61     public void addRelationshipFromGenericVnfToVnfm(final GenericVnf vnf, final String vnfmId) {
62         if (vnf.getRelationshipList() == null) {
63             vnf.setRelationshipList(new RelationshipList());
64         }
65         final RelationshipList vnfmRelationshiplist = vnf.getRelationshipList();
66         vnfmRelationshiplist.getRelationship().add(createRelationshipToVnfm(vnfmId));
67
68         aaiServiceProvider.invokePutGenericVnf(vnf);
69     }
70
71     private Relationship createRelationshipToVnfm(final String vnfmId) {
72         final Relationship relationship = new Relationship();
73         relationship.setRelatedTo("esr-vnfm");
74         relationship.setRelationshipLabel("tosca.relationships.DependsOn");
75         relationship.setRelatedLink("/aai/" + AAIVersion.LATEST
76                 + AAIUriFactory.createResourceUri(AAIObjectType.VNFM, vnfmId).build().toString());
77         relationship.getRelationshipData().add(createRelationshipData("esr-vnfm.vnfm-id", vnfmId));
78         return relationship;
79     }
80
81     private RelationshipData createRelationshipData(final String key, final String value) {
82         final RelationshipData data = new RelationshipData();
83         data.setRelationshipKey(key);
84         data.setRelationshipValue(value);
85         return data;
86     }
87
88     /**
89      * Get the VNFM assigned for use for the given generic VNF.
90      *
91      * @param vnf the generic VNF
92      * @return the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
93      */
94     public EsrVnfm getAssignedVnfm(final GenericVnf vnf) {
95         for (final Relationship relationship : vnf.getRelationshipList() == null ? Collections.<Relationship>emptyList()
96                 : vnf.getRelationshipList().getRelationship()) {
97             if ("esr-vnfm".equals(relationship.getRelatedTo())) {
98                 return getRelatedVnfmId(relationship);
99             }
100         }
101         return null;
102     }
103
104     private EsrVnfm getRelatedVnfmId(final Relationship relationship) {
105         for (final RelationshipData relationshipData : relationship.getRelationshipData()) {
106             if ("esr-vnfm.vnfm-id".equals(relationshipData.getRelationshipKey())) {
107                 logger.debug("VNFM URL from GenericVnf relataionship: " + relationshipData.getRelationshipValue());
108                 return aaiServiceProvider.invokeGetVnfm(relationshipData.getRelationshipValue());
109             }
110         }
111         return null;
112     }
113
114     /**
115      * Select a VNFM to use for the given generic VNF. Should only be used when no VNFM has already been
116      * assigned to the VNF.
117      *
118      * @param vnf the generic VNF
119      * @return the VNFM to use
120      */
121     public EsrVnfm selectVnfm(final GenericVnf vnf) {
122         final EsrVnfmList vnfmsInEsr = aaiServiceProvider.invokeGetVnfms();
123
124         if (vnfmsInEsr == null) {
125             throw new VnfmNotFoundException("No VNFMs found in AAI ESR");
126         }
127         logger.debug("VNFMs in ESR: " + vnfmsInEsr);
128
129         for (final EsrVnfm vnfm : vnfmsInEsr.getEsrVnfm()) {
130             if (vnfmHasMatchingEsrSystemInfoType(vnfm, vnf.getNfType())) {
131                 return vnfm;
132             }
133         }
134         throw new VnfmNotFoundException("No matching VNFM found in AAI ESR");
135     }
136
137     private boolean vnfmHasMatchingEsrSystemInfoType(final EsrVnfm vnfm, final String type) {
138         logger.debug("Checking VNFM ID: " + vnfm + ": " + vnfm.getVnfmId());
139
140         final EsrSystemInfoList systemInfolist = aaiServiceProvider.invokeGetVnfmEsrSystemInfoList(vnfm.getVnfmId());
141         if (systemInfolist != null) {
142             for (final EsrSystemInfo esrSystemInfo : systemInfolist.getEsrSystemInfo()) {
143                 if (esrSystemInfo.getType().equals(type)) {
144                     logger.debug("Matched VNFM ID: " + vnfm + ", based on type");
145                     return true;
146                 }
147             }
148         }
149         return false;
150     }
151
152 }