Use MSB for service discovery 01/15301/1
authorwangxinyuan10113332 <wang.xinyuan1@zte.com.cn>
Tue, 26 Sep 2017 03:01:49 +0000 (11:01 +0800)
committerwangxinyuan10113332 <wang.xinyuan1@zte.com.cn>
Tue, 26 Sep 2017 03:01:49 +0000 (11:01 +0800)
Change-Id: I524946a1c352b5e9225b7fb37d35fa03a3f77e82
Issue-ID: POLICY-172
Signed-off-by: wangxinyuan10113332 <wang.xinyuan1@zte.com.cn>
controlloop/common/msb/pom.xml [new file with mode: 0644]
controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceException.java [new file with mode: 0644]
controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceFactory.java [new file with mode: 0644]
controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceManager.java [new file with mode: 0644]
controlloop/common/msb/src/main/java/org/onap/policy/msb/client/Node.java [new file with mode: 0644]
controlloop/common/msb/src/main/resources/msb.policy.properties [new file with mode: 0644]
controlloop/common/msb/src/test/java/org/onap/policy/msb/client/MSBServiceManagerTest.java [new file with mode: 0644]
controlloop/common/pom.xml

diff --git a/controlloop/common/msb/pom.xml b/controlloop/common/msb/pom.xml
new file mode 100644 (file)
index 0000000..9b66a11
--- /dev/null
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>model-impl</artifactId>
+        <groupId>org.onap.policy.drools-applications</groupId>
+        <version>1.1.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>msb</artifactId>
+    <dependencies>
+        <dependency>
+            <groupId>org.onap.msb.java-sdk</groupId>
+            <artifactId>msb-java-sdk</artifactId>
+            <version>1.0.0-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.12</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.powermock</groupId>
+            <artifactId>powermock-module-junit4</artifactId>
+            <version>1.6.5</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.powermock</groupId>
+            <artifactId>powermock-api-mockito</artifactId>
+            <version>1.6.5</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-classic</artifactId>
+            <version>1.2.3</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-core</artifactId>
+            <version>1.2.3</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+</project>
\ No newline at end of file
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 (file)
index 0000000..f5bbdb5
--- /dev/null
@@ -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 (file)
index 0000000..cc3ff71
--- /dev/null
@@ -0,0 +1,107 @@
+/*******************************************************************************\r
+ * Copyright 2017 ZTE, Inc. and others.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ ******************************************************************************/\r
+package org.onap.policy.msb.client;\r
+\r
+import org.onap.msb.sdk.discovery.common.RouteException;\r
+import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;\r
+import org.onap.msb.sdk.discovery.entity.NodeInfo;\r
+import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+import java.io.FileInputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.nio.file.Files;\r
+import java.nio.file.Path;\r
+import java.nio.file.Paths;\r
+import java.util.Iterator;\r
+import java.util.Properties;\r
+\r
+\r
+public class MSBServiceFactory {\r
+    private static final Logger logger = LoggerFactory.getLogger(MSBServiceFactory.class);\r
+    private static final String msbPropertyFile = "msb.policy.properties";\r
+    private static final String MSB_IP = "msb.ip";\r
+    private static final String MSB_PORT = "msb.port";\r
+    private MSBServiceClient msbClient;\r
+    private Properties properties;\r
+\r
+    public MSBServiceFactory() throws MSBServiceException,IOException{\r
+        this.init();\r
+        this.msbClient = new MSBServiceClient(properties.getProperty(MSB_IP), Integer.parseInt(properties.getProperty(MSB_PORT)));\r
+    }\r
+    public MSBServiceFactory (MSBServiceClient msbClient) {\r
+        this.msbClient = msbClient;\r
+    }\r
+\r
+    private void init() throws MSBServiceException,IOException  {\r
+        properties = new Properties();\r
+        Path file = Paths.get(System.getProperty(msbPropertyFile));\r
+        if (file == null) {\r
+            throw new MSBServiceException("No msb.policy.properties specified.");\r
+        }\r
+        if (Files.notExists(file)) {\r
+            throw new MSBServiceException("No msb.policy.properties specified.");\r
+        }\r
+\r
+        if (Files.isReadable(file) == false) {\r
+            throw new MSBServiceException ("Repository is NOT readable: " + file.toAbsolutePath());\r
+        }\r
+        try(InputStream is = new FileInputStream(file.toFile())){\r
+            properties.load(is);\r
+        }\r
+    }\r
+\r
+\r
+    public Node getNode(String serviceName,String version){\r
+        return this.build(serviceName,version);\r
+    }\r
+\r
+    public Node getNode(String actor){\r
+        Node node = null;\r
+        switch (actor) {\r
+            case "AAI":\r
+                node = this.build("aai-search","v11");\r
+                return node;\r
+            case "SO":\r
+                node = this.build("so","v2");\r
+                return node;\r
+            case "VFC":\r
+                node = this.build("nfvo-nslcm","v1");\r
+                return node;\r
+            default:\r
+                logger.info("MSBServiceManager: policy has an unknown actor.");\r
+        }\r
+        return node;\r
+    }\r
+\r
+    private Node build(String serviceName,String version){\r
+        Node node = new Node();\r
+        node.setName(serviceName);\r
+        try {\r
+            MicroServiceFullInfo serviceInfo = msbClient.queryMicroServiceInfo(serviceName,version);\r
+            Iterator iterator = serviceInfo.getNodes().iterator();\r
+            while(iterator.hasNext()) {\r
+                NodeInfo nodeInfo = (NodeInfo)iterator.next();\r
+                node.setIp(nodeInfo.getIp());\r
+                node.setPort(nodeInfo.getPort());\r
+            }\r
+        } catch (RouteException e) {\r
+            logger.info("MSBServiceManager:",e);\r
+        }\r
+        return node;\r
+    }\r
+}\r
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 (file)
index 0000000..cbff8d8
--- /dev/null
@@ -0,0 +1,58 @@
+/*******************************************************************************\r
+ * Copyright 2017 ZTE, Inc. and others.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ ******************************************************************************/\r
+package org.onap.policy.msb.client;\r
+\r
+import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+import java.io.IOException;\r
+import java.io.Serializable;\r
+\r
+public class MSBServiceManager implements Serializable {\r
+    private static final Logger logger = LoggerFactory.getLogger(MSBServiceManager.class);\r
+    private static final long serialVersionUID = -2517971308551895215L;\r
+    private MSBServiceFactory factory;\r
+\r
+    public MSBServiceManager() throws MSBServiceException,IOException {\r
+        this.factory = new MSBServiceFactory();\r
+    }\r
+\r
+    public MSBServiceManager(MSBServiceClient msbClient){\r
+\r
+        this.factory = new MSBServiceFactory(msbClient);\r
+    }\r
+\r
+    /**\r
+     * Get the IP and port of the components registered in the MSB\r
+     * @param actor  AAI or SO or VFC\r
+     * @return\r
+     */\r
+    public Node getNode(String actor){\r
+\r
+        return factory.getNode(actor);\r
+    }\r
+\r
+    /**\r
+     * Get the IP and port of the components registered in the MSB\r
+     * @param serviceName  the service name registered in the MSB\r
+     * @param version the service version registered in the MSB\r
+     * @return\r
+     */\r
+    public Node getNode(String serviceName,String version){\r
+\r
+        return factory.getNode(serviceName,version);\r
+    }\r
+\r
+}\r
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 (file)
index 0000000..5c06939
--- /dev/null
@@ -0,0 +1,56 @@
+/*******************************************************************************\r
+ * Copyright 2017 ZTE, Inc. and others.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ ******************************************************************************/\r
+package org.onap.policy.msb.client;\r
+\r
+import java.io.Serializable;\r
+\r
+public class Node implements Serializable {\r
+    private static final long serialVersionUID = -5028618045561310837L;\r
+    private String name;\r
+    private String ip;\r
+    private String port;\r
+\r
+    public String getName() {\r
+        return name;\r
+    }\r
+\r
+    public void setName(String name) {\r
+        this.name = name;\r
+    }\r
+\r
+    public String getIp() {\r
+        return ip;\r
+    }\r
+\r
+    public void setIp(String ip) {\r
+        this.ip = ip;\r
+    }\r
+\r
+    public String getPort() {\r
+        return port;\r
+    }\r
+\r
+    public void setPort(String port) {\r
+        this.port = port;\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return "Node{" +\r
+                "name='" + name + '\'' +\r
+                ", ip='" + ip + '\'' +\r
+                ", port='" + port + '\'' +\r
+                '}';\r
+    }\r
+}\r
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 (file)
index 0000000..bb372ec
--- /dev/null
@@ -0,0 +1,22 @@
+###\r
+# ============LICENSE_START=======================================================\r
+# ONAP-PDP\r
+# ================================================================================\r
+# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
+# ================================================================================\r
+# Licensed under the Apache License, Version 2.0 (the "License");\r
+# you may not use this file except in compliance with the License.\r
+# You may obtain a copy of the License at\r
+#\r
+#      http://www.apache.org/licenses/LICENSE-2.0\r
+#\r
+# Unless required by applicable law or agreed to in writing, software\r
+# distributed under the License is distributed on an "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+# See the License for the specific language governing permissions and\r
+# limitations under the License.\r
+# ============LICENSE_END=========================================================\r
+###\r
+\r
+msb.ip=127.0.0.1\r
+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 (file)
index 0000000..9ab54f7
--- /dev/null
@@ -0,0 +1,116 @@
+/*******************************************************************************\r
+ * Copyright 2017 ZTE, Inc. and others.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ ******************************************************************************/\r
+package org.onap.policy.msb.client;\r
+\r
+import org.junit.*;\r
+import org.mockito.Mock;\r
+import org.mockito.MockitoAnnotations;\r
+import org.onap.msb.sdk.discovery.common.RouteException;\r
+import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;\r
+import org.onap.msb.sdk.discovery.entity.NodeInfo;\r
+import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;\r
+import org.onap.policy.msb.client.MSBServiceManager;\r
+import org.onap.policy.msb.client.Node;\r
+\r
+import java.net.InetAddress;\r
+import java.net.UnknownHostException;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
+import static org.junit.Assert.assertNotNull;\r
+import static org.junit.Assert.assertNull;\r
+import static org.junit.Assert.assertTrue;\r
+import static org.mockito.Mockito.when;\r
+\r
+public class MSBServiceManagerTest {\r
+    @Mock\r
+    private MSBServiceClient msbClient;\r
+\r
+    private MSBServiceManager msbManager;\r
+\r
+    public MSBServiceManagerTest(){}\r
+\r
+    @BeforeClass\r
+    public static void setUpClass(){}\r
+\r
+    @AfterClass\r
+    public static void tearDownClass(){}\r
+\r
+    @Before\r
+    public void setUp() throws Exception {\r
+        MockitoAnnotations.initMocks(this);\r
+        msbManager = new MSBServiceManager(msbClient);\r
+    }\r
+\r
+    @After\r
+    public void tearDown() {\r
+    }\r
+\r
+    @Test\r
+    public void testByActor () throws RouteException,UnknownHostException {\r
+        MicroServiceFullInfo serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8843");\r
+        when(msbClient.queryMicroServiceInfo("aai-search","v11")).thenReturn(serviceInfo);\r
+        Node node = msbManager.getNode("AAI");\r
+        assertNotNull(node);\r
+        serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8840");\r
+        when(msbClient.queryMicroServiceInfo("so","v2")).thenReturn(serviceInfo);\r
+        node = msbManager.getNode("SO");\r
+        assertNotNull(node);\r
+\r
+        serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8082");\r
+        when(msbClient.queryMicroServiceInfo("nfvo-nslcm","v1")).thenReturn(serviceInfo);\r
+        node = msbManager.getNode("VFC");\r
+        assertNotNull(node);\r
+\r
+    }\r
+\r
+    @Test\r
+    public void testByActor_when_actorNotExist_returnNull () throws RouteException,UnknownHostException {\r
+        MicroServiceFullInfo serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8843");\r
+        when(msbClient.queryMicroServiceInfo("aai-search","v11")).thenReturn(serviceInfo);\r
+        Node node = msbManager.getNode("DDD");\r
+        assertNull(node);\r
+    }\r
+\r
+    @Test\r
+    public void testByServiceNameAndVersion () throws RouteException,UnknownHostException {\r
+        MicroServiceFullInfo serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8843");\r
+        when(msbClient.queryMicroServiceInfo("aai-search","v11")).thenReturn(serviceInfo);\r
+        Node node = msbManager.getNode("aai-search","v11");\r
+        assertNotNull(node);\r
+    }\r
+\r
+    @Test\r
+    public void testByServiceNameAndVersion_when_serice_notRegistedToMSB () throws RouteException,UnknownHostException {\r
+        MicroServiceFullInfo serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8843");\r
+        when(msbClient.queryMicroServiceInfo("aai-search","v11")).thenThrow(new RouteException());\r
+        Node node = msbManager.getNode("aai-search","v11");\r
+        assertNotNull(node);\r
+        assertTrue(node.getName() == "aai-search");\r
+        assertTrue(node.getIp() == null);\r
+        assertTrue(node.getPort() == null);\r
+    }\r
+\r
+    public static MicroServiceFullInfo build(String ip,String port){\r
+        MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo();\r
+        Set<NodeInfo> nodes = new HashSet<NodeInfo>();\r
+        NodeInfo node= new NodeInfo();\r
+        node.setPort(port);\r
+        node.setIp(ip);\r
+        nodes.add(node);\r
+        serviceInfo.setNodes(nodes);\r
+        return serviceInfo;\r
+    }\r
+\r
+}\r
index 39f1b70..8725d79 100644 (file)
@@ -40,6 +40,7 @@
        <module>policy-yaml</module>
        <module>simulators</module>
        <module>feature-controlloop-utils</module>
+               <module>msb</module>
     </modules>