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.ArrayList;
26 import java.util.Base64;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.List;
31 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
32 import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
33 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient;
34 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
35 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorConfigChangeListener;
36 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
37 import org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl.config.VESCollectorCfgImpl;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class VESCollectorClient implements VESCollectorService, IConfigChangedListener, AutoCloseable {
42 private static final Logger LOG = LoggerFactory.getLogger(VESCollectorClient.class);
43 private final VESCollectorCfgImpl vesConfig;
44 private final ConfigurationFileRepresentation cfg;
45 private BaseHTTPClient httpClient;
46 private final Map<String, String> headerMap;
47 private List<VESCollectorConfigChangeListener> registeredObjects;
49 public VESCollectorClient(ConfigurationFileRepresentation config) {
50 registeredObjects = new ArrayList<VESCollectorConfigChangeListener>();
51 this.vesConfig = new VESCollectorCfgImpl(config);
53 this.cfg.registerConfigChangedListener(this);
55 httpClient = new BaseHTTPClient(getBaseUrl());
57 this.headerMap = new HashMap<>();
58 this.headerMap.put("Content-Type", "application/json");
59 this.headerMap.put("Accept", "application/json");
61 setAuthorization(getConfig().getUsername(), getConfig().getPassword());
65 public VESCollectorCfgImpl getConfig() {
66 return this.vesConfig;
69 public String getBaseUrl() {
70 LOG.debug("IP Address is - {}", getConfig().getIP());
71 if (!getConfig().getTLSEnabled()) {
72 return "http://" + getConfig().getIP() + ":" + getConfig().getPort();
74 return "https://" + getConfig().getIP() + ":" + getConfig().getPort();
78 private void setAuthorization(String username, String password) {
79 if (getConfig().getTLSEnabled()) {
80 String credentials = username + ":" + password;
81 this.headerMap.put("Authorization",
82 "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes())));
90 public boolean publishVESMessage(String message) {
91 LOG.info("In VESClient - {} ", message);
92 BaseHTTPResponse response;
94 String uri = "eventListener" + "/" + getConfig().getVersion();
95 response = httpClient.sendRequest(uri, "POST", message, headerMap);
96 LOG.debug("finished with responsecode {}", response.code);
97 return response.code == 200;
98 } catch (IOException e) {
99 LOG.warn("problem publishing VES message {} ", e.getMessage());
106 public void close() throws Exception {
107 this.cfg.unregisterConfigChangedListener(this);
111 public void onConfigChanged() {
112 httpClient.setBaseUrl(getBaseUrl());
113 setAuthorization(getConfig().getUsername(), getConfig().getPassword());
114 Iterator<VESCollectorConfigChangeListener> it = registeredObjects.iterator();
115 while (it.hasNext()) {
116 VESCollectorConfigChangeListener o = it.next();
117 o.notify(getConfig());
122 public void registerForChanges(VESCollectorConfigChangeListener o) {
123 registeredObjects.add(o);
127 public void deregister(VESCollectorConfigChangeListener o) {
128 registeredObjects.remove(o);