Add missing tests
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / notification / GenericVnfManager.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification;
17
18 import com.nokia.cbam.lcm.v32.model.VnfInfo;
19 import java.util.ArrayList;
20 import java.util.NoSuchElementException;
21 import java.util.Optional;
22 import org.onap.aai.model.GenericVnf;
23 import org.onap.aai.model.Relationship;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProviderForSo;
27 import org.slf4j.Logger;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.stereotype.Component;
30
31 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager.SERVICE_NAME;
32 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
33 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
34
35 /**
36  * Responsible for managing the {@link GenericVnf} in AAI
37  */
38 @Component
39 public class GenericVnfManager extends AbstractManager {
40     private static final long MAX_MS_TO_WAIT_FOR_VNF_TO_APPEAR = 30 * 1000L;
41     private static Logger logger = org.slf4j.LoggerFactory.getLogger(GenericVnfManager.class);
42
43     @Autowired
44     GenericVnfManager(AAIRestApiProvider aaiRestApiProvider, CbamRestApiProviderForSo cbamRestApiProvider) {
45         super(aaiRestApiProvider, cbamRestApiProvider);
46     }
47
48     static Relationship linkTo(String vnfId) {
49         Relationship relationship = new Relationship();
50         relationship.setRelatedTo("generic-vnf");
51         relationship.setRelationshipData(new ArrayList<>());
52         relationship.getRelationshipData().add(buildRelationshipData("generic-vnf.vnf-id", vnfId));
53         return relationship;
54     }
55
56     private static Relationship linkToNs(String nsId) {
57         Relationship relationship = new Relationship();
58         relationship.setRelatedTo("service-instance");
59         relationship.setRelationshipData(new ArrayList<>());
60         relationship.getRelationshipData().add(buildRelationshipData("service-instance.service-instance-id", nsId));
61         return relationship;
62     }
63
64     @Override
65     protected Logger getLogger() {
66         return logger;
67     }
68
69     public void createOrUpdate(String vnfId, boolean inMaintenance, String vnfmId, Optional<String> nsId) {
70         try {
71             GenericVnf vnf = waitForVnfToAppearInAai(vnfId);
72             updateFields(vnf, vnfId, inMaintenance, vnfmId, nsId);
73         } catch (NoSuchElementException e) {
74             try {
75                 logger.warn("The VNF with " + vnfId + " identifier did not appear in time", e);
76                 updateFields(new GenericVnf(), vnfId, inMaintenance, vnfmId, nsId);
77             } catch (Exception e2) {
78                 logger.warn("The VNF with " + vnfId + " identifier has been created since after the maximal wait for VNF to appear timeout", e2);
79                 //the VNF might have been created since the last poll
80                 updateFields(getExistingVnf(vnfId), vnfId, inMaintenance, vnfmId, nsId);
81             }
82         }
83     }
84
85     private void updateFields(GenericVnf vnf, String vnfId, boolean inMaintenance, String vnfmId, Optional<String> nsId) {
86         try {
87             VnfInfo vnfInfo = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdGet(vnfId, CbamRestApiProvider.NOKIA_LCM_API_VERSION).blockingFirst();
88             vnf.setVnfName(vnfInfo.getName());
89         } catch (RuntimeException e) {
90             throw buildFatalFailure(logger, "Unable to query VNF with " + vnfId + " identifier from CBAM", e);
91         }
92         vnf.setVnfId(vnfId);
93         vnf.setInMaint(inMaintenance);
94         vnf.setNfType(SERVICE_NAME);
95         //FIXME whould be good to know if this parameter is relevant or not? (mandatory)
96         vnf.setVnfType("NokiaVNF");
97         vnf.setIsClosedLoopDisabled(inMaintenance);
98         if (vnf.getRelationshipList() == null) {
99             vnf.setRelationshipList(new ArrayList<>());
100         }
101         if (nsId.isPresent()) {
102             addSingletonRelation(vnf.getRelationshipList(), linkToNs(nsId.get()));
103         }
104         else{
105             logger.warn("Not linking VNF with {} identifier to any NS", vnfId);
106         }
107         aaiRestApiProvider.getNetworkApi().createOrUpdateNetworkGenericVnfsGenericVnf(vnf.getVnfId(), vnf).blockingFirst();
108     }
109
110     private GenericVnf waitForVnfToAppearInAai(String vnfId) {
111         long timeoutInMs = systemFunctions().currentTimeMillis() + MAX_MS_TO_WAIT_FOR_VNF_TO_APPEAR;
112         while (timeoutInMs - systemFunctions().currentTimeMillis() > 0) {
113             try {
114                 return getExistingVnf(vnfId);
115             } catch (NoSuchElementException e) {
116                 logger.debug("Unable to get VNF with " + vnfId + " identifier", e);
117             }
118             systemFunctions().sleep(3 * 1000L);
119         }
120         throw new NoSuchElementException();
121     }
122
123     private GenericVnf getExistingVnf(String vnfId) {
124         return aaiRestApiProvider.getNetworkApi().getNetworkGenericVnfsGenericVnf(vnfId, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null).blockingFirst();
125     }
126 }