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 com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Base64;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
33 import org.eclipse.jdt.annotation.NonNull;
34 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
35 import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
36 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient;
37 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.NotificationProxyParserImpl;
39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NotificationProxyParser;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorConfigChangeListener;
41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESCommonEventHeaderPOJO;
43 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESFaultFieldsPOJO;
44 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESMessage;
45 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESNotificationFieldsPOJO;
46 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESPNFRegistrationFieldsPOJO;
47 import org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl.config.VESCollectorCfgImpl;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class VESCollectorServiceImpl implements VESCollectorService, IConfigChangedListener, AutoCloseable {
52 private static final Logger LOG = LoggerFactory.getLogger(VESCollectorServiceImpl.class);
53 private final VESCollectorCfgImpl vesConfig;
54 private final ConfigurationFileRepresentation cfg;
55 private BaseHTTPClient httpClient;
56 private final Map<String, String> headerMap;
57 private List<VESCollectorConfigChangeListener> registeredObjects;
58 private final ObjectMapper objMapper;
61 public VESCollectorServiceImpl(ConfigurationFileRepresentation config) {
62 registeredObjects = new ArrayList<VESCollectorConfigChangeListener>();
63 this.vesConfig = new VESCollectorCfgImpl(config);
65 this.cfg.registerConfigChangedListener(this);
66 this.objMapper = new ObjectMapper();
68 httpClient = new BaseHTTPClient(getBaseUrl(), this.vesConfig.isTrustAllCerts());
70 this.headerMap = new HashMap<>();
71 this.headerMap.put("Content-Type", "application/json");
72 this.headerMap.put("Accept", "application/json");
74 setAuthorization(getConfig().getUsername(), getConfig().getPassword());
78 public VESCollectorCfgImpl getConfig() {
79 return this.vesConfig;
82 public String getBaseUrl() {
83 LOG.debug("IP Address is - {}", getConfig().getIP());
84 if (!getConfig().getTLSEnabled()) {
85 return "http://" + getConfig().getIP() + ":" + getConfig().getPort();
87 return "https://" + getConfig().getIP() + ":" + getConfig().getPort();
91 private void setAuthorization(String username, String password) {
92 if (getConfig().getTLSEnabled()) {
93 String credentials = username + ":" + password;
94 this.headerMap.put("Authorization",
95 "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes())));
101 public boolean publishVESMessage(VESMessage message) {
102 LOG.info("In VESClient - {} ", message.getMessage());
103 BaseHTTPResponse response;
105 String uri = "eventListener" + "/" + getConfig().getVersion();
106 response = httpClient.sendRequest(uri, "POST", message.getMessage(), headerMap);
107 LOG.debug("finished with responsecode {}", response.code);
108 return response.code == 200;
109 } catch (IOException e) {
110 LOG.warn("problem publishing VES message {} ", e.getMessage());
117 public void close() throws Exception {
118 this.cfg.unregisterConfigChangedListener(this);
122 public void onConfigChanged() {
123 LOG.debug("In onConfigChanged - isTrustAllCerts = {} getBaseUrl = {}", getConfig().isTrustAllCerts(), getBaseUrl());
124 httpClient = new BaseHTTPClient(getBaseUrl(), this.vesConfig.isTrustAllCerts());
125 setAuthorization(getConfig().getUsername(), getConfig().getPassword());
126 Iterator<VESCollectorConfigChangeListener> it = registeredObjects.iterator();
127 while (it.hasNext()) {
128 VESCollectorConfigChangeListener o = it.next();
129 o.notify(getConfig());
134 public void registerForChanges(VESCollectorConfigChangeListener o) {
135 registeredObjects.add(o);
139 public void deregister(VESCollectorConfigChangeListener o) {
140 registeredObjects.remove(o);
144 public @NonNull NotificationProxyParser getNotificationProxyParser() {
145 return new NotificationProxyParserImpl();
149 * Generates VES Event JSON containing commonEventHeader and notificationFields fields
151 * @param commonEventHeader
153 * @return VESMessage - representing the VESEvent JSON
154 * @throws JsonProcessingException
157 public VESMessage generateVESEvent(VESCommonEventHeaderPOJO commonEventHeader,
158 VESNotificationFieldsPOJO notifFields) throws JsonProcessingException {
159 Map<String, Object> innerEvent = new HashMap<String, Object>();
160 innerEvent.put("commonEventHeader", commonEventHeader);
161 innerEvent.put("notificationFields", notifFields);
163 Map<String, Object> outerEvent = new HashMap<String, Object>();
164 outerEvent.put("event", innerEvent);
165 LOG.info("in generateVESEvent - {}", objMapper.writeValueAsString(outerEvent));
166 return new VESMessage(objMapper.writeValueAsString(outerEvent));
170 * Generates VES Event JSON containing commonEventHeader and faultFields fields
172 * @param commonEventHeader
174 * @return VESMessage - representing the VES Event JSON
175 * @throws JsonProcessingException
178 public VESMessage generateVESEvent(VESCommonEventHeaderPOJO commonEventHeader, VESFaultFieldsPOJO faultFields) throws JsonProcessingException {
179 Map<String, Object> innerEvent = new HashMap<String, Object>();
180 innerEvent.put("commonEventHeader", commonEventHeader);
181 innerEvent.put("faultFields", faultFields);
183 Map<String, Object> outerEvent = new HashMap<String, Object>();
184 outerEvent.put("event", innerEvent);
185 return new VESMessage(objMapper.writeValueAsString(outerEvent));
189 * Generates VES Event JSON containing commonEventHeader and pnfRegistration fields
191 * @param commonEventHeader
192 * @param pnfRegistrationFields
193 * @return VESMessage - representing the VES Event JSON
194 * @throws JsonProcessingException
197 public VESMessage generateVESEvent(VESCommonEventHeaderPOJO commonEventHeader, VESPNFRegistrationFieldsPOJO pnfRegistrationFields) throws JsonProcessingException {
198 Map<String, Object> innerEvent = new HashMap<String, Object>();
199 innerEvent.put("commonEventHeader", commonEventHeader);
200 innerEvent.put("pnfRegistration", pnfRegistrationFields);
202 Map<String, Object> outerEvent = new HashMap<String, Object>();
203 outerEvent.put("event", innerEvent);
204 return new VESMessage(objMapper.writeValueAsString(outerEvent));