blob: 43f4ad79cd50d1ad24a7d1740926ca594442eb4b [file] [log] [blame]
Andrea Campanellac2d754b2016-03-29 17:51:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Andrea Campanellac2d754b2016-03-29 17:51:07 -07003 *
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 Campanellac2d754b2016-03-29 17:51:07 -070016package org.onosproject.provider.snmp.device.impl;
17
Andrea Campanellac2d754b2016-03-29 17:51:07 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.google.common.annotations.Beta;
20import com.google.common.collect.Sets;
21import org.onlab.packet.IpAddress;
22import org.onosproject.core.ApplicationId;
Ray Milkey6c013742017-08-15 10:16:43 -070023import org.onosproject.net.config.ConfigException;
Andrea Campanellac2d754b2016-03-29 17:51:07 -070024import org.onosproject.net.config.Config;
25
26import java.util.Set;
27
28/**
29 * Configuration decoder for SNMP provider.
Andrea Campanella59b549d2017-04-14 21:58:16 +020030 * @deprecated 1.10.0 Kingfisher
Andrea Campanellac2d754b2016-03-29 17:51:07 -070031 */
Andrea Campanella59b549d2017-04-14 21:58:16 +020032@Deprecated
Andrea Campanellac2d754b2016-03-29 17:51:07 -070033@Beta
34public 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