Fix java check style issue
[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"); 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.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;
25
26 public class ConsulCatalogServiceWrapper {
27
28     private static ConsulCatalogServiceWrapper instance = new ConsulCatalogServiceWrapper();
29
30     private ConsulCatalogServiceWrapper() {}
31
32     public static ConsulCatalogServiceWrapper getInstance() {
33         return instance;
34     }
35
36     private static final Logger LOGGER = LoggerFactory.getLogger(ConsulAgentServiceWrapper.class);
37
38
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();
43
44         CatalogNode catalogNode = transformCatalogNode(agentService);
45
46
47         int registerResult =
48                         HttpClientUtil.httpPostWithJSON(consulRegisterurl, JacksonJsonUtil.beanToJson(catalogNode));
49
50         return registerResult;
51
52     }
53
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();
57
58         String nodeJson =
59                         "{\"Node\": \"" + DiscoverUtil.EXTERNAL_NODE_NAME + "\",\"ServiceID\": \"" + serviceId + "\"}";
60
61         int delResult = HttpClientUtil.httpPostWithJSON(consulDelurl, nodeJson);
62
63         return delResult;
64     }
65
66
67     private CatalogNode transformCatalogNode(AgentService agentService) {
68         ConsulService consulService = new ConsulService();
69
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());
75
76         CatalogNode catalogNode = new CatalogNode(consulService);
77
78         return catalogNode;
79     }
80
81 }