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