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.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core;
19 import com.nokia.cbam.lcn.v32.api.SubscriptionsApi;
20 import com.nokia.cbam.lcn.v32.model.*;
21 import java.util.ArrayList;
22 import org.onap.msb.model.MicroServiceFullInfo;
23 import org.onap.msb.model.MicroServiceInfo;
24 import org.onap.msb.model.Node;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
27 import org.slf4j.Logger;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.beans.factory.annotation.Value;
30 import org.springframework.stereotype.Component;
32 import static com.nokia.cbam.lcn.v32.model.SubscriptionAuthentication.TypeEnum.NONE;
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 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCN_API_VERSION;
36 import static org.slf4j.LoggerFactory.getLogger;
39 * Responsible for registering the driver in the core systems.
42 public class SelfRegistrationManager {
43 public static final String DRIVER_VERSION = "v1";
44 public static final String SERVICE_NAME = "NokiaSVNFM";
45 // 1 means internal 0 means core :)
46 public static final String INTERNAL_SERVICE = "1";
47 public static final String SWAGGER_API_DEFINITION = "self.swagger.json";
48 private static Logger logger = getLogger(SelfRegistrationManager.class);
49 private final DriverProperties driverProperties;
50 private final MsbApiProvider msbApiProvider;
51 private final CbamRestApiProvider cbamRestApiProvider;
53 @Value("${driverMsbExternalIp}")
54 private String driverMsbExternalIp;
55 @Value("${driverVnfmExternalIp}")
56 private String driverVnfmExternalIp;
57 @Value("${server.port}")
58 private String driverPort;
59 private volatile boolean ready = false;
62 SelfRegistrationManager(DriverProperties driverProperties, MsbApiProvider msbApiProvider, CbamRestApiProvider cbamRestApiProvider) {
63 this.cbamRestApiProvider = cbamRestApiProvider;
64 this.msbApiProvider = msbApiProvider;
65 this.driverProperties = driverProperties;
69 * Register the driver in micro-service bus and subscribe to LCNs from CBAM
71 public void register() {
72 //the order is important (only publish it's existence after the subscription has been created)
73 subscribeToLcn(driverProperties.getVnfmId());
75 registerMicroService();
76 } catch (RuntimeException e) {
77 deleteSubscription(driverProperties.getVnfmId());
84 * De-register the VNFM driver from the micro-service bus
86 public void deRegister() {
88 logger.info("Cancelling micro service registration");
89 msbApiProvider.getMsbApi().deleteMicroService(SERVICE_NAME, DRIVER_VERSION, null, null).blockingFirst();
90 } catch (Exception e) {
91 //ONAP throws 500 internal server error, but deletes the micro service
92 boolean serviceFoundAfterDelete = false;
94 msbApiProvider.getMsbApi().getMicroService_0(SERVICE_NAME, DRIVER_VERSION, null, null, null, null, null);
95 serviceFoundAfterDelete = true;
96 } catch (Exception e1) {
97 logger.info("Unable to query " + SERVICE_NAME + " from MSB (so the service was successfully deleted)", e1);
98 // the micro service was deleted (even though 500 HTTP code was reported)
100 if (serviceFoundAfterDelete) {
101 throw buildFatalFailure(logger, "Unable to deRegister Nokia VNFM driver", e);
104 deleteSubscription(driverProperties.getVnfmId());
108 * @return the swagger API definition
110 public byte[] getSwaggerApiDefinition() {
111 return systemFunctions().loadFile(SWAGGER_API_DEFINITION);
114 private String getDriverVnfmUrl() {
115 return "http://" + driverVnfmExternalIp + ":" + driverPort + DriverProperties.BASE_URL;
118 private void deleteSubscription(String vnfmId) {
119 logger.info("Deleting CBAM LCN subscription");
120 SubscriptionsApi lcnApi = cbamRestApiProvider.getCbamLcnApi(vnfmId);
122 String callbackUrl = getDriverVnfmUrl() + DriverProperties.LCN_URL;
123 for (Subscription subscription : lcnApi.subscriptionsGet(NOKIA_LCN_API_VERSION).blockingFirst()) {
124 if (subscription.getCallbackUrl().equals(callbackUrl)) {
125 logger.info("Deleting subscription with {} identifier", subscription.getId());
126 lcnApi.subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION).blockingFirst();
129 } catch (Exception e) {
130 throw buildFatalFailure(logger, "Unable to delete CBAM LCN subscription", e);
134 private MicroServiceFullInfo registerMicroService() {
135 logger.info("Registering micro service");
136 MicroServiceInfo microServiceInfo = new MicroServiceInfo();
137 microServiceInfo.setUrl(DriverProperties.BASE_URL);
138 //the PATH should not be set
139 microServiceInfo.setProtocol(MicroServiceInfo.ProtocolEnum.REST);
140 microServiceInfo.setVisualRange(MicroServiceInfo.VisualRangeEnum._1);
141 microServiceInfo.setServiceName(SERVICE_NAME);
142 microServiceInfo.setVersion(DRIVER_VERSION);
143 microServiceInfo.setEnableSsl(false);
144 Node node = new Node();
145 microServiceInfo.setNodes(new ArrayList<>());
146 microServiceInfo.getNodes().add(node);
147 node.setIp(driverMsbExternalIp);
148 node.setPort(driverPort);
151 return msbApiProvider.getMsbApi().addMicroService(microServiceInfo, true, false).blockingFirst();
152 } catch (Exception e) {
153 throw buildFatalFailure(logger, "Unable to register Nokia VNFM driver", e);
157 private void subscribeToLcn(String vnfmId) {
158 String callbackUrl = getDriverVnfmUrl() + DriverProperties.LCN_URL;
159 logger.info("Subscribing to CBAM LCN {} with callback to {}", driverProperties.getCbamLcnUrl(), callbackUrl);
160 SubscriptionsApi lcnApi = cbamRestApiProvider.getCbamLcnApi(vnfmId);
162 for (Subscription subscription : lcnApi.subscriptionsGet(NOKIA_LCN_API_VERSION).blockingFirst()) {
163 if (subscription.getCallbackUrl().equals(callbackUrl)) {
164 logger.warn("The subscription with {} identifier has the same callback URL", subscription.getId());
168 CreateSubscriptionRequest request = new CreateSubscriptionRequest();
169 request.setFilter(new SubscriptionFilter());
170 request.getFilter().setNotificationTypes(new ArrayList<>());
171 request.getFilter().getNotificationTypes().add(VnfNotificationType.VNFLIFECYCLECHANGENOTIFICATION);
172 request.setCallbackUrl(callbackUrl);
173 request.getFilter().addOperationTypesItem(OperationType.HEAL);
174 request.getFilter().addOperationTypesItem(OperationType.INSTANTIATE);
175 request.getFilter().addOperationTypesItem(OperationType.SCALE);
176 request.getFilter().addOperationTypesItem(OperationType.TERMINATE);
177 SubscriptionAuthentication subscriptionAuthentication = new SubscriptionAuthentication();
178 subscriptionAuthentication.setType(NONE);
179 request.setAuthentication(subscriptionAuthentication);
180 Subscription createdSubscription = lcnApi.subscriptionsPost(request, NOKIA_LCN_API_VERSION).blockingFirst();
181 logger.info("Subscribed to LCN with {} identifier", createdSubscription.getId());
182 } catch (Exception e) {
183 throw buildFatalFailure(logger, "Unable to subscribe to CBAM LCN", e);
188 * @return is the component ready to serve requests
190 public boolean isReady() {