cc3ff71e35dd91ac6ce02938f2a28fc2a60d320e
[policy/drools-applications.git] / controlloop / common / msb / src / main / java / org / onap / policy / msb / client / MSBServiceFactory.java
1 /*******************************************************************************\r
2  * Copyright 2017 ZTE, Inc. and others.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
5  * in compliance with the License. You may obtain a copy of the License at\r
6  *\r
7  * http://www.apache.org/licenses/LICENSE-2.0\r
8  *\r
9  * Unless required by applicable law or agreed to in writing, software distributed under the License\r
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
11  * or implied. See the License for the specific language governing permissions and limitations under\r
12  * the License.\r
13  ******************************************************************************/\r
14 package org.onap.policy.msb.client;\r
15 \r
16 import org.onap.msb.sdk.discovery.common.RouteException;\r
17 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;\r
18 import org.onap.msb.sdk.discovery.entity.NodeInfo;\r
19 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;\r
20 import org.slf4j.Logger;\r
21 import org.slf4j.LoggerFactory;\r
22 \r
23 import java.io.FileInputStream;\r
24 import java.io.FileNotFoundException;\r
25 import java.io.IOException;\r
26 import java.io.InputStream;\r
27 import java.nio.file.Files;\r
28 import java.nio.file.Path;\r
29 import java.nio.file.Paths;\r
30 import java.util.Iterator;\r
31 import java.util.Properties;\r
32 \r
33 \r
34 public class MSBServiceFactory {\r
35     private static final Logger logger = LoggerFactory.getLogger(MSBServiceFactory.class);\r
36     private static final String msbPropertyFile = "msb.policy.properties";\r
37     private static final String MSB_IP = "msb.ip";\r
38     private static final String MSB_PORT = "msb.port";\r
39     private MSBServiceClient msbClient;\r
40     private Properties properties;\r
41 \r
42     public MSBServiceFactory() throws MSBServiceException,IOException{\r
43         this.init();\r
44         this.msbClient = new MSBServiceClient(properties.getProperty(MSB_IP), Integer.parseInt(properties.getProperty(MSB_PORT)));\r
45     }\r
46     public MSBServiceFactory (MSBServiceClient msbClient) {\r
47         this.msbClient = msbClient;\r
48     }\r
49 \r
50     private void init() throws MSBServiceException,IOException  {\r
51         properties = new Properties();\r
52         Path file = Paths.get(System.getProperty(msbPropertyFile));\r
53         if (file == null) {\r
54             throw new MSBServiceException("No msb.policy.properties specified.");\r
55         }\r
56         if (Files.notExists(file)) {\r
57             throw new MSBServiceException("No msb.policy.properties specified.");\r
58         }\r
59 \r
60         if (Files.isReadable(file) == false) {\r
61             throw new MSBServiceException ("Repository is NOT readable: " + file.toAbsolutePath());\r
62         }\r
63         try(InputStream is = new FileInputStream(file.toFile())){\r
64             properties.load(is);\r
65         }\r
66     }\r
67 \r
68 \r
69     public Node getNode(String serviceName,String version){\r
70         return this.build(serviceName,version);\r
71     }\r
72 \r
73     public Node getNode(String actor){\r
74         Node node = null;\r
75         switch (actor) {\r
76             case "AAI":\r
77                 node = this.build("aai-search","v11");\r
78                 return node;\r
79             case "SO":\r
80                 node = this.build("so","v2");\r
81                 return node;\r
82             case "VFC":\r
83                 node = this.build("nfvo-nslcm","v1");\r
84                 return node;\r
85             default:\r
86                 logger.info("MSBServiceManager: policy has an unknown actor.");\r
87         }\r
88         return node;\r
89     }\r
90 \r
91     private Node build(String serviceName,String version){\r
92         Node node = new Node();\r
93         node.setName(serviceName);\r
94         try {\r
95             MicroServiceFullInfo serviceInfo = msbClient.queryMicroServiceInfo(serviceName,version);\r
96             Iterator iterator = serviceInfo.getNodes().iterator();\r
97             while(iterator.hasNext()) {\r
98                 NodeInfo nodeInfo = (NodeInfo)iterator.next();\r
99                 node.setIp(nodeInfo.getIp());\r
100                 node.setPort(nodeInfo.getPort());\r
101             }\r
102         } catch (RouteException e) {\r
103             logger.info("MSBServiceManager:",e);\r
104         }\r
105         return node;\r
106     }\r
107 }\r