73a279029de69297fa7b56ca3cbd923e2b2fae04
[msb/discovery.git] / sdclient / discovery-service / src / main / java / org / onap / msb / sdclient / wrapper / ConsulServiceWrapper.java
1 /**
2  * Copyright 2016-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.sdclient.wrapper;
15
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.msb.sdclient.core.AgentService;
25 import org.onap.msb.sdclient.core.CatalogService;
26 import org.onap.msb.sdclient.core.Check;
27 import org.onap.msb.sdclient.core.ConsulResponse;
28 import org.onap.msb.sdclient.core.HealthService;
29 import org.onap.msb.sdclient.core.HealthService.Service;
30 import org.onap.msb.sdclient.core.KeyVaulePair;
31 import org.onap.msb.sdclient.core.MicroServiceFullInfo;
32 import org.onap.msb.sdclient.core.MicroServiceInfo;
33 import org.onap.msb.sdclient.core.Node;
34 import org.onap.msb.sdclient.core.NodeAddress;
35 import org.onap.msb.sdclient.core.NodeInfo;
36 import org.onap.msb.sdclient.core.exception.ExtendedInternalServerErrorException;
37 import org.onap.msb.sdclient.core.exception.ExtendedNotFoundException;
38 import org.onap.msb.sdclient.core.exception.UnprocessableEntityException;
39 import org.onap.msb.sdclient.wrapper.util.ConfigUtil;
40 import org.onap.msb.sdclient.wrapper.util.DiscoverUtil;
41 import org.onap.msb.sdclient.wrapper.util.HttpClientUtil;
42 import org.onap.msb.sdclient.wrapper.util.JacksonJsonUtil;
43 import org.onap.msb.sdclient.wrapper.util.RegExpTestUtil;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 import com.fasterxml.jackson.core.type.TypeReference;
48
49 public class ConsulServiceWrapper {
50
51
52     private static ConsulServiceWrapper instance = new ConsulServiceWrapper();
53
54
55     private ConsulServiceWrapper() {}
56
57     public static ConsulServiceWrapper getInstance() {
58         return instance;
59     }
60
61     private static final Logger LOGGER = LoggerFactory.getLogger(ConsulServiceWrapper.class);
62
63     /**
64      * Title: getAllMicroServiceInstances Description: 获取全部服务
65      * 
66      * @return
67      * @see com.zte.ums.nfv.eco.hsif.msb.core.IMSBService#getAllMicroServiceInstances()
68      */
69     public List<MicroServiceFullInfo> getAllMicroServiceInstances() {
70
71         String consulServiceUrl =
72                         (new StringBuilder().append("http://").append(ConfigUtil.getInstance().getConsulAddress())
73                                         .append(DiscoverUtil.CONSUL_CATALOG_URL).append("/services")).toString();
74
75         String resultJson = HttpClientUtil.httpGet(consulServiceUrl);
76         Map<String, String[]> catalogServiceMap = (Map<String, String[]>) JacksonJsonUtil.jsonToMapBean(resultJson);
77
78         List<MicroServiceFullInfo> microServiceFullInfoArray = new ArrayList<MicroServiceFullInfo>();
79
80         if (catalogServiceMap.isEmpty()) {
81             return microServiceFullInfoArray;
82         }
83
84         for (Map.Entry<String, String[]> entry : catalogServiceMap.entrySet()) {
85             Set<String> versionSet = new HashSet<String>();
86
87             Set<String> nsSet = new HashSet<String>();
88             nsSet.add("");
89
90             String consul_serviceName = entry.getKey().toString();
91             String[] tagList = entry.getValue();
92
93             for (String tag : tagList) {
94
95                 if (tag.startsWith("\"base\"")) {
96                     String ms_base_json = tag.split("\"base\":")[1];
97
98                     try {
99                         Map<String, String> baseMap =
100                                         (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_base_json, Map.class);
101                         if (baseMap.get("version") != null) {
102                             versionSet.add(baseMap.get("version"));
103                         } else {
104                             versionSet.add("");
105                         }
106                     } catch (Exception e) {
107                         LOGGER.error(e.getMessage());
108                     }
109
110                     continue;
111
112                 }
113
114                 if (tag.startsWith("\"ns\"")) {
115                     String ms_ns_json = tag.split("\"ns\":")[1];
116
117                     try {
118                         Map<String, String> namespaceMap =
119                                         (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_ns_json, Map.class);
120                         if (namespaceMap.get("namespace") != null) {
121                             nsSet.add(namespaceMap.get("namespace"));
122                         } else {
123                             nsSet.add("");
124                         }
125
126                         continue;
127
128                     } catch (Exception e) {
129                         // TODO Auto-generated catch block
130                         LOGGER.error(e.getMessage());
131                     }
132
133                     continue;
134
135                 }
136
137
138             }
139
140             for (String ms_version : versionSet) {
141                 for (String ns : nsSet) {
142                     MicroServiceFullInfo microServiceFullInfo =
143                                     getMicroServiceInstanceForAll(consul_serviceName, ms_version, ns);
144                     if (microServiceFullInfo != null && !"consul".equals(microServiceFullInfo.getServiceName())) {
145
146                         microServiceFullInfoArray.add(microServiceFullInfo);
147                     }
148                 }
149             }
150
151
152         }
153
154
155         return microServiceFullInfoArray;
156     }
157
158
159
160     /**
161      * @Title getMicroServiceInstanceForAll
162      * @Description TODO(通过列表遍历获取单个服务信息)
163      * @param consul_serviceName
164      * @param version
165      * @param namespace
166      * @return
167      * @return MicroServiceFullInfo
168      */
169     public MicroServiceFullInfo getMicroServiceInstanceForAll(String consul_serviceName, String version,
170                     String namespace) {
171
172         try {
173             ConsulResponse consulResponse = getHealthServices(consul_serviceName, false, "", "");
174             if (consulResponse == null) {
175                 LOGGER.error("microservice not found: serviceName-" + consul_serviceName + ", namespace-" + namespace);
176                 return null;
177             }
178
179             String serviceName = consul_serviceName;
180             if (StringUtils.isNotBlank(namespace)) {
181                 if (consul_serviceName.endsWith("-" + namespace)) {
182                     serviceName = consul_serviceName.substring(0, consul_serviceName.length() - namespace.length() - 1);
183                 }
184             }
185
186
187             ConsulResponse serviceResponse =
188                             getMicroServiceInfo(consulResponse, serviceName, version, false, "", namespace);
189             return (MicroServiceFullInfo) serviceResponse.getResponse();
190         } catch (Exception e) {
191             if (StringUtils.isNotBlank(namespace)) {
192                 LOGGER.error("get service List have error:serviceName[" + consul_serviceName + "],version[" + version
193                                 + "],namespace[" + namespace + "]:" + e.getMessage());
194             }
195         }
196
197         return null;
198     }
199
200
201     /**
202      * @Title getMicroServiceInstance
203      * @Description TODO(通过Rest接口获取单个服务信息)
204      * @param serviceName
205      * @param version
206      * @param ifPassStatus
207      * @param wait
208      * @param index
209      * @param labels
210      * @param namespace
211      * @return
212      * @return ConsulResponse
213      */
214     public ConsulResponse getMicroServiceInstance(String serviceName, String version, boolean ifPassStatus, String wait,
215                     String index, String labels, String namespace) {
216
217         if ("null".equals(version)) {
218             version = "";
219         }
220
221
222         checkServiceNameAndVersion(serviceName, version);
223
224         if (!RegExpTestUtil.labelRegExpTest(labels)) {
225             throw new UnprocessableEntityException(
226                             "get MicroServiceInfo FAIL: The label query parameter format is wrong (key:value)");
227         }
228
229         String consul_serviceName = getServiceName4Consul(serviceName, namespace);
230
231         ConsulResponse consulResponse = getHealthServices(consul_serviceName, ifPassStatus, wait, index);
232         if (consulResponse == null) {
233             String errInfo = "microservice not found: serviceName-" + serviceName + ", namespace-" + namespace;
234             throw new ExtendedNotFoundException(errInfo);
235         }
236
237         return getMicroServiceInfo(consulResponse, serviceName, version, ifPassStatus, labels, namespace);
238
239     }
240
241
242
243     /**
244      * Title: getMicroServiceInstance Description:获取指定服务信息
245      * 
246      * @param serviceName
247      * @param version
248      * @return
249      * @see com.zte.ums.nfv.eco.hsif.msb.core.IMSBService#getMicroServiceInstance(java.lang.String,
250      *      java.lang.String)
251      */
252
253     public ConsulResponse getMicroServiceInfo(ConsulResponse consulResponse, String serviceName, String version,
254                     boolean ifPassStatus, String labels, String namespace) {
255         // TODO Auto-generated method stub
256
257
258
259         String resultJson = (String) consulResponse.getResponse();
260         List<HealthService> healthServiceList =
261                         JacksonJsonUtil.jsonToListBean(resultJson, new TypeReference<List<HealthService>>() {});
262
263
264         if (healthServiceList == null || healthServiceList.size() == 0) {
265             String errInfo = "microservice not found: serviceName-" + serviceName + ", namespace-" + namespace;
266             throw new ExtendedNotFoundException(errInfo);
267
268         }
269
270         try {
271
272             // label query,format key:value|value2,key2:value2
273             boolean islabelQuery = false;
274             Map<String, String> query_labelMap = new HashMap<String, String>();
275             if (StringUtils.isNotBlank(labels)) {
276                 islabelQuery = true;
277                 String[] routeLabels = StringUtils.split(labels, ",");
278
279                 for (int i = 0; i < routeLabels.length; i++) {
280                     String[] labelArray = StringUtils.split(routeLabels[i], ":");
281                     query_labelMap.put(labelArray[0], labelArray[1]);
282                 }
283             }
284
285
286             MicroServiceFullInfo microServiceInfo = new MicroServiceFullInfo();
287             Set<NodeInfo> nodes = new HashSet<NodeInfo>();
288             Set<String> serviceLabels = new HashSet<String>();
289             Set<KeyVaulePair> serviceMetadatas = new HashSet<KeyVaulePair>();
290             Set<String> serviceNetworkPlane = new HashSet<String>();
291             String nodeNamespace = "";
292
293
294
295             for (HealthService healthService : healthServiceList) {
296                 Service service = healthService.getService();
297                 List<String> tagList = service.getTags();
298
299                 String ms_url = "", ms_version = "", ms_protocol = "", ms_status = "", ms_publish_port = "",
300                                 ms_is_manual = "", ms_visualRange = "1", ms_network_plane_type = "", ms_lb_policy = "",
301                                 ms_host = "", ms_path = "";
302                 List<KeyVaulePair> ms_metadata = new ArrayList<KeyVaulePair>();
303
304                 List<String> nodeLabels = new ArrayList<String>();
305                 Map<String, String> labelMap = new HashMap<String, String>();
306
307                 NodeInfo node = new NodeInfo();
308
309                 node.setIp(service.getAddress());
310                 node.setPort(String.valueOf(service.getPort()));
311                 node.setNodeId(service.getId());
312
313
314
315                 try {
316
317                     for (String tag : tagList) {
318
319
320                         if (tag.startsWith("\"base\"")) {
321                             String ms_base_json = tag.split("\"base\":")[1];
322
323                             Map<String, String> baseMap =
324                                             (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_base_json, Map.class);
325                             ms_url = (baseMap.get("url") == null ? "" : baseMap.get("url"));
326                             ms_version = (baseMap.get("version") == null ? "" : baseMap.get("version"));
327                             ms_protocol = (baseMap.get("protocol") == null ? "" : baseMap.get("protocol"));
328                             ms_status = (baseMap.get("status") == null ? "1" : baseMap.get("status"));
329
330                             if (baseMap.get("publish_port") != null) {
331                                 ms_publish_port = (baseMap.get("publish_port"));
332                             }
333
334                             if (baseMap.get("is_manual") != null) {
335                                 ms_is_manual = baseMap.get("is_manual");
336
337                             }
338
339                             if (baseMap.get("ha_role") != null) {
340                                 node.setHa_role(baseMap.get("ha_role"));
341                             }
342
343                             if (baseMap.get("host") != null) {
344                                 ms_host = baseMap.get("host");
345                             }
346
347                             if (baseMap.get("path") != null) {
348                                 ms_path = baseMap.get("path");
349                             }
350
351                             continue;
352                         }
353
354                         if (tag.startsWith("\"labels\"")) {
355                             String ms_labels_json = "{" + tag.split("\"labels\":\\{")[1];
356                             labelMap = (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_labels_json, Map.class);
357
358
359
360                             for (Map.Entry<String, String> labelEntry : labelMap.entrySet()) {
361                                 if ("visualRange".equals(labelEntry.getKey())) {
362                                     ms_visualRange = labelEntry.getValue();
363                                 } else if ("network_plane_type".equals(labelEntry.getKey())) {
364                                     ms_network_plane_type = labelEntry.getValue();
365                                 } else {
366                                     nodeLabels.add(labelEntry.getKey() + ":" + labelEntry.getValue());
367                                 }
368
369                             }
370
371
372                             continue;
373                         }
374
375                         if (tag.startsWith("\"ns\"")) {
376                             String ms_namespace_json = tag.split("\"ns\":")[1];
377                             Map<String, String> namespaceMap = (Map<String, String>) JacksonJsonUtil
378                                             .jsonToBean(ms_namespace_json, Map.class);
379
380                             if (namespaceMap.get("namespace") != null) {
381                                 nodeNamespace = namespaceMap.get("namespace");
382                             } else {
383                                 nodeNamespace = "";
384                             }
385
386                             continue;
387                         }
388
389                         if (tag.startsWith("\"lb\"")) {
390                             String ms_lb_json = tag.split("\"lb\":")[1];
391                             Map<String, String> lbMap =
392                                             (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_lb_json, Map.class);
393
394                             if (lbMap.get("lb_policy") != null) {
395                                 ms_lb_policy = lbMap.get("lb_policy");
396                                 if (ms_lb_policy.startsWith("hash") || ms_lb_policy.equals("ip_hash")) {
397                                     ms_lb_policy = "ip_hash";
398                                 }
399
400                             }
401
402                             if (lbMap.get("lb_server_params") != null) {
403                                 node.setLb_server_params(lbMap.get("lb_server_params").replace(" ", ","));
404                             }
405
406                             continue;
407                         }
408
409                         if (tag.startsWith("\"checks\"")) {
410                             String ms_check_json = tag.split("\"checks\":")[1];
411                             Map<String, String> checkMap =
412                                             (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_check_json, Map.class);
413
414
415                             // 自动注册健康检查
416                             if (StringUtils.isNotBlank(checkMap.get("ttl"))) {
417                                 node.setCheckType("TTL");
418                                 node.setTtl(checkMap.get("ttl"));
419                             } else if (StringUtils.isNotBlank(checkMap.get("http"))) {
420                                 node.setCheckType("HTTP");
421                                 node.setCheckUrl(checkMap.get("http"));
422                                 if (checkMap.get("interval") != null)
423                                     node.setCheckInterval(checkMap.get("interval"));
424                                 if (checkMap.get("timeout") != null)
425                                     node.setCheckTimeOut(checkMap.get("timeout"));
426                             } else if (StringUtils.isNotBlank(checkMap.get("tcp"))) {
427                                 node.setCheckType("TCP");
428                                 node.setCheckUrl(checkMap.get("tcp"));
429                                 if (checkMap.get("interval") != null)
430                                     node.setCheckInterval(checkMap.get("interval"));
431                                 if (checkMap.get("timeout") != null)
432                                     node.setCheckTimeOut(checkMap.get("timeout"));
433                             }
434
435                             continue;
436                         }
437
438                         if (tag.startsWith("\"metadata\"")) {
439                             String ms_metadata_json = "{" + tag.split("\"metadata\":\\{")[1];
440                             Map<String, String> metadataMap = (Map<String, String>) JacksonJsonUtil
441                                             .jsonToBean(ms_metadata_json, Map.class);
442
443
444
445                             for (Map.Entry<String, String> entry : metadataMap.entrySet()) {
446                                 KeyVaulePair keyVaulePair = new KeyVaulePair();
447                                 keyVaulePair.setKey(entry.getKey());
448                                 keyVaulePair.setValue(entry.getValue());
449                                 ms_metadata.add(keyVaulePair);
450                             }
451
452                             continue;
453                         }
454
455
456
457                     }
458
459                 } catch (Exception e) {
460                     LOGGER.error(serviceName + " read tag  throw exception", e);
461                 }
462
463                 // 健康检查信息
464                 List<Check> checks = healthService.getChecks();
465                 node.setStatus("passing");
466                 for (Check check : checks) {
467                     if (!"passing".equals(check.getStatus())) {
468                         node.setStatus(check.getStatus());
469                         break;
470                     }
471                 }
472
473                 if (!ms_version.equals(version)) {
474                     continue;
475                 }
476
477                 // namespace过滤
478                 if (!namespace.equals(nodeNamespace)) {
479                     continue;
480                 }
481
482                 // 标签过滤
483                 if (islabelQuery) {
484                     boolean ifMatchLabel = false;
485                     for (Map.Entry<String, String> query_entry : query_labelMap.entrySet()) {
486                         String key = query_entry.getKey();
487                         String value = query_entry.getValue();
488                         if (StringUtils.isBlank(labelMap.get(key))) {
489                             continue;
490                         }
491
492                         String[] queryTagArray = StringUtils.split(value, "|");
493                         String[] serviceTagArray = StringUtils.split(labelMap.get(key), "|");
494                         if (DiscoverUtil.contain(queryTagArray, serviceTagArray)) {
495                             ifMatchLabel = true;
496                             break;
497                         }
498
499                     }
500
501                     if (!ifMatchLabel) {
502                         continue;
503                     }
504                 }
505
506
507                 nodes.add(node);
508                 serviceLabels.addAll(nodeLabels);
509                 serviceMetadatas.addAll(ms_metadata);
510
511                 String[] network_plane_array = StringUtils.split(ms_network_plane_type, "|");
512                 for (int i = 0; i < network_plane_array.length; i++) {
513                     serviceNetworkPlane.add(network_plane_array[i]);
514                 }
515
516
517
518                 microServiceInfo.setServiceName(serviceName);
519                 microServiceInfo.setUrl(ms_url);
520                 microServiceInfo.setVersion(ms_version);
521                 microServiceInfo.setProtocol(ms_protocol);
522                 microServiceInfo.setStatus(ms_status);
523                 microServiceInfo.setPublish_port(ms_publish_port);
524                 microServiceInfo.setIs_manual(Boolean.parseBoolean(ms_is_manual));
525                 microServiceInfo.setVisualRange(ms_visualRange);
526
527                 microServiceInfo.setLb_policy(ms_lb_policy);
528                 microServiceInfo.setNamespace(namespace);
529                 microServiceInfo.setHost(ms_host);
530                 microServiceInfo.setPath(ms_path);
531             }
532
533
534             if (nodes.isEmpty()) {
535
536
537                 String errInfo = "microservice not found: serviceName-" + serviceName + ",version-" + version
538                                 + ",namespace-" + namespace + ",labels-" + labels;
539                 throw new ExtendedNotFoundException(errInfo);
540
541             }
542
543
544
545             microServiceInfo.setLabels(new ArrayList<String>(serviceLabels));
546             microServiceInfo.setMetadata(new ArrayList<KeyVaulePair>(serviceMetadatas));
547             microServiceInfo.setNodes(nodes);
548             microServiceInfo.setNetwork_plane_type(StringUtils.join(serviceNetworkPlane.toArray(), "|"));
549
550
551
552             return new ConsulResponse(microServiceInfo, consulResponse.getIndex());
553
554
555         } catch (ExtendedNotFoundException e) {
556             throw e;
557         } catch (Exception e) {
558             throw new ExtendedInternalServerErrorException(e.getMessage());
559         }
560
561
562     }
563
564
565
566     public MicroServiceFullInfo updateMicroServiceInstance(String serviceName, String version, String namespace,
567                     MicroServiceInfo microServiceInfo, String requestIP, boolean is_manual) {
568         // 数据格式效验
569         checkMicroServiceInfo(microServiceInfo);
570         deleteMicroService(serviceName, version, namespace);
571         return saveMicroServiceInstance(microServiceInfo, true, requestIP, is_manual);
572     }
573
574     /**
575      * Title: saveMicroServiceInstance Description: 保存服务信息
576      * 
577      * @param microServiceInfo
578      * @param createOrUpdate true:添加或追加更新 false:覆盖
579      * @param requestIP 访问请求IP地址
580      * @return
581      * @see com.zte.ums.nfv.eco.hsif.msb.core.IMSBService#saveMicroServiceInstance(org.onap.msb.sdclient.core.MicroServiceInfo,
582      *      boolean, java.lang.String)
583      */
584     public MicroServiceFullInfo saveMicroServiceInstance(MicroServiceInfo microServiceInfo, boolean createOrUpdate,
585                     String requestIP, boolean is_manual) {
586
587         // 数据格式效验
588         checkMicroServiceInfo(microServiceInfo);
589
590         String serviceName = microServiceInfo.getServiceName().trim();
591
592         if (createOrUpdate == false) {
593             // 覆盖原记录,先删除后添加
594             try {
595                 deleteMicroService(microServiceInfo.getServiceName(), microServiceInfo.getVersion(),
596                                 microServiceInfo.getNamespace());
597             } catch (ExtendedNotFoundException e) {
598                 String errInfo = "microservice not found: serviceName-" + microServiceInfo.getServiceName()
599                                 + ",version-" + microServiceInfo.getVersion() + " ,namespace-"
600                                 + microServiceInfo.getNamespace();
601                 LOGGER.warn(errInfo);
602             }
603
604
605
606         }
607
608         Set<Node> nodes = microServiceInfo.getNodes();
609         String[] visualRangeArray = StringUtils.split(microServiceInfo.getVisualRange(), "|");
610
611         try {
612
613             for (Node node : nodes) {
614                 AgentService agentService = new AgentService();
615
616                 if (StringUtils.isBlank(node.getIp())) {
617                     node.setIp(requestIP);
618                 }
619
620                 String serverId = microServiceInfo.getNamespace() + "_" + serviceName + "_" + node.getIp() + "_"
621                                 + node.getPort();
622
623
624                 List<String> tags = new ArrayList<String>();
625
626                 Map<String, String> baseMap = new HashMap<String, String>();
627                 Map<String, String> lbMap = new HashMap<String, String>();
628                 Map<String, String> labelMap = new HashMap<String, String>();
629                 Map<String, String> metadataMap = new HashMap<String, String>();
630                 Map<String, String> checkMap = new HashMap<String, String>();
631                 Map<String, String> nsMap = new HashMap<String, String>();
632                 // Map<String, String> nodeMap = new HashMap<String, String>();
633
634                 baseMap.put("url", microServiceInfo.getUrl());
635                 baseMap.put("protocol", microServiceInfo.getProtocol());
636                 baseMap.put("version", microServiceInfo.getVersion());
637
638                 baseMap.put("status", "1");
639                 baseMap.put("is_manual", Boolean.toString(is_manual));
640
641                 // TCP和UDP协议保存 nginx端口和负载均衡策略
642                 if (StringUtils.isNotBlank(microServiceInfo.getPublish_port())) {
643                     baseMap.put("publish_port", microServiceInfo.getPublish_port());
644                 }
645                 String lb_policy = microServiceInfo.getLb_policy();
646
647                 // 保存服务的负载均衡策略
648                 if (StringUtils.isNotBlank(lb_policy)) {
649                     switch (lb_policy) {
650                         case "round-robin":
651                             break;
652                         case "ip_hash":
653                             if ("TCP".equals(microServiceInfo.getProtocol())
654                                             || "UDP".equals(microServiceInfo.getProtocol())) {
655                                 lbMap.put("lb_policy", "hash $remote_addr");
656                             } else {
657                                 lbMap.put("lb_policy", "ip_hash");
658                             }
659                             break;
660                         default:
661                             lbMap.put("lb_policy", lb_policy);
662                             break;
663                     }
664
665                 }
666
667                 if (StringUtils.isNotBlank(node.getLb_server_params())) {
668                     lbMap.put("lb_server_params", node.getLb_server_params().trim().replace(",", " "));
669
670                 }
671
672                 if (StringUtils.isNotBlank(node.getHa_role())) {
673                     baseMap.put("ha_role", node.getHa_role());
674                 }
675
676                 if (StringUtils.isNotBlank(microServiceInfo.getHost())) {
677                     baseMap.put("host", microServiceInfo.getHost().toLowerCase());
678                 }
679
680                 if (StringUtils.isNotBlank(microServiceInfo.getPath())) {
681                     baseMap.put("path", microServiceInfo.getPath());
682                 }
683
684                 // 保存健康检查参数
685                 if (StringUtils.isNotBlank(node.getCheckType())) {
686
687                     AgentService.Check check = agentService.createCheck();
688
689                     if ("TTL".equals(node.getCheckType())) {
690                         check.setTtl(node.getTtl());
691                         checkMap.put("ttl", node.getTtl());
692
693                     } else if ("HTTP".equals(node.getCheckType())) {
694                         check.setInterval(node.getCheckInterval());
695                         check.setHttp(node.getCheckUrl());
696                         check.setTimeout(node.getCheckTimeOut());
697
698                         checkMap.put("http", node.getCheckUrl());
699                         checkMap.put("interval", node.getCheckInterval());
700                         checkMap.put("timeout", node.getCheckTimeOut());
701                     } else if ("TCP".equals(node.getCheckType())) {
702                         check.setInterval(node.getCheckInterval());
703                         check.setTcp(node.getCheckUrl());
704                         check.setTimeout(node.getCheckTimeOut());
705
706                         checkMap.put("tcp", node.getCheckUrl());
707                         checkMap.put("interval", node.getCheckInterval());
708                         checkMap.put("timeout", node.getCheckTimeOut());
709                     }
710
711                     agentService.setCheck(check);
712                 }
713
714
715                 List<KeyVaulePair> keyVaulePairs = microServiceInfo.getMetadata();
716
717                 if (keyVaulePairs != null && keyVaulePairs.size() > 0) {
718                     for (KeyVaulePair keyVaulePair : keyVaulePairs) {
719                         metadataMap.put(keyVaulePair.getKey(), keyVaulePair.getValue());
720                     }
721                 }
722
723                 // 同步过滤参数组合为json格式存储
724                 labelMap.put("visualRange", StringUtils.join(visualRangeArray, "|"));
725
726                 if (StringUtils.isNotBlank(microServiceInfo.getNetwork_plane_type())) {
727                     labelMap.put("network_plane_type", microServiceInfo.getNetwork_plane_type());
728                 }
729                 if (microServiceInfo.getLabels() != null) {
730                     for (String label : microServiceInfo.getLabels()) {
731                         String[] labelArray = StringUtils.split(label, ":");
732                         if (labelArray.length == 2) {
733                             labelMap.put(labelArray[0], labelArray[1]);
734                         }
735                     }
736                 }
737
738                 if (StringUtils.isNotBlank(microServiceInfo.getNamespace())) {
739                     nsMap.put("namespace", microServiceInfo.getNamespace());
740                 }
741
742
743
744                 tags.add("\"base\":" + JacksonJsonUtil.beanToJson(baseMap));
745                 if (!lbMap.isEmpty())
746                     tags.add("\"lb\":" + JacksonJsonUtil.beanToJson(lbMap));
747                 if (!checkMap.isEmpty())
748                     tags.add("\"checks\":" + JacksonJsonUtil.beanToJson(checkMap));
749                 if (!labelMap.isEmpty())
750                     tags.add("\"labels\":" + JacksonJsonUtil.beanToJson(labelMap));
751                 if (!metadataMap.isEmpty())
752                     tags.add("\"metadata\":" + JacksonJsonUtil.beanToJson(metadataMap));
753                 if (!nsMap.isEmpty())
754                     tags.add("\"ns\":" + JacksonJsonUtil.beanToJson(nsMap));
755
756                 agentService.setTags(tags);
757
758                 agentService.setAddress(node.getIp());
759                 agentService.setId(serverId);
760                 agentService.setPort(Integer.parseInt(node.getPort()));
761
762                 String consul_serviceName = getServiceName4Consul(serviceName, microServiceInfo.getNamespace());
763
764
765                 agentService.setName(consul_serviceName);
766
767                 int registerResult;
768                 if (DiscoverUtil.CONSUL_REGISTER_MODE.equals(ConfigUtil.getInstance().getConsulRegisterMode())) {
769                     registerResult = ConsulCatalogServiceWrapper.getInstance().saveService(agentService);
770                 } else {
771                     registerResult = ConsulAgentServiceWrapper.getInstance().saveService(agentService);
772                 }
773
774                 if (registerResult != 200) {
775                     throw new Exception("register consul service fail:" + registerResult);
776                 }
777
778
779
780             }
781
782             LOGGER.info("save microservice success: serviceName-" + microServiceInfo.getServiceName() + ",version-"
783                             + microServiceInfo.getVersion() + " ,namespace-" + microServiceInfo.getNamespace());
784
785             return getMicroServiceInstance(serviceName, microServiceInfo.getVersion(), microServiceInfo.getNamespace());
786
787
788         } catch (ExtendedNotFoundException e) {
789             throw e;
790         } catch (Exception e) {
791             LOGGER.error("register consul service throw exception", e);
792             throw new ExtendedInternalServerErrorException(e.getMessage());
793
794         }
795
796
797
798     }
799
800     public MicroServiceFullInfo getMicroServiceInstance(String serviceName, String version, String namespace) {
801         ConsulResponse serviceResponse = getMicroServiceInstance(serviceName, version, false, "", "", "", namespace);
802         return (MicroServiceFullInfo) serviceResponse.getResponse();
803     }
804
805
806     /**
807      * Title: deleteMicroService Description: 删除服务信息
808      * 
809      * @param serviceName
810      * @param version
811      * @see com.zte.ums.nfv.eco.hsif.msb.core.IMSBService#deleteMicroService(java.lang.String,
812      *      java.lang.String)
813      */
814     public void deleteMicroService(String serviceName, String version, String namespace) {
815
816
817         if ("null".equals(version)) {
818             version = "";
819         }
820
821         checkServiceNameAndVersion(serviceName, version);
822
823
824         String consul_serviceName = getServiceName4Consul(serviceName, namespace);
825
826         List<CatalogService> catalogServiceList = getConsulServices(consul_serviceName, version);
827
828
829         if (catalogServiceList == null || catalogServiceList.size() == 0) {
830             String errInfo = "microservice not found: serviceName-" + serviceName + ",version-" + version
831                             + " ,namespace-" + namespace;
832             throw new ExtendedNotFoundException(errInfo);
833
834         }
835
836         boolean ifFindServiceForNS = false;
837
838         for (CatalogService catalogService : catalogServiceList) {
839
840             List<String> tagList = catalogService.getServiceTags();
841             String serviceNamespace = "", serviceVersion = "";
842             try {
843
844                 for (String tag : tagList) {
845
846                     if (tag.startsWith("\"ns\"")) {
847                         String ms_ns_json = tag.split("\"ns\":")[1];
848
849
850                         Map<String, String> nsMap =
851                                         (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_ns_json, Map.class);
852                         if (nsMap.get("namespace") != null) {
853                             serviceNamespace = nsMap.get("namespace");
854                         }
855
856                         continue;
857                     }
858
859                     if (tag.startsWith("\"base\"")) {
860                         String ms_base_json = tag.split("\"base\":")[1];
861
862
863                         Map<String, String> baseMap =
864                                         (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_base_json, Map.class);
865                         if (baseMap.get("version") != null) {
866                             serviceVersion = baseMap.get("version");
867                         }
868
869
870                         continue;
871
872                     }
873
874                 }
875
876             } catch (Exception e) {
877                 LOGGER.error(serviceName + " read tag  throw exception", e);
878             }
879
880             if (!serviceNamespace.equals(namespace)) {
881                 continue;
882             }
883
884             if (!serviceVersion.equals(version)) {
885                 continue;
886             }
887             ifFindServiceForNS = true;
888             String serviceID = catalogService.getServiceId();
889             try {
890
891                 int delResult;
892                 if (DiscoverUtil.CONSUL_REGISTER_MODE.equals(ConfigUtil.getInstance().getConsulRegisterMode())) {
893                     delResult = ConsulCatalogServiceWrapper.getInstance().deleteService(serviceID);
894                 } else {
895                     delResult = ConsulAgentServiceWrapper.getInstance().deleteService(serviceID);
896                 }
897
898                 if (delResult != 200) {
899                     throw new Exception("delete consul service fail:" + delResult);
900                 }
901
902
903             } catch (Exception e) {
904                 LOGGER.error("delete consul service throw exception", e);
905                 throw new ExtendedInternalServerErrorException(e.getMessage());
906
907             }
908
909         }
910
911
912         if (!ifFindServiceForNS) {
913             String errInfo = "microservice not found: serviceName-" + serviceName + ",version-" + version
914                             + ",namespace-" + namespace;
915             throw new ExtendedNotFoundException(errInfo);
916         }
917
918         LOGGER.info("microservice delete success: serviceName-" + serviceName + ",version-" + version + ",namespace-"
919                         + namespace);
920
921     }
922
923     /**
924      * Title: deleteMicroServiceInstance Description: 刪除服务的节点信息
925      * 
926      * @param serviceName
927      * @param version
928      * @param ip
929      * @param port
930      * @see com.zte.ums.nfv.eco.hsif.msb.core.IMSBService#deleteMicroServiceInstance(java.lang.String,
931      *      java.lang.String, java.lang.String, java.lang.String)
932      */
933     public void deleteMicroServiceInstance(String serviceName, String version, String namespace, String ip,
934                     String port) {
935         if ("null".equals(version)) {
936             version = "";
937         }
938
939         checkServiceNameAndVersion(serviceName, version);
940
941
942         if (!RegExpTestUtil.ipRegExpTest(ip)) {
943             throw new UnprocessableEntityException(
944                             "delete MicroServiceInfo FAIL:IP(" + ip + ")is not a valid IP address");
945         }
946
947         if (!RegExpTestUtil.portRegExpTest(port)) {
948             throw new UnprocessableEntityException(
949                             "delete MicroServiceInfo FAIL:Port(" + port + ")is not a valid Port address");
950         }
951
952         String consul_serviceName = getServiceName4Consul(serviceName, namespace);
953
954         List<CatalogService> catalogServiceList = getConsulServices(consul_serviceName, version);
955
956
957         if (catalogServiceList == null || catalogServiceList.size() == 0) {
958             String errInfo = "microservice not found: serviceName-" + serviceName + ",version-" + version;
959             LOGGER.warn(errInfo);
960             throw new ExtendedNotFoundException(errInfo);
961
962         }
963
964         String node = "", serviceID = "";
965         boolean ifFindBNode = false;
966
967
968         for (CatalogService catalogService : catalogServiceList) {
969
970             String serviceAddress = catalogService.getServiceAddress();
971             String servicePort = String.valueOf(catalogService.getServicePort());
972
973
974
975             List<String> tagList = catalogService.getServiceTags();
976             String ms_version = "", ms_namespace = "";
977             try {
978
979                 for (String tag : tagList) {
980
981                     if (tag.startsWith("\"base\"")) {
982                         String ms_base_json = tag.split("\"base\":")[1];
983
984
985
986                         Map<String, String> baseMap =
987                                         (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_base_json, Map.class);
988                         if (baseMap.get("version") != null) {
989                             ms_version = baseMap.get("version");
990                         }
991
992
993                     }
994
995                     if (tag.startsWith("\"ns\"")) {
996                         String ms_ns_json = tag.split("\"ns\":")[1];
997
998
999
1000                         Map<String, String> nsMap =
1001                                         (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_ns_json, Map.class);
1002                         if (nsMap.get("namespace") != null) {
1003                             ms_namespace = nsMap.get("namespace");
1004                         }
1005
1006
1007                     }
1008                 }
1009
1010             } catch (Exception e) {
1011                 LOGGER.error(serviceName + " read tag  throw exception", e);
1012             }
1013             if (serviceAddress.equals(ip) && servicePort.equals(port) && ms_version.equals(version)
1014                             && ms_namespace.equals(namespace)) {
1015                 node = catalogService.getNode();
1016                 serviceID = catalogService.getServiceId();
1017                 ifFindBNode = true;
1018                 break;
1019             }
1020
1021
1022         }
1023
1024         if (!ifFindBNode) {
1025             throw new ExtendedNotFoundException("delete MicroServiceInfo FAIL: node-" + ip + ":" + port + " namespace-"
1026                             + namespace + " not found ");
1027         }
1028
1029
1030
1031         try {
1032             int delResult;
1033             if (DiscoverUtil.CONSUL_REGISTER_MODE.equals(ConfigUtil.getInstance().getConsulRegisterMode())) {
1034                 delResult = ConsulCatalogServiceWrapper.getInstance().deleteService(serviceID);
1035             } else {
1036                 delResult = ConsulAgentServiceWrapper.getInstance().deleteService(serviceID);
1037             }
1038
1039             if (delResult != 200) {
1040                 throw new Exception("delete consul service fail:" + delResult);
1041             }
1042
1043
1044         } catch (Exception e) {
1045             LOGGER.error("delete consul service throw exception", e);
1046             throw new ExtendedInternalServerErrorException(e.getMessage());
1047
1048         }
1049
1050     }
1051
1052     /**
1053      * @Title getConsulServices
1054      * @Description TODO(通过方法:根据服务名\版本号获取consul服务信息)
1055      * @param serviceName
1056      * @return
1057      * @return List<CatalogService>
1058      */
1059     private List<CatalogService> getConsulServices(String serviceName, String version) {
1060         // serviceName = serviceName.replace("/", "*");
1061         String consulServiceUrl = (new StringBuilder().append("http://")
1062                         .append(ConfigUtil.getInstance().getConsulAddress()).append(DiscoverUtil.CONSUL_CATALOG_URL)
1063                         .append("/service/").append(serviceName)).toString();
1064
1065         String resultJson = HttpClientUtil.httpGet(consulServiceUrl);
1066         List<CatalogService> catalogServiceList = (List<CatalogService>) JacksonJsonUtil.jsonToListBean(resultJson);
1067
1068         for (CatalogService catalogService : catalogServiceList) {
1069
1070
1071             List<String> tagList = catalogService.getServiceTags();
1072             String ms_version = "";
1073             try {
1074                 for (String tag : tagList) {
1075
1076                     if (tag.startsWith("\"base\"")) {
1077                         String ms_base_json = tag.split("\"base\":")[1];
1078
1079
1080                         Map<String, String> baseMap =
1081                                         (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_base_json, Map.class);
1082                         if (baseMap.get("version") != null) {
1083                             ms_version = baseMap.get("version");
1084                         }
1085
1086                         break;
1087
1088                     }
1089                 }
1090             } catch (Exception e) {
1091                 LOGGER.error(serviceName + " read tag  throw exception", e);
1092             }
1093             if (!ms_version.equals(version)) {
1094                 catalogServiceList.remove(catalogService);
1095                 break;
1096             }
1097
1098
1099         }
1100         return catalogServiceList;
1101     }
1102
1103     /**
1104      * @Title getHealthServices
1105      * @Description TODO(通过方法:根据服务名获取consul服务健康检查信息)
1106      * @param serviceName
1107      * @return List<HealthService>
1108      */
1109     private ConsulResponse getHealthServices(String serviceName, boolean ifPassStatus, String wait, String index) {
1110         // serviceName = serviceName.replace("/", "*");
1111         StringBuilder healthServiceUrlBuilder =
1112                         new StringBuilder().append("http://").append(ConfigUtil.getInstance().getConsulAddress())
1113                                         .append(DiscoverUtil.CONSUL_HEALTH_URL).append(serviceName);
1114
1115         if (ifPassStatus) {
1116             healthServiceUrlBuilder.append("?passing");
1117         }
1118
1119         if (StringUtils.isNotBlank(wait) && StringUtils.isNotBlank(index)) {
1120             if (ifPassStatus) {
1121                 healthServiceUrlBuilder.append("&wait=").append(wait).append("&index=").append(index);
1122             } else {
1123                 healthServiceUrlBuilder.append("?wait=").append(wait).append("&index=").append(index);
1124             }
1125         }
1126
1127         return HttpClientUtil.httpWaitGet(healthServiceUrlBuilder.toString());
1128
1129     }
1130
1131
1132
1133     public void healthCheckbyTTL(String serviceName, String version, String namespace, NodeAddress checkNode) {
1134         // TODO Auto-generated method stub
1135         if ("null".equals(version)) {
1136             version = "";
1137         }
1138
1139         checkServiceNameAndVersion(serviceName, version);
1140
1141
1142         if (!RegExpTestUtil.ipRegExpTest(checkNode.getIp())) {
1143             throw new UnprocessableEntityException(
1144                             "healthCheck by TTL FAIL:IP(" + checkNode.getIp() + ")is not a valid IP address");
1145         }
1146
1147         if (!RegExpTestUtil.portRegExpTest(checkNode.getPort())) {
1148             throw new UnprocessableEntityException(
1149                             "healthCheck by TTL FAIL:Port(" + checkNode.getPort() + ")is not a valid Port address");
1150         }
1151
1152         String consul_serviceName = getServiceName4Consul(serviceName, namespace);
1153
1154         List<CatalogService> catalogServiceList = getConsulServices(consul_serviceName, version);
1155
1156
1157         if (catalogServiceList == null || catalogServiceList.size() == 0) {
1158             String errInfo = "microservice not found: serviceName-" + serviceName + ",version-" + version;
1159             LOGGER.warn(errInfo);
1160             throw new ExtendedNotFoundException(errInfo);
1161
1162         }
1163
1164
1165         boolean ifFindBNode = false;
1166
1167
1168         for (CatalogService catalogService : catalogServiceList) {
1169
1170             String serviceAddress = catalogService.getServiceAddress();
1171             String servicePort = String.valueOf(catalogService.getServicePort());
1172             boolean ifttlCheck = false;
1173
1174
1175             List<String> tagList = catalogService.getServiceTags();
1176             String ms_version = "", ms_namespace = "";
1177             try {
1178
1179                 for (String tag : tagList) {
1180
1181                     if (tag.startsWith("\"base\"")) {
1182                         String ms_base_json = tag.split("\"base\":")[1];
1183
1184                         Map<String, String> baseMap =
1185                                         (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_base_json, Map.class);
1186                         if (baseMap.get("version") != null) {
1187                             ms_version = baseMap.get("version");
1188                         }
1189                     }
1190
1191                     if (tag.startsWith("\"ns\"")) {
1192                         String ms_ns_json = tag.split("\"ns\":")[1];
1193
1194                         Map<String, String> nsMap =
1195                                         (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_ns_json, Map.class);
1196                         if (nsMap.get("namespace") != null) {
1197                             ms_namespace = nsMap.get("namespace");
1198                         }
1199                     }
1200
1201
1202                     if (tag.startsWith("\"checks\"")) {
1203                         String ms_check_json = tag.split("\"checks\":")[1];
1204                         Map<String, String> checkMap =
1205                                         (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_check_json, Map.class);
1206
1207                         // 自动注册健康检查
1208                         if (StringUtils.isNotBlank(checkMap.get("ttl"))) {
1209                             ifttlCheck = true;
1210                         }
1211                     }
1212                 }
1213
1214             } catch (Exception e) {
1215                 LOGGER.error(serviceName + " read tag  throw exception", e);
1216             }
1217
1218
1219
1220             if (serviceAddress.equals(checkNode.getIp()) && servicePort.equals(checkNode.getPort())
1221                             && ms_version.equals(version) && ms_namespace.equals(namespace)) {
1222                 if (!ifttlCheck) {
1223                     throw new ExtendedNotFoundException(
1224                                     "healthCheck by TTL FAIL: Service is not enabled TTL health check ");
1225                 }
1226                 ifFindBNode = true;
1227                 break;
1228             }
1229
1230
1231         }
1232
1233
1234
1235         if (!ifFindBNode) {
1236             throw new ExtendedNotFoundException("healthCheck by TTL FAIL: node-" + checkNode.getIp() + ":"
1237                             + checkNode.getPort() + " namespace-" + namespace + " not found ");
1238         }
1239
1240
1241
1242         try {
1243             String checkID = (new StringBuilder().append("service:").append(namespace).append("_").append(serviceName)
1244                             .append("_").append(checkNode.getIp()).append("_").append(checkNode.getPort())).toString();
1245
1246             String consulServiceUrl =
1247                             (new StringBuilder().append("http://").append(ConfigUtil.getInstance().getConsulAddress())
1248                                             .append(DiscoverUtil.CONSUL_AGENT_TTL_URL).append(checkID)).toString();
1249
1250             String result = HttpClientUtil.httpGet(consulServiceUrl);
1251             if ("CheckID does not have associated TTL".equals(result)) {
1252                 throw new ExtendedNotFoundException(
1253                                 "healthCheck by TTL FAIL: Service is not enabled TTL health check ");
1254             }
1255
1256         } catch (ExtendedInternalServerErrorException e) {
1257             throw e;
1258         } catch (Exception e) {
1259             throw new ExtendedInternalServerErrorException("healthCheck by TTL FAIL:" + e.getMessage());
1260         }
1261
1262
1263
1264     }
1265
1266     // public MicroServiceFullInfo getApigatewayServiceInfo4Host(String namespace){
1267     // return getMicroServiceInstance(DiscoverUtil.APIGATEWAY_SERVINCE, "v1", namespace);
1268     // }
1269     //
1270
1271
1272
1273     public List<MicroServiceFullInfo> getMicroServiceForNodes(String serviceName, String version, boolean ifPassStatus,
1274                     String labels, String namespace) {
1275         // TODO Auto-generated method stub
1276         if ("null".equals(version)) {
1277             version = "";
1278         }
1279
1280         checkServiceNameAndVersion(serviceName, version);
1281
1282         if (!RegExpTestUtil.labelRegExpTest(labels)) {
1283             throw new UnprocessableEntityException(
1284                             "get MicroServiceInfo FAIL: The label query parameter format is wrong (key:value)");
1285         }
1286
1287
1288         String consul_serviceName = getServiceName4Consul(serviceName, namespace);
1289
1290         ConsulResponse consulResponse = getHealthServices(consul_serviceName, ifPassStatus, "", "");
1291         if (consulResponse == null) {
1292             String errInfo = "microservice not found: serviceName-" + serviceName;
1293             throw new ExtendedNotFoundException(errInfo);
1294         }
1295         String resultJson = (String) consulResponse.getResponse();
1296         List<HealthService> healthServiceList =
1297                         JacksonJsonUtil.jsonToListBean(resultJson, new TypeReference<List<HealthService>>() {});
1298
1299
1300         if (healthServiceList == null || healthServiceList.size() == 0) {
1301             String errInfo = "microservice not found: serviceName-" + serviceName;
1302             throw new ExtendedNotFoundException(errInfo);
1303         }
1304
1305         try {
1306
1307             // label query,format key:value|value2,key2:value2
1308             boolean islabelQuery = false;
1309             Map<String, String> query_labelMap = new HashMap<String, String>();
1310             if (StringUtils.isNotBlank(labels)) {
1311                 islabelQuery = true;
1312                 String[] routeLabels = StringUtils.split(labels, ",");
1313
1314                 for (int i = 0; i < routeLabels.length; i++) {
1315                     String[] labelArray = StringUtils.split(routeLabels[i], ":");
1316                     query_labelMap.put(labelArray[0], labelArray[1]);
1317                 }
1318             }
1319
1320             List<MicroServiceFullInfo> microServiceInfoList = new ArrayList<MicroServiceFullInfo>();
1321
1322
1323
1324             for (HealthService healthService : healthServiceList) {
1325
1326                 Set<NodeInfo> nodes = new HashSet<NodeInfo>();
1327                 Set<String> serviceLabels = new HashSet<String>();
1328                 String nodeNamespace = "";
1329                 MicroServiceFullInfo microServiceInfo = new MicroServiceFullInfo();
1330
1331                 Service service = healthService.getService();
1332                 List<String> tagList = service.getTags();
1333
1334                 String ms_url = "", ms_version = "", ms_protocol = "", ms_status = "", ms_publish_port = "",
1335                                 ms_is_manual = "", ms_visualRange = "1", ms_network_plane_type = "", ms_lb_policy = "",
1336                                 ms_host = "", ms_path = "";
1337                 List<KeyVaulePair> ms_metadata = new ArrayList<KeyVaulePair>();
1338
1339                 List<String> nodeLabels = new ArrayList<String>();
1340                 Map<String, String> labelMap = new HashMap<String, String>();
1341
1342                 NodeInfo node = new NodeInfo();
1343
1344                 node.setIp(service.getAddress());
1345                 node.setPort(String.valueOf(service.getPort()));
1346                 node.setNodeId(service.getId());
1347
1348
1349
1350                 try {
1351
1352                     for (String tag : tagList) {
1353
1354
1355                         if (tag.startsWith("\"base\"")) {
1356                             String ms_base_json = tag.split("\"base\":")[1];
1357
1358                             Map<String, String> baseMap =
1359                                             (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_base_json, Map.class);
1360                             ms_url = (baseMap.get("url") == null ? "" : baseMap.get("url"));
1361                             ms_version = (baseMap.get("version") == null ? "" : baseMap.get("version"));
1362                             ms_protocol = (baseMap.get("protocol") == null ? "" : baseMap.get("protocol"));
1363                             ms_status = (baseMap.get("status") == null ? "1" : baseMap.get("status"));
1364
1365                             if (baseMap.get("publish_port") != null) {
1366                                 ms_publish_port = (baseMap.get("publish_port"));
1367                             }
1368
1369                             if (baseMap.get("is_manual") != null) {
1370                                 ms_is_manual = baseMap.get("is_manual");
1371
1372                             }
1373
1374                             if (baseMap.get("ha_role") != null) {
1375                                 node.setHa_role(baseMap.get("ha_role"));
1376                             }
1377
1378                             if (baseMap.get("host") != null) {
1379                                 ms_host = baseMap.get("host");
1380                             }
1381
1382                             if (baseMap.get("path") != null) {
1383                                 ms_path = baseMap.get("path");
1384                             }
1385
1386                             continue;
1387                         }
1388
1389                         if (tag.startsWith("\"labels\"")) {
1390                             String ms_labels_json = "{" + tag.split("\"labels\":\\{")[1];
1391                             labelMap = (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_labels_json, Map.class);
1392
1393
1394
1395                             for (Map.Entry<String, String> labelEntry : labelMap.entrySet()) {
1396                                 if ("visualRange".equals(labelEntry.getKey())) {
1397                                     ms_visualRange = labelEntry.getValue();
1398                                 } else if ("network_plane_type".equals(labelEntry.getKey())) {
1399                                     ms_network_plane_type = labelEntry.getValue();
1400                                 } else {
1401                                     nodeLabels.add(labelEntry.getKey() + ":" + labelEntry.getValue());
1402                                 }
1403
1404                             }
1405
1406
1407                             continue;
1408                         }
1409
1410                         if (tag.startsWith("\"ns\"")) {
1411                             String ms_namespace_json = tag.split("\"ns\":")[1];
1412                             Map<String, String> namespaceMap = (Map<String, String>) JacksonJsonUtil
1413                                             .jsonToBean(ms_namespace_json, Map.class);
1414
1415                             if (namespaceMap.get("namespace") != null) {
1416                                 nodeNamespace = namespaceMap.get("namespace");
1417                             } else {
1418                                 nodeNamespace = "";
1419                             }
1420
1421                             continue;
1422                         }
1423
1424                         if (tag.startsWith("\"lb\"")) {
1425                             String ms_lb_json = tag.split("\"lb\":")[1];
1426                             Map<String, String> lbMap =
1427                                             (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_lb_json, Map.class);
1428
1429                             if (lbMap.get("lb_policy") != null) {
1430                                 ms_lb_policy = lbMap.get("lb_policy");
1431                                 if (ms_lb_policy.startsWith("hash") || ms_lb_policy.equals("ip_hash")) {
1432                                     ms_lb_policy = "ip_hash";
1433                                 }
1434
1435                             }
1436
1437                             if (lbMap.get("lb_server_params") != null) {
1438                                 node.setLb_server_params(lbMap.get("lb_server_params").replace(" ", ","));
1439                             }
1440
1441                             continue;
1442                         }
1443
1444                         if (tag.startsWith("\"checks\"")) {
1445                             String ms_check_json = tag.split("\"checks\":")[1];
1446                             Map<String, String> checkMap =
1447                                             (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_check_json, Map.class);
1448
1449
1450                             // 自动注册健康检查
1451                             if (StringUtils.isNotBlank(checkMap.get("ttl"))) {
1452                                 node.setCheckType("TTL");
1453                                 node.setTtl(checkMap.get("ttl"));
1454                             } else if (StringUtils.isNotBlank(checkMap.get("http"))) {
1455                                 node.setCheckType("HTTP");
1456                                 node.setCheckUrl(checkMap.get("http"));
1457                                 if (checkMap.get("interval") != null)
1458                                     node.setCheckInterval(checkMap.get("interval"));
1459                                 if (checkMap.get("timeout") != null)
1460                                     node.setCheckTimeOut(checkMap.get("timeout"));
1461                             } else if (StringUtils.isNotBlank(checkMap.get("tcp"))) {
1462                                 node.setCheckType("TCP");
1463                                 node.setCheckUrl(checkMap.get("tcp"));
1464                                 if (checkMap.get("interval") != null)
1465                                     node.setCheckInterval(checkMap.get("interval"));
1466                                 if (checkMap.get("timeout") != null)
1467                                     node.setCheckTimeOut(checkMap.get("timeout"));
1468                             }
1469
1470                             continue;
1471                         }
1472
1473                         if (tag.startsWith("\"metadata\"")) {
1474                             String ms_metadata_json = "{" + tag.split("\"metadata\":\\{")[1];
1475                             Map<String, String> metadataMap = (Map<String, String>) JacksonJsonUtil
1476                                             .jsonToBean(ms_metadata_json, Map.class);
1477
1478
1479
1480                             for (Map.Entry<String, String> entry : metadataMap.entrySet()) {
1481                                 KeyVaulePair keyVaulePair = new KeyVaulePair();
1482                                 keyVaulePair.setKey(entry.getKey());
1483                                 keyVaulePair.setValue(entry.getValue());
1484                                 ms_metadata.add(keyVaulePair);
1485                             }
1486
1487                             continue;
1488                         }
1489
1490
1491
1492                     }
1493
1494                 } catch (Exception e) {
1495                     LOGGER.error(serviceName + " read tag  throw exception", e);
1496                 }
1497
1498                 // 健康检查信息
1499                 List<Check> checks = healthService.getChecks();
1500                 node.setStatus("passing");
1501                 for (Check check : checks) {
1502                     if (!"passing".equals(check.getStatus())) {
1503                         node.setStatus(check.getStatus());
1504                         break;
1505                     }
1506                 }
1507
1508                 if (!ms_version.equals(version)) {
1509                     continue;
1510                 }
1511
1512                 // namespace过滤
1513                 if (!namespace.equals(nodeNamespace)) {
1514                     continue;
1515                 }
1516
1517                 // 标签过滤
1518                 if (islabelQuery) {
1519                     boolean ifMatchLabel = false;
1520                     for (Map.Entry<String, String> query_entry : query_labelMap.entrySet()) {
1521                         String key = query_entry.getKey();
1522                         String value = query_entry.getValue();
1523                         if (StringUtils.isBlank(labelMap.get(key))) {
1524                             continue;
1525                         }
1526
1527                         String[] queryTagArray = StringUtils.split(value, "|");
1528                         String[] serviceTagArray = StringUtils.split(labelMap.get(key), "|");
1529                         if (DiscoverUtil.contain(queryTagArray, serviceTagArray)) {
1530                             ifMatchLabel = true;
1531                             break;
1532                         }
1533
1534                     }
1535
1536                     if (!ifMatchLabel) {
1537                         continue;
1538                     }
1539                 }
1540
1541
1542                 nodes.add(node);
1543                 serviceLabels.addAll(nodeLabels);
1544
1545                 microServiceInfo.setServiceName(serviceName);
1546                 microServiceInfo.setUrl(ms_url);
1547                 microServiceInfo.setVersion(ms_version);
1548                 microServiceInfo.setProtocol(ms_protocol);
1549                 microServiceInfo.setStatus(null);
1550                 microServiceInfo.setPublish_port(ms_publish_port);
1551                 microServiceInfo.setIs_manual(Boolean.parseBoolean(ms_is_manual));
1552                 microServiceInfo.setVisualRange(ms_visualRange);
1553                 microServiceInfo.setNetwork_plane_type(ms_network_plane_type);
1554                 microServiceInfo.setLb_policy(ms_lb_policy);
1555                 microServiceInfo.setHost(ms_host);
1556                 microServiceInfo.setPath(ms_path);
1557
1558                 microServiceInfo.setMetadata(ms_metadata);
1559                 microServiceInfo.setNamespace(namespace);
1560                 microServiceInfo.setLabels(new ArrayList<String>(serviceLabels));
1561                 microServiceInfo.setNodes(nodes);
1562
1563                 microServiceInfoList.add(microServiceInfo);
1564             }
1565
1566
1567
1568             if (microServiceInfoList.size() == 0) {
1569                 String errInfo = "microservice not found: serviceName-" + serviceName + ",version-" + version
1570                                 + ",namespace-" + namespace + ",labels-" + labels;
1571                 throw new ExtendedNotFoundException(errInfo);
1572             }
1573
1574
1575
1576             return microServiceInfoList;
1577
1578
1579         } catch (ExtendedNotFoundException e) {
1580             throw e;
1581         } catch (Exception e) {
1582             throw new ExtendedInternalServerErrorException(e.getMessage());
1583         }
1584     }
1585
1586
1587     private String getServiceName4Consul(String serviceName, String namespace) {
1588         String consul_serviceName;
1589
1590         if (StringUtils.isNotBlank(namespace)) {
1591             // if (DiscoverUtil.APIGATEWAY_SERVINCE_DEFAULT.equals(namespace)) {
1592             // consul_serviceName=serviceName;
1593             // }else{
1594             consul_serviceName = serviceName + "-" + namespace;
1595             // }
1596         } else {
1597             consul_serviceName = serviceName;
1598         }
1599         return consul_serviceName;
1600     }
1601
1602
1603
1604     private void checkMicroServiceInfo(MicroServiceInfo microServiceInfo) {
1605
1606         if (StringUtils.isBlank(microServiceInfo.getServiceName())
1607                         || StringUtils.isBlank(microServiceInfo.getProtocol())) {
1608             throw new UnprocessableEntityException("register MicroServiceInfo FAIL: Some required fields are empty");
1609         }
1610
1611         if (microServiceInfo.getNodes() == null || microServiceInfo.getNodes().size() == 0) {
1612             throw new UnprocessableEntityException("register MicroServiceInfo FAIL: Nodes fields are empty");
1613         }
1614
1615         if (!RegExpTestUtil.serviceNameRegExpTest(microServiceInfo.getServiceName().trim())) {
1616             throw new UnprocessableEntityException("register MicroServiceInfo FAIL:ServiceName("
1617                             + microServiceInfo.getServiceName() + ")  format error");
1618         }
1619
1620         if (StringUtils.isNotBlank(microServiceInfo.getHost())) {
1621             if (!RegExpTestUtil.serviceNameRegExpTest(microServiceInfo.getHost().trim())) {
1622                 throw new UnprocessableEntityException(
1623                                 "register MicroServiceInfo host (" + microServiceInfo.getHost() + ")  format error");
1624             }
1625         }
1626
1627         if (StringUtils.isNotBlank(microServiceInfo.getLb_policy())) {
1628             if (!DiscoverUtil.checkExist(DiscoverUtil.LB_POLICY_LIST, microServiceInfo.getLb_policy().trim(), ",")) {
1629                 throw new UnprocessableEntityException("register MicroServiceInfo FAIL:lb_policy is wrong,value range:("
1630                                 + DiscoverUtil.LB_POLICY_LIST + ")");
1631             }
1632
1633         }
1634
1635         if (StringUtils.isNotBlank(microServiceInfo.getVersion())) {
1636             if (!RegExpTestUtil.versionRegExpTest(microServiceInfo.getVersion())) {
1637                 throw new UnprocessableEntityException("register MicroServiceInfo FAIL:version is not a valid  format");
1638
1639             }
1640         }
1641
1642
1643
1644         if (StringUtils.isNotBlank(microServiceInfo.getUrl())) {
1645
1646             String url = microServiceInfo.getUrl();
1647             if (!"/".equals(url)) {
1648                 if (!url.startsWith("/")) {
1649                     url = "/" + url;
1650                     microServiceInfo.setUrl(url);
1651                 }
1652
1653                 if (url.endsWith("/")) {
1654                     url = url.substring(0, url.length() - 1);
1655                     microServiceInfo.setUrl(url);
1656                 }
1657             }
1658
1659             if (!RegExpTestUtil.urlRegExpTest(url)) {
1660                 throw new UnprocessableEntityException(
1661                                 "register MicroServiceInfo FAIL:url (" + url + ") is not a valid format");
1662             }
1663
1664         } else {
1665             microServiceInfo.setUrl("/");
1666         }
1667
1668
1669         if (StringUtils.isNotBlank(microServiceInfo.getPath())) {
1670
1671             String path = microServiceInfo.getPath();
1672             if (!"/".equals(path)) {
1673                 if (!path.startsWith("/")) {
1674                     path = "/" + path;
1675                     microServiceInfo.setPath(path);
1676                 }
1677
1678                 if (path.endsWith("/")) {
1679                     path = path.substring(0, path.length() - 1);
1680                     microServiceInfo.setPath(path);
1681                 }
1682             }
1683
1684             if (!RegExpTestUtil.urlRegExpTest(path)) {
1685                 throw new UnprocessableEntityException(
1686                                 "register MicroServiceInfo FAIL:path (" + path + ") is not a valid format");
1687
1688             }
1689
1690
1691
1692         }
1693
1694
1695         for (Node node : microServiceInfo.getNodes()) {
1696
1697             if (StringUtils.isNotBlank(node.getIp())) {
1698                 if (!RegExpTestUtil.ipRegExpTest(node.getIp())) {
1699                     throw new UnprocessableEntityException(
1700                                     "register MicroServiceInfo FAIL:IP(" + node.getIp() + ")is not a valid ip address");
1701                 }
1702             }
1703
1704             if (!RegExpTestUtil.portRegExpTest(node.getPort())) {
1705                 throw new UnprocessableEntityException("register MicroServiceInfo FAIL:Port(" + node.getPort()
1706                                 + ")is not a valid Port address");
1707             }
1708
1709
1710             if (StringUtils.isNotBlank(node.getLb_server_params())) {
1711                 try {
1712                     String[] lb_server_params_array = node.getLb_server_params().split(",");
1713                     for (int i = 0; i < lb_server_params_array.length; i++) {
1714                         String params = lb_server_params_array[i].split("=")[0];
1715                         if (!DiscoverUtil.checkExist(DiscoverUtil.LB_PARAMS_LIST, params, ",")) {
1716                             throw new UnprocessableEntityException(
1717                                             "register MicroServiceInfo FAIL:lb_server_params is wrong:"
1718                                                             + lb_server_params_array[i]);
1719                         }
1720                     }
1721                 } catch (Exception e) {
1722                     throw new UnprocessableEntityException(
1723                                     "register MicroServiceInfo FAIL:lb_server_params'format is wrong:"
1724                                                     + node.getLb_server_params());
1725                 }
1726
1727             }
1728
1729             if (StringUtils.isNotBlank(node.getCheckType())) {
1730                 if (!DiscoverUtil.checkExist(DiscoverUtil.CHECK_TYPE_LIST, node.getCheckType().trim(), ",")) {
1731                     throw new UnprocessableEntityException(
1732                                     "register MicroServiceInfo FAIL:checkType is wrong,value range:("
1733                                                     + DiscoverUtil.CHECK_TYPE_LIST + ")");
1734                 }
1735
1736
1737                 if ("HTTP".equals(node.getCheckType()) || "TCP".equals(node.getCheckType())) {
1738                     String checkUrl = node.getCheckUrl();
1739                     if (StringUtils.isBlank(checkUrl)) {
1740                         throw new UnprocessableEntityException(
1741                                         "register MicroServiceInfo FAIL:checkUrl field is empty");
1742                     }
1743
1744                     if ("HTTP".equals(node.getCheckType())) {
1745
1746
1747                         if (RegExpTestUtil.httpUrlRegExpTest(checkUrl)) {
1748                             if (!checkUrl.startsWith("http://")) {
1749                                 checkUrl = "http://" + checkUrl;
1750                                 node.setCheckUrl(checkUrl);
1751                             }
1752                         } else {
1753                             if (!checkUrl.startsWith("/")) {
1754                                 checkUrl = "/" + checkUrl;
1755                             }
1756                             checkUrl = "http://" + node.getIp() + ":" + node.getPort() + checkUrl;
1757                             node.setCheckUrl(checkUrl);
1758                         }
1759                     }
1760
1761
1762                 }
1763
1764             }
1765
1766             if (StringUtils.isNotBlank(node.getHa_role())) {
1767                 if (!DiscoverUtil.checkExist(DiscoverUtil.CHECK_HA_ROLE_LIST, node.getHa_role().trim(), ",")) {
1768                     throw new UnprocessableEntityException(
1769                                     "register MicroServiceInfo FAIL:ha_role is wrong,value range:("
1770                                                     + DiscoverUtil.CHECK_HA_ROLE_LIST + ")");
1771                 }
1772             }
1773
1774
1775         }
1776
1777
1778
1779         String[] visualRangeArray = StringUtils.split(microServiceInfo.getVisualRange(), "|");
1780         for (int i = 0; i < visualRangeArray.length; i++) {
1781             if (!DiscoverUtil.checkExist(DiscoverUtil.VISUAL_RANGE_LIST, visualRangeArray[i], ",")) {
1782                 throw new UnprocessableEntityException("register MicroServiceInfo FAIL:type is wrong,value range:("
1783                                 + DiscoverUtil.VISUAL_RANGE_LIST + ")");
1784             }
1785         }
1786
1787         microServiceInfo.setProtocol(microServiceInfo.getProtocol().toUpperCase());
1788         if (!DiscoverUtil.checkExist(DiscoverUtil.PROTOCOL_LIST, microServiceInfo.getProtocol().trim(), ",")) {
1789             throw new UnprocessableEntityException("register MicroServiceInfo FAIL:Protocol is wrong,value range:("
1790                             + DiscoverUtil.PROTOCOL_LIST + ")");
1791         }
1792
1793         if (microServiceInfo.getLabels() != null) {
1794             for (String label : microServiceInfo.getLabels()) {
1795                 if (!RegExpTestUtil.labelRegExpTest(label)) {
1796                     throw new UnprocessableEntityException("register MicroServiceInfo FAIL:label[" + label
1797                                     + "] is not a valid format(key:value)");
1798                 }
1799             }
1800         }
1801
1802
1803
1804         // 判断自定义发布端口
1805         if (StringUtils.isNotBlank(microServiceInfo.getPublish_port())) {
1806
1807             if (DiscoverUtil.checkExist(DiscoverUtil.HTTP_PROTOCOL, microServiceInfo.getProtocol())) {
1808
1809                 if (microServiceInfo.getPublish_port().contains("|")) {
1810
1811                     String[] publishPortArray = StringUtils.split(microServiceInfo.getPublish_port(), "|");
1812
1813                     int portNum = publishPortArray.length;
1814
1815                     // 判断端口格式
1816                     for (int i = 0; i < portNum; i++) {
1817                         if (!RegExpTestUtil.portRegExpTest(publishPortArray[i])) {
1818                             throw new UnprocessableEntityException("register MicroServiceInfo FAIL:Public Port("
1819                                             + publishPortArray[i] + ")is not a valid Port address");
1820                         }
1821                     }
1822
1823                     // 判断端口数量
1824                     if (portNum == 0 || portNum > 2) {
1825                         throw new UnprocessableEntityException(
1826                                         "register MicroServiceInfo FAIL:Public Port num is wrong:" + portNum);
1827                     } else if (portNum == 2) {
1828                         // 判断端口值是否一样
1829                         if (publishPortArray[0].equals(publishPortArray[1])) {
1830                             throw new UnprocessableEntityException(
1831                                             "register MicroServiceInfo FAIL:Two ports have the same value :"
1832                                                             + publishPortArray[0]);
1833                         }
1834                     } else if (portNum == 1) {
1835                         throw new UnprocessableEntityException(
1836                                         "register MicroServiceInfo FAIL:Two ports have one null value");
1837                     }
1838                 } else {
1839                     if (!RegExpTestUtil.portRegExpTest(microServiceInfo.getPublish_port())) {
1840                         throw new UnprocessableEntityException("register MicroServiceInfo FAIL:Public Port("
1841                                         + microServiceInfo.getPublish_port() + ")is not a valid Port address");
1842                     }
1843                 }
1844
1845             } else if ("TCP".equals(microServiceInfo.getProtocol()) || "UDP".equals(microServiceInfo.getProtocol())) {
1846                 if (!RegExpTestUtil.portRegExpTest(microServiceInfo.getPublish_port())) {
1847                     throw new UnprocessableEntityException("register MicroServiceInfo FAIL:Public Port("
1848                                     + microServiceInfo.getPublish_port() + ")is not a valid Port address");
1849                 }
1850
1851                 int tcpUdpPortRangeStart = Integer.parseInt(ConfigUtil.getInstance().getTcpudpPortRangeStart());
1852                 int tcpUdpPortRangeEnd = Integer.parseInt(ConfigUtil.getInstance().getTcpudpPortRangeEnd());
1853                 int iPublishPort = Integer.parseInt(microServiceInfo.getPublish_port());
1854
1855                 if (iPublishPort > tcpUdpPortRangeEnd || iPublishPort < tcpUdpPortRangeStart) {
1856                     throw new UnprocessableEntityException("register MicroServiceInfo FAIL:Public_Port Range ("
1857                                     + tcpUdpPortRangeStart + "-" + tcpUdpPortRangeEnd + ")");
1858                 }
1859
1860             } else {
1861                 microServiceInfo.setPublish_port("");
1862             }
1863
1864
1865
1866         }
1867
1868         // 检查同名不同协议注册
1869         try {
1870             List<MicroServiceFullInfo> serviceList = getMicroServiceForNodes(microServiceInfo.getServiceName(),
1871                             microServiceInfo.getVersion(), false, "", microServiceInfo.getNamespace());
1872             if (serviceList != null && serviceList.size() > 0) {
1873                 for (MicroServiceFullInfo service : serviceList) {
1874                     if (!service.getProtocol().equalsIgnoreCase(microServiceInfo.getProtocol())) {
1875                         throw new UnprocessableEntityException(
1876                                         "register MicroServiceInfo FAIL:There is a same service ,but different protocol--"
1877                                                         + service.getProtocol());
1878                     }
1879
1880                 }
1881             }
1882         } catch (ExtendedNotFoundException e) {
1883             // LOGGER.info("register MicroServiceInfo CHECK ok for protocol:service is not fond");
1884         }
1885
1886
1887     }
1888
1889
1890     private void checkServiceNameAndVersion(String serviceName, String version) {
1891         if (StringUtils.isBlank(serviceName)) {
1892             throw new UnprocessableEntityException("check MicroServiceInfo FAIL:serviceName  can't be empty");
1893         }
1894
1895         if (!RegExpTestUtil.serviceNameRegExpTest(serviceName)) {
1896             throw new UnprocessableEntityException(
1897                             "check MicroServiceInfo FAIL:ServiceName(" + serviceName + ") format error");
1898         }
1899
1900         if (StringUtils.isNotBlank(version)) {
1901             if (!RegExpTestUtil.versionRegExpTest(version)) {
1902                 throw new UnprocessableEntityException("check MicroServiceInfo FAIL:version is not a valid  format");
1903             }
1904         }
1905     }
1906
1907
1908
1909 }