aboutsummaryrefslogtreecommitdiffstats
path: root/openo-portal/portal-package/src/main/java
diff options
context:
space:
mode:
author“shentao” <shentao@chinamobile.com>2016-10-27 15:02:54 +0800
committer“shentao” <shentao@chinamobile.com>2016-10-27 15:02:54 +0800
commitc60278ca93270bb9f49c65339dbe34c039551fe0 (patch)
tree6f994a36e04ba07cc5db92ba90b8be06d8074d85 /openo-portal/portal-package/src/main/java
parent320b831367c168e0067ba58b765d709020fee64e (diff)
issue-id GSO-64
Change-Id: I0c6433c630c3afac827140df7bcafa643f94b90d Signed-off-by: “shentao” <shentao@chinamobile.com>
Diffstat (limited to 'openo-portal/portal-package/src/main/java')
-rw-r--r--openo-portal/portal-package/src/main/java/org/openo/portal/bean/MsbRegisterBean.java80
-rw-r--r--openo-portal/portal-package/src/main/java/org/openo/portal/bean/ServiceNodeBean.java48
-rw-r--r--openo-portal/portal-package/src/main/java/org/openo/portal/system/CommonUtil.java27
-rw-r--r--openo-portal/portal-package/src/main/java/org/openo/portal/system/Constants.java20
-rw-r--r--openo-portal/portal-package/src/main/java/org/openo/portal/system/RegisterService.java142
-rw-r--r--openo-portal/portal-package/src/main/java/org/openo/portal/system/SystemListener.java36
6 files changed, 353 insertions, 0 deletions
diff --git a/openo-portal/portal-package/src/main/java/org/openo/portal/bean/MsbRegisterBean.java b/openo-portal/portal-package/src/main/java/org/openo/portal/bean/MsbRegisterBean.java
new file mode 100644
index 00000000..c5b53d03
--- /dev/null
+++ b/openo-portal/portal-package/src/main/java/org/openo/portal/bean/MsbRegisterBean.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2016, CMCC Technologies Co., Ltd.
+ *
+ * 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.openo.portal.bean;
+
+import java.util.List;
+
+public class MsbRegisterBean {
+ private String serviceName = "";
+
+ private String lb_policy = "";
+
+ private String url = "";
+
+ private String protocol = "";
+
+ private String visualRange = "";
+
+ private List<ServiceNodeBean> nodes;
+
+ public String getServiceName() {
+ return serviceName;
+ }
+
+ public void setServiceName(String serviceName) {
+ this.serviceName = serviceName;
+ }
+
+ public String getLb_policy() {
+ return lb_policy;
+ }
+
+ public void setLb_policy(String lb_policy) {
+ this.lb_policy = lb_policy;
+ }
+
+ public String getUrl() {
+ return url;
+ }
+
+ public void setUrl(String url) {
+ this.url = url;
+ }
+
+ public String getProtocol() {
+ return protocol;
+ }
+
+ public void setProtocol(String protocol) {
+ this.protocol = protocol;
+ }
+
+ public String getVisualRange() {
+ return visualRange;
+ }
+
+ public void setVisualRange(String visualRange) {
+ this.visualRange = visualRange;
+ }
+
+ public List<ServiceNodeBean> getNodes() {
+ return nodes;
+ }
+
+ public void setNodes(List<ServiceNodeBean> nodes) {
+ this.nodes = nodes;
+ }
+}
diff --git a/openo-portal/portal-package/src/main/java/org/openo/portal/bean/ServiceNodeBean.java b/openo-portal/portal-package/src/main/java/org/openo/portal/bean/ServiceNodeBean.java
new file mode 100644
index 00000000..0c20ac63
--- /dev/null
+++ b/openo-portal/portal-package/src/main/java/org/openo/portal/bean/ServiceNodeBean.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2016, CMCC Technologies Co., Ltd.
+ *
+ * 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.openo.portal.bean;
+
+public class ServiceNodeBean {
+ private String ip = "";
+
+ private String port = "";
+
+ private int ttl = 0;
+
+ 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;
+ }
+
+ public int getTtl() {
+ return ttl;
+ }
+
+ public void setTtl(int ttl) {
+ this.ttl = ttl;
+ }
+}
diff --git a/openo-portal/portal-package/src/main/java/org/openo/portal/system/CommonUtil.java b/openo-portal/portal-package/src/main/java/org/openo/portal/system/CommonUtil.java
new file mode 100644
index 00000000..b28bb1ef
--- /dev/null
+++ b/openo-portal/portal-package/src/main/java/org/openo/portal/system/CommonUtil.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2016, CMCC Technologies Co., Ltd.
+ *
+ * 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.openo.portal.system;
+
+public class CommonUtil {
+
+ public static boolean isEmpty(String str) {
+ if (null == str || "".equals(str.trim())) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+}
diff --git a/openo-portal/portal-package/src/main/java/org/openo/portal/system/Constants.java b/openo-portal/portal-package/src/main/java/org/openo/portal/system/Constants.java
new file mode 100644
index 00000000..b27c73e6
--- /dev/null
+++ b/openo-portal/portal-package/src/main/java/org/openo/portal/system/Constants.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016, CMCC Technologies Co., Ltd.
+ *
+ * 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.openo.portal.system;
+
+public class Constants {
+ public final static String MARK_COLON = ":";
+}
diff --git a/openo-portal/portal-package/src/main/java/org/openo/portal/system/RegisterService.java b/openo-portal/portal-package/src/main/java/org/openo/portal/system/RegisterService.java
new file mode 100644
index 00000000..01ba3526
--- /dev/null
+++ b/openo-portal/portal-package/src/main/java/org/openo/portal/system/RegisterService.java
@@ -0,0 +1,142 @@
+/*
+ * Copyright 2016, CMCC Technologies Co., Ltd.
+ *
+ * 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.openo.portal.system;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+
+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.openo.portal.bean.MsbRegisterBean;
+import org.openo.portal.bean.ServiceNodeBean;
+
+public class RegisterService {
+
+ 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(url, registerObj, "");
+ // TODO
+ System.out.println(registerResponse);
+ }
+ } catch (Exception e){
+ // TODO
+ e.printStackTrace();
+ }
+ }
+
+ 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 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;
+ }
+ }
+ } catch (Exception e) {
+ // TODO
+ e.printStackTrace();;
+ }
+
+ return response;
+ }
+}
diff --git a/openo-portal/portal-package/src/main/java/org/openo/portal/system/SystemListener.java b/openo-portal/portal-package/src/main/java/org/openo/portal/system/SystemListener.java
new file mode 100644
index 00000000..05447232
--- /dev/null
+++ b/openo-portal/portal-package/src/main/java/org/openo/portal/system/SystemListener.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2016, CMCC Technologies Co., Ltd.
+ *
+ * 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.openo.portal.system;
+
+import java.io.File;
+
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+
+public class SystemListener implements ServletContextListener {
+
+ public void contextDestroyed(ServletContextEvent sce) {
+ }
+
+ public void contextInitialized(ServletContextEvent servletContextEvent) {
+ // TODO
+ System.out.println("portal register task begin");
+ String registerFilePath = SystemListener.class.getClassLoader().getResource("").getPath() + "portalConfig" + File.separator + "msb_register.xml";
+ RegisterService.registerMsb(registerFilePath);
+ // TODO
+ System.out.println("portal register task ended.");
+ }
+}