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.msb.impl;
19 import java.io.IOException;
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.RouteResult;
25 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util.CommonUtil;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.msb.inf.IMsbMgmr;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
34 import com.alibaba.fastjson.JSON;
35 import com.google.gson.Gson;
38 public class MsbMgmrImpl implements IMsbMgmr {
39 private static final Logger logger = LoggerFactory.getLogger(MsbMgmrImpl.class);
42 AdaptorEnv adaptorEnv;
44 private Gson gson = new Gson();
47 public void register() {
49 String vfcAdaptorInfoJsonStr = readVfcAdaptorInfoFromJsonFile();
51 JSON json = com.alibaba.fastjson.JSON.parseObject(vfcAdaptorInfoJsonStr);
52 MicroServiceInfo msinfo = com.alibaba.fastjson.JSON.toJavaObject(json , MicroServiceInfo.class);
54 MSBServiceClient msbClient = new MSBServiceClient(adaptorEnv.getMsbIp(), adaptorEnv.getMsbPort());
55 MicroServiceFullInfo microServiceInfo = msbClient.registerMicroServiceInfo(msinfo);
56 logger.info("Registered service response info is " + microServiceInfo.toString());
58 } catch (RouteException e) {
59 logger.error("RouteException Failed to register nokia vnfm driver! ", e);
60 } catch (IOException e) {
61 logger.error("IOException Failed to register nokia vnfm driver! ", e);
66 private String readVfcAdaptorInfoFromJsonFile() throws IOException {
67 String filePath = "/etc/adapterInfo/vnfmadapterinfo.json";
68 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
74 public void unregister() {
76 String jsonStr = readVfcAdaptorInfoFromJsonFile();
77 MicroServiceInfo msinfo = gson.fromJson(jsonStr, MicroServiceInfo.class);
79 MSBServiceClient msbClient = new MSBServiceClient(adaptorEnv.getMsbIp(), adaptorEnv.getMsbPort());
80 RouteResult routeResult = msbClient.cancelMicroServiceInfo(msinfo.getServiceName(), msinfo.getVersion());
81 logger.info("unregistered service response info is " + routeResult.toString());
83 } catch (IOException e) {
84 logger.error("Failed to read vfcadaptor info! ", e);
85 } catch (RouteException e) {
86 logger.error("Failed to register nokia vnfm driver! ", e);
90 public String getServiceUrlInMsbBySeriveNameAndPort(String serviceName, String version) throws RouteException
92 String serviceUrl = null;
93 MSBServiceClient msbClient = new MSBServiceClient(adaptorEnv.getMsbIp(), adaptorEnv.getMsbPort());
94 MicroServiceFullInfo microServiceInfo = msbClient.queryMicroServiceInfo(serviceName, version);
95 if(null == microServiceInfo)
97 logger.error("There is no service in MSB for serviceName = {} and version = {}", serviceName, version);
101 serviceUrl = microServiceInfo.getUrl();
102 logger.info("Service Url in MSB for serviceName = {} and version = {} is {}", serviceName, version, serviceUrl);
108 public void setAdaptorEnv(AdaptorEnv env) {
109 this.adaptorEnv = env;
112 public static final void main(String[] args) {
113 MsbMgmrImpl impl = new MsbMgmrImpl();