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,
 
  79             DeviceManagerImpl deviceManager) {
 
  80         this.dcaeSender = ecompSender;
 
  81         this.entityName = entityName;
 
  82         this.deviceManager = deviceManager;
 
  83         this.heartbeatIntervallSeconds = heartbeatIntervallSeconds;
 
  87      * Create a heartbeat message.
 
  89      * @return Result string with answer from server
 
  91     public String postHeartBeat() {
 
  92         String epochTimeMicrosecondsString = getEpochTimeMicroseconds();
 
  93         String body = assembleHeartbeatFromTemplate(null, epochTimeMicrosecondsString, heartbeatsequence++,
 
  94                 NETCONFTIME_CONVERTER.getTimeStampAsNetconfString()).toString();
 
  95         return dcaeSender.sendDcaePost(body);
 
  99      * ONF 1.2 Problem Notification
 
 101      * @param mountPointName self-explaining
 
 102      * @param notification Notification input
 
 103      * @return String with answer
 
 106     public String postNotification(String mountPointName, ProblemNotificationXml notification) {
 
 108         String problemName = notification.getProblem();
 
 109         String sequence = notification.getCounter();
 
 110         String objId = notification.getObjectId();
 
 111         String severity = convert(notification.getSeverity());
 
 112         String timeStamp = convert(notification.getTimeStamp());
 
 114         String body = assembleEventNotificationFromTemplate(null, timeStamp, sequence, mountPointName, objId,
 
 115                 problemName, severity, notification.getTimeStamp()).toString();
 
 117         return dcaeSender.sendDcaePost(body);
 
 121      * Setup a connection to URL with authorisation header
 
 123      * @param url e.g. "https://plan.fritz.box:9092/ux/#" or "
 
 124      * @param basicAuth authorisation header like "Basic SGVyYmVydDpIZXJiZXJ0"
 
 125      * @param insertContentHeader
 
 126      * @return Null in case of error or the URLConnection
 
 127      * @throws IOException
 
 128      * @throws MalformedURLException
 
 130     static @Nullable HttpURLConnection openConnection(URL url, String basicAuth, boolean insertContentHeader,
 
 131             @Nullable SSLContext sc) throws MalformedURLException, IOException {
 
 133         //Prepare the connection
 
 134         HttpURLConnection newHttpConnection = null;
 
 136             URLConnection newConnection = url.openConnection();
 
 137             if (newConnection instanceof HttpURLConnection) {
 
 138                 LOG.debug("Setup connection to {} ", url.toString());
 
 140                 newHttpConnection = (HttpURLConnection) newConnection;
 
 142                 newHttpConnection.setDoOutput(true); // Triggers POST.
 
 143                 newHttpConnection.setRequestProperty("Accept-Charset", charset);
 
 144                 if (basicAuth != null) {
 
 145                     newHttpConnection.setRequestProperty("Authorization", basicAuth);
 
 147                 if (insertContentHeader) {
 
 148                     newHttpConnection.setRequestProperty("Content-Type", CONTENT_TYPE_APPJSON);
 
 151                 if (newHttpConnection instanceof HttpsURLConnection) {
 
 152                     LOG.debug("SSL connection setup with trust all.");
 
 153                     HttpsURLConnection newHttpsConnection = (HttpsURLConnection) newHttpConnection;
 
 155                         newHttpsConnection.setSSLSocketFactory(sc.getSocketFactory());
 
 157                         LOG.warn("No SSL Contect available");
 
 159                     newHttpsConnection.setHostnameVerifier(allHostsValid);
 
 162                 LOG.warn("URL not a HTTP protocol: {}", url);
 
 165         return newHttpConnection;
 
 169      * Private function for message creation and with templates
 
 173      * Get actual microseconds
 
 177     private String getEpochTimeMicroseconds() {
 
 178         long microseconds = System.nanoTime() / 1000;
 
 179         return String.valueOf(microseconds);
 
 183      * Assemble heartbeat message
 
 185      * @param sb StringBuffer to be used or null to allocate
 
 186      * @param epochTimeMicrosecondsString Text with time stamp
 
 187      * @param sequence integer sequence number
 
 188      * @param eventTimeValueNetconfFormatString like this: 2018-05-14T05:32:17.292Z
 
 189      * @return StringBuffer with result
 
 191     private StringBuffer assembleHeartbeatFromTemplate(StringBuffer sb, String epochTimeMicrosecondsString,
 
 192             int sequence, String eventTimeValueNetconfFormatString) {
 
 195             sb = new StringBuffer();
 
 200                 + "        \"commonEventHeader\": {\n"
 
 201                 + "            \"domain\": \"heartbeat\",\n"
 
 202                 + "            \"eventId\": \"testpattern-ab305d54-85b4-a31b-7db2-fb6b9e546015\",\n"
 
 203                 + "            \"eventName\": \"heartbeat_Controller\",\n"
 
 204                 + "            \"eventType\": \"Controller\",\n"
 
 205                 + "            \"priority\": \"Low\",\n"
 
 206                 + "            \"reportingEntityId\": \"\",\n"
 
 207                 + "            \"reportingEntityName\": \"" + entityName + "\",\n"
 
 208                 + "            \"sequence\": " + String.valueOf(sequence) + ",\n"
 
 209                 + "            \"sourceId\": \"\",\n"
 
 210                 + "            \"sourceName\": \"" + entityName + "\",\n"
 
 211                 + "            \"startEpochMicrosec\": " + epochTimeMicrosecondsString + ",\n"
 
 212                 + "            \"lastEpochMicrosec\": " + epochTimeMicrosecondsString + ",\n"
 
 213                 + "            \"version\": 3.0\n"
 
 215                 + "        \"heartbeatFields\": {\n"
 
 216                 + "            \"additionalFields\": [\n"
 
 218                 + "                   \"name\": \"eventTime\",\n"
 
 219                 + "                   \"value\": \"" + eventTimeValueNetconfFormatString + "\"\n"
 
 222                 + "            \"heartbeatFieldsVersion\": 1.0,\n"
 
 223                 + "            \"heartbeatInterval\": " + heartbeatIntervallSeconds + "\n"
 
 233      * Assemble notification message
 
 235      * @param sb StringBuffer to be used or null to allocate
 
 236      * @param epochTimeMicrosecondsString Text with time stamp
 
 237      * @param sequence integer sequence number
 
 238      * @param mountpointName
 
 242      * @return StringBuffer with result
 
 245     private StringBuffer assembleEventNotificationFromTemplate(StringBuffer sb, String epochTimeMicrosecondsString,
 
 246             String sequence, String mountpointName, String objId, String problemName, String severity,
 
 247             String eventTimeValueNetconfFormatString) {
 
 250             sb = new StringBuffer();
 
 253         NetworkElement optionalNe =
 
 254                 deviceManager != null ? deviceManager.getNeByMountpoint(mountpointName) : null;
 
 255         InventoryInformationDcae neInventory = InventoryInformationDcae.getDefault();
 
 256         if (optionalNe != null) {
 
 257             Optional<InventoryProvider> inventoryProvider = optionalNe.getService(InventoryProvider.class);
 
 258             if (inventoryProvider.isPresent()) {
 
 259                 neInventory = inventoryProvider.get().getInventoryInformation();
 
 266                 + "        \"commonEventHeader\": {\n"
 
 267                 + "            \"domain\": \"fault\",\n"
 
 268                 + "            \"eventId\": \"" + mountpointName + "_" + objId + "_" + problemName + "\",\n"
 
 269                 + "            \"eventName\": \"" + eventNamePrefix + "_" + problemName + "\",\n"
 
 270                 + "            \"eventType\": \"" + eventType + "\",\n"
 
 271                 + "            \"sequence\": " + sequence + ",\n"
 
 272                 + "            \"priority\": \"High\",\n"
 
 273                 + "            \"reportingEntityId\": \"\",\n"
 
 274                 + "            \"reportingEntityName\": \"" + entityName + "\",\n"
 
 275                 + "            \"sourceId\": \"\",\n"
 
 276                 + "            \"sourceName\": \"" + mountpointName + "\",\n"
 
 277                 + "            \"startEpochMicrosec\": " + epochTimeMicrosecondsString + ",\n"
 
 278                 + "            \"lastEpochMicrosec\": " + epochTimeMicrosecondsString + ",\n"
 
 279                 + "            \"version\": 3.0\n"
 
 281                 + "        \"faultFields\": {\n"
 
 282                 + "            \"alarmAdditionalInformation\": [\n"
 
 284                 + "                   \"name\": \"eventTime\",\n"
 
 285                 + "                   \"value\": \"" + eventTimeValueNetconfFormatString + "\"\n"
 
 288                 + "                   \"name\": \"equipType\",\n"
 
 289                 + "                   \"value\": \"" + neInventory.getType() + "\"\n"
 
 292                 + "                   \"name\": \"vendor\",\n"
 
 293                 + "                   \"value\": \"" + neInventory.getVendor() + "\"\n"
 
 295                 + "                   \"name\": \"model\",\n"
 
 296                 + "                   \"value\": \"" + neInventory.getModel() + "\"\n"
 
 299                 + "            \"faultFieldsVersion\":2.0,\n"
 
 300                 + "            \"eventSourceType\": \"" + eventSourceType  + "\",\n"
 
 301                 + "            \"alarmCondition\": \"" + problemName + "\",\n"
 
 302                 + "            \"alarmInterfaceA\": \"" + objId + "\",\n"
 
 303                 + "            \"specificProblem\": \"" + problemName + "\",\n"
 
 304                 + "            \"eventSeverity\": \"" + severity + "\",\n"
 
 305                 + "            \"vfStatus\": \"Active\"\n"
 
 315      * Convert internal type formats into the Ecomp format
 
 318     private String convert(InternalSeverity severity) {
 
 329                 return DCAE_CRITICAL;
 
 336      * Time has to be converted into milliseconds
 
 338      * @param timeAsString time as string
 
 341     private String convert(String timeAsString) {
 
 343         long microseconds = -1;
 
 345             microseconds = NETCONFTIME_CONVERTER.getTimeStampFromNetconfAsMilliseconds(timeAsString) * 1000;
 
 346         } catch (IllegalArgumentException e) {
 
 347             LOG.info("Can not convert timeAsString", e);
 
 349         return String.valueOf(microseconds);