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 java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.BaseRequest;
29 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient;
30 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class PNFMountPointClient extends BaseHTTPClient {
36 private static final Logger LOG = LoggerFactory.getLogger(PNFMountPointClient.class);
37 private static final String MOUNTPOINT_URI =
38 "restconf/config/network-topology:network-topology/topology/topology-netconf/node/";
39 private final Map<String, String> headerMap;
41 private static final String SSH_PAYLOAD = "<node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\">\n"
42 + " <node-id>@device-name@</node-id>\n"
43 + " <host xmlns=\"urn:opendaylight:netconf-node-topology\">@device-ip@</host>\n"
44 + " <port xmlns=\"urn:opendaylight:netconf-node-topology\">@device-port@</port>\n"
45 + " <username xmlns=\"urn:opendaylight:netconf-node-topology\">@username@</username>\n"
46 + " <password xmlns=\"urn:opendaylight:netconf-node-topology\">@password@</password>\n"
47 + " <tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only>\n"
48 + " <!-- non-mandatory fields with default values, you can safely remove these if you do not wish to override any of these values-->\n"
49 + " <reconnect-on-changed-schema xmlns=\"urn:opendaylight:netconf-node-topology\">false</reconnect-on-changed-schema>\n"
50 + " <connection-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">20000</connection-timeout-millis>\n"
51 + " <max-connection-attempts xmlns=\"urn:opendaylight:netconf-node-topology\">0</max-connection-attempts>\n"
52 + " <between-attempts-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">2000</between-attempts-timeout-millis>\n"
53 + " <sleep-factor xmlns=\"urn:opendaylight:netconf-node-topology\">1.5</sleep-factor>\n"
54 + " <!-- keepalive-delay set to 0 turns off keepalives-->\n"
55 + " <keepalive-delay xmlns=\"urn:opendaylight:netconf-node-topology\">120</keepalive-delay>\n"
59 private static final String TLS_PAYLOAD = "<node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\">\n"
60 + " <node-id>@device-name@</node-id>\n"
61 + " <host xmlns=\"urn:opendaylight:netconf-node-topology\">@device-ip@</host>\n"
62 + " <port xmlns=\"urn:opendaylight:netconf-node-topology\">@device-port@</port>\n"
63 + " <key-based xmlns=\"urn:opendaylight:netconf-node-topology\">\n"
64 + " <username xmlns=\"urn:opendaylight:netconf-node-topology\">@username@</username>\n"
65 + " <key-id>@key-id@</key-id>\n"
67 + " <tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only>\n"
68 + " <protocol xmlns=\"urn:opendaylight:netconf-node-topology\">\n"
69 + " <name>TLS</name>\n"
71 + "<!-- non-mandatory fields with default values, you can safely remove these if you do not wish to override any of these values-->\n"
72 + "<reconnect-on-changed-schema xmlns=\"urn:opendaylight:netconf-node-topology\">false</reconnect-on-changed-schema>\n"
73 + "<connection-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">20000</connection-timeout-millis>\n"
74 + "<max-connection-attempts xmlns=\"urn:opendaylight:netconf-node-topology\">0</max-connection-attempts>\n"
75 + "<between-attempts-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">2000</between-attempts-timeout-millis>\n"
76 + "<sleep-factor xmlns=\"urn:opendaylight:netconf-node-topology\">1.5</sleep-factor>\n"
77 + "<!-- keepalive-delay set to 0 turns off keepalives-->\n"
78 + "<keepalive-delay xmlns=\"urn:opendaylight:netconf-node-topology\">120</keepalive-delay>\n"
82 public PNFMountPointClient(String baseUrl) {
85 this.headerMap = new HashMap<>();
86 this.headerMap.put("Content-Type", "application/xml");
87 this.headerMap.put("Accept", "application/xml");
90 public void setAuthorization(String username, String password) {
91 String credentials = username + ":" + password;
92 this.headerMap.put("Authorization", "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes())));
96 public boolean pnfMountPointCreate(@NonNull String pnfName, @NonNull String ipAddress, @NonNull String protocol, String keyId,
97 String username, String password, @NonNull String commPort) {
99 if (protocol.equals("TLS")) {
100 message = updateTLSPayload(pnfName, ipAddress, username, keyId, commPort);
102 message = updatePayload(pnfName, ipAddress, username, password, commPort);
104 LOG.debug("Payload after updating values is: {}", redactMessage(message, protocol));
105 return pnfRequest(pnfName, "PUT", message) == 200;
109 private static String updatePayload(String pnfName, String ipAddress, String username, String password,
112 return SSH_PAYLOAD.replace("@device-name@", pnfName)
113 .replace("@device-ip@", ipAddress)
114 .replace("@device-port@", portNo)
115 .replace("@username@", username)
116 .replace("@password@", password);
120 private static String updateTLSPayload(String pnfName, String ipAddress, String username, String keyId,
123 return TLS_PAYLOAD.replace("@device-name@", pnfName)
124 .replace("@device-ip@", ipAddress)
125 .replace("@username@", username)
126 .replace("@key-id@", keyId)
127 .replace("@device-port@", portNo);
131 private int pnfRequest(String pnfName, String method, String message) {
132 LOG.info("In pnfRequest - {} : {} ", pnfName, method);
133 BaseHTTPResponse response;
135 String uri = MOUNTPOINT_URI + BaseRequest.urlEncodeValue(pnfName);
136 response = this.sendRequest(uri, method, message, headerMap);
137 LOG.debug("finished with responsecode {}", response.code);
138 return response.code;
139 } catch (IOException e) {
140 LOG.warn("problem registering {} : {}", pnfName, e.getMessage());
145 private String redactMessage(String message, String protocol) {
147 if (("TLS").equals(protocol)) {
148 REGEX = "(<key-id.*>)(.*)(<\\/key-id>)";
150 REGEX = "(<password.*>)(.*)(<\\/password>)";
152 Pattern p = Pattern.compile(REGEX, Pattern.MULTILINE);
153 Matcher matcher = p.matcher(message);
154 return matcher.replaceAll("$1*********$3");