Update consul version to 1.4.3
[msb/discovery.git] / sdclient / discovery-service / src / main / java / org / onap / msb / sdclient / wrapper / ConsulAgentServiceWrapper.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 org.onap.msb.sdclient.core.AgentService;
17 import org.onap.msb.sdclient.wrapper.util.ConfigUtil;
18 import org.onap.msb.sdclient.wrapper.util.DiscoverUtil;
19 import org.onap.msb.sdclient.wrapper.util.HttpClientUtil;
20 import org.onap.msb.sdclient.wrapper.util.JacksonJsonUtil;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24
25 public class ConsulAgentServiceWrapper {
26
27     private static ConsulAgentServiceWrapper instance = new ConsulAgentServiceWrapper();
28
29     private ConsulAgentServiceWrapper() {}
30
31     public static ConsulAgentServiceWrapper getInstance() {
32         return instance;
33     }
34
35     private static final Logger LOGGER = LoggerFactory.getLogger(ConsulAgentServiceWrapper.class);
36
37
38     public synchronized int saveService(AgentService agentService) throws Exception {
39         String consulRegisterurl =
40                         (new StringBuilder().append("http://").append(ConfigUtil.getInstance().getConsulAddress())
41                                         .append(DiscoverUtil.CONSUL_AGENT_URL).append("/register")).toString();
42
43
44         int registerResult =
45                         HttpClientUtil.httpPutWithJSON(consulRegisterurl, JacksonJsonUtil.beanToJson(agentService));
46
47         return registerResult;
48
49     }
50
51     public synchronized int deleteService(String serviceId) throws Exception {
52         String consulDelurl = (new StringBuilder().append("http://").append(ConfigUtil.getInstance().getConsulAddress())
53                         .append(DiscoverUtil.CONSUL_AGENT_URL).append("/deregister/").append(serviceId)).toString();
54
55         int delResult = HttpClientUtil.httpPutWithJSON(consulDelurl, "");
56
57         return delResult;
58     }
59
60 }