blob: 8776b3d50b0a56ef629ad8bb72eda0447e282c7d [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;
senthil9d33aa22021-10-05 09:59:32 +053024import org.snmp4j.mp.SnmpConstants;
Andrea Campanella59b549d2017-04-14 21:58:16 +020025
26/**
27 * Configuration to push devices to the SNMP provider.
28 */
29@Beta
30public class SnmpDeviceConfig extends Config<DeviceId> {
31
Laszlo Papp9655b182017-01-31 13:01:45 -080032 private static final String PROTOCOL = "protocol";
33 private static final String NOTIFICATION_PROTOCOL = "notificationProtocol";
Andrea Campanella59b549d2017-04-14 21:58:16 +020034 private static final String IP = "ip";
35 private static final String PORT = "port";
Laszlo Papp9655b182017-01-31 13:01:45 -080036 private static final int DEFAULT_PORT = 161;
37 private static final String NOTIFICATION_PORT = "notificationPort";
Andrea Campanella59b549d2017-04-14 21:58:16 +020038 private static final String USERNAME = "username";
39 private static final String PASSWORD = "password";
senthil9d33aa22021-10-05 09:59:32 +053040 public static final String VERSION = "version";
41 public static final String SECURITY_NAME = "securityName";
42 public static final String SECURITY_LEVEL = "securityLevel";
43 public static final String AUTH_PROTOCOL = "authProtocol";
44 public static final String AUTH_PASSWORD = "authPassword";
45 public static final String PRIVACY_PROTOCOL = "privacyProtocol";
46 public static final String PRIVACY_PASSWORD = "privacyPassword";
47 public static final String CONTEXT_NAME = "contextName";
48
Andrea Campanella59b549d2017-04-14 21:58:16 +020049
50 @Override
51 public boolean isValid() {
Laszlo Papp10f0d512018-06-18 15:35:51 +010052 return hasOnlyFields(PROTOCOL, NOTIFICATION_PROTOCOL, IP, PORT,
53 NOTIFICATION_PORT, USERNAME, PASSWORD) &&
Andrea Campanella59b549d2017-04-14 21:58:16 +020054 ip() != null;
55 }
56
57 /**
Laszlo Papp9655b182017-01-31 13:01:45 -080058 * Gets the protocol of the SNMP device.
59 *
60 * @return protocol
61 */
62 public String protocol() {
63 return get(PROTOCOL, "udp");
64 }
65
66 /**
67 * Gets the notification protocol of the SNMP device.
68 *
69 * @return notification protocol
70 */
71 public String notificationProtocol() {
72 return get(NOTIFICATION_PROTOCOL, "udp");
73 }
74
75 /**
Andrea Campanella59b549d2017-04-14 21:58:16 +020076 * Gets the Ip of the SNMP device.
77 *
78 * @return ip
79 */
80 public IpAddress ip() {
81 return IpAddress.valueOf(get(IP, extractIpPort().getKey()));
82 }
83
84 /**
85 * Gets the port of the SNMP device.
86 *
87 * @return port
88 */
89 public int port() {
90 return get(PORT, extractIpPort().getValue());
91 }
92
93 /**
Laszlo Papp9655b182017-01-31 13:01:45 -080094 * Gets the notification port of the SNMP device.
95 *
96 * @return notification port
97 */
98 public int notificationPort() {
99 return get(NOTIFICATION_PORT, 0);
100 }
101
102 /**
Andrea Campanella59b549d2017-04-14 21:58:16 +0200103 * Gets the username of the SNMP device.
104 *
105 * @return username
106 */
107 public String username() {
108 return get(USERNAME, "");
109 }
110
111 /**
112 * Gets the password of the SNMP device.
113 *
114 * @return password
115 */
116 public String password() {
117 return get(PASSWORD, "");
118 }
119
senthil9d33aa22021-10-05 09:59:32 +0530120 /**
121 * Gets the version of the SNMP device.
122 *
123 * @return snmp version
124 */
125 public int version() {
126 return get(VERSION, SnmpConstants.version2c);
127 }
128
129 /**
130 * Gets the securityName of the SNMPv3 device.
131 *
132 * @return securityName
133 */
134 public String securityName() {
135 return get(SECURITY_NAME, "");
136 }
137
138 /**
139 * Gets the securityLevel of the SNMPv3 device.
140 *
141 * @return securityLevel
142 */
143 public String securityLevel() {
144 return get(SECURITY_LEVEL, "");
145 }
146
147 /**
148 * Gets the authProtocol of the SNMPv3 device.
149 *
150 * @return authProtocol
151 */
152 public String authProtocol() {
153 return get(AUTH_PROTOCOL, "");
154 }
155
156 /**
157 * Gets the authPassword of the SNMPv3 device.
158 *
159 * @return authPassword
160 */
161 public String authPassword() {
162 return get(AUTH_PASSWORD, "");
163 }
164
165 /**
166 * Gets the privacyProtocol of the SNMPv3 device.
167 *
168 * @return privacyProtocol
169 */
170 public String privacyProtocol() {
171 return get(PRIVACY_PROTOCOL, "");
172 }
173
174 /**
175 * Gets the privacyPassword of the SNMPv3 device.
176 *
177 * @return privacyPassword
178 */
179 public String privacyPassword() {
180 return get(PRIVACY_PASSWORD, "");
181 }
182
183 /**
184 * Gets the context name of the SNMPv3 device.
185 *
186 * @return context name
187 */
188 public String contextName() {
189 return get(CONTEXT_NAME, "");
190 }
Andrea Campanella59b549d2017-04-14 21:58:16 +0200191
192 private Pair<String, Integer> extractIpPort() {
Laszlo Papp9655b182017-01-31 13:01:45 -0800193 String info = subject.uri().getSchemeSpecificPart();
194 int portSeparator = info.lastIndexOf(':');
195 if (portSeparator == -1) {
196 return Pair.of(info, DEFAULT_PORT);
Andrea Campanella59b549d2017-04-14 21:58:16 +0200197 }
Laszlo Papp9655b182017-01-31 13:01:45 -0800198
199 String ip = info.substring(0, portSeparator);
200 int port = Integer.parseInt(info.substring(portSeparator + 1));
201 return Pair.of(ip, port);
Andrea Campanella59b549d2017-04-14 21:58:16 +0200202 }
Laszlo Papp9655b182017-01-31 13:01:45 -0800203}