add fixes for wt sulfur
[ccsdk/features.git] / sdnr / wt / mountpoint-registrar / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / mountpointregistrar / impl / PNFMountPointClient.java
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 static org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.MessageClient.MessageType.xml;
23 import static org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.MessageClient.SendMethod.PUT;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.BaseRequest;
29
30 public class PNFMountPointClient extends MessageClient {
31
32     private static final String MOUNTPOINT_URI =
33             "rests/data/network-topology:network-topology/topology=topology-netconf/node=";
34     public static final String DEVICE_NAME = "@device-name@", DEVICE_IP = "@device-ip@", DEVICE_PORT = "@device-port@",
35             USERNAME = "@username@", PASSWORD = "@password@", KEY_ID = "@key-id@";
36     private static final String PROTOCOL = "protocol_sec";
37     public static List<String> REQUIRED_FIELDS_SSH = List.of(PROTOCOL, DEVICE_NAME, DEVICE_IP, DEVICE_PORT, USERNAME, PASSWORD);
38     public static List<String> REQUIRED_FIELDS_TLS = List.of(PROTOCOL, DEVICE_NAME, DEVICE_IP, DEVICE_PORT, USERNAME, KEY_ID);
39
40     private static final String SSH_PAYLOAD = "<node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\">\n"
41             + "  <node-id>" + DEVICE_NAME + "</node-id>\n"
42             + "  <host xmlns=\"urn:opendaylight:netconf-node-topology\">" + DEVICE_IP + "</host>\n"
43             + "  <port xmlns=\"urn:opendaylight:netconf-node-topology\">" + DEVICE_PORT + "</port>\n"
44             + "  <username xmlns=\"urn:opendaylight:netconf-node-topology\">" + USERNAME + "</username>\n"
45             + "  <password xmlns=\"urn:opendaylight:netconf-node-topology\">" + PASSWORD + "</password>\n"
46             + "  <tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only>\n"
47             + "  <!-- non-mandatory fields with default values, you can safely remove these if you do not wish to override any of these values-->\n"
48             + "  <reconnect-on-changed-schema xmlns=\"urn:opendaylight:netconf-node-topology\">false</reconnect-on-changed-schema>\n"
49             + "  <connection-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">20000</connection-timeout-millis>\n"
50             + "  <max-connection-attempts xmlns=\"urn:opendaylight:netconf-node-topology\">0</max-connection-attempts>\n"
51             + "  <between-attempts-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">2000</between-attempts-timeout-millis>\n"
52             + "  <sleep-factor xmlns=\"urn:opendaylight:netconf-node-topology\">1.5</sleep-factor>\n"
53             + "  <!-- keepalive-delay set to 0 turns off keepalives-->\n"
54             + "  <keepalive-delay xmlns=\"urn:opendaylight:netconf-node-topology\">120</keepalive-delay>\n"
55             + "</node>";
56
57     private static final String TLS_PAYLOAD = "<node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\">\n"
58             + "  <node-id>" + DEVICE_NAME + "</node-id>\n"
59             + "  <host xmlns=\"urn:opendaylight:netconf-node-topology\">" + DEVICE_IP + "</host>\n"
60             + "  <port xmlns=\"urn:opendaylight:netconf-node-topology\">" + DEVICE_PORT + "</port>\n"
61             + "  <key-based xmlns=\"urn:opendaylight:netconf-node-topology\">\n"
62             + "  <username xmlns=\"urn:opendaylight:netconf-node-topology\">" + USERNAME + "</username>\n"
63             + "  <key-id>" + KEY_ID + "</key-id>\n"
64             + "  </key-based>\n"
65             + "  <tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only>\n"
66             + "  <protocol xmlns=\"urn:opendaylight:netconf-node-topology\">\n"
67             + "  <name>TLS</name>\n"
68             + "  </protocol>\n"
69             + "<!-- non-mandatory fields with default values, you can safely remove these if you do not wish to override any of these values-->\n"
70             + "<reconnect-on-changed-schema xmlns=\"urn:opendaylight:netconf-node-topology\">false</reconnect-on-changed-schema>\n"
71             + "<connection-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">20000</connection-timeout-millis>\n"
72             + "<max-connection-attempts xmlns=\"urn:opendaylight:netconf-node-topology\">0</max-connection-attempts>\n"
73             + "<between-attempts-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">2000</between-attempts-timeout-millis>\n"
74             + "<sleep-factor xmlns=\"urn:opendaylight:netconf-node-topology\">1.5</sleep-factor>\n"
75             + "<!-- keepalive-delay set to 0 turns off keepalives-->\n"
76             + "<keepalive-delay xmlns=\"urn:opendaylight:netconf-node-topology\">120</keepalive-delay>\n"
77             + "</node>";
78
79
80     public PNFMountPointClient(String baseUrl) {
81         super(baseUrl, MOUNTPOINT_URI);
82     }
83
84     @Override
85     public String prepareMessageFromPayloadMap(Map<String, String> notificationPayloadMap) {
86         updateNotificationUriWithPnfName(notificationPayloadMap.get(DEVICE_NAME));
87         String message = "";
88         if(!notificationPayloadMap.containsKey(PROTOCOL)) {
89             return message;
90         }
91         if(notificationPayloadMap.get(PROTOCOL).equals("SSH")) {
92             message = super.prepareMessageFromPayloadMap(notificationPayloadMap, SSH_PAYLOAD, REQUIRED_FIELDS_SSH);
93         } else if(notificationPayloadMap.get(PROTOCOL).equals("TLS")) {
94             message = super.prepareMessageFromPayloadMap(notificationPayloadMap, TLS_PAYLOAD, REQUIRED_FIELDS_TLS);
95         }
96         return message;
97     }
98
99     private void updateNotificationUriWithPnfName(String pnfName) {
100         setNotificationUri(MOUNTPOINT_URI + BaseRequest.urlEncodeValue(pnfName));
101     }
102
103     @Override
104     public boolean sendNotification(String message) {
105         return super.sendNotification(message, PUT, xml);
106     }
107
108     public static Map<String, String> createPNFNotificationPayloadMap(@NonNull String pnfName, @NonNull String ipAddress,
109                                                                       @NonNull String commPort, @NonNull String protocol,
110                                                                       String username, String password, String keyId) {
111         HashMap<String, String> map = new HashMap<>();
112         map.put(DEVICE_NAME, pnfName);
113         map.put(DEVICE_IP, ipAddress);
114         map.put(DEVICE_PORT, commPort);
115         map.put(PROTOCOL, protocol);
116         map.put(USERNAME, username);
117         map.put(PASSWORD, password);
118         map.put(KEY_ID, keyId);
119         return map;
120     }
121
122 }