ace1c0a39b74b13e9d827a10cec459519d8df4a5
[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
26 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient;
27 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
28
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class PNFMountPointClient extends BaseHTTPClient {
33
34         private static final Logger LOG = LoggerFactory.getLogger(PNFMountPointClient.class);
35         private static final String MOUNTPOINT_URI = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/";
36         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" +
52                         "</node>";
53         private static final String TLS_PAYLOAD = "<node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\">\n" +
54                         "  <node-id>@device-name@</node-id>\n" +
55                         "  <host xmlns=\"urn:opendaylight:netconf-node-topology\">@device-ip@</host>\n" +
56                         "  <port xmlns=\"urn:opendaylight:netconf-node-topology\">@device-port@</port>\n" +
57                         "  <key-based xmlns=\"urn:opendaylight:netconf-node-topology\">\n" +
58                         "         <username xmlns=\"urn:opendaylight:netconf-node-topology\">@username@</username>\n" +
59                         "         <key-id>@key-id@</key-id>\n" +
60                         "  </key-based>\n" +
61                         "  <tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only>\n" +
62                         "  <protocol xmlns=\"urn:opendaylight:netconf-node-topology\">\n" +
63                         "       <name>TLS</name>\n" +
64                         "  </protocol>\n" +
65                         "       <!-- non-mandatory fields with default values, you can safely remove these if you do not wish to override any of these values-->\n" +
66                         "       <reconnect-on-changed-schema xmlns=\"urn:opendaylight:netconf-node-topology\">false</reconnect-on-changed-schema>\n" +
67                         "       <connection-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">20000</connection-timeout-millis>\n" +
68                         "       <max-connection-attempts xmlns=\"urn:opendaylight:netconf-node-topology\">0</max-connection-attempts>\n" +
69                         "       <between-attempts-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">2000</between-attempts-timeout-millis>\n" +
70                         "       <sleep-factor xmlns=\"urn:opendaylight:netconf-node-topology\">1.5</sleep-factor>\n" +
71                         "       <!-- keepalive-delay set to 0 turns off keepalives-->\n" +
72                         "       <keepalive-delay xmlns=\"urn:opendaylight:netconf-node-topology\">120</keepalive-delay>\n" +
73                         "</node>";
74
75         public PNFMountPointClient(String baseUrl) {
76                 super(baseUrl);
77
78                 this.headerMap = new HashMap<>();
79         this.headerMap.put("Content-Type", "application/xml");
80         this.headerMap.put("Accept", "application/xml");
81         }
82
83         public void setAuthorization(String username, String password) {
84                 String credentials = username + ":" + password;
85                 this.headerMap.put("Authorization",
86                                 "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes())));
87
88         }
89
90         public boolean pnfMountPointCreate(String pnfName, String ipv4Address, String protocol, String keyId, String username, String password, String commPort) {
91                 String message = "";
92                 if (protocol.equals("TLS")) {
93                         message = updateTLSPayload(pnfName, ipv4Address, username, keyId, commPort);
94                 } else { //SSH
95                         message = updatePayload(pnfName, ipv4Address, username, password, commPort);
96                         LOG.debug("Payload after updating values is: {}",message);
97                 }
98                 return pnfRequest(pnfName, "PUT", message) == 200;
99
100         }
101
102         private static String updatePayload(String pnfName, String ipv4Address, String username, String password, String portNo) {
103                 return SSH_PAYLOAD.replace("@device-name@", pnfName)
104                                                         .replace("@device-ip@", ipv4Address)
105                                                         .replace("@device-port@", portNo)
106                                                         .replace("@username@",  username)
107                                                         .replace("@password@",  password);
108         }
109
110         private static String updateTLSPayload(String pnfName, String ipv4Address, String username, String keyId, String portNo) {
111                 return TLS_PAYLOAD.replace("@device-name@", pnfName)
112                                                         .replace("@device-ip@", ipv4Address)
113                                                         .replace("@username@", username)
114                                                         .replace("@key-id@", keyId)
115                                                         .replace("@device-port@", portNo);
116         }
117
118          private int pnfRequest(String pnfName, String method, String message) {
119                  LOG.info("In pnfRequest - {} : {} : {}",pnfName,method,message);
120                 BaseHTTPResponse response;
121                 try {
122                     //String uri = "http://localhost:8181/restconf/config/network-topology:network-topology/topology/topology-netconf/node/" + pnfName;
123                     String uri = MOUNTPOINT_URI + pnfName;
124                     response = this.sendRequest(uri, method, message, headerMap);
125                     LOG.debug("finished with responsecode {}", response.code);
126                     return response.code;
127                 } catch (IOException e) {
128                     LOG.warn("problem registering {} : {}", pnfName, e.getMessage());
129                     return -1;
130                 }
131             }
132
133 }