Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 3 | * |
| 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 Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 17 | package org.onosproject.snmp; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import org.apache.commons.lang3.tuple.Pair; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.config.Config; |
senthil | 9d33aa2 | 2021-10-05 09:59:32 +0530 | [diff] [blame^] | 24 | import org.snmp4j.mp.SnmpConstants; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Configuration to push devices to the SNMP provider. |
| 28 | */ |
| 29 | @Beta |
| 30 | public class SnmpDeviceConfig extends Config<DeviceId> { |
| 31 | |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 32 | private static final String PROTOCOL = "protocol"; |
| 33 | private static final String NOTIFICATION_PROTOCOL = "notificationProtocol"; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 34 | private static final String IP = "ip"; |
| 35 | private static final String PORT = "port"; |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 36 | private static final int DEFAULT_PORT = 161; |
| 37 | private static final String NOTIFICATION_PORT = "notificationPort"; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 38 | private static final String USERNAME = "username"; |
| 39 | private static final String PASSWORD = "password"; |
senthil | 9d33aa2 | 2021-10-05 09:59:32 +0530 | [diff] [blame^] | 40 | 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 Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 49 | |
| 50 | @Override |
| 51 | public boolean isValid() { |
Laszlo Papp | 10f0d51 | 2018-06-18 15:35:51 +0100 | [diff] [blame] | 52 | return hasOnlyFields(PROTOCOL, NOTIFICATION_PROTOCOL, IP, PORT, |
| 53 | NOTIFICATION_PORT, USERNAME, PASSWORD) && |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 54 | ip() != null; |
| 55 | } |
| 56 | |
| 57 | /** |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 58 | * 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 Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 76 | * 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 Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 94 | * 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 Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 103 | * 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 | |
senthil | 9d33aa2 | 2021-10-05 09:59:32 +0530 | [diff] [blame^] | 120 | /** |
| 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 Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 191 | |
| 192 | private Pair<String, Integer> extractIpPort() { |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 193 | String info = subject.uri().getSchemeSpecificPart(); |
| 194 | int portSeparator = info.lastIndexOf(':'); |
| 195 | if (portSeparator == -1) { |
| 196 | return Pair.of(info, DEFAULT_PORT); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 197 | } |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 198 | |
| 199 | String ip = info.substring(0, portSeparator); |
| 200 | int port = Integer.parseInt(info.substring(portSeparator + 1)); |
| 201 | return Pair.of(ip, port); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 202 | } |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 203 | } |