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