d14c51ed1e22cbba9a395dab6bf19a55e2055274
[msb/discovery.git] / sdclient / discovery-service / src / main / java / org / onap / msb / sdclient / wrapper / ConsulCatalogServiceWrapper.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.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;
27
28 public class ConsulCatalogServiceWrapper {
29
30   private static ConsulCatalogServiceWrapper instance = new ConsulCatalogServiceWrapper();
31
32   private ConsulCatalogServiceWrapper() {}
33
34   public static ConsulCatalogServiceWrapper getInstance() {
35     return instance;
36   }
37   private static final Logger LOGGER = LoggerFactory.getLogger(ConsulAgentServiceWrapper.class);
38   
39
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();
44
45     CatalogNode catalogNode=transformCatalogNode(agentService);   
46     
47     
48     int registerResult =
49         HttpClientUtil.httpPostWithJSON(consulRegisterurl,
50             JacksonJsonUtil.beanToJson(catalogNode));
51     
52     return registerResult;
53    
54   }
55   
56   public synchronized int deleteService(String serviceId) throws Exception{
57     String consulDelurl =
58         (new StringBuilder().append("http://").append(ConfigUtil.getInstance().getConsulAddress())
59             .append(DiscoverUtil.CONSUL_CATALOG_URL).append("/deregister"))
60             .toString();
61     
62     String nodeJson="{\"Node\": \""+DiscoverUtil.EXTERNAL_NODE_NAME+"\",\"ServiceID\": \""+serviceId+"\"}";
63
64     int delResult = HttpClientUtil.httpPostWithJSON(consulDelurl, nodeJson);
65     
66     return delResult;
67   }
68   
69   
70   private CatalogNode transformCatalogNode(AgentService agentService){
71     ConsulService consulService=new ConsulService();  
72     
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());
78     
79     CatalogNode catalogNode=new CatalogNode(consulService);
80     
81     return catalogNode;
82   }
83
84 }