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.types.VESStndDefinedFieldsPOJO;
48 import org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl.config.VESCollectorCfgImpl;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class VESCollectorServiceImpl implements VESCollectorService, IConfigChangedListener, AutoCloseable {
53 private static final Logger LOG = LoggerFactory.getLogger(VESCollectorServiceImpl.class);
54 private final VESCollectorCfgImpl vesConfig;
55 private final ConfigurationFileRepresentation cfg;
56 private BaseHTTPClient httpClient;
57 private final Map<String, String> headerMap;
58 private List<VESCollectorConfigChangeListener> registeredObjects;
59 private final ObjectMapper objMapper;
62 public VESCollectorServiceImpl(ConfigurationFileRepresentation config) {
63 registeredObjects = new ArrayList<VESCollectorConfigChangeListener>();
64 this.vesConfig = new VESCollectorCfgImpl(config);
66 this.cfg.registerConfigChangedListener(this);
67 this.objMapper = new ObjectMapper();
69 httpClient = new BaseHTTPClient(getBaseUrl(), this.vesConfig.isTrustAllCerts());
71 this.headerMap = new HashMap<>();
72 this.headerMap.put("Content-Type", "application/json");
73 this.headerMap.put("Accept", "application/json");
75 setAuthorization(getConfig().getUsername(), getConfig().getPassword());
79 public VESCollectorCfgImpl getConfig() {
80 return this.vesConfig;
83 public String getBaseUrl() {
84 LOG.debug("IP Address is - {}", getConfig().getIP());
85 if (!getConfig().getTLSEnabled()) {
86 return "http://" + getConfig().getIP() + ":" + getConfig().getPort();
88 return "https://" + getConfig().getIP() + ":" + getConfig().getPort();
92 private void setAuthorization(String username, String password) {
93 if (getConfig().getTLSEnabled()) {
94 String credentials = username + ":" + password;
95 this.headerMap.put("Authorization",
96 "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes())));
102 public boolean publishVESMessage(VESMessage message) {
103 LOG.debug("In VESClient - {} ", message.getMessage());
104 BaseHTTPResponse response;
106 String uri = "eventListener" + "/" + getConfig().getVersion();
107 response = httpClient.sendRequest(uri, "POST", message.getMessage(), headerMap);
108 LOG.debug("finished with responsecode {}", response.code);
109 return response.code == 200;
110 } catch (IOException e) {
111 LOG.warn("problem publishing VES message {} ", e.getMessage());
118 public void close() throws Exception {
119 this.cfg.unregisterConfigChangedListener(this);
123 public void onConfigChanged() {
124 LOG.debug("In onConfigChanged - isTrustAllCerts = {} getBaseUrl = {}", getConfig().isTrustAllCerts(), getBaseUrl());
125 httpClient = new BaseHTTPClient(getBaseUrl(), this.vesConfig.isTrustAllCerts());
126 setAuthorization(getConfig().getUsername(), getConfig().getPassword());
127 Iterator<VESCollectorConfigChangeListener> it = registeredObjects.iterator();
128 while (it.hasNext()) {
129 VESCollectorConfigChangeListener o = it.next();
130 o.notify(getConfig());
135 public void registerForChanges(VESCollectorConfigChangeListener o) {
136 registeredObjects.add(o);
140 public void deregister(VESCollectorConfigChangeListener o) {
141 registeredObjects.remove(o);
145 public @NonNull NotificationProxyParser getNotificationProxyParser() {
146 return new NotificationProxyParserImpl();
150 * Generates VES Event JSON containing commonEventHeader and notificationFields fields
152 * @param commonEventHeader
154 * @return VESMessage - representing the VESEvent JSON
155 * @throws JsonProcessingException
158 public VESMessage generateVESEvent(VESCommonEventHeaderPOJO commonEventHeader,
159 VESNotificationFieldsPOJO notifFields) throws JsonProcessingException {
160 Map<String, Object> innerEvent = new HashMap<String, Object>();
161 innerEvent.put("commonEventHeader", commonEventHeader);
162 innerEvent.put("notificationFields", notifFields);
164 Map<String, Object> outerEvent = new HashMap<String, Object>();
165 outerEvent.put("event", innerEvent);
166 LOG.debug("In generateVESEvent - {}", objMapper.writeValueAsString(outerEvent));
167 return new VESMessage(objMapper.writeValueAsString(outerEvent));
171 * Generates VES Event JSON containing commonEventHeader and faultFields fields
173 * @param commonEventHeader
175 * @return VESMessage - representing the VES Event JSON
176 * @throws JsonProcessingException
179 public VESMessage generateVESEvent(VESCommonEventHeaderPOJO commonEventHeader, VESFaultFieldsPOJO faultFields) throws JsonProcessingException {
180 Map<String, Object> innerEvent = new HashMap<String, Object>();
181 innerEvent.put("commonEventHeader", commonEventHeader);
182 innerEvent.put("faultFields", faultFields);
184 Map<String, Object> outerEvent = new HashMap<String, Object>();
185 outerEvent.put("event", innerEvent);
186 return new VESMessage(objMapper.writeValueAsString(outerEvent));
190 * Generates VES Event JSON containing commonEventHeader and pnfRegistration fields
192 * @param commonEventHeader
193 * @param pnfRegistrationFields
194 * @return VESMessage - representing the VES Event JSON
195 * @throws JsonProcessingException
198 public VESMessage generateVESEvent(VESCommonEventHeaderPOJO commonEventHeader, VESPNFRegistrationFieldsPOJO pnfRegistrationFields) throws JsonProcessingException {
199 Map<String, Object> innerEvent = new HashMap<String, Object>();
200 innerEvent.put("commonEventHeader", commonEventHeader);
201 innerEvent.put("pnfRegistrationFields", pnfRegistrationFields);
203 Map<String, Object> outerEvent = new HashMap<String, Object>();
204 outerEvent.put("event", innerEvent);
205 return new VESMessage(objMapper.writeValueAsString(outerEvent));
209 * Generates VES Event JSON containing commonEventHeader and stndDefined fields
211 * @param commonEventHeader
212 * @param stndDefinedFields
213 * @return VESMessage - representing the VES Event JSON
214 * @throws JsonProcessingException
217 public VESMessage generateVESEvent(VESCommonEventHeaderPOJO commonEventHeader,
218 VESStndDefinedFieldsPOJO stndDefinedFields) throws JsonProcessingException {
219 Map<String, Object> innerEvent = new HashMap<String, Object>();
220 innerEvent.put("commonEventHeader", commonEventHeader);
221 innerEvent.put("stndDefinedFields", stndDefinedFields);
223 Map<String, Object> outerEvent = new HashMap<String, Object>();
224 outerEvent.put("event", innerEvent);
225 return new VESMessage(objMapper.writeValueAsString(outerEvent));