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