diff options
Diffstat (limited to 'src/main/java/org/onap')
16 files changed, 1607 insertions, 0 deletions
diff --git a/src/main/java/org/onap/msb/sdk/discovery/MSBService.java b/src/main/java/org/onap/msb/sdk/discovery/MSBService.java new file mode 100644 index 0000000..e0b8596 --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/MSBService.java @@ -0,0 +1,311 @@ +/******************************************************************************* + * 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.msb.sdk.discovery; + +import org.apache.commons.lang3.StringUtils; +import org.onap.msb.sdk.discovery.common.RouteConst; +import org.onap.msb.sdk.discovery.common.RouteException; +import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo; +import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; +import org.onap.msb.sdk.discovery.entity.NodeAddress; +import org.onap.msb.sdk.discovery.entity.RouteResult; +import org.onap.msb.sdk.discovery.util.HttpClientUtil; +import org.onap.msb.sdk.discovery.util.JacksonJsonUtil; +import org.onap.msb.sdk.discovery.util.RegExpTestUtil; + + + +public class MSBService { + + /** + * @Title queryMicroServiceInfo + * @Description TODO(查询单个微服务) + * @param msbAddress 微服务服务器地址( ip:port 或 域名地址) + * @param serviceName 服务名[必填,若自定义服务名包含/,用*代替] + * @param version 版本号[若无版本号,传空字符串] + * @throws RouteException + * @return MicroServiceFullInfo + */ + public MicroServiceFullInfo queryMicroServiceInfo(String msbAddress, String serviceName, + String version) throws RouteException { + + // 服务名空值检查 + if (StringUtils.isBlank(serviceName)) { + throw new RouteException("ServiceName can't be empty", "DATA_FORMAT_ERROR"); + } + + // 版本号格式检查 + if (StringUtils.isNotBlank(version)) { + if (!RegExpTestUtil.versionRegExpTest(version)) { + throw new RouteException("version is not a valid format", "DATA_FORMAT_ERROR"); + } + } else { + version = "null"; + } + + MicroServiceFullInfo microServiceInfo = null; + + + String apiRouteUrl = (new StringBuilder().append("http://").append(msbAddress) + .append(RouteConst.MSB_ROUTE_URL).append("/").append(serviceName).append("/version/") + .append(version).append("?ifPassStatus=true")).toString(); + + String resultJson = HttpClientUtil.httpGet(apiRouteUrl); + microServiceInfo = + (MicroServiceFullInfo) JacksonJsonUtil.jsonToBean(resultJson, MicroServiceFullInfo.class); + + return microServiceInfo; + } + + + /** + * @Title registerMicroServiceInfo + * @Description TODO(注册微服务-默认追加方式) + * @param msbAddress 微服务服务器地址( ip:port 或 域名地址) + * @param microServiceInfo 微服务注册实体类 + * @throws RouteException + * @return MicroServiceFullInfo + */ + public MicroServiceFullInfo registerMicroServiceInfo(String msbAddress, + MicroServiceInfo microServiceInfo) throws RouteException { + return this.registerMicroServiceInfo(msbAddress, microServiceInfo, true); + + } + + /** + * @Title registerMicroServiceInfo + * @Description TODO(注册微服务) + * @param msbAddress 微服务服务器地址( ip:port 或 域名地址) + * @param microServiceInfo 微服务注册实体类 + * @param createOrUpdate true:新增或追加更新 ,false:重新添加 + * @throws RouteException + * @return MicroServiceFullInfo + */ + public MicroServiceFullInfo registerMicroServiceInfo(String msbAddress, + MicroServiceInfo microServiceInfo, boolean createOrUpdate) throws RouteException { + + // 必填项空值检查 + if (StringUtils.isBlank(microServiceInfo.getServiceName()) + || StringUtils.isBlank(microServiceInfo.getProtocol()) + || microServiceInfo.getNodes().size() == 0) { + + throw new RouteException( + "register MicroServiceInfo FAIL: Some MicroServiceInfo's required fields are empty", + "DATA_FORMAT_ERROR"); + + } + + + // 版本号格式检查 + if (StringUtils.isNotBlank(microServiceInfo.getVersion())) { + if (!RegExpTestUtil.versionRegExpTest(microServiceInfo.getVersion())) { + throw new RouteException("register MicroServiceInfo FAIL:version is not a valid format", + "DATA_FORMAT_ERROR"); + + } + } + + + + // 服务协议取值范围检查 + if (!RouteConst.checkExistProtocol(microServiceInfo.getProtocol().trim())) { + throw new RouteException("register MicroServiceInfo FAIL:Protocol is wrong,value range:(" + + RouteConst.listProtocol() + ")", "DATA_FORMAT_ERROR"); + + } + + + + String apiRouteJson = JacksonJsonUtil.beanToJson(microServiceInfo); + + String apiRouteUrl = + (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL) + .append("?createOrUpdate=").append(createOrUpdate)).toString(); + + String resultJson = HttpClientUtil.httpPostWithJSON(apiRouteUrl, apiRouteJson); + + MicroServiceFullInfo microServiceFullInfo = + (MicroServiceFullInfo) JacksonJsonUtil.jsonToBean(resultJson, MicroServiceFullInfo.class); + + return microServiceFullInfo; + } + + /** + * @Title cancelMicroServiceInfo + * @Description TODO(注销全部微服务) + * @param msbAddress 微服务服务器地址( ip:port 或 域名地址) + * @param serviceName 服务名[必填,若自定义服务名包含/,用*代替] + * @param version 版本号[若无版本号,传空字符串] + * @throws RouteException + * @return RouteResult + */ + public RouteResult cancelMicroServiceInfo(String msbAddress, String serviceName, String version) + throws RouteException { + RouteResult result = new RouteResult(); + + // 服务名空值检查 + if (StringUtils.isBlank(serviceName)) { + throw new RouteException("cancel MicroServiceInfo FAIL:ServiceName can't be empty", + "DATA_FORMAT_ERROR"); + + } + + // 版本号格式检查 + if (StringUtils.isNotBlank(version)) { + if (!RegExpTestUtil.versionRegExpTest(version)) { + throw new RouteException("cancel MicroServiceInfo FAIL:version is not a valid format", + "DATA_FORMAT_ERROR"); + + } + } else { + version = "null"; + } + + + String url = + (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL)) + .append("/").append(serviceName).append("/version/").append(version).toString(); + + HttpClientUtil.delete(url, null); + + + result.setResult(RouteConst.REQUEST_SUCCESS); + result.setInfo("cancel MicroServiceInfo success"); + + + return result; + } + + /** + * @Title cancelMicroServiceInfo + * @Description TODO(注销单个微服务) + * @param msbAddress 微服务服务器地址( ip:port 或 域名地址) + * @param serviceName 服务名[必填,若自定义服务名包含/,用*代替] + * @param version 版本号[若无版本号,传空字符串] + * @param ip + * @param port + * @throws RouteException + * @return RouteResult + */ + public RouteResult cancelMicroServiceInfo(String msbAddress, String serviceName, String version, + String ip, String port) throws RouteException { + + RouteResult result = new RouteResult(); + + // 服务名空值检查 + if (StringUtils.isBlank(serviceName)) { + throw new RouteException("cancel MicroServiceInfo FAIL:ServiceName can't be empty", + "DATA_FORMAT_ERROR"); + + } + + + // HOST空值和格式检查 + if (StringUtils.isBlank(ip)) { + throw new RouteException("cancel MicroServiceInfo FAIL:ip can't be empty", + "DATA_FORMAT_ERROR"); + } + + if (StringUtils.isBlank(port)) { + throw new RouteException("cancel MicroServiceInfo FAIL:port can't be empty", + "DATA_FORMAT_ERROR"); + + } + + // 版本号格式检查 + if (StringUtils.isNotBlank(version)) { + if (!RegExpTestUtil.versionRegExpTest(version)) { + throw new RouteException("cancel MicroServiceInfo FAIL:version is not a valid format", + "DATA_FORMAT_ERROR"); + } + } else { + version = "null"; + } + + + String url = + (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL)) + .append("/").append(serviceName).append("/version/").append(version).append("/nodes/") + .append(ip).append("/").append(port).toString(); + + HttpClientUtil.delete(url, null); + + + result.setResult(RouteConst.REQUEST_SUCCESS); + result.setInfo("cancel MicroServiceInfo success"); + + return result; + } + + + /** + * @Title healthCheckbyTTL + * @Description TODO(请求服务实例TTL健康检查) + * @param msbAddress + * @param serviceName 服务名 + * @param version 版本号[若无版本号,传空字符串] + * @param ip 实例IP + * @param port 实例端口 + * @throws RouteException + * @return CheckNode + */ + public NodeAddress healthCheckbyTTL(String msbAddress, String serviceName, String version, + String ip, String port) throws RouteException { + + // 服务名空值检查 + if (StringUtils.isBlank(serviceName)) { + throw new RouteException("ServiceName can't be empty", "DATA_FORMAT_ERROR"); + } + + // 版本号格式检查 + if (StringUtils.isNotBlank(version)) { + if (!RegExpTestUtil.versionRegExpTest(version)) { + throw new RouteException("version is not a valid format", "DATA_FORMAT_ERROR"); + } + } else { + version = "null"; + } + + + // HOST空值和格式检查 + if (StringUtils.isBlank(ip)) { + throw new RouteException("healthCheck by TTL FAIL:ip can't be empty", "DATA_FORMAT_ERROR"); + + } + + if (StringUtils.isBlank(port)) { + throw new RouteException("healthCheck by TTL FAIL:port can't be empty", "DATA_FORMAT_ERROR"); + + } + + + + NodeAddress checkNode = new NodeAddress(ip, port); + + + String healthCheckJson = JacksonJsonUtil.beanToJson(checkNode); + + String healthCheckUrl = + (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL) + .append("/").append(serviceName).append("/version/").append(version).append("/ttl")) + .toString(); + + HttpClientUtil.httpPutWithJSON(healthCheckUrl, healthCheckJson); + + return checkNode; + } + + + +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/common/RouteConst.java b/src/main/java/org/onap/msb/sdk/discovery/common/RouteConst.java new file mode 100644 index 0000000..7a40510 --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/common/RouteConst.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * 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.msb.sdk.discovery.common; + +public class RouteConst { + public static final int SC_OK = 200; + public static final int SC_POST_OK = 201; + public static final int SC_DEL_OK = 204; + public static final int SC_NOTFOND = 404; + public static final String MSB_ROUTE_URL = "/api/microservices/v1/services"; + public static final String REQUEST_SUCCESS = "SUCCESS"; + public static final String REQUEST_FAIL = "FAIL"; + + public static final String LB_POLICY_LIST = "round-robin,ip_hash,least_conn"; + public static final String LB_PARAMS_LIST = "weight,max_fails,fail_timeout"; + public static final String SERVER_TYPE_LIST = "api,iui,custom,p2p"; + public static final String CHECK_TYPE_LIST = "HTTP,TCP,TTL"; + public static final String APIGATEWAY_NAME = "apigateway"; + public static final String VISUAL_RANGE_IN = "1"; + + + + public enum Protocol { + REST, HTTP, MQ, FTP, SNMP, TCP, UDP, UI + } + + public static boolean checkExistProtocol(String value) { + for (Protocol protocol : Protocol.values()) { + if (protocol.name().equals(value)) { + return true; + } + } + return false; + } + + + public static boolean checkExist(String list, String value) { + String[] listArray = list.split(","); + for (int i = 0; i < listArray.length; i++) { + if (value.equals(listArray[i])) + return true; + } + return false; + } + + public static String listProtocol() { + String list = ""; + for (Protocol protocol : Protocol.values()) { + list += protocol.name() + " | "; + } + return list; + } + +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/common/RouteException.java b/src/main/java/org/onap/msb/sdk/discovery/common/RouteException.java new file mode 100644 index 0000000..4ba1473 --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/common/RouteException.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * 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.msb.sdk.discovery.common; + +public class RouteException extends Exception { + private String errorMsg; + private String errorCode; + + public RouteException() {} + + public RouteException(String errorMsg, String errorCode) { + super(errorMsg); + this.errorMsg = errorMsg; + this.errorCode = errorCode; + } + + public String getErrorMsg() { + return this.errorMsg; + } + + public void setErrorMsg(String errorMsg) { + this.errorMsg = errorMsg; + } + + public String getErrorCode() { + return this.errorCode; + } + + public void setErrorCode(String errorCode) { + this.errorCode = errorCode; + } +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/entity/CustomDateSerializer.java b/src/main/java/org/onap/msb/sdk/discovery/entity/CustomDateSerializer.java new file mode 100644 index 0000000..0dfbace --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/entity/CustomDateSerializer.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * 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.msb.sdk.discovery.entity; + +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; + +public class CustomDateSerializer extends JsonSerializer<Date> { + + @Override + public void serialize(Date value, JsonGenerator jsonGenerator, SerializerProvider provider) + throws IOException, JsonProcessingException { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"); + jsonGenerator.writeString(sdf.format(value)); + } + + +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/entity/KeyVaulePair.java b/src/main/java/org/onap/msb/sdk/discovery/entity/KeyVaulePair.java new file mode 100644 index 0000000..e262068 --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/entity/KeyVaulePair.java @@ -0,0 +1,52 @@ +/******************************************************************************* + * 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.msb.sdk.discovery.entity; + +import java.io.Serializable; + +public class KeyVaulePair implements Serializable { + private static final long serialVersionUID = 1L; + + private String key; + private String value; + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public KeyVaulePair() { + + } + + public KeyVaulePair(String key, String value) { + this.key = key; + this.value = value; + + } + + + +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/entity/MicroServiceFullInfo.java b/src/main/java/org/onap/msb/sdk/discovery/entity/MicroServiceFullInfo.java new file mode 100644 index 0000000..698092e --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/entity/MicroServiceFullInfo.java @@ -0,0 +1,78 @@ +/******************************************************************************* + * 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.msb.sdk.discovery.entity; + +import java.io.Serializable; +import java.util.Set; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class MicroServiceFullInfo extends Service<NodeInfo> implements Serializable { + private static final long serialVersionUID = 1L; + + private Set<NodeInfo> nodes; + + // 状态 0:不可用 1:可用,预留 + private String status = "1"; + + public Set<NodeInfo> getNodes() { + return nodes; + } + + public void setNodes(Set<NodeInfo> nodes) { + this.nodes = nodes; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + + public String toString() { + StringBuffer buf = new StringBuffer(); + buf.append("MicroService List:").append("\r\n");; + buf.append("serviceName:").append(getServiceName()).append("\r\n"); + buf.append("version:").append(getVersion()).append("\r\n"); + buf.append("url:").append(getUrl()).append("\r\n"); + buf.append("protocol:").append(getProtocol()).append("\r\n"); + buf.append("visualRange:").append(getVisualRange()).append("\r\n"); + buf.append("nodes:").append("\r\n"); + + for (NodeInfo nodeInstace : this.nodes) { + buf.append(" nodeId-").append(nodeInstace.getNodeId()).append("\r\n"); + buf.append(" ip-").append(nodeInstace.getIp()).append("\r\n"); + buf.append(" port-").append(nodeInstace.getPort()).append("\r\n"); + buf.append(" ttl-").append(nodeInstace.getTtl()).append("\r\n"); + buf.append(" Created_at-").append(nodeInstace.getCreated_at()).append("\r\n"); + buf.append(" Updated_at-").append(nodeInstace.getUpdated_at()).append("\r\n"); + buf.append(" Expiration-").append(nodeInstace.getExpiration()).append("\r\n"); + } + buf.append("metadata:").append("\r\n"); + + if (this.getMetadata() != null && this.getMetadata().size() > 0) { + for (KeyVaulePair keyVaulePair : this.getMetadata()) { + buf.append(" key-").append(keyVaulePair.getKey()).append("\r"); + buf.append(" value-").append(keyVaulePair.getValue()).append("\r\n"); + + } + } + return buf.toString(); + } + +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/entity/MicroServiceInfo.java b/src/main/java/org/onap/msb/sdk/discovery/entity/MicroServiceInfo.java new file mode 100644 index 0000000..00ffe81 --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/entity/MicroServiceInfo.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * 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.msb.sdk.discovery.entity; + +import java.io.Serializable; +import java.util.Set; + +/** + * @ClassName: MicroServiceInfo Bean + * @Description:微服务信息实体类 + * @author 10188044 + * @date 2016-1-19 + */ +public class MicroServiceInfo extends Service<Node> implements Serializable { + private static final long serialVersionUID = 1L; + + private Set<Node> nodes; + + public Set<Node> getNodes() { + return nodes; + } + + public void setNodes(Set<Node> nodes) { + this.nodes = nodes; + } + + public String toString() { + StringBuffer buf = new StringBuffer(); + buf.append("MicroService List:").append("\r\n");; + buf.append("serviceName:").append(getServiceName()).append("\r\n"); + buf.append("version:").append(getVersion()).append("\r\n"); + buf.append("url:").append(getUrl()).append("\r\n"); + buf.append("protocol:").append(getProtocol()).append("\r\n"); + buf.append("visualRange:").append(getVisualRange()).append("\r\n"); + buf.append("nodes:").append("\r\n"); + + for (Node nodeInstace : this.nodes) { + buf.append(" ip-").append(nodeInstace.getIp()).append("\r\n"); + buf.append(" port-").append(nodeInstace.getPort()).append("\r\n"); + buf.append(" ttl-").append(nodeInstace.getTtl()).append("\r\n"); + } + return buf.toString(); + } +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/entity/Node.java b/src/main/java/org/onap/msb/sdk/discovery/entity/Node.java new file mode 100644 index 0000000..c28f232 --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/entity/Node.java @@ -0,0 +1,166 @@ +/******************************************************************************* + * 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.msb.sdk.discovery.entity; + +import java.io.Serializable; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class Node implements Serializable { + private static final long serialVersionUID = 1L; + + private String ip; + + private String port; + + private String ttl = ""; + + // 负载均衡策略参数 + private String lb_server_params = ""; + + // 健康检查参数 + private String checkType = ""; + private String checkUrl = ""; + private String checkInterval = ""; + private String checkTimeOut = ""; + + private String ha_role = ""; + + + public String getHa_role() { + return ha_role; + } + + + /** + * @Title setHa_role + * @Description TODO(实例主备状态,取值范围:active,standby) + * @param ha_role + * @return void + */ + public void setHa_role(String ha_role) { + this.ha_role = ha_role; + } + + + + 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 String getTtl() { + return ttl; + } + + public void setTtl(String ttl) { + this.ttl = ttl; + } + + + @Override + public String toString() { + // TODO Auto-generated method stub + return (new StringBuilder().append(ip).append(":").append(port).append(" ttl:").append(ttl)) + .toString(); + } + + public String getLb_server_params() { + return lb_server_params; + } + + /** + * @Title setLb_server_params + * @Description TODO(针对协议为TCP或UDP的服务实例配置负载均衡参数) <br> + * 支持的server参数设置: <br> + * 1. weight: server的权重,不显式设置默认为1 <br> + * 2. + * max_fails:在fail_timeout设置的时间间隔内,允许失败尝试的次数,不显式设置默认为1.比如fail_timeout=30s,max_fails=6,那表示在30s内如果6次连接失败,那认定该server为unavailable + * <br> + * 3. fail_timeout:判定server为unavailable的时间间隔。如果不显式设置,默认设置为10s + * @param lb_server_params (示例:weight=5,max_fails=3,fail_timeout=30s) + * @return void + */ + public void setLb_server_params(String lb_server_params) { + this.lb_server_params = lb_server_params; + } + + public String getCheckType() { + return checkType; + } + + /** + * @Title setCheckType + * @Description TODO(健康检查类型,可选范围:TTL,HTTP,TCP) <br> + * 1.HTTP保活由"MSB"每隔checkInterval时间向"服务提供方"发送 GET请求。如果请求返回任何2xx状态码,检测成功。 <br> + * 2.TCP保活原理同HTTP,协议不同。 <br> + * 3.TTL保活由"服务提供方"向"MSB"发送HTTP请求以保持联通状态,在checkInterval设置的时间间隔内未能收到保活请求,服务发现将此服务状态设置为无效。 + * @param checkType + * @return void + */ + public void setCheckType(String checkType) { + this.checkType = checkType; + } + + public String getCheckUrl() { + return checkUrl; + } + + /** + * @Title setCheckUrl + * @Description TODO(健康检查类型为HTTP或TCP,填写检查URL) + * @param checkUrl + * @return void + */ + public void setCheckUrl(String checkUrl) { + this.checkUrl = checkUrl; + } + + public String getCheckInterval() { + return checkInterval; + } + + /** + * @Title setCheckInterval + * @Description TODO(健康检查轮询时间,单位:秒) + * @param checkInterval + * @return void + */ + public void setCheckInterval(String checkInterval) { + this.checkInterval = checkInterval; + } + + + public String getCheckTimeOut() { + return checkTimeOut; + } + + + public void setCheckTimeOut(String checkTimeOut) { + this.checkTimeOut = checkTimeOut; + } + +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/entity/NodeAddress.java b/src/main/java/org/onap/msb/sdk/discovery/entity/NodeAddress.java new file mode 100644 index 0000000..bd544d7 --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/entity/NodeAddress.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * 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.msb.sdk.discovery.entity; + +import java.io.Serializable; + +public class NodeAddress implements Serializable { + private static final long serialVersionUID = 1L; + + + private String ip; + + + private String port; + + + 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 NodeAddress() { + + } + + public NodeAddress(String ip, String port) { + this.ip = ip; + this.port = port; + } + +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/entity/NodeInfo.java b/src/main/java/org/onap/msb/sdk/discovery/entity/NodeInfo.java new file mode 100644 index 0000000..316b548 --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/entity/NodeInfo.java @@ -0,0 +1,80 @@ +/******************************************************************************* + * 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.msb.sdk.discovery.entity; + +import java.util.Date; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class NodeInfo extends Node { + + private static final long serialVersionUID = 8955786461351557306L; + + private String nodeId; + + private String status; // 实例健康检查状态 + + @JsonSerialize(using = CustomDateSerializer.class) + private Date expiration; + + @JsonSerialize(using = CustomDateSerializer.class) + private Date created_at; + + @JsonSerialize(using = CustomDateSerializer.class) + private Date updated_at; + + public Date getExpiration() { + return expiration; + } + + public void setExpiration(Date expiration) { + this.expiration = expiration; + } + + public Date getCreated_at() { + return created_at; + } + + public void setCreated_at(Date created_at) { + this.created_at = created_at; + } + + public Date getUpdated_at() { + return updated_at; + } + + public void setUpdated_at(Date updated_at) { + this.updated_at = updated_at; + } + + public String getNodeId() { + return nodeId; + } + + public void setNodeId(String nodeId) { + this.nodeId = nodeId; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/entity/RouteResult.java b/src/main/java/org/onap/msb/sdk/discovery/entity/RouteResult.java new file mode 100644 index 0000000..9e266fa --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/entity/RouteResult.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * 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.msb.sdk.discovery.entity; + +/** + * @ClassName: ApiRouteResult + * @Description: TODO(ApiRoute操作返回类) + * @author tanghua10186366 + * @date 2015年9月25日 上午11:24:42 + * + */ +public class RouteResult { + private String result; + private String info; + + public String getInfo() { + return info; + } + + public void setInfo(String info) { + this.info = info; + } + + public String getResult() { + return result; + } + + public void setResult(String result) { + this.result = result; + } +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/entity/Service.java b/src/main/java/org/onap/msb/sdk/discovery/entity/Service.java new file mode 100644 index 0000000..a89ded9 --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/entity/Service.java @@ -0,0 +1,180 @@ +/******************************************************************************* + * 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.msb.sdk.discovery.entity; + +import java.io.Serializable; +import java.util.List; +import java.util.Set; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class Service<T> implements Serializable { + private static final long serialVersionUID = 1L; + // 服务名[必填] + private String serviceName; + // 版本号 ( 格式:v(小写)+数字) + private String version = ""; + // 服务url (格式:url地址以/开头,不能以/结尾) + private String url = ""; + // 服务对应协议,比如REST、UI、HTTP、MQ、FTP、SNMP、TCP、UDP + private String protocol = ""; + // 服务的可见范围 0:系统间 1:系统内 + private String visualRange = "1"; + + // 负载均衡策略类型 + private String lb_policy = ""; + + + private String host = ""; + + private String path = ""; + + private Set<T> nodes; + + // 服务自身属性的键值对 + private List<KeyVaulePair> metadata; + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public String getLb_policy() { + return lb_policy; + } + + /** + * @Title setLb_policy + * @Description TODO(针对协议为TCP或UDP的服务选择负载均衡策略) + * @param lb_policy (可选策略:round-robin,ip_hash) + * @return void + */ + public void setLb_policy(String lb_policy) { + this.lb_policy = lb_policy; + } + + public List<KeyVaulePair> getMetadata() { + return metadata; + } + + /** + * @Title setMetadata + * @Description TODO(配置服务特有的附加属性键值对) + * @param metadata + * @return void + */ + public void setMetadata(List<KeyVaulePair> metadata) { + this.metadata = metadata; + } + + public Set<T> getNodes() { + return nodes; + } + + /** + * @Title setNodes + * @Description TODO(配置服务的服务器实例列表) + * @param nodes + * @return void + */ + public void setNodes(Set<T> nodes) { + this.nodes = nodes; + } + + public String getServiceName() { + return serviceName; + } + + /** + * + * @Title setServiceName + * @Description TODO(服务名[必填]) + * @param serviceName + * @return void + */ + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } + + public String getVersion() { + return version; + } + + /** + * + * @Title setVersion + * @Description TODO(版本号 ( 格式:v(小写)+数字)) + * @param version + * @return void + */ + public void setVersion(String version) { + this.version = version; + } + + public String getUrl() { + return url; + } + + /** + * @Title setUrl + * @Description TODO(目标服务URL地址 (格式:url地址以/开头,不能以/结尾)) + * @param url + * @return void + */ + public void setUrl(String url) { + this.url = url; + } + + public String getProtocol() { + return protocol; + } + + /** + * @Title setProtocol + * @Description TODO(服务对应协议,比如REST、MQ、FTP、SNMP[必填]) + * @param protocol + * @return void + */ + public void setProtocol(String protocol) { + this.protocol = protocol; + } + + + public String getVisualRange() { + return visualRange; + } + + /** + * @Title setVisualRange + * @Description TODO(服务的可见范围 系统间:0 系统内:1 ,多个可见范围用 "|"分隔,如"0|1" ) + * @param visualRange + * @return void + */ + public void setVisualRange(String visualRange) { + this.visualRange = visualRange; + } + +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/util/HttpClientUtil.java b/src/main/java/org/onap/msb/sdk/discovery/util/HttpClientUtil.java new file mode 100644 index 0000000..b3bcf0b --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/util/HttpClientUtil.java @@ -0,0 +1,182 @@ +/******************************************************************************* + * 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.msb.sdk.discovery.util; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; + +import org.apache.http.NameValuePair; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.utils.URLEncodedUtils; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; +import org.onap.msb.sdk.discovery.common.RouteConst; +import org.onap.msb.sdk.discovery.common.RouteException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HttpClientUtil { + + private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class); + + public static String httpPostWithJSON(String url, String params) throws RouteException { + String result = null; + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(url); + httpPost.addHeader("Content-type", "application/json; charset=utf-8"); + httpPost.setHeader("Accept", "application/json"); + httpPost.setEntity(new StringEntity(params, Charset.forName("UTF-8"))); + try { + CloseableHttpResponse res = httpClient.execute(httpPost); + result = EntityUtils.toString(res.getEntity()); + if (res.getStatusLine().getStatusCode() != RouteConst.SC_POST_OK) { + throw new RouteException(result, "SERVICE_GET_ERR"); + } + res.close(); + } catch (IOException e) { + String errorMsg = url + ":httpPostWithJSON connect faild"; + throwsRouteException(errorMsg, e, "POST_CONNECT_FAILD"); + } finally { + try { + httpClient.close(); + } catch (IOException e) { + String errorMsg = url + ":close httpClient faild"; + throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD"); + } + } + + return result; + + } + + public static void delete(String url, String parameter) throws RouteException { + String result = null; + String baseUrl; + if (parameter != null) { + List<NameValuePair> params = new ArrayList<NameValuePair>(); + params.add(new BasicNameValuePair("serviceName", parameter)); + baseUrl = url + "?" + URLEncodedUtils.format(params, "UTF-8"); + } else { + baseUrl = url; + } + + CloseableHttpClient httpClient = HttpClients.createDefault();; + try { + + HttpDelete httpDelete = new HttpDelete(baseUrl); + CloseableHttpResponse res = httpClient.execute(httpDelete); + + if (res.getStatusLine().getStatusCode() != RouteConst.SC_DEL_OK) { + throw new RouteException(EntityUtils.toString(res.getEntity()), "SERVICE_DELETE_ERR"); + } + + res.close(); + } catch (IOException e) { + String errorMsg = baseUrl + ":delete connect faild"; + throwsRouteException(errorMsg, e, "DELETE_CONNECT_FAILD"); + } finally { + try { + httpClient.close(); + } catch (IOException e) { + String errorMsg = baseUrl + ":close httpClient faild"; + throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD"); + } + } + + + } + + public static String httpGet(String url) throws RouteException { + String result = null; + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet(url); + httpGet.addHeader("Content-type", "application/json; charset=utf-8"); + httpGet.setHeader("Accept", "application/json"); + try { + CloseableHttpResponse res = httpClient.execute(httpGet); + result = EntityUtils.toString(res.getEntity()); + if (res.getStatusLine().getStatusCode() != RouteConst.SC_OK) { + throw new RouteException(result, "SERVICE_GET_ERR"); + } + res.close(); + } catch (ClientProtocolException e) { + String errorMsg = url + ":httpGetWithJSON connect faild"; + throwsRouteException(errorMsg, e, "GET_CONNECT_FAILD"); + } catch (IOException e) { + String errorMsg = url + ":httpGetWithJSON connect faild"; + throwsRouteException(errorMsg, e, "GET_CONNECT_FAILD"); + } finally { + try { + httpClient.close(); + } catch (IOException e) { + String errorMsg = url + ":close httpClient faild"; + throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD"); + } + } + + return result; + + } + + + public static String httpPutWithJSON(String url, String params) throws RouteException { + String result = null; + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPut httpPut = new HttpPut(url); + httpPut.addHeader("Content-type", "application/json; charset=utf-8"); + httpPut.setHeader("Accept", "application/json"); + httpPut.setEntity(new StringEntity(params, Charset.forName("UTF-8"))); + try { + CloseableHttpResponse res = httpClient.execute(httpPut); + result = EntityUtils.toString(res.getEntity()); + if (res.getStatusLine().getStatusCode() != RouteConst.SC_POST_OK) { + throw new RouteException(result, "SERVICE_GET_ERR"); + } + res.close(); + } catch (IOException e) { + String errorMsg = url + ":httpPostWithJSON connect faild"; + throwsRouteException(errorMsg, e, "POST_CONNECT_FAILD"); + } finally { + try { + httpClient.close(); + } catch (IOException e) { + String errorMsg = url + ":close httpClient faild"; + throwsRouteException(errorMsg, e, "CLOSE_CONNECT_FAILD"); + } + } + + return result; + + } + + + + private static void throwsRouteException(String errorMsg, Exception e, String errorCode) + throws RouteException { + String msg = errorMsg + ".errorMsg:" + e.getMessage(); + logger.error(msg); + throw new RouteException(errorMsg, errorCode); + } + +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/util/JacksonJsonUtil.java b/src/main/java/org/onap/msb/sdk/discovery/util/JacksonJsonUtil.java new file mode 100644 index 0000000..382dc64 --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/util/JacksonJsonUtil.java @@ -0,0 +1,132 @@ +/******************************************************************************* + * 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. + ******************************************************************************/ +/** + * Copyright (C) 2015 ZTE, Inc. and others. All rights reserved. (ZTE) + * + * 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.msb.sdk.discovery.util; + +import org.onap.msb.sdk.discovery.common.RouteException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JacksonJsonUtil { + + private static final Logger logger = LoggerFactory.getLogger(JacksonJsonUtil.class); + + private static ObjectMapper mapper; + + /** + * 获取ObjectMapper实例 + * + * @param createNew 方式:true,新实例;false,存在的mapper实例 + * @return + */ + public static synchronized ObjectMapper getMapperInstance() { + if (mapper == null) { + mapper = new ObjectMapper(); + } + return mapper; + } + + /** + * 将java对象转换成json字符串 + * + * @param obj 准备转换的对象 + * @return json字符串 + * @throws Exception + */ + public static String beanToJson(Object obj) throws RouteException { + String json = null; + try { + ObjectMapper objectMapper = getMapperInstance(); + json = objectMapper.writeValueAsString(obj); + } catch (Exception e) { + String errorMsg = "Class beanToJson faild"; + throwsRouteException(errorMsg, e, "BEAN_TO_JSON_FAILD"); + } + return json; + } + + + + /** + * 将json字符串转换成java对象 + * + * @param json 准备转换的json字符串 + * @param cls 准备转换的类 + * @return + * @throws Exception + */ + public static Object jsonToBean(String json, Class<?> cls) throws RouteException { + Object vo = null; + try { + ObjectMapper objectMapper = getMapperInstance(); + + + vo = objectMapper.readValue(json, cls); + + } catch (Exception e) { + String errorMsg = cls + " JsonTobean faild:" + e.getMessage(); + throwsRouteException(errorMsg, e, "JSON_TO_BEAN_FAILD"); + } + return vo; + } + + + /** + * 将json字符串转换成java集合对象 + * + * @param json 准备转换的json字符串 + * @param cls 准备转换的类 + * @return + * @throws Exception + */ + + public static <T> T jsonToListBean(String json, TypeReference<T> valueTypeRef) { + try { + + ObjectMapper objectMapper = getMapperInstance(); + + + return objectMapper.readValue(json, valueTypeRef); + + } catch (Exception e) { + String errorMsg = " JsonTobean faild:" + e.getMessage(); + logger.error(errorMsg); + } + return null; + } + + + + private static void throwsRouteException(String errorMsg, Exception e, String errorCode) + throws RouteException { + String msg = errorMsg + ".errorMsg:" + e.getMessage(); + logger.error(msg); + throw new RouteException(errorMsg, errorCode); + } +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/util/MsbUtil.java b/src/main/java/org/onap/msb/sdk/discovery/util/MsbUtil.java new file mode 100644 index 0000000..2361d9f --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/util/MsbUtil.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * 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.msb.sdk.discovery.util; + +import java.util.HashSet; +import java.util.Set; + +import org.apache.commons.lang3.StringUtils; +import org.onap.msb.sdk.discovery.entity.Node; +import org.onap.msb.sdk.discovery.entity.NodeInfo; + +/** + * @ClassName: MsbUtil + * @Description: TODO(msb功能工具方法类) + * @author tanghua10186366 + * @date 2017年6月26日 + * + */ +public class MsbUtil { + + + /** + * @Title getConsulServiceName + * @Description TODO(通过服务名和命名空间组装conusl存储名,用于服务变化监听) + * @param serviceName + * @param namespace + * @return String + */ + public static String getConsulServiceName(String serviceName, String namespace) { + if (StringUtils.isEmpty(namespace)) { + return serviceName; + } else { + return serviceName + "-" + namespace; + } + } + + public static Set<NodeInfo> getLbNodes(Node lbNode) { + Set<NodeInfo> nodes = new HashSet<NodeInfo>(); + NodeInfo nodeInfo = new NodeInfo(); + nodeInfo.setIp(lbNode.getIp()); + nodeInfo.setPort(lbNode.getPort()); + nodes.add(nodeInfo); + + return nodes; + } + +} diff --git a/src/main/java/org/onap/msb/sdk/discovery/util/RegExpTestUtil.java b/src/main/java/org/onap/msb/sdk/discovery/util/RegExpTestUtil.java new file mode 100644 index 0000000..9afeb7c --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/discovery/util/RegExpTestUtil.java @@ -0,0 +1,75 @@ +/******************************************************************************* + * 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.msb.sdk.discovery.util; + +import java.util.regex.Pattern; + +public class RegExpTestUtil { + + public static boolean serviceNameRegExpTest(String serviceName) { + + String serviceNameReg = "^([0-9a-zA-Z]|-|_)*$"; + return Pattern.matches(serviceNameReg, serviceName); + + } + + + public static boolean hostRegExpTest(String host) { + + String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)" + ":(\\d{1,5})$"; + return Pattern.matches(hostReg, host); + + } + + public static boolean ipRegExpTest(String ip) { + + String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"; + return Pattern.matches(hostReg, ip); + + } + + public static boolean portRegExpTest(String port) { + + String hostReg = "^\\d{1,5}$"; + return Pattern.matches(hostReg, port); + + } + + public static boolean versionRegExpTest(String version) { + + String versionReg = "^v\\d+(\\.\\d+)?$"; + return Pattern.matches(versionReg, version); + + } + + public static boolean urlRegExpTest(String url) { + if (url.equals("/")) + return true; + String urlReg = "^\\/.*((?!\\/).)$"; + return Pattern.matches(urlReg, url); + + } + + + + public static void main(String[] args) { + System.out.println(urlRegExpTest("/")); + } +} |