From 65500fbeb926f723336b515efabc4db07ef3d4a6 Mon Sep 17 00:00:00 2001 From: wangxinyuan10113332 Date: Tue, 26 Sep 2017 11:01:49 +0800 Subject: Use MSB for service discovery Change-Id: I524946a1c352b5e9225b7fb37d35fa03a3f77e82 Issue-ID: POLICY-172 Signed-off-by: wangxinyuan10113332 --- .../policy/msb/client/MSBServiceException.java | 40 ++++++++ .../onap/policy/msb/client/MSBServiceFactory.java | 107 +++++++++++++++++++++ .../onap/policy/msb/client/MSBServiceManager.java | 58 +++++++++++ .../main/java/org/onap/policy/msb/client/Node.java | 56 +++++++++++ 4 files changed, 261 insertions(+) create mode 100644 controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceException.java create mode 100644 controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceFactory.java create mode 100644 controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceManager.java create mode 100644 controlloop/common/msb/src/main/java/org/onap/policy/msb/client/Node.java (limited to 'controlloop/common/msb/src/main/java') diff --git a/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceException.java b/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceException.java new file mode 100644 index 000000000..f5bbdb5f2 --- /dev/null +++ b/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceException.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. and others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + ******************************************************************************/ + +package org.onap.policy.msb.client; + +public class MSBServiceException extends Exception { + + public MSBServiceException() { + super(); + } + + public MSBServiceException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + + public MSBServiceException(String message, Throwable cause) { + super(message, cause); + } + + public MSBServiceException(String message) { + super(message); + } + + public MSBServiceException(Throwable cause) { + super(cause); + } + +} diff --git a/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceFactory.java b/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceFactory.java new file mode 100644 index 000000000..cc3ff71e3 --- /dev/null +++ b/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceFactory.java @@ -0,0 +1,107 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. and others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + ******************************************************************************/ +package org.onap.policy.msb.client; + +import org.onap.msb.sdk.discovery.common.RouteException; +import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo; +import org.onap.msb.sdk.discovery.entity.NodeInfo; +import org.onap.msb.sdk.httpclient.msb.MSBServiceClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Iterator; +import java.util.Properties; + + +public class MSBServiceFactory { + private static final Logger logger = LoggerFactory.getLogger(MSBServiceFactory.class); + private static final String msbPropertyFile = "msb.policy.properties"; + private static final String MSB_IP = "msb.ip"; + private static final String MSB_PORT = "msb.port"; + private MSBServiceClient msbClient; + private Properties properties; + + public MSBServiceFactory() throws MSBServiceException,IOException{ + this.init(); + this.msbClient = new MSBServiceClient(properties.getProperty(MSB_IP), Integer.parseInt(properties.getProperty(MSB_PORT))); + } + public MSBServiceFactory (MSBServiceClient msbClient) { + this.msbClient = msbClient; + } + + private void init() throws MSBServiceException,IOException { + properties = new Properties(); + Path file = Paths.get(System.getProperty(msbPropertyFile)); + if (file == null) { + throw new MSBServiceException("No msb.policy.properties specified."); + } + if (Files.notExists(file)) { + throw new MSBServiceException("No msb.policy.properties specified."); + } + + if (Files.isReadable(file) == false) { + throw new MSBServiceException ("Repository is NOT readable: " + file.toAbsolutePath()); + } + try(InputStream is = new FileInputStream(file.toFile())){ + properties.load(is); + } + } + + + public Node getNode(String serviceName,String version){ + return this.build(serviceName,version); + } + + public Node getNode(String actor){ + Node node = null; + switch (actor) { + case "AAI": + node = this.build("aai-search","v11"); + return node; + case "SO": + node = this.build("so","v2"); + return node; + case "VFC": + node = this.build("nfvo-nslcm","v1"); + return node; + default: + logger.info("MSBServiceManager: policy has an unknown actor."); + } + return node; + } + + private Node build(String serviceName,String version){ + Node node = new Node(); + node.setName(serviceName); + try { + MicroServiceFullInfo serviceInfo = msbClient.queryMicroServiceInfo(serviceName,version); + Iterator iterator = serviceInfo.getNodes().iterator(); + while(iterator.hasNext()) { + NodeInfo nodeInfo = (NodeInfo)iterator.next(); + node.setIp(nodeInfo.getIp()); + node.setPort(nodeInfo.getPort()); + } + } catch (RouteException e) { + logger.info("MSBServiceManager:",e); + } + return node; + } +} diff --git a/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceManager.java b/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceManager.java new file mode 100644 index 000000000..cbff8d88a --- /dev/null +++ b/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceManager.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. and others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + ******************************************************************************/ +package org.onap.policy.msb.client; + +import org.onap.msb.sdk.httpclient.msb.MSBServiceClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.Serializable; + +public class MSBServiceManager implements Serializable { + private static final Logger logger = LoggerFactory.getLogger(MSBServiceManager.class); + private static final long serialVersionUID = -2517971308551895215L; + private MSBServiceFactory factory; + + public MSBServiceManager() throws MSBServiceException,IOException { + this.factory = new MSBServiceFactory(); + } + + public MSBServiceManager(MSBServiceClient msbClient){ + + this.factory = new MSBServiceFactory(msbClient); + } + + /** + * Get the IP and port of the components registered in the MSB + * @param actor AAI or SO or VFC + * @return + */ + public Node getNode(String actor){ + + return factory.getNode(actor); + } + + /** + * Get the IP and port of the components registered in the MSB + * @param serviceName the service name registered in the MSB + * @param version the service version registered in the MSB + * @return + */ + public Node getNode(String serviceName,String version){ + + return factory.getNode(serviceName,version); + } + +} diff --git a/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/Node.java b/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/Node.java new file mode 100644 index 000000000..5c06939bf --- /dev/null +++ b/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/Node.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. and others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + ******************************************************************************/ +package org.onap.policy.msb.client; + +import java.io.Serializable; + +public class Node implements Serializable { + private static final long serialVersionUID = -5028618045561310837L; + private String name; + private String ip; + private String port; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public String getPort() { + return port; + } + + public void setPort(String port) { + this.port = port; + } + + @Override + public String toString() { + return "Node{" + + "name='" + name + '\'' + + ", ip='" + ip + '\'' + + ", port='" + port + '\'' + + '}'; + } +} -- cgit 1.2.3-korg