2  * ============LICENSE_START========================================================================
 
   3  * ONAP : ccsdk feature sdnr wt mountpoint-registrar
 
   4  * =================================================================================================
 
   5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
 
   6  * Copyright (C) 2021 Samsung Electronics Intellectual Property. All rights reserved.
 
   7  * =================================================================================================
 
   8  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 
   9  * in compliance with the License. You may obtain a copy of the License at
 
  11  * http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software distributed under the License
 
  14  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 
  15  * or implied. See the License for the specific language governing permissions and limitations under
 
  17  * ============LICENSE_END==========================================================================
 
  20 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.vesdomain.pnfreg;
 
  22 import com.fasterxml.jackson.databind.JsonNode;
 
  23 import com.fasterxml.jackson.databind.ObjectMapper;
 
  24 import java.io.IOException;
 
  26 import org.eclipse.jdt.annotation.Nullable;
 
  27 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.config.GeneralConfig;
 
  28 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.StrimziKafkaVESMsgConsumerImpl;
 
  29 import org.slf4j.Logger;
 
  30 import org.slf4j.LoggerFactory;
 
  32 public class StrimziKafkaPNFRegVESMsgConsumer extends StrimziKafkaVESMsgConsumerImpl {
 
  34     private static final Logger LOG = LoggerFactory.getLogger(StrimziKafkaPNFRegVESMsgConsumer.class);
 
  35     private static final String DEFAULT_PROTOCOL = "SSH";
 
  36     private static final String DEFAULT_PORT = "17830";
 
  37     private static final String DEFAULT_USERNAME = "netconf";
 
  38     private static final String DEFAULT_PASSWORD = "netconf";
 
  41     public StrimziKafkaPNFRegVESMsgConsumer(GeneralConfig generalConfig) {
 
  46     public void processMsg(String msg) {
 
  47         LOG.debug("Message from Kafka topic is - {} ", msg);
 
  51         String pnfCommProtocol;
 
  55         String pnfKeyId = null;
 
  59         String pnfPasswd = null;
 
  60         String reportingEntityName;
 
  61         ObjectMapper oMapper = new ObjectMapper();
 
  62         JsonNode sKafkaMessageRootNode;
 
  64             sKafkaMessageRootNode = oMapper.readTree(msg);
 
  65             reportingEntityName = sKafkaMessageRootNode.at("/event/commonEventHeader/reportingEntityName").textValue();
 
  66             if (reportingEntityName.equals("ONAP SDN-R")) {
 
  68                         "VES PNF Registration message generated by SDNR, hence no need to process any further; Ignoring the received message");
 
  72             pnfId = sKafkaMessageRootNode.at("/event/commonEventHeader/sourceName").textValue();
 
  73             pnfIPAddress = getPNFIPAddress(sKafkaMessageRootNode);
 
  75                     sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/protocol").textValue();
 
  76             pnfCommPort = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/oamPort").textValue();
 
  77             if (pnfCommProtocol != null) {
 
  78                 if (pnfCommProtocol.equalsIgnoreCase("TLS")) {
 
  79                     // Read username and keyId
 
  81                             sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/keyId").textValue();
 
  82                     pnfUsername = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/username")
 
  84                 } else if (pnfCommProtocol.equalsIgnoreCase("SSH")) {
 
  85                     // Read username and password
 
  86                     pnfUsername = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/username")
 
  88                     pnfPasswd = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/password")
 
  91                     // log warning - Unknown protocol
 
  92                     LOG.warn("Only SSH and TLS protocols supported. Protocol specified in VES message is - {}",
 
  93                             pnfCommProtocol, ". Defaulting to SSH");
 
  94                     pnfCommProtocol = DEFAULT_PROTOCOL;
 
  95                     pnfCommPort = DEFAULT_PORT;
 
  96                     pnfUsername = DEFAULT_USERNAME;
 
  97                     pnfPasswd = DEFAULT_PASSWORD;
 
 100                 LOG.warn("Protocol not specified in VES message, Defaulting to SSH");
 
 101                 pnfCommProtocol = DEFAULT_PROTOCOL;
 
 102                 pnfCommPort = DEFAULT_PORT;
 
 103                 pnfUsername = DEFAULT_USERNAME;
 
 104                 pnfPasswd = DEFAULT_PASSWORD;
 
 108                     "PNF Fields - ID - {} : IP Address - {} : Protocol - {} : TLS Key ID - {} : User - {} : Port - {}",
 
 109                     pnfId, pnfIPAddress, pnfCommProtocol, pnfKeyId, pnfUsername, pnfCommPort);
 
 111             String baseUrl = getBaseUrl();
 
 112             String sdnrUser = getSDNRUser();
 
 113             String sdnrPasswd = getSDNRPasswd();
 
 115             if (hasNullInRequiredField(pnfId, pnfIPAddress, pnfCommPort, pnfCommProtocol, pnfUsername)) {
 
 116                 LOG.warn("One of the mandatory fields has a null value - pnfId = {} : pnfIPAddress = {} : " +
 
 117                          "pnfCommProtocol = {} : pnfUsername {} : pnfCommPort {} - not invoking mountpoint creation",
 
 118                           pnfId, pnfIPAddress, pnfCommProtocol, pnfUsername, pnfCommPort);
 
 122             Map<String, String> payloadMap = PNFMountPointClient.createPNFNotificationPayloadMap(pnfId, pnfIPAddress,
 
 123                     pnfCommPort, pnfCommProtocol, pnfUsername, pnfPasswd, pnfKeyId);
 
 125             PNFMountPointClient mountPointClient = new PNFMountPointClient(baseUrl);
 
 126             LOG.debug("Setting RESTConf Authorization values - {} : {}", sdnrUser, sdnrPasswd);
 
 127             mountPointClient.setAuthorization(sdnrUser, sdnrPasswd);
 
 128             String message = mountPointClient.prepareMessageFromPayloadMap(payloadMap);
 
 129             mountPointClient.sendNotification(message);
 
 131         } catch (IOException e) {
 
 132             LOG.info("Cannot parse json object, ignoring the received PNF Registration VES Message. Reason: {}",
 
 137     private boolean hasNullInRequiredField(String pnfId, String pnfIPAddress, String pnfCommPort,
 
 138                                            String pnfCommProtocol, String pnfUsername) {
 
 140         return pnfId == null || pnfIPAddress == null || pnfCommProtocol == null ||
 
 141                 pnfCommPort == null || pnfUsername == null;
 
 144     private String getPNFIPAddress(JsonNode sKafkaMessageRootNode) {
 
 145         String ipAddress = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/oamV6IpAddress").textValue();
 
 146         if (ipAddress != null && ipAddress != "")
 
 149         ipAddress = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/oamV4IpAddress").textValue();
 
 150         if (ipAddress != null && ipAddress != "")