961f7fef3985903b642abc7085c2d49f8c73e61b
[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  * Copyright (C) 2021 Samsung Electronics Intellectual Property. All rights reserved.
7  * =================================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software distributed under the License
14  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
15  * or implied. See the License for the specific language governing permissions and limitations under
16  * the License.
17  * ============LICENSE_END==========================================================================
18  */
19
20 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl;
21
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.BaseRequest;
24
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import static org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.MessageClient.MessageType.*;
30 import static org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.MessageClient.SendMethod.PUT;
31
32 public class PNFMountPointClient extends MessageClient {
33
34     private static final String MOUNTPOINT_URI =
35             "restconf/config/network-topology:network-topology/topology/topology-netconf/node/";
36     public static final String DEVICE_NAME = "@device-name@", DEVICE_IP = "@device-ip@", DEVICE_PORT = "@device-port@",
37             USERNAME = "@username@", PASSWORD = "@password@", KEY_ID = "@key-id@";
38     private static final String PROTOCOL = "protocol";
39     public static List<String> REQUIRED_FIELDS_SSH = List.of(PROTOCOL, DEVICE_NAME, DEVICE_IP, DEVICE_PORT, USERNAME, PASSWORD);
40     public static List<String> REQUIRED_FIELDS_TLS = List.of(PROTOCOL, DEVICE_NAME, DEVICE_IP, DEVICE_PORT, USERNAME, KEY_ID);
41
42     private static final String SSH_PAYLOAD = "<node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\">\n"
43             + "  <node-id>" + DEVICE_NAME + "</node-id>\n"
44             + "  <host xmlns=\"urn:opendaylight:netconf-node-topology\">" + DEVICE_IP + "</host>\n"
45             + "  <port xmlns=\"urn:opendaylight:netconf-node-topology\">" + DEVICE_PORT + "</port>\n"
46             + "  <username xmlns=\"urn:opendaylight:netconf-node-topology\">" + USERNAME + "</username>\n"
47             + "  <password xmlns=\"urn:opendaylight:netconf-node-topology\">" + PASSWORD + "</password>\n"
48             + "  <tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only>\n"
49             + "  <!-- non-mandatory fields with default values, you can safely remove these if you do not wish to override any of these values-->\n"
50             + "  <reconnect-on-changed-schema xmlns=\"urn:opendaylight:netconf-node-topology\">false</reconnect-on-changed-schema>\n"
51             + "  <connection-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">20000</connection-timeout-millis>\n"
52             + "  <max-connection-attempts xmlns=\"urn:opendaylight:netconf-node-topology\">0</max-connection-attempts>\n"
53             + "  <between-attempts-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">2000</between-attempts-timeout-millis>\n"
54             + "  <sleep-factor xmlns=\"urn:opendaylight:netconf-node-topology\">1.5</sleep-factor>\n"
55             + "  <!-- keepalive-delay set to 0 turns off keepalives-->\n"
56             + "  <keepalive-delay xmlns=\"urn:opendaylight:netconf-node-topology\">120</keepalive-delay>\n"
57             + "</node>";
58
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"
66             + "  </key-based>\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"
70             + "  </protocol>\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"
79             + "</node>";
80
81
82     public PNFMountPointClient(String baseUrl) {
83         super(baseUrl, MOUNTPOINT_URI);
84     }
85
86     @Override
87     public String prepareMessageFromPayloadMap(Map<String, String> notificationPayloadMap) {
88         updateNotificationUriWithPnfName(notificationPayloadMap.get(DEVICE_NAME));
89         String message = "";
90         if(!notificationPayloadMap.containsKey(PROTOCOL)) {
91             return message;
92         }
93         if(notificationPayloadMap.get(PROTOCOL).equals("SSH")) {
94             message = super.prepareMessageFromPayloadMap(notificationPayloadMap, SSH_PAYLOAD, REQUIRED_FIELDS_SSH);
95         } else if(notificationPayloadMap.get(PROTOCOL).equals("TLS")) {
96             message = super.prepareMessageFromPayloadMap(notificationPayloadMap, TLS_PAYLOAD, REQUIRED_FIELDS_TLS);
97         }
98         return message;
99     }
100
101     private void updateNotificationUriWithPnfName(String pnfName) {
102         setNotificationUri(MOUNTPOINT_URI + BaseRequest.urlEncodeValue(pnfName));
103     }
104
105     @Override
106     public boolean sendNotification(String message) {
107         return super.sendNotification(message, PUT, xml);
108     }
109
110     public static Map<String, String> createPNFNotificationPayloadMap(@NonNull String pnfName, @NonNull String ipAddress,
111                                                                       @NonNull String commPort, @NonNull String protocol,
112                                                                       String username, String password, String keyId) {
113         HashMap<String, String> map = new HashMap<>();
114         map.put(DEVICE_NAME, pnfName);
115         map.put(DEVICE_IP, ipAddress);
116         map.put(DEVICE_PORT, commPort);
117         map.put(PROTOCOL, protocol);
118         map.put(USERNAME, username);
119         map.put(PASSWORD, password);
120         map.put(KEY_ID, keyId);
121         return map;
122     }
123
124 }