Fix sonar issues
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / core / SelfRegistrationManager.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
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core;
18
19 import com.nokia.cbam.lcn.v32.ApiException;
20 import com.nokia.cbam.lcn.v32.api.SubscriptionsApi;
21 import com.nokia.cbam.lcn.v32.model.*;
22 import org.onap.msb.sdk.discovery.common.RouteException;
23 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
24 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
25 import org.onap.msb.sdk.discovery.entity.Node;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
29 import org.slf4j.Logger;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.beans.factory.annotation.Value;
32 import org.springframework.stereotype.Component;
33
34 import java.util.ArrayList;
35 import java.util.HashSet;
36
37 import static com.nokia.cbam.lcn.v32.model.SubscriptionAuthentication.TypeEnum.NONE;
38 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
39 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCN_API_VERSION;
40 import static org.slf4j.LoggerFactory.getLogger;
41
42 /**
43  * Responsible for registering the driver in the core systems.
44  */
45 @Component
46 public class SelfRegistrationManager {
47     public static final String DRIVER_VERSION = "v1";
48     public static final String SERVICE_NAME = "NokiaSVNFM";
49     // 1 means internal 0 means core :)
50     public static final String INTERNAL_SERVICE = "1";
51     public static final String SWAGGER_API_DEFINITION = "self.swagger.json";
52     private static Logger logger = getLogger(SelfRegistrationManager.class);
53     private final DriverProperties driverProperties;
54     private final MsbApiProvider msbApiProvider;
55     private final CbamRestApiProvider cbamRestApiProvider;
56
57     @Value("${driverMsbExternalIp}")
58     private String driverMsbExternalIp;
59     @Value("${driverVnfmExternalIp}")
60     private String driverVnfmExternalIp;
61     @Value("${server.port}")
62     private String driverPort;
63     private volatile boolean ready = false;
64
65     @Autowired
66     SelfRegistrationManager(DriverProperties driverProperties, MsbApiProvider msbApiProvider, CbamRestApiProvider cbamRestApiProvider) {
67         this.cbamRestApiProvider = cbamRestApiProvider;
68         this.msbApiProvider = msbApiProvider;
69         this.driverProperties = driverProperties;
70     }
71
72     /**
73      * Register the driver in micro-service bus and subscribe to LCNs from CBAM
74      */
75     public void register() {
76         //the order is important (only publish it's existence after the subscription has been created)
77         subscribeToLcn(driverProperties.getVnfmId());
78         try {
79             registerMicroService();
80         } catch (RuntimeException e) {
81             deleteSubscription(driverProperties.getVnfmId());
82             throw e;
83         }
84         ready = true;
85     }
86
87     /**
88      * De-register the VNFM driver from the micro-service bus
89      */
90     public void deRegister() {
91         try {
92             logger.info("Cancelling micro service registration");
93             msbApiProvider.getMsbClient().cancelMicroServiceInfo(SERVICE_NAME, DRIVER_VERSION);
94         } catch (RouteException e) {
95             //ONAP throws 500 internal server error, but deletes the micro service
96             try {
97                 msbApiProvider.getMsbClient().queryMicroServiceInfo(SERVICE_NAME, DRIVER_VERSION);
98                 //the micro service still exists
99                 throw buildFatalFailure(logger, "Unable to deRegister Nokia VNFM driver", e);
100             } catch (RouteException e1) {
101                 logger.info("Unable to query " + SERVICE_NAME + " from MSB (so the service was successfully deleted)", e1);
102                 // the micro service was deleted (even though 500 HTTP code was reported)
103             }
104         }
105         deleteSubscription(driverProperties.getVnfmId());
106     }
107
108     /**
109      * @return the swagger API definition
110      */
111     public byte[] getSwaggerApiDefinition() {
112         return SystemFunctions.systemFunctions().loadFile(SWAGGER_API_DEFINITION);
113     }
114
115     private String getDriverVnfmUrl() {
116         return "http://" + driverVnfmExternalIp + ":" + driverPort + DriverProperties.BASE_URL;
117     }
118
119     private void deleteSubscription(String vnfmId) {
120         logger.info("Deleting CBAM LCN subscription");
121         SubscriptionsApi lcnApi = cbamRestApiProvider.getCbamLcnApi(vnfmId);
122         try {
123             String callbackUrl = getDriverVnfmUrl() + DriverProperties.LCN_URL;
124             for (Subscription subscription : lcnApi.subscriptionsGet(NOKIA_LCN_API_VERSION)) {
125                 if (subscription.getCallbackUrl().equals(callbackUrl)) {
126                     lcnApi.subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION);
127                 }
128             }
129         } catch (ApiException e) {
130             throw buildFatalFailure(logger, "Unable to delete CBAM LCN subscription", e);
131         }
132     }
133
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("REST");
140         microServiceInfo.setVisualRange(INTERNAL_SERVICE);
141         microServiceInfo.setServiceName(SERVICE_NAME);
142         microServiceInfo.setVersion(DRIVER_VERSION);
143         //FIXME set enable_ssl to false after the field has been added to MSB SDK https://jira.onap.org/browse/MSB-151
144         //currently defaults to false, which is good
145         Node node = new Node();
146         microServiceInfo.setNodes(new HashSet<>());
147         microServiceInfo.getNodes().add(node);
148         node.setIp(driverMsbExternalIp);
149         node.setPort(driverPort);
150         node.setTtl("0");
151         try {
152             return msbApiProvider.getMsbClient().registerMicroServiceInfo(microServiceInfo);
153         } catch (RouteException e) {
154             throw buildFatalFailure(logger, "Unable to register Nokia VNFM driver", e);
155         }
156     }
157
158     private void subscribeToLcn(String vnfmId) {
159         String callbackUrl = getDriverVnfmUrl() + DriverProperties.LCN_URL;
160         logger.info("Subscribing to CBAM LCN {} with callback to {}", driverProperties.getCbamLcnUrl(), callbackUrl);
161         SubscriptionsApi lcnApi = cbamRestApiProvider.getCbamLcnApi(vnfmId);
162         try {
163             for (Subscription subscription : lcnApi.subscriptionsGet(NOKIA_LCN_API_VERSION)) {
164                 if (subscription.getCallbackUrl().equals(callbackUrl)) {
165                     return;
166                 }
167             }
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             lcnApi.subscriptionsPost(request, NOKIA_LCN_API_VERSION);
181         } catch (ApiException e) {
182             throw buildFatalFailure(logger, "Unable to subscribe to CBAM LCN", e);
183         }
184     }
185
186     /**
187      * @return is the component ready to serve requests
188      */
189     public boolean isReady() {
190         return ready;
191     }
192 }