2 * Copyright 2016-2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.msb.sdclient.wrapper;
18 import org.onap.msb.sdclient.core.AgentService;
19 import org.onap.msb.sdclient.core.CatalogNode;
20 import org.onap.msb.sdclient.core.ConsulService;
21 import org.onap.msb.sdclient.wrapper.util.ConfigUtil;
22 import org.onap.msb.sdclient.wrapper.util.DiscoverUtil;
23 import org.onap.msb.sdclient.wrapper.util.HttpClientUtil;
24 import org.onap.msb.sdclient.wrapper.util.JacksonJsonUtil;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class ConsulCatalogServiceWrapper {
30 private static ConsulCatalogServiceWrapper instance = new ConsulCatalogServiceWrapper();
32 private ConsulCatalogServiceWrapper() {}
34 public static ConsulCatalogServiceWrapper getInstance() {
37 private static final Logger LOGGER = LoggerFactory.getLogger(ConsulAgentServiceWrapper.class);
40 public synchronized int saveService(AgentService agentService) throws Exception{
41 String consulRegisterurl =
42 (new StringBuilder().append("http://").append(ConfigUtil.getInstance().getConsulAddress())
43 .append(DiscoverUtil.CONSUL_CATALOG_URL).append("/register")).toString();
45 CatalogNode catalogNode=transformCatalogNode(agentService);
49 HttpClientUtil.httpPostWithJSON(consulRegisterurl,
50 JacksonJsonUtil.beanToJson(catalogNode));
52 return registerResult;
56 public synchronized int deleteService(String serviceId) throws Exception{
58 (new StringBuilder().append("http://").append(ConfigUtil.getInstance().getConsulAddress())
59 .append(DiscoverUtil.CONSUL_CATALOG_URL).append("/deregister"))
62 String nodeJson="{\"Node\": \""+DiscoverUtil.EXTERNAL_NODE_NAME+"\",\"ServiceID\": \""+serviceId+"\"}";
64 int delResult = HttpClientUtil.httpPostWithJSON(consulDelurl, nodeJson);
70 private CatalogNode transformCatalogNode(AgentService agentService){
71 ConsulService consulService=new ConsulService();
73 consulService.setAddress(agentService.getAddress());
74 consulService.setId(agentService.getId());
75 consulService.setPort(agentService.getPort());
76 consulService.setService(agentService.getName());
77 consulService.setTags(agentService.getTags());
79 CatalogNode catalogNode=new CatalogNode(consulService);