blob: 47446ea5a1f65a1bf7c239c61d54976453c41b08 [file] [log] [blame]
Andrea Campanella59b549d2017-04-14 21:58:16 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Andrea Campanella59b549d2017-04-14 21:58:16 +02003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Laszlo Papp9655b182017-01-31 13:01:45 -080017package org.onosproject.snmp;
Andrea Campanella59b549d2017-04-14 21:58:16 +020018
19import com.google.common.annotations.Beta;
20import org.apache.commons.lang3.tuple.Pair;
21import org.onlab.packet.IpAddress;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.config.Config;
24
25/**
26 * Configuration to push devices to the SNMP provider.
27 */
28@Beta
29public class SnmpDeviceConfig extends Config<DeviceId> {
30
Laszlo Papp9655b182017-01-31 13:01:45 -080031 private static final String PROTOCOL = "protocol";
32 private static final String NOTIFICATION_PROTOCOL = "notificationProtocol";
Andrea Campanella59b549d2017-04-14 21:58:16 +020033 private static final String IP = "ip";
34 private static final String PORT = "port";
Laszlo Papp9655b182017-01-31 13:01:45 -080035 private static final int DEFAULT_PORT = 161;
36 private static final String NOTIFICATION_PORT = "notificationPort";
Andrea Campanella59b549d2017-04-14 21:58:16 +020037 private static final String USERNAME = "username";
38 private static final String PASSWORD = "password";
39
40 @Override
41 public boolean isValid() {
Laszlo Papp10f0d512018-06-18 15:35:51 +010042 return hasOnlyFields(PROTOCOL, NOTIFICATION_PROTOCOL, IP, PORT,
43 NOTIFICATION_PORT, USERNAME, PASSWORD) &&
Andrea Campanella59b549d2017-04-14 21:58:16 +020044 ip() != null;
45 }
46
47 /**
Laszlo Papp9655b182017-01-31 13:01:45 -080048 * Gets the protocol of the SNMP device.
49 *
50 * @return protocol
51 */
52 public String protocol() {
53 return get(PROTOCOL, "udp");
54 }
55
56 /**
57 * Gets the notification protocol of the SNMP device.
58 *
59 * @return notification protocol
60 */
61 public String notificationProtocol() {
62 return get(NOTIFICATION_PROTOCOL, "udp");
63 }
64
65 /**
Andrea Campanella59b549d2017-04-14 21:58:16 +020066 * Gets the Ip of the SNMP device.
67 *
68 * @return ip
69 */
70 public IpAddress ip() {
71 return IpAddress.valueOf(get(IP, extractIpPort().getKey()));
72 }
73
74 /**
75 * Gets the port of the SNMP device.
76 *
77 * @return port
78 */
79 public int port() {
80 return get(PORT, extractIpPort().getValue());
81 }
82
83 /**
Laszlo Papp9655b182017-01-31 13:01:45 -080084 * Gets the notification port of the SNMP device.
85 *
86 * @return notification port
87 */
88 public int notificationPort() {
89 return get(NOTIFICATION_PORT, 0);
90 }
91
92 /**
Andrea Campanella59b549d2017-04-14 21:58:16 +020093 * Gets the username of the SNMP device.
94 *
95 * @return username
96 */
97 public String username() {
98 return get(USERNAME, "");
99 }
100
101 /**
102 * Gets the password of the SNMP device.
103 *
104 * @return password
105 */
106 public String password() {
107 return get(PASSWORD, "");
108 }
109
110
111 private Pair<String, Integer> extractIpPort() {
Laszlo Papp9655b182017-01-31 13:01:45 -0800112 String info = subject.uri().getSchemeSpecificPart();
113 int portSeparator = info.lastIndexOf(':');
114 if (portSeparator == -1) {
115 return Pair.of(info, DEFAULT_PORT);
Andrea Campanella59b549d2017-04-14 21:58:16 +0200116 }
Laszlo Papp9655b182017-01-31 13:01:45 -0800117
118 String ip = info.substring(0, portSeparator);
119 int port = Integer.parseInt(info.substring(portSeparator + 1));
120 return Pair.of(ip, port);
Andrea Campanella59b549d2017-04-14 21:58:16 +0200121 }
Laszlo Papp9655b182017-01-31 13:01:45 -0800122}