2 * Copyright 2016-2017 ZTE, Inc. and others.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
14 package org.onap.msb.sdclient.wrapper;
16 import org.onap.msb.sdclient.core.AgentService;
17 import org.onap.msb.sdclient.core.CatalogNode;
18 import org.onap.msb.sdclient.core.ConsulService;
19 import org.onap.msb.sdclient.wrapper.util.ConfigUtil;
20 import org.onap.msb.sdclient.wrapper.util.DiscoverUtil;
21 import org.onap.msb.sdclient.wrapper.util.HttpClientUtil;
22 import org.onap.msb.sdclient.wrapper.util.JacksonJsonUtil;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class ConsulCatalogServiceWrapper {
28 private static ConsulCatalogServiceWrapper instance = new ConsulCatalogServiceWrapper();
30 private ConsulCatalogServiceWrapper() {}
32 public static ConsulCatalogServiceWrapper getInstance() {
36 private static final Logger LOGGER = LoggerFactory.getLogger(ConsulAgentServiceWrapper.class);
39 public synchronized int saveService(AgentService agentService) throws Exception {
40 String consulRegisterurl =
41 (new StringBuilder().append("http://").append(ConfigUtil.getInstance().getConsulAddress())
42 .append(DiscoverUtil.CONSUL_CATALOG_URL).append("/register")).toString();
44 CatalogNode catalogNode = transformCatalogNode(agentService);
48 HttpClientUtil.httpPostWithJSON(consulRegisterurl, JacksonJsonUtil.beanToJson(catalogNode));
50 return registerResult;
54 public synchronized int deleteService(String serviceId) throws Exception {
55 String consulDelurl = (new StringBuilder().append("http://").append(ConfigUtil.getInstance().getConsulAddress())
56 .append(DiscoverUtil.CONSUL_CATALOG_URL).append("/deregister")).toString();
59 "{\"Node\": \"" + DiscoverUtil.EXTERNAL_NODE_NAME + "\",\"ServiceID\": \"" + serviceId + "\"}";
61 int delResult = HttpClientUtil.httpPostWithJSON(consulDelurl, nodeJson);
67 private CatalogNode transformCatalogNode(AgentService agentService) {
68 ConsulService consulService = new ConsulService();
70 consulService.setAddress(agentService.getAddress());
71 consulService.setId(agentService.getId());
72 consulService.setPort(agentService.getPort());
73 consulService.setService(agentService.getName());
74 consulService.setTags(agentService.getTags());
76 CatalogNode catalogNode = new CatalogNode(consulService);