3b1f71215e7974a8b69ca2a814c2c2243cddf23d
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl;
23
24 import java.io.IOException;
25 import java.util.Base64;
26 import java.util.HashMap;
27 import java.util.Map;
28 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
29 import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
30 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient;
31 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl.config.VESCollectorCfgImpl;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class VESCollectorClient implements VESCollectorService, IConfigChangedListener, AutoCloseable {
38     private static final Logger LOG = LoggerFactory.getLogger(VESCollectorClient.class);
39     private final VESCollectorCfgImpl vesConfig;
40     private final ConfigurationFileRepresentation cfg;
41     private BaseHTTPClient httpClient;
42     private final Map<String, String> headerMap;
43
44     public VESCollectorClient(ConfigurationFileRepresentation config) {
45         this.vesConfig = new VESCollectorCfgImpl(config);
46         this.cfg = config;
47         this.cfg.registerConfigChangedListener(this);
48
49         httpClient = new BaseHTTPClient(getBaseUrl());
50
51         this.headerMap = new HashMap<>();
52         this.headerMap.put("Content-Type", "application/json");
53         this.headerMap.put("Accept", "application/json");
54
55         setAuthorization(getConfig().getUsername(), getConfig().getPassword());
56     }
57
58     @Override
59     public VESCollectorCfgImpl getConfig() {
60         return this.vesConfig;
61     }
62
63     public String getBaseUrl() {
64         LOG.debug("IP Address is - {}", getConfig().getIP());
65         if (!getConfig().getTLSEnabled()) {
66             return "http://" + getConfig().getIP() + ":" + getConfig().getPort();
67         } else {
68             return "https://" + getConfig().getIP() + ":" + getConfig().getPort();
69         }
70     }
71
72     private void setAuthorization(String username, String password) {
73         if (getConfig().getTLSEnabled()) {
74             String credentials = username + ":" + password;
75             this.headerMap.put("Authorization",
76                     "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes())));
77         }
78
79     }
80
81
82
83     @Override
84     public boolean publishVESMessage(String message) {
85         LOG.info("In VESClient - {} ", message);
86         BaseHTTPResponse response;
87         try {
88             String uri = "eventListener" + "/" + getConfig().getVersion();
89             response = httpClient.sendRequest(uri, "POST", message, headerMap);
90             LOG.debug("finished with responsecode {}", response.code);
91             return response.code == 200;
92         } catch (IOException e) {
93             LOG.warn("problem publishing VES message {} ", e.getMessage());
94             return false;
95         }
96
97     }
98
99     @Override
100     public void close() throws Exception {
101         this.cfg.unregisterConfigChangedListener(this);
102     }
103
104     @Override
105     public void onConfigChanged() {
106         httpClient.setBaseUrl(getBaseUrl());
107         setAuthorization(getConfig().getUsername(), getConfig().getPassword());
108     }
109
110 }