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 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl;
 
  21 import java.io.IOException;
 
  22 import java.util.Base64;
 
  23 import java.util.HashMap;
 
  25 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient;
 
  26 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
 
  27 import org.slf4j.Logger;
 
  28 import org.slf4j.LoggerFactory;
 
  30 public class PNFMountPointClient extends BaseHTTPClient {
 
  32     private static final Logger LOG = LoggerFactory.getLogger(PNFMountPointClient.class);
 
  33     private static final String MOUNTPOINT_URI =
 
  34             "restconf/config/network-topology:network-topology/topology/topology-netconf/node/";
 
  35     private final Map<String, String> headerMap;
 
  37     private static final String SSH_PAYLOAD = "<node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\">\n"
 
  38             + "  <node-id>@device-name@</node-id>\n"
 
  39             + "  <host xmlns=\"urn:opendaylight:netconf-node-topology\">@device-ip@</host>\n"
 
  40             + "  <port xmlns=\"urn:opendaylight:netconf-node-topology\">@device-port@</port>\n"
 
  41             + "  <username xmlns=\"urn:opendaylight:netconf-node-topology\">@username@</username>\n"
 
  42             + "  <password xmlns=\"urn:opendaylight:netconf-node-topology\">@password@</password>\n"
 
  43             + "  <tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only>\n"
 
  44             + "  <!-- non-mandatory fields with default values, you can safely remove these if you do not wish to override any of these values-->\n"
 
  45             + "  <reconnect-on-changed-schema xmlns=\"urn:opendaylight:netconf-node-topology\">false</reconnect-on-changed-schema>\n"
 
  46             + "  <connection-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">20000</connection-timeout-millis>\n"
 
  47             + "  <max-connection-attempts xmlns=\"urn:opendaylight:netconf-node-topology\">0</max-connection-attempts>\n"
 
  48             + "  <between-attempts-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">2000</between-attempts-timeout-millis>\n"
 
  49             + "  <sleep-factor xmlns=\"urn:opendaylight:netconf-node-topology\">1.5</sleep-factor>\n"
 
  50             + "  <!-- keepalive-delay set to 0 turns off keepalives-->\n"
 
  51             + "  <keepalive-delay xmlns=\"urn:opendaylight:netconf-node-topology\">120</keepalive-delay>\n"
 
  55     private static final String TLS_PAYLOAD = "<node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\">\n"
 
  56             + "  <node-id>@device-name@</node-id>\n"
 
  57             + "  <host xmlns=\"urn:opendaylight:netconf-node-topology\">@device-ip@</host>\n"
 
  58             + "  <port xmlns=\"urn:opendaylight:netconf-node-topology\">@device-port@</port>\n"
 
  59             + "  <key-based xmlns=\"urn:opendaylight:netconf-node-topology\">\n"
 
  60             + "   <username xmlns=\"urn:opendaylight:netconf-node-topology\">@username@</username>\n"
 
  61             + "   <key-id>@key-id@</key-id>\n"
 
  63             + "  <tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only>\n"
 
  64             + "  <protocol xmlns=\"urn:opendaylight:netconf-node-topology\">\n"
 
  65             + " <name>TLS</name>\n"
 
  67             + " <!-- non-mandatory fields with default values, you can safely remove these if you do not wish to override any of these values-->\n"
 
  68             + " <reconnect-on-changed-schema xmlns=\"urn:opendaylight:netconf-node-topology\">false</reconnect-on-changed-schema>\n"
 
  69             + " <connection-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">20000</connection-timeout-millis>\n"
 
  70             + " <max-connection-attempts xmlns=\"urn:opendaylight:netconf-node-topology\">0</max-connection-attempts>\n"
 
  71             + " <between-attempts-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">2000</between-attempts-timeout-millis>\n"
 
  72             + " <sleep-factor xmlns=\"urn:opendaylight:netconf-node-topology\">1.5</sleep-factor>\n"
 
  73             + " <!-- keepalive-delay set to 0 turns off keepalives-->\n"
 
  74             + " <keepalive-delay xmlns=\"urn:opendaylight:netconf-node-topology\">120</keepalive-delay>\n"
 
  78     public PNFMountPointClient(String baseUrl) {
 
  81         this.headerMap = new HashMap<>();
 
  82         this.headerMap.put("Content-Type", "application/xml");
 
  83         this.headerMap.put("Accept", "application/xml");
 
  86     public void setAuthorization(String username, String password) {
 
  87         String credentials = username + ":" + password;
 
  88         this.headerMap.put("Authorization", "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes())));
 
  92     public boolean pnfMountPointCreate(String pnfName, String ipv4Address, String protocol, String keyId,
 
  93             String username, String password, String commPort) {
 
  95         if (protocol.equals("TLS")) {
 
  96             message = updateTLSPayload(pnfName, ipv4Address, username, keyId, commPort);
 
  98             message = updatePayload(pnfName, ipv4Address, username, password, commPort);
 
  99             LOG.debug("Payload after updating values is: {}", message);
 
 101         return pnfRequest(pnfName, "PUT", message) == 200;
 
 105     private static String updatePayload(String pnfName, String ipv4Address, String username, String password,
 
 108         return SSH_PAYLOAD.replace("@device-name@", pnfName)
 
 109                 .replace("@device-ip@", ipv4Address)
 
 110                 .replace("@device-port@", portNo)
 
 111                 .replace("@username@", username)
 
 112                 .replace("@password@", password);
 
 116     private static String updateTLSPayload(String pnfName, String ipv4Address, String username, String keyId,
 
 119         return TLS_PAYLOAD.replace("@device-name@", pnfName)
 
 120                 .replace("@device-ip@", ipv4Address)
 
 121                 .replace("@username@", username)
 
 122                 .replace("@key-id@", keyId)
 
 123                 .replace("@device-port@", portNo);
 
 127     private int pnfRequest(String pnfName, String method, String message) {
 
 128         LOG.info("In pnfRequest - {} : {} : {}", pnfName, method, message);
 
 129         BaseHTTPResponse response;
 
 131             //String uri = "http://localhost:8181/restconf/config/network-topology:network-topology/topology/topology-netconf/node/" + pnfName;
 
 132             String uri = MOUNTPOINT_URI + pnfName;
 
 133             response = this.sendRequest(uri, method, message, headerMap);
 
 134             LOG.debug("finished with responsecode {}", response.code);
 
 135             return response.code;
 
 136         } catch (IOException e) {
 
 137             LOG.warn("problem registering {} : {}", pnfName, e.getMessage());