Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [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 | */ |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 16 | package org.onosproject.provider.snmp.device.impl; |
| 17 | |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.core.ApplicationId; |
Ray Milkey | 6c01374 | 2017-08-15 10:16:43 -0700 | [diff] [blame] | 23 | import org.onosproject.net.config.ConfigException; |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 24 | import org.onosproject.net.config.Config; |
| 25 | |
| 26 | import java.util.Set; |
| 27 | |
| 28 | /** |
| 29 | * Configuration decoder for SNMP provider. |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 30 | * @deprecated 1.10.0 Kingfisher |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 31 | */ |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 32 | @Deprecated |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 33 | @Beta |
| 34 | public class SnmpProviderConfig extends Config<ApplicationId> { |
| 35 | |
| 36 | public static final String CONFIG_VALUE_ERROR = "Error parsing config value"; |
| 37 | private static final String IP = "ip"; |
| 38 | private static final int DEFAULT_TCP_PORT = 830; |
| 39 | private static final String PORT = "port"; |
| 40 | private static final String NAME = "username"; |
| 41 | private static final String PASSWORD = "password"; |
| 42 | |
| 43 | /** |
| 44 | * Retrieves a set of SnmpDeviceInfo containing all the device |
| 45 | * configuration pertaining to the SNMP device provider. |
| 46 | * @return set of device configurations. |
| 47 | * |
| 48 | * @throws ConfigException if configuration can't be read |
| 49 | */ |
| 50 | public Set<SnmpDeviceInfo> getDevicesInfo() throws ConfigException { |
| 51 | Set<SnmpDeviceInfo> deviceInfos = Sets.newHashSet(); |
| 52 | |
| 53 | try { |
| 54 | for (JsonNode node : array) { |
| 55 | String ip = node.path(IP).asText(); |
| 56 | IpAddress ipAddr = ip.isEmpty() ? null : IpAddress.valueOf(ip); |
| 57 | int port = node.path(PORT).asInt(DEFAULT_TCP_PORT); |
| 58 | String name = node.path(NAME).asText(); |
| 59 | String password = node.path(PASSWORD).asText(); |
| 60 | deviceInfos.add(new SnmpDeviceInfo(ipAddr, port, name, password)); |
| 61 | |
| 62 | } |
| 63 | } catch (IllegalArgumentException e) { |
| 64 | throw new ConfigException(CONFIG_VALUE_ERROR, e); |
| 65 | } |
| 66 | |
| 67 | return deviceInfos; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Contains information about a SNMP device retrieved form the net-cfg subsystem. |
| 72 | */ |
| 73 | public class SnmpDeviceInfo { |
| 74 | private final IpAddress ip; |
| 75 | private final int port; |
| 76 | private final String username; |
| 77 | private final String password; |
| 78 | |
| 79 | /** |
| 80 | * Build an information object containing the given device specifics. |
| 81 | * @param ip ip |
| 82 | * @param port port |
| 83 | * @param username username |
| 84 | * @param password password (a.k.a community in SNMP) |
| 85 | */ |
| 86 | public SnmpDeviceInfo(IpAddress ip, int port, String username, String password) { |
| 87 | this.ip = ip; |
| 88 | this.port = port; |
| 89 | this.username = username; |
| 90 | this.password = password; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns IpAddress of the device. |
| 95 | * @return ip |
| 96 | */ |
| 97 | public IpAddress ip() { |
| 98 | return ip; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns port of the device. |
| 103 | * @return port |
| 104 | */ |
| 105 | public int port() { |
| 106 | return port; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns username of the device. |
| 111 | * @return username |
| 112 | */ |
| 113 | public String username() { |
| 114 | return username; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns password of the device. |
| 119 | * @return password |
| 120 | */ |
| 121 | public String password() { |
| 122 | return password; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | } |
| 127 | |