50% Code Coverage-MSB Java SDK
[msb/java-sdk.git] / src / main / java / org / onap / msb / sdk / discovery / util / MsbUtil.java
1 /*******************************************************************************
2  * Copyright 2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  ******************************************************************************/
14 package org.onap.msb.sdk.discovery.util;
15
16 import java.util.HashSet;
17 import java.util.Set;
18
19 import org.apache.commons.lang3.StringUtils;
20 import org.onap.msb.sdk.discovery.common.RouteException;
21 import org.onap.msb.sdk.discovery.entity.Node;
22 import org.onap.msb.sdk.discovery.entity.NodeInfo;
23
24 /**
25  * @ClassName: MsbUtil
26  * @Description: TODO(msb功能工具方法类)
27  * @author tanghua10186366
28  * @date 2017年6月26日
29  * 
30  */
31 public class MsbUtil {
32
33
34   /**
35    * @Title getConsulServiceName
36    * @Description TODO(通过服务名和命名空间组装conusl存储名,用于服务变化监听)
37    * @param serviceName
38    * @param namespace
39    * @return String
40    */
41   public static String getConsulServiceName(String serviceName, String namespace) {
42     if (StringUtils.isEmpty(namespace)) {
43       return serviceName;
44     } else {
45       return serviceName + "-" + namespace;
46     }
47   }
48
49   public static Set<NodeInfo> getLbNodes(Node lbNode) {
50     Set<NodeInfo> nodes = new HashSet<NodeInfo>();
51     NodeInfo nodeInfo = new NodeInfo();
52     nodeInfo.setIp(lbNode.getIp());
53     nodeInfo.setPort(lbNode.getPort());
54     nodes.add(nodeInfo);
55
56     return nodes;
57   }
58   
59   public static void checkServiceName(String serviceName) throws RouteException{
60         
61             if (StringUtils.isBlank(serviceName)) {
62               throw new RouteException("ServiceName  can't be empty", "DATA_FORMAT_ERROR");
63             }
64   }
65   
66   public static String checkVersion(String version) throws RouteException{
67         // 版本号格式检查
68             if (StringUtils.isNotBlank(version)) {
69               if (!RegExpTestUtil.versionRegExpTest(version)) {
70                 throw new RouteException("version is not a valid  format", "DATA_FORMAT_ERROR");
71               }
72             } else {
73               version = "null";
74             }
75             
76             return version;
77   }
78   
79   public static void checkHost(String ip,String port) throws RouteException{
80         // HOST空值和格式检查
81             if (StringUtils.isBlank(ip)) {
82               throw new RouteException("ip can't be empty", "DATA_FORMAT_ERROR");
83
84             }
85
86             if (StringUtils.isBlank(port)) {
87               throw new RouteException("port can't be empty", "DATA_FORMAT_ERROR");
88
89             }
90   }
91
92 }