2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
19 ecompProvider.sendProblemNotification(ownKeyName, notificationXml);
20 * ECOMP Messages are generated an send to destination
24 package org.onap.ccsdk.features.sdnr.wt.devicemanager.dcaeconnector.impl;
26 import java.io.IOException;
27 import java.net.HttpURLConnection;
28 import java.net.MalformedURLException;
30 import java.net.URLConnection;
31 import java.util.Optional;
32 import javax.net.ssl.HostnameVerifier;
33 import javax.net.ssl.HttpsURLConnection;
34 import javax.net.ssl.SSLContext;
35 import org.eclipse.jdt.annotation.Nullable;
36 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.NetconfTimeStamp;
37 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl;
38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerImpl;
39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.InternalSeverity;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml;
41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.InventoryProvider;
42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
43 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.InventoryInformationDcae;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class DcaeMessages {
49 private static final Logger LOG = LoggerFactory.getLogger(DcaeSenderImpl.class);
51 private static final String DCAE_NORMAL = "NORMAL";
52 private static final String DCAE_MINOR = "MINOR";
53 private static final String DCAE_WARNING = "WARNING";
54 private static final String DCAE_CRITICAL = "CRITICAL";
55 private static final String DCAE_MAJOR = "MAJOR";
57 private static final String eventNamePrefix = "fault_Microwave_Radio_Alarms";
58 private static final String eventType = "Microwave_Radio_Alarms";
59 private static final String eventSourceType = "Microwave_Radio";
61 private static final String charset = "UTF-8";
63 private static final HostnameVerifier allHostsValid = (hostname, session) -> true;
65 private static final String CONTENT_TYPE_APPJSON = "application/json";
67 private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStampImpl.getConverter();
69 //Configurable parameters
70 private final DcaeSender dcaeSender;
71 private final int heartbeatIntervallSeconds;
72 private final String entityName;
73 private final DeviceManagerImpl deviceManager;
76 private int heartbeatsequence = 0;
78 public DcaeMessages(DcaeSender ecompSender, String entityName, Integer heartbeatIntervallSeconds, DeviceManagerImpl deviceManager) {
79 this.dcaeSender = ecompSender;
80 this.entityName = entityName;
81 this.deviceManager = deviceManager;
82 this.heartbeatIntervallSeconds = heartbeatIntervallSeconds;
86 * Create a heartbeat message.
87 * @return Result string with answer from server
89 public String postHeartBeat() {
90 String epochTimeMicrosecondsString = getEpochTimeMicroseconds();
91 String body = assembleHeartbeatFromTemplate(null,
92 epochTimeMicrosecondsString,
94 NETCONFTIME_CONVERTER.getTimeStampAsNetconfString()).toString();
95 return dcaeSender.sendDcaePost( body);
99 * ONF 1.2 Problem Notification
100 * @param mountPointName self-explaining
101 * @param notification Notification input
102 * @return String with answer
105 public String postNotification(String mountPointName, ProblemNotificationXml notification) {
107 String problemName = notification.getProblem();
108 String sequence = notification.getCounter();
109 String objId = notification.getObjectId();
110 String severity = convert( notification.getSeverity());
111 String timeStamp = convert( notification.getTimeStamp() );
113 String body = assembleEventNotificationFromTemplate(null,
115 mountPointName, objId, problemName, severity, notification.getTimeStamp() ).toString();
117 return dcaeSender.sendDcaePost( body);
121 * Setup a connection to URL with authorisation header
122 * @param url e.g. "https://plan.fritz.box:9092/ux/#" or "
123 * @param basicAuth authorisation header like "Basic SGVyYmVydDpIZXJiZXJ0"
124 * @param insertContentHeader
125 * @return Null in case of error or the URLConnection
126 * @throws IOException
127 * @throws MalformedURLException
129 static @Nullable HttpURLConnection openConnection( URL url, String basicAuth, boolean insertContentHeader, @Nullable SSLContext sc) throws MalformedURLException, IOException {
131 //Prepare the connection
132 HttpURLConnection newHttpConnection = null;
134 URLConnection newConnection = url.openConnection();
135 if (newConnection instanceof HttpURLConnection) {
136 LOG.debug("Setup connection to {} ", url.toString());
138 newHttpConnection = (HttpURLConnection)newConnection;
140 newHttpConnection.setDoOutput(true); // Triggers POST.
141 newHttpConnection.setRequestProperty("Accept-Charset", charset);
142 if (basicAuth != null) {
143 newHttpConnection.setRequestProperty("Authorization", basicAuth);
145 if (insertContentHeader) {
146 newHttpConnection.setRequestProperty("Content-Type", CONTENT_TYPE_APPJSON);
149 if (newHttpConnection instanceof HttpsURLConnection) {
150 LOG.debug("SSL connection setup with trust all.");
151 HttpsURLConnection newHttpsConnection = (HttpsURLConnection)newHttpConnection;
153 newHttpsConnection.setSSLSocketFactory(sc.getSocketFactory());
155 LOG.warn("No SSL Contect available");
157 newHttpsConnection.setHostnameVerifier(allHostsValid);
160 LOG.warn("URL not a HTTP protocol: {}", url);
163 return newHttpConnection;
167 * Private function for message creation and with templates
171 * Get actual microseconds
174 private String getEpochTimeMicroseconds() {
175 long microseconds = System.nanoTime() / 1000;
176 return String.valueOf(microseconds);
180 * Assemble heartbeat message
181 * @param sb StringBuffer to be used or null to allocate
182 * @param epochTimeMicrosecondsString Text with time stamp
183 * @param sequence integer sequence number
184 * @param eventTimeValueNetconfFormatString like this: 2018-05-14T05:32:17.292Z
185 * @return StringBuffer with result
187 private StringBuffer assembleHeartbeatFromTemplate(
189 String epochTimeMicrosecondsString,
191 String eventTimeValueNetconfFormatString) {
194 sb = new StringBuffer();
198 " \"commonEventHeader\": {\n" +
199 " \"domain\": \"heartbeat\",\n" +
200 " \"eventId\": \"testpattern-ab305d54-85b4-a31b-7db2-fb6b9e546015\",\n" +
201 " \"eventName\": \"heartbeat_Controller\",\n" +
202 " \"eventType\": \"Controller\",\n" +
203 " \"priority\": \"Low\",\n" +
204 " \"reportingEntityId\": \"\",\n" +
205 " \"reportingEntityName\": \""+entityName+"\",\n" +
206 " \"sequence\": "+String.valueOf(sequence)+",\n" +
207 " \"sourceId\": \"\",\n" +
208 " \"sourceName\": \""+entityName+"\",\n" +
209 " \"startEpochMicrosec\": "+epochTimeMicrosecondsString+",\n" +
210 " \"lastEpochMicrosec\": "+epochTimeMicrosecondsString+",\n" +
211 " \"version\": 3.0\n" +
213 " \"heartbeatFields\": {\n" +
214 " \"additionalFields\": [\n" +
216 " \"name\": \"eventTime\",\n" +
217 " \"value\": \""+eventTimeValueNetconfFormatString+"\"\n" +
220 " \"heartbeatFieldsVersion\": 1.0,\n" +
221 " \"heartbeatInterval\": "+heartbeatIntervallSeconds+"\n" +
231 * Assemble notification message
232 * @param sb StringBuffer to be used or null to allocate
233 * @param epochTimeMicrosecondsString Text with time stamp
234 * @param sequence integer sequence number
235 * @param mountpointName
239 * @return StringBuffer with result
242 private StringBuffer assembleEventNotificationFromTemplate(StringBuffer sb,
243 String epochTimeMicrosecondsString, String sequence,
244 String mountpointName, String objId, String problemName, String severity, String eventTimeValueNetconfFormatString
248 sb = new StringBuffer();
251 NetworkElement optionalNe = deviceManager != null ? deviceManager.getNeByMountpoint(mountpointName) : null;
252 InventoryInformationDcae neInventory = InventoryInformationDcae.getDefault();
253 if (optionalNe != null) {
254 Optional<InventoryProvider> inventoryProvider = optionalNe.getService(InventoryProvider.class);
255 if (inventoryProvider.isPresent()) {
256 neInventory = inventoryProvider.get().getInventoryInformation();
262 " \"commonEventHeader\": {\n" +
263 " \"domain\": \"fault\",\n" +
264 " \"eventId\": \""+mountpointName+"_"+objId+"_"+problemName+"\",\n" +
265 " \"eventName\": \""+eventNamePrefix+"_"+problemName+"\",\n" +
266 " \"eventType\": \""+eventType+"\",\n" +
267 " \"sequence\": "+sequence+",\n" +
268 " \"priority\": \"High\",\n" +
269 " \"reportingEntityId\": \"\",\n" +
270 " \"reportingEntityName\": \""+entityName+"\",\n" +
271 " \"sourceId\": \"\",\n" +
272 " \"sourceName\": \""+mountpointName+"\",\n" +
273 " \"startEpochMicrosec\": "+epochTimeMicrosecondsString+",\n" +
274 " \"lastEpochMicrosec\": "+epochTimeMicrosecondsString+",\n" +
275 " \"version\": 3.0\n" +
277 " \"faultFields\": {\n" +
278 " \"alarmAdditionalInformation\": [\n" +
280 " \"name\": \"eventTime\",\n" +
281 " \"value\": \""+eventTimeValueNetconfFormatString+"\"\n" +
284 " \"name\": \"equipType\",\n" +
285 " \"value\": \""+neInventory.getType()+"\"\n" +
288 " \"name\": \"vendor\",\n" +
289 " \"value\": \""+neInventory.getVendor()+"\"\n" +
292 " \"name\": \"model\",\n" +
293 " \"value\": \""+neInventory.getModel()+"\"\n" +
296 " \"faultFieldsVersion\":2.0,\n" +
297 " \"eventSourceType\": \""+eventSourceType+"\",\n" +
298 " \"alarmCondition\": \""+problemName+"\",\n" +
299 " \"alarmInterfaceA\": \""+objId+"\",\n" +
300 " \"specificProblem\": \""+problemName+"\",\n" +
301 " \"eventSeverity\": \""+severity+"\",\n" +
302 " \"vfStatus\": \"Active\"\n" +
312 * Convert internal type formats into the Ecomp format
315 private String convert(InternalSeverity severity ) {
326 return DCAE_CRITICAL;
333 * Time has to be converted into milliseconds
334 * @param timeAsString time as string
337 private String convert(String timeAsString) {
339 long microseconds = -1;
341 microseconds = NETCONFTIME_CONVERTER.getTimeStampFromNetconfAsMilliseconds(timeAsString) * 1000;
342 } catch (IllegalArgumentException e) {
343 LOG.info("Can not convert timeAsString", e);
345 return String.valueOf(microseconds);