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 {
54 private static final Logger LOG = LoggerFactory.getLogger(VESCollectorServiceImpl.class);
55 private final VESCollectorCfgImpl vesConfig;
56 private final ConfigurationFileRepresentation cfg;
57 private BaseHTTPClient httpClient;
58 private final Map<String, String> headerMap;
59 private List<VESCollectorConfigChangeListener> registeredObjects;
60 private final ObjectMapper objMapper;
63 public VESCollectorServiceImpl(ConfigurationFileRepresentation config) {
64 registeredObjects = new ArrayList<VESCollectorConfigChangeListener>();
65 this.vesConfig = new VESCollectorCfgImpl(config);
67 this.cfg.registerConfigChangedListener(this);
68 this.objMapper = new ObjectMapper();
70 httpClient = new BaseHTTPClient(getBaseUrl(), this.vesConfig.isTrustAllCerts());
72 this.headerMap = new HashMap<>();
73 this.headerMap.put("Content-Type", "application/json");
74 this.headerMap.put("Accept", "application/json");
76 setAuthorization(getConfig().getUsername(), getConfig().getPassword());
80 public VESCollectorCfgImpl getConfig() {
81 return this.vesConfig;
84 public String getBaseUrl() {
85 LOG.debug("IP Address is - {}", getConfig().getIP());
86 if (!getConfig().getTLSEnabled()) {
87 return "http://" + getConfig().getIP() + ":" + getConfig().getPort();
89 return "https://" + getConfig().getIP() + ":" + getConfig().getPort();
93 private void setAuthorization(String username, String password) {
94 if (getConfig().getTLSEnabled()) {
95 String credentials = username + ":" + password;
96 this.headerMap.put("Authorization",
97 "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes())));
103 public boolean publishVESMessage(VESMessage message) {
104 LOG.debug("In VESClient - {} ", message.getMessage());
105 BaseHTTPResponse response;
107 String uri = "eventListener" + "/" + getConfig().getVersion();
108 response = httpClient.sendRequest(uri, "POST", message.getMessage(), headerMap);
109 LOG.debug("finished with responsecode {}", response.code);
110 return response.code == 200;
111 } catch (IOException e) {
112 LOG.warn("problem publishing VES message {} ", e.getMessage());
119 public void close() throws Exception {
120 this.cfg.unregisterConfigChangedListener(this);
124 public void onConfigChanged() {
125 LOG.debug("In onConfigChanged - isTrustAllCerts = {} getBaseUrl = {}", getConfig().isTrustAllCerts(),
127 httpClient = new BaseHTTPClient(getBaseUrl(), this.vesConfig.isTrustAllCerts());
128 setAuthorization(getConfig().getUsername(), getConfig().getPassword());
129 Iterator<VESCollectorConfigChangeListener> it = registeredObjects.iterator();
130 while (it.hasNext()) {
131 VESCollectorConfigChangeListener o = it.next();
132 o.notify(getConfig());
137 public void registerForChanges(VESCollectorConfigChangeListener o) {
138 registeredObjects.add(o);
142 public void deregister(VESCollectorConfigChangeListener o) {
143 registeredObjects.remove(o);
147 public @NonNull NotificationProxyParser getNotificationProxyParser() {
148 return new NotificationProxyParserImpl();
152 * Generates VES Event JSON containing commonEventHeader and notificationFields fields
154 * @param commonEventHeader
156 * @return VESMessage - representing the VESEvent JSON
157 * @throws JsonProcessingException
160 public VESMessage generateVESEvent(VESCommonEventHeaderPOJO commonEventHeader,
161 VESNotificationFieldsPOJO notifFields) throws JsonProcessingException {
162 final var evt = Map.of("event",
163 Map.of("commonEventHeader", commonEventHeader, "notificationFields", notifFields));
164 final var str = objMapper.writeValueAsString(evt);
165 LOG.debug("In generateVESEvent - {}", str);
166 return new VESMessage(str);
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)
179 throws JsonProcessingException {
180 final var evt = Map.of("event",
181 Map.of("commonEventHeader", commonEventHeader, "faultFields", faultFields));
182 return new VESMessage(objMapper.writeValueAsString(evt));
186 * Generates VES Event JSON containing commonEventHeader and pnfRegistration fields
188 * @param commonEventHeader
189 * @param pnfRegistrationFields
190 * @return VESMessage - representing the VES Event JSON
191 * @throws JsonProcessingException
194 public VESMessage generateVESEvent(VESCommonEventHeaderPOJO commonEventHeader,
195 VESPNFRegistrationFieldsPOJO pnfRegistrationFields) throws JsonProcessingException {
196 final var evt = Map.of("event",
197 Map.of("commonEventHeader", commonEventHeader, "pnfRegistrationFields", pnfRegistrationFields));
198 return new VESMessage(objMapper.writeValueAsString(evt));
202 * Generates VES Event JSON containing commonEventHeader and stndDefined fields
204 * @param commonEventHeader
205 * @param stndDefinedFields
206 * @return VESMessage - representing the VES Event JSON
207 * @throws JsonProcessingException
210 public VESMessage generateVESEvent(VESCommonEventHeaderPOJO commonEventHeader,
211 VESStndDefinedFieldsPOJO stndDefinedFields) throws JsonProcessingException {
212 final var evt = Map.of("event",
213 Map.of("commonEventHeader", commonEventHeader, "stndDefinedFields", stndDefinedFields));
214 return new VESMessage(objMapper.writeValueAsString(evt));