029ae4c5513385e10d85393610e2c301c6db44f9
[ccsdk/features.git] /
1 /*
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18
19 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl;
20
21 import java.io.IOException;
22 import java.util.Base64;
23 import java.util.HashMap;
24 import java.util.Map;
25 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.BaseRequest;
26 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient;
27 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class PNFMountPointClient extends BaseHTTPClient {
32
33     private static final Logger LOG = LoggerFactory.getLogger(PNFMountPointClient.class);
34     private static final String MOUNTPOINT_URI =
35             "restconf/config/network-topology:network-topology/topology/topology-netconf/node/";
36     private final Map<String, String> headerMap;
37     // @formatter:off
38     private static final String SSH_PAYLOAD = "<node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\">\n"
39             + "  <node-id>@device-name@</node-id>\n"
40             + "  <host xmlns=\"urn:opendaylight:netconf-node-topology\">@device-ip@</host>\n"
41             + "  <port xmlns=\"urn:opendaylight:netconf-node-topology\">@device-port@</port>\n"
42             + "  <username xmlns=\"urn:opendaylight:netconf-node-topology\">@username@</username>\n"
43             + "  <password xmlns=\"urn:opendaylight:netconf-node-topology\">@password@</password>\n"
44             + "  <tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only>\n"
45             + "  <!-- non-mandatory fields with default values, you can safely remove these if you do not wish to override any of these values-->\n"
46             + "  <reconnect-on-changed-schema xmlns=\"urn:opendaylight:netconf-node-topology\">false</reconnect-on-changed-schema>\n"
47             + "  <connection-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">20000</connection-timeout-millis>\n"
48             + "  <max-connection-attempts xmlns=\"urn:opendaylight:netconf-node-topology\">0</max-connection-attempts>\n"
49             + "  <between-attempts-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">2000</between-attempts-timeout-millis>\n"
50             + "  <sleep-factor xmlns=\"urn:opendaylight:netconf-node-topology\">1.5</sleep-factor>\n"
51             + "  <!-- keepalive-delay set to 0 turns off keepalives-->\n"
52             + "  <keepalive-delay xmlns=\"urn:opendaylight:netconf-node-topology\">120</keepalive-delay>\n"
53             + "</node>";
54     // @formatter:on
55     // @formatter:off
56     private static final String TLS_PAYLOAD = "<node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\">\n"
57             + "  <node-id>@device-name@</node-id>\n"
58             + "  <host xmlns=\"urn:opendaylight:netconf-node-topology\">@device-ip@</host>\n"
59             + "  <port xmlns=\"urn:opendaylight:netconf-node-topology\">@device-port@</port>\n"
60             + "  <key-based xmlns=\"urn:opendaylight:netconf-node-topology\">\n"
61             + "  <username xmlns=\"urn:opendaylight:netconf-node-topology\">@username@</username>\n"
62             + "  <key-id>@key-id@</key-id>\n"
63             + "  </key-based>\n"
64             + "  <tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only>\n"
65             + "  <protocol xmlns=\"urn:opendaylight:netconf-node-topology\">\n"
66             + "  <name>TLS</name>\n"
67             + "  </protocol>\n"
68             + "<!-- non-mandatory fields with default values, you can safely remove these if you do not wish to override any of these values-->\n"
69             + "<reconnect-on-changed-schema xmlns=\"urn:opendaylight:netconf-node-topology\">false</reconnect-on-changed-schema>\n"
70             + "<connection-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">20000</connection-timeout-millis>\n"
71             + "<max-connection-attempts xmlns=\"urn:opendaylight:netconf-node-topology\">0</max-connection-attempts>\n"
72             + "<between-attempts-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">2000</between-attempts-timeout-millis>\n"
73             + "<sleep-factor xmlns=\"urn:opendaylight:netconf-node-topology\">1.5</sleep-factor>\n"
74             + "<!-- keepalive-delay set to 0 turns off keepalives-->\n"
75             + "<keepalive-delay xmlns=\"urn:opendaylight:netconf-node-topology\">120</keepalive-delay>\n"
76             + "</node>";
77     // @formatter:on
78
79     public PNFMountPointClient(String baseUrl) {
80         super(baseUrl);
81
82         this.headerMap = new HashMap<>();
83         this.headerMap.put("Content-Type", "application/xml");
84         this.headerMap.put("Accept", "application/xml");
85     }
86
87     public void setAuthorization(String username, String password) {
88         String credentials = username + ":" + password;
89         this.headerMap.put("Authorization", "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes())));
90
91     }
92
93     public boolean pnfMountPointCreate(String pnfName, String ipv4Address, String protocol, String keyId,
94             String username, String password, String commPort) {
95         String message = "";
96         if (protocol.equals("TLS")) {
97             message = updateTLSPayload(pnfName, ipv4Address, username, keyId, commPort);
98         } else { //SSH
99             message = updatePayload(pnfName, ipv4Address, username, password, commPort);
100             LOG.debug("Payload after updating values is: {}", message);
101         }
102         return pnfRequest(pnfName, "PUT", message) == 200;
103
104     }
105
106     private static String updatePayload(String pnfName, String ipv4Address, String username, String password,
107             String portNo) {
108         // @formatter:off
109         return SSH_PAYLOAD.replace("@device-name@", pnfName)
110                 .replace("@device-ip@", ipv4Address)
111                 .replace("@device-port@", portNo)
112                 .replace("@username@", username)
113                 .replace("@password@", password);
114         // @formatter:on
115     }
116
117     private static String updateTLSPayload(String pnfName, String ipv4Address, String username, String keyId,
118             String portNo) {
119         // @formatter:off
120         return TLS_PAYLOAD.replace("@device-name@", pnfName)
121                 .replace("@device-ip@", ipv4Address)
122                 .replace("@username@", username)
123                 .replace("@key-id@", keyId)
124                 .replace("@device-port@", portNo);
125         // @formatter:on
126     }
127
128     private int pnfRequest(String pnfName, String method, String message) {
129         LOG.info("In pnfRequest - {} : {} : {}", pnfName, method, message);
130         BaseHTTPResponse response;
131         try {
132             String uri = MOUNTPOINT_URI + BaseRequest.urlEncodeValue(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());
138             return -1;
139         }
140     }
141
142 }