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 org.onap.msb.sdk.discovery.common.RouteException;
22 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
23 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
24 import org.onap.msb.sdk.discovery.entity.Node;
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.beans.factory.annotation.Value;
31 import org.springframework.stereotype.Component;
33 import java.util.ArrayList;
34 import java.util.HashSet;
36 import static com.nokia.cbam.lcn.v32.model.SubscriptionAuthentication.TypeEnum.NONE;
37 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
38 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCN_API_VERSION;
39 import static org.slf4j.LoggerFactory.getLogger;
42 * Responsible for registering the driver in the core systems.
45 public class SelfRegistrationManager {
46 public static final String DRIVER_VERSION = "v1";
47 public static final String SERVICE_NAME = "NokiaSVNFM";
48 // 1 means internal 0 means core :)
49 public static final String INTERNAL_SERVICE = "1";
50 public static final String SWAGGER_API_DEFINITION = "self.swagger.json";
51 private static Logger logger = getLogger(SelfRegistrationManager.class);
52 private final DriverProperties driverProperties;
53 private final MsbApiProvider msbApiProvider;
54 private final CbamRestApiProvider cbamRestApiProvider;
56 @Value("${driverMsbExternalIp}")
57 private String driverMsbExternalIp;
58 @Value("${driverVnfmExternalIp}")
59 private String driverVnfmExternalIp;
60 @Value("${server.port}")
61 private String driverPort;
62 private volatile boolean ready = false;
65 SelfRegistrationManager(DriverProperties driverProperties, MsbApiProvider msbApiProvider, CbamRestApiProvider cbamRestApiProvider) {
66 this.cbamRestApiProvider = cbamRestApiProvider;
67 this.msbApiProvider = msbApiProvider;
68 this.driverProperties = driverProperties;
72 * Register the driver in micro-service bus and subscribe to LCNs from CBAM
74 public void register() {
75 //the order is important (only publish it's existence after the subscription has been created)
76 subscribeToLcn(driverProperties.getVnfmId());
78 registerMicroService();
79 } catch (RuntimeException e) {
80 deleteSubscription(driverProperties.getVnfmId());
87 * De-register the VNFM driver from the micro-service bus
89 public void deRegister() {
91 logger.info("Cancelling micro service registration");
92 msbApiProvider.getMsbClient().cancelMicroServiceInfo(SERVICE_NAME, DRIVER_VERSION);
93 } catch (RouteException e) {
94 //ONAP throws 500 internal server error, but deletes the micro service
96 msbApiProvider.getMsbClient().queryMicroServiceInfo(SERVICE_NAME, DRIVER_VERSION);
97 //the micro service still exists
98 throw buildFatalFailure(logger, "Unable to deRegister Nokia VNFM driver", e);
99 } catch (RouteException e1) {
100 logger.info("Unable to query " + SERVICE_NAME + " from MSB (so the service was successfully deleted)", e1);
101 // the micro service was deleted (even though 500 HTTP code was reported)
104 deleteSubscription(driverProperties.getVnfmId());
108 * @return the swagger API definition
110 public byte[] getSwaggerApiDefinition() {
111 return SystemFunctions.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 lcnApi.subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION);
128 } catch (Exception e) {
129 throw buildFatalFailure(logger, "Unable to delete CBAM LCN subscription", e);
133 private MicroServiceFullInfo registerMicroService() {
134 logger.info("Registering micro service");
135 MicroServiceInfo microServiceInfo = new MicroServiceInfo();
136 microServiceInfo.setUrl(DriverProperties.BASE_URL);
137 //the PATH should not be set
138 microServiceInfo.setProtocol("REST");
139 microServiceInfo.setVisualRange(INTERNAL_SERVICE);
140 microServiceInfo.setServiceName(SERVICE_NAME);
141 microServiceInfo.setVersion(DRIVER_VERSION);
142 microServiceInfo.setEnable_ssl(true);
143 Node node = new Node();
144 microServiceInfo.setNodes(new HashSet<>());
145 microServiceInfo.getNodes().add(node);
146 node.setIp(driverMsbExternalIp);
147 node.setPort(driverPort);
150 return msbApiProvider.getMsbClient().registerMicroServiceInfo(microServiceInfo);
151 } catch (RouteException e) {
152 throw buildFatalFailure(logger, "Unable to register Nokia VNFM driver", e);
156 private void subscribeToLcn(String vnfmId) {
157 String callbackUrl = getDriverVnfmUrl() + DriverProperties.LCN_URL;
158 logger.info("Subscribing to CBAM LCN {} with callback to {}", driverProperties.getCbamLcnUrl(), callbackUrl);
159 SubscriptionsApi lcnApi = cbamRestApiProvider.getCbamLcnApi(vnfmId);
161 for (Subscription subscription : lcnApi.subscriptionsGet(NOKIA_LCN_API_VERSION).blockingFirst()) {
162 if (subscription.getCallbackUrl().equals(callbackUrl)) {
166 CreateSubscriptionRequest request = new CreateSubscriptionRequest();
167 request.setFilter(new SubscriptionFilter());
168 request.getFilter().setNotificationTypes(new ArrayList<>());
169 request.getFilter().getNotificationTypes().add(VnfNotificationType.VNFLIFECYCLECHANGENOTIFICATION);
170 request.setCallbackUrl(callbackUrl);
171 request.getFilter().addOperationTypesItem(OperationType.HEAL);
172 request.getFilter().addOperationTypesItem(OperationType.INSTANTIATE);
173 request.getFilter().addOperationTypesItem(OperationType.SCALE);
174 request.getFilter().addOperationTypesItem(OperationType.TERMINATE);
175 SubscriptionAuthentication subscriptionAuthentication = new SubscriptionAuthentication();
176 subscriptionAuthentication.setType(NONE);
177 request.setAuthentication(subscriptionAuthentication);
178 lcnApi.subscriptionsPost(request, NOKIA_LCN_API_VERSION);
179 } catch (Exception e) {
180 throw buildFatalFailure(logger, "Unable to subscribe to CBAM LCN", e);
185 * @return is the component ready to serve requests
187 public boolean isReady() {