22b4e5f22a20a3c1fcf2cda52cdd2b99c383bef4
[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.http.BaseHTTPClient;
26 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class PNFMountPointClient extends BaseHTTPClient {
31
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;
36     // @formatter:off
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"
52             + "</node>";
53     // @formatter:on
54     // @formatter:off
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"
62             + "  </key-based>\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"
66             + "  </protocol>\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"
75             + "</node>";
76     // @formatter:on
77
78     public PNFMountPointClient(String baseUrl) {
79         super(baseUrl);
80
81         this.headerMap = new HashMap<>();
82         this.headerMap.put("Content-Type", "application/xml");
83         this.headerMap.put("Accept", "application/xml");
84     }
85
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())));
89
90     }
91
92     public boolean pnfMountPointCreate(String pnfName, String ipv4Address, String protocol, String keyId,
93             String username, String password, String commPort) {
94         String message = "";
95         if (protocol.equals("TLS")) {
96             message = updateTLSPayload(pnfName, ipv4Address, username, keyId, commPort);
97         } else { //SSH
98             message = updatePayload(pnfName, ipv4Address, username, password, commPort);
99             LOG.debug("Payload after updating values is: {}", message);
100         }
101         return pnfRequest(pnfName, "PUT", message) == 200;
102
103     }
104
105     private static String updatePayload(String pnfName, String ipv4Address, String username, String password,
106             String portNo) {
107         // @formatter:off
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);
113         // @formatter:on
114     }
115
116     private static String updateTLSPayload(String pnfName, String ipv4Address, String username, String keyId,
117             String portNo) {
118         // @formatter:off
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);
124         // @formatter:on
125     }
126
127     private int pnfRequest(String pnfName, String method, String message) {
128         LOG.info("In pnfRequest - {} : {} : {}", pnfName, method, message);
129         BaseHTTPResponse response;
130         try {
131             String uri = MOUNTPOINT_URI + pnfName;
132             response = this.sendRequest(uri, method, message, headerMap);
133             LOG.debug("finished with responsecode {}", response.code);
134             return response.code;
135         } catch (IOException e) {
136             LOG.warn("problem registering {} : {}", pnfName, e.getMessage());
137             return -1;
138         }
139     }
140
141 }