blob: 0c3486a5a10c382ba317f6741717541dbddb81cc [file] [log] [blame]
Andrea Campanellac2d754b2016-03-29 17:51:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
23import org.onosproject.incubator.net.config.basics.ConfigException;
24import org.onosproject.net.config.Config;
25
26import java.util.Set;
27
28/**
29 * Configuration decoder for SNMP provider.
30 */
31@Beta
32public class SnmpProviderConfig extends Config<ApplicationId> {
33
34 public static final String CONFIG_VALUE_ERROR = "Error parsing config value";
35 private static final String IP = "ip";
36 private static final int DEFAULT_TCP_PORT = 830;
37 private static final String PORT = "port";
38 private static final String NAME = "username";
39 private static final String PASSWORD = "password";
40
41 /**
42 * Retrieves a set of SnmpDeviceInfo containing all the device
43 * configuration pertaining to the SNMP device provider.
44 * @return set of device configurations.
45 *
46 * @throws ConfigException if configuration can't be read
47 */
48 public Set<SnmpDeviceInfo> getDevicesInfo() throws ConfigException {
49 Set<SnmpDeviceInfo> deviceInfos = Sets.newHashSet();
50
51 try {
52 for (JsonNode node : array) {
53 String ip = node.path(IP).asText();
54 IpAddress ipAddr = ip.isEmpty() ? null : IpAddress.valueOf(ip);
55 int port = node.path(PORT).asInt(DEFAULT_TCP_PORT);
56 String name = node.path(NAME).asText();
57 String password = node.path(PASSWORD).asText();
58 deviceInfos.add(new SnmpDeviceInfo(ipAddr, port, name, password));
59
60 }
61 } catch (IllegalArgumentException e) {
62 throw new ConfigException(CONFIG_VALUE_ERROR, e);
63 }
64
65 return deviceInfos;
66 }
67
68 /**
69 * Contains information about a SNMP device retrieved form the net-cfg subsystem.
70 */
71 public class SnmpDeviceInfo {
72 private final IpAddress ip;
73 private final int port;
74 private final String username;
75 private final String password;
76
77 /**
78 * Build an information object containing the given device specifics.
79 * @param ip ip
80 * @param port port
81 * @param username username
82 * @param password password (a.k.a community in SNMP)
83 */
84 public SnmpDeviceInfo(IpAddress ip, int port, String username, String password) {
85 this.ip = ip;
86 this.port = port;
87 this.username = username;
88 this.password = password;
89 }
90
91 /**
92 * Returns IpAddress of the device.
93 * @return ip
94 */
95 public IpAddress ip() {
96 return ip;
97 }
98
99 /**
100 * Returns port of the device.
101 * @return port
102 */
103 public int port() {
104 return port;
105 }
106
107 /**
108 * Returns username of the device.
109 * @return username
110 */
111 public String username() {
112 return username;
113 }
114
115 /**
116 * Returns password of the device.
117 * @return password
118 */
119 public String password() {
120 return password;
121 }
122 }
123
124}
125