aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/common/msb/src
diff options
context:
space:
mode:
authorwangxinyuan10113332 <wang.xinyuan1@zte.com.cn>2017-09-26 11:01:49 +0800
committerwangxinyuan10113332 <wang.xinyuan1@zte.com.cn>2017-09-26 11:01:49 +0800
commit65500fbeb926f723336b515efabc4db07ef3d4a6 (patch)
tree6b96cc4a48b8ab8cd162488fe4194f28e4f61058 /controlloop/common/msb/src
parent742270864ddc3f89320b815fa9cd85caf83fca46 (diff)
Use MSB for service discovery
Change-Id: I524946a1c352b5e9225b7fb37d35fa03a3f77e82 Issue-ID: POLICY-172 Signed-off-by: wangxinyuan10113332 <wang.xinyuan1@zte.com.cn>
Diffstat (limited to 'controlloop/common/msb/src')
-rw-r--r--controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceException.java40
-rw-r--r--controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceFactory.java107
-rw-r--r--controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceManager.java58
-rw-r--r--controlloop/common/msb/src/main/java/org/onap/policy/msb/client/Node.java56
-rw-r--r--controlloop/common/msb/src/main/resources/msb.policy.properties22
-rw-r--r--controlloop/common/msb/src/test/java/org/onap/policy/msb/client/MSBServiceManagerTest.java116
6 files changed, 399 insertions, 0 deletions
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 + '\'' +
+ '}';
+ }
+}
diff --git a/controlloop/common/msb/src/main/resources/msb.policy.properties b/controlloop/common/msb/src/main/resources/msb.policy.properties
new file mode 100644
index 000000000..bb372ec85
--- /dev/null
+++ b/controlloop/common/msb/src/main/resources/msb.policy.properties
@@ -0,0 +1,22 @@
+###
+# ============LICENSE_START=======================================================
+# ONAP-PDP
+# ================================================================================
+# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+# ================================================================================
+# 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.
+# ============LICENSE_END=========================================================
+###
+
+msb.ip=127.0.0.1
+msb.port=10081 \ No newline at end of file
diff --git a/controlloop/common/msb/src/test/java/org/onap/policy/msb/client/MSBServiceManagerTest.java b/controlloop/common/msb/src/test/java/org/onap/policy/msb/client/MSBServiceManagerTest.java
new file mode 100644
index 000000000..9ab54f7ea
--- /dev/null
+++ b/controlloop/common/msb/src/test/java/org/onap/policy/msb/client/MSBServiceManagerTest.java
@@ -0,0 +1,116 @@
+/*******************************************************************************
+ * 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.junit.*;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+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.onap.policy.msb.client.MSBServiceManager;
+import org.onap.policy.msb.client.Node;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.when;
+
+public class MSBServiceManagerTest {
+ @Mock
+ private MSBServiceClient msbClient;
+
+ private MSBServiceManager msbManager;
+
+ public MSBServiceManagerTest(){}
+
+ @BeforeClass
+ public static void setUpClass(){}
+
+ @AfterClass
+ public static void tearDownClass(){}
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ msbManager = new MSBServiceManager(msbClient);
+ }
+
+ @After
+ public void tearDown() {
+ }
+
+ @Test
+ public void testByActor () throws RouteException,UnknownHostException {
+ MicroServiceFullInfo serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8843");
+ when(msbClient.queryMicroServiceInfo("aai-search","v11")).thenReturn(serviceInfo);
+ Node node = msbManager.getNode("AAI");
+ assertNotNull(node);
+ serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8840");
+ when(msbClient.queryMicroServiceInfo("so","v2")).thenReturn(serviceInfo);
+ node = msbManager.getNode("SO");
+ assertNotNull(node);
+
+ serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8082");
+ when(msbClient.queryMicroServiceInfo("nfvo-nslcm","v1")).thenReturn(serviceInfo);
+ node = msbManager.getNode("VFC");
+ assertNotNull(node);
+
+ }
+
+ @Test
+ public void testByActor_when_actorNotExist_returnNull () throws RouteException,UnknownHostException {
+ MicroServiceFullInfo serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8843");
+ when(msbClient.queryMicroServiceInfo("aai-search","v11")).thenReturn(serviceInfo);
+ Node node = msbManager.getNode("DDD");
+ assertNull(node);
+ }
+
+ @Test
+ public void testByServiceNameAndVersion () throws RouteException,UnknownHostException {
+ MicroServiceFullInfo serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8843");
+ when(msbClient.queryMicroServiceInfo("aai-search","v11")).thenReturn(serviceInfo);
+ Node node = msbManager.getNode("aai-search","v11");
+ assertNotNull(node);
+ }
+
+ @Test
+ public void testByServiceNameAndVersion_when_serice_notRegistedToMSB () throws RouteException,UnknownHostException {
+ MicroServiceFullInfo serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8843");
+ when(msbClient.queryMicroServiceInfo("aai-search","v11")).thenThrow(new RouteException());
+ Node node = msbManager.getNode("aai-search","v11");
+ assertNotNull(node);
+ assertTrue(node.getName() == "aai-search");
+ assertTrue(node.getIp() == null);
+ assertTrue(node.getPort() == null);
+ }
+
+ public static MicroServiceFullInfo build(String ip,String port){
+ MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo();
+ Set<NodeInfo> nodes = new HashSet<NodeInfo>();
+ NodeInfo node= new NodeInfo();
+ node.setPort(port);
+ node.setIp(ip);
+ nodes.add(node);
+ serviceInfo.setNodes(nodes);
+ return serviceInfo;
+ }
+
+}