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; |
| 24 | |
| 25 | /** |
| 26 | * Configuration to push devices to the SNMP provider. |
| 27 | */ |
| 28 | @Beta |
| 29 | public class SnmpDeviceConfig extends Config<DeviceId> { |
| 30 | |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 31 | private static final String PROTOCOL = "protocol"; |
| 32 | private static final String NOTIFICATION_PROTOCOL = "notificationProtocol"; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 33 | private static final String IP = "ip"; |
| 34 | private static final String PORT = "port"; |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 35 | private static final int DEFAULT_PORT = 161; |
| 36 | private static final String NOTIFICATION_PORT = "notificationPort"; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 37 | private static final String USERNAME = "username"; |
| 38 | private static final String PASSWORD = "password"; |
| 39 | |
| 40 | @Override |
| 41 | public boolean isValid() { |
Laszlo Papp | 10f0d51 | 2018-06-18 15:35:51 +0100 | [diff] [blame] | 42 | return hasOnlyFields(PROTOCOL, NOTIFICATION_PROTOCOL, IP, PORT, |
| 43 | NOTIFICATION_PORT, USERNAME, PASSWORD) && |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 44 | ip() != null; |
| 45 | } |
| 46 | |
| 47 | /** |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 48 | * 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 Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 66 | * 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 Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 84 | * 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 Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 93 | * 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 Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 112 | String info = subject.uri().getSchemeSpecificPart(); |
| 113 | int portSeparator = info.lastIndexOf(':'); |
| 114 | if (portSeparator == -1) { |
| 115 | return Pair.of(info, DEFAULT_PORT); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 116 | } |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 117 | |
| 118 | String ip = info.substring(0, portSeparator); |
| 119 | int port = Integer.parseInt(info.substring(portSeparator + 1)); |
| 120 | return Pair.of(ip, port); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 121 | } |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 122 | } |