1 /*******************************************************************************
\r
2 * Copyright 2017 ZTE, Inc. and others.
\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
7 * http://www.apache.org/licenses/LICENSE-2.0
\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
13 ******************************************************************************/
\r
14 package org.onap.policy.msb.client;
\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
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
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
42 public MSBServiceFactory() throws MSBServiceException,IOException{
\r
44 this.msbClient = new MSBServiceClient(properties.getProperty(MSB_IP), Integer.parseInt(properties.getProperty(MSB_PORT)));
\r
46 public MSBServiceFactory (MSBServiceClient msbClient) {
\r
47 this.msbClient = msbClient;
\r
50 private void init() throws MSBServiceException,IOException {
\r
51 properties = new Properties();
\r
52 Path file = Paths.get(System.getProperty(msbPropertyFile));
\r
54 throw new MSBServiceException("No msb.policy.properties specified.");
\r
56 if (Files.notExists(file)) {
\r
57 throw new MSBServiceException("No msb.policy.properties specified.");
\r
60 if (Files.isReadable(file) == false) {
\r
61 throw new MSBServiceException ("Repository is NOT readable: " + file.toAbsolutePath());
\r
63 try(InputStream is = new FileInputStream(file.toFile())){
\r
64 properties.load(is);
\r
69 public Node getNode(String serviceName,String version){
\r
70 return this.build(serviceName,version);
\r
73 public Node getNode(String actor){
\r
77 node = this.build("aai-search","v11");
\r
80 node = this.build("so","v2");
\r
83 node = this.build("nfvo-nslcm","v1");
\r
86 logger.info("MSBServiceManager: policy has an unknown actor.");
\r
91 private Node build(String serviceName,String version){
\r
92 Node node = new Node();
\r
93 node.setName(serviceName);
\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
102 } catch (RouteException e) {
\r
103 logger.info("MSBServiceManager:",e);
\r