2b9b3882cea9c4daa5fe2b61de67ff3c3046d9bb
[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 org.onap.aai.domain.yang.v11.GenericVnf;
20 import org.onap.aai.domain.yang.v11.Relationship;
21 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
22 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
25 import org.slf4j.Logger;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.context.annotation.Conditional;
28 import org.springframework.stereotype.Component;
29
30 import java.util.NoSuchElementException;
31
32 import static java.lang.String.format;
33 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider.AAIService.NETWORK;
34 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
35 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
36
37 /**
38  * Responsible for managing the {@link GenericVnf} in AAI
39  */
40 @Component
41 @Conditional(value = Conditions.UseForDirect.class)
42 class GenericVnfManager extends AbstractManager {
43     private static final String VNF_URL = "/generic-vnfs/generic-vnf/%s";
44     private static final long MAX_MS_TO_WAIT_FOR_VNF_TO_APPEAR = 30 * 1000L;
45     private static Logger logger = org.slf4j.LoggerFactory.getLogger(GenericVnfManager.class);
46
47     @Autowired
48     GenericVnfManager(AAIRestApiProvider aaiRestApiProvider, CbamRestApiProvider cbamRestApiProvider, DriverProperties driverProperties) {
49         super(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
50     }
51
52     static Relationship linkTo(String vnfId) {
53         Relationship relationship = new Relationship();
54         relationship.setRelatedTo("generic-vnf");
55         relationship.getRelationshipData().add(buildRelationshipData("generic-vnf.vnf-id", vnfId));
56         return relationship;
57     }
58
59     @Override
60     protected Logger getLogger() {
61         return logger;
62     }
63
64     void createOrUpdate(String vnfId, boolean inMaintenance) {
65         try {
66             GenericVnf vnf = waitForVnfToAppearInAai(vnfId);
67             updateFields(vnf, vnfId, inMaintenance);
68         } catch (NoSuchElementException e) {
69             try {
70                 logger.warn("The VNF with " + vnfId + " identifier did not appear in time", e);
71                 updateFields(OBJECT_FACTORY.createGenericVnf(), vnfId, inMaintenance);
72             } catch (Exception e2) {
73                 logger.warn("The VNF with " + vnfId + " identifier has been created since after the maximal wait for VNF to appear timeout", e2);
74                 //the VNF might have been created since the last poll
75                 updateFields(getExistingVnf(vnfId), vnfId, inMaintenance);
76             }
77         }
78     }
79
80     GenericVnf getExistingVnf(String vnfId) {
81         return aaiRestApiProvider.get(logger, NETWORK, format(VNF_URL, vnfId), GenericVnf.class);
82     }
83
84     private void updateFields(GenericVnf vnf, String vnfId, boolean inMaintenance) {
85         try {
86             VnfInfo vnfInfo = cbamRestApiProvider.getCbamLcmApi(driverProperties.getVnfmId()).vnfsVnfInstanceIdGet(vnfId, CbamRestApiProvider.NOKIA_LCM_API_VERSION).blockingFirst();
87             vnf.setVnfName(vnfInfo.getName());
88         } catch (RuntimeException e) {
89             throw buildFatalFailure(logger, "Unable to query VNF with " + vnfId + " identifier from CBAM", e);
90         }
91         vnf.setVnfId(vnfId);
92         vnf.setInMaint(inMaintenance);
93         vnf.setVnfInstanceId(vnfId);
94         //FIXME whould be good to know if this parameter is relevant or not? (mandatory)
95         vnf.setVnfType("NokiaVNF");
96         vnf.setIsClosedLoopDisabled(inMaintenance);
97         aaiRestApiProvider.put(logger, NETWORK, format(VNF_URL, vnf.getVnfId()), vnf, Void.class);
98     }
99
100     private GenericVnf waitForVnfToAppearInAai(String vnfId) {
101         long timeoutInMs = systemFunctions().currentTimeMillis() + MAX_MS_TO_WAIT_FOR_VNF_TO_APPEAR;
102         while (timeoutInMs - systemFunctions().currentTimeMillis() > 0) {
103             try {
104                 return aaiRestApiProvider.get(logger, NETWORK, format(VNF_URL, vnfId), GenericVnf.class);
105             } catch (NoSuchElementException e) {
106                 logger.debug("Unable to get VNF with " + vnfId + " identifier", e);
107             }
108             systemFunctions().sleep(3 * 1000L);
109         }
110         throw new NoSuchElementException();
111     }
112
113 }