modify copyright year
[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");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.msb.sdclient.wrapper;
17
18 import org.apache.commons.lang3.StringUtils;
19 import org.onap.msb.sdclient.core.AgentService;
20 import org.onap.msb.sdclient.core.Node;
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;
27
28
29 public class ConsulAgentServiceWrapper {
30
31   private static ConsulAgentServiceWrapper instance = new ConsulAgentServiceWrapper();
32
33   private ConsulAgentServiceWrapper() {}
34
35   public static ConsulAgentServiceWrapper getInstance() {
36     return instance;
37   }
38   private static final Logger LOGGER = LoggerFactory.getLogger(ConsulAgentServiceWrapper.class);
39   
40
41   public synchronized int  saveService(AgentService agentService) throws Exception{
42     String consulRegisterurl =
43         (new StringBuilder().append("http://").append(ConfigUtil.getInstance().getConsulAddress())
44             .append(DiscoverUtil.CONSUL_AGENT_URL).append("/register")).toString();
45
46
47     int registerResult =
48         HttpClientUtil.httpPostWithJSON(consulRegisterurl,
49             JacksonJsonUtil.beanToJson(agentService));
50     
51     return registerResult;
52    
53   }
54   
55   public synchronized int deleteService(String serviceId) throws Exception{
56     String consulDelurl =
57         (new StringBuilder().append("http://").append(ConfigUtil.getInstance().getConsulAddress())
58             .append(DiscoverUtil.CONSUL_AGENT_URL).append("/deregister/").append(serviceId))
59             .toString();
60
61     int delResult = HttpClientUtil.httpPostWithJSON(consulDelurl, "");
62     
63     return delResult;
64   }
65   
66 }