diff options
author | 2017-10-17 01:09:17 +0000 | |
---|---|---|
committer | 2017-10-17 01:09:17 +0000 | |
commit | 20f2faf42e9833039a911e53e89f7ca4251352a8 (patch) | |
tree | 6a18d3d58cd86032b359d3ecb67e18a74710ab0c /usecaseui-common/src/main/java/org/onap/portal/system/RegisterService.java | |
parent | 905ab8a66e8fa64d54ead6207e52eb3b749fc0db (diff) | |
parent | b7503ec5b347cf30dd5bfbc27dac076fbb6f5418 (diff) |
Merge "add msb config"
Diffstat (limited to 'usecaseui-common/src/main/java/org/onap/portal/system/RegisterService.java')
-rw-r--r-- | usecaseui-common/src/main/java/org/onap/portal/system/RegisterService.java | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/usecaseui-common/src/main/java/org/onap/portal/system/RegisterService.java b/usecaseui-common/src/main/java/org/onap/portal/system/RegisterService.java new file mode 100644 index 00000000..d5896917 --- /dev/null +++ b/usecaseui-common/src/main/java/org/onap/portal/system/RegisterService.java @@ -0,0 +1,181 @@ +/** + * Copyright 2016-2017 ZTE Corporation. + * + * 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.portal.system; + +import java.io.File; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.*; + +import net.sf.json.JSONObject; + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.util.EntityUtils; +import org.apache.commons.httpclient.HttpStatus; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.onap.msb.sdk.discovery.common.RouteException; +import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; +import org.onap.msb.sdk.discovery.entity.Node; +import org.onap.msb.sdk.httpclient.msb.MSBServiceClient; +import org.onap.portal.bean.MsbRegisterBean; +import org.onap.portal.bean.ServiceNodeBean; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RegisterService { + + private final static Logger logger = LoggerFactory.getLogger(RegisterService.class); + + public static HashMap<String, String> msbHostMap = new HashMap<String, String>(); + public static List<HashMap<String, String>> registerList = new ArrayList<HashMap<String, String>>(); + + public static void registerMsb(String configPath) { + File inputXml = new File(configPath); + SAXReader saxReader = new SAXReader(); + + try { + Document document = saxReader.read(inputXml); + Element rootNode = document.getRootElement(); + getMsbHostInfo(rootNode); + getRegisterList(rootNode); + + String url = msbHostMap.get("hostIp") + Constants.MARK_COLON + msbHostMap.get("hostPort") + msbHostMap.get("msbApiRootDomain"); + for (int i = 0; i < registerList.size(); i++) { + HashMap<String, String> registerInfo = registerList.get(i); + + ServiceNodeBean serviceNode = new ServiceNodeBean(); + serviceNode.setIp(registerInfo.get("ip")); + serviceNode.setPort(registerInfo.get("port")); + serviceNode.setTtl(Integer.valueOf(registerInfo.get("ttl"))); + List<ServiceNodeBean> nodeList = new ArrayList<ServiceNodeBean>(); + nodeList.add(serviceNode); + + MsbRegisterBean registerBean = new MsbRegisterBean(); + registerBean.setNodes(nodeList); + registerBean.setServiceName(registerInfo.get("serviceName")); + registerBean.setUrl(registerInfo.get("url")); + registerBean.setProtocol(registerInfo.get("protocol")); + registerBean.setVisualRange(registerInfo.get("visualRange")); + registerBean.setLb_policy(registerInfo.get("lb_policy")); + + JSONObject registerObj = JSONObject.fromObject(registerBean); + String registerResponse = registerPortalService(registerBean.getServiceName(), url, registerObj, ""); + } + logger.info("open-o portal register task succeeded."); + } catch (Exception e){ + logger.error("open-o portal register task failed."); + logger.error(e.getMessage()); + } + } + + private static void getMsbHostInfo(Element rootNode) { + Element msbHostNode = rootNode.element("msbHost"); + Iterator iter = msbHostNode.elementIterator(); + + while (iter.hasNext()) { + Element element = (Element) iter.next(); + msbHostMap.put(element.getName(), element.getText()); + } + } + + private static void getRegisterList(Element rootNode) { + Element registerListNode = rootNode.element("registerList"); + Iterator iRegisterList = registerListNode.elementIterator(); + + while (iRegisterList.hasNext()) { + HashMap<String, String> registerMap = new HashMap<String, String>(); + Element registerInfo = (Element) iRegisterList.next(); + Iterator iRegisterInfo = registerInfo.elementIterator(); + + while (iRegisterInfo.hasNext()) { + Element element = (Element) iRegisterInfo.next(); + registerMap.put(element.getName(), element.getText()); + } + registerList.add(registerMap); + } + } + + private static String registerPortalService(String serviceName, String url, JSONObject json, String token) { + DefaultHttpClient client = new DefaultHttpClient(); + HttpPost post = new HttpPost(url); + String response = null; + + try { + if (null != json) { + StringEntity s = new StringEntity(json.toString()); + s.setContentEncoding("UTF-8"); + s.setContentType("application/json"); + post.setEntity(s); + } + if (!CommonUtil.isEmpty(token)) { + post.addHeader("X-Auth-Token", token); + } + HttpResponse res = client.execute(post); + if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK + || res.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) { + String result = EntityUtils.toString(res.getEntity()); + if (!CommonUtil.isEmpty(result)) { + response = result; + } else { + response = null; + } + } + logger.info("register task [" + serviceName + "] completed successfully."); + } catch (Exception e) { + logger.error("register task [" + serviceName + "] failed because of errors."); + logger.error(e.getMessage()); + } + + return response; + } + + public static void registerUUI() { + String msbAddress = System.getenv("MSB_ADDR"); + if (msbAddress == null || msbAddress.isEmpty()) { + return; + } + if (msbAddress.contains(":")) { + String[] ipAndPort = msbAddress.split(":"); + MSBServiceClient msbClient = new MSBServiceClient(ipAndPort[0], Integer.parseInt(ipAndPort[1])); + + MicroServiceInfo msinfo = new MicroServiceInfo(); + msinfo.setServiceName("usecase-ui"); + msinfo.setVersion("v1"); + msinfo.setUrl("/usecase-ui"); + msinfo.setProtocol("UI"); + msinfo.setVisualRange("1"); + + try { + Set<Node> nodes = new HashSet<>(); + Node node1 = new Node(); + node1.setIp(InetAddress.getLocalHost().getHostAddress()); + node1.setPort("8080"); + nodes.add(node1); + msinfo.setNodes(nodes); + msbClient.registerMicroServiceInfo(msinfo, false); + } catch (UnknownHostException e) { + e.printStackTrace(); + } catch (RouteException e) { + e.printStackTrace(); + } + } + } +} |