2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl;
24 import java.io.IOException;
25 import java.util.Base64;
26 import java.util.HashMap;
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;
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;
44 public VESCollectorClient(ConfigurationFileRepresentation config) {
45 this.vesConfig = new VESCollectorCfgImpl(config);
47 this.cfg.registerConfigChangedListener(this);
49 httpClient = new BaseHTTPClient(getBaseUrl());
51 this.headerMap = new HashMap<>();
52 this.headerMap.put("Content-Type", "application/json");
53 this.headerMap.put("Accept", "application/json");
55 setAuthorization(getConfig().getUsername(), getConfig().getPassword());
59 public VESCollectorCfgImpl getConfig() {
60 return this.vesConfig;
63 public String getBaseUrl() {
64 LOG.debug("IP Address is - {}", getConfig().getIP());
65 if (!getConfig().getTLSEnabled()) {
66 return "http://" + getConfig().getIP() + ":" + getConfig().getPort();
68 return "https://" + getConfig().getIP() + ":" + getConfig().getPort();
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())));
84 public boolean publishVESMessage(String message) {
85 LOG.info("In VESClient - {} ", message);
86 BaseHTTPResponse response;
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());
100 public void close() throws Exception {
101 this.cfg.unregisterConfigChangedListener(this);
105 public void onConfigChanged() {
106 httpClient.setBaseUrl(getBaseUrl());
107 setAuthorization(getConfig().getUsername(), getConfig().getPassword());