2 * Copyright 2016-2017, Nokia Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification;
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;
30 import java.util.NoSuchElementException;
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;
38 * Responsible for managing the {@link GenericVnf} in AAI
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);
48 GenericVnfManager(AAIRestApiProvider aaiRestApiProvider, CbamRestApiProvider cbamRestApiProvider, DriverProperties driverProperties) {
49 super(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
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));
60 protected Logger getLogger() {
64 void createOrUpdate(String vnfId, boolean inMaintenance) {
66 GenericVnf vnf = waitForVnfToAppearInAai(vnfId);
67 updateFields(vnf, vnfId, inMaintenance);
68 } catch (NoSuchElementException e) {
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);
80 GenericVnf getExistingVnf(String vnfId) {
81 return aaiRestApiProvider.get(logger, NETWORK, format(VNF_URL, vnfId), GenericVnf.class);
84 private void updateFields(GenericVnf vnf, String vnfId, boolean inMaintenance) {
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);
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);
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) {
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);
108 systemFunctions().sleep(3 * 1000L);
110 throw new NoSuchElementException();