blob: 906b2d8c36f3836c10ceca371aa77165b3186fb8 [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 */
16
17package org.onosproject.provider.snmp.device.impl;
18
19/*
20 * Copyright 2015 Open Networking Laboratory
21 *
22 * Licensed under the Apache License, Version 2.0 (the "License");
23 * you may not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 * http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
33 */
34
35import com.fasterxml.jackson.databind.JsonNode;
36import com.google.common.annotations.Beta;
37import com.google.common.collect.Sets;
38import org.onlab.packet.IpAddress;
39import org.onosproject.core.ApplicationId;
40import org.onosproject.incubator.net.config.basics.ConfigException;
41import org.onosproject.net.config.Config;
42
43import java.util.Set;
44
45/**
46 * Configuration decoder for SNMP provider.
47 */
48@Beta
49public class SnmpProviderConfig extends Config<ApplicationId> {
50
51 public static final String CONFIG_VALUE_ERROR = "Error parsing config value";
52 private static final String IP = "ip";
53 private static final int DEFAULT_TCP_PORT = 830;
54 private static final String PORT = "port";
55 private static final String NAME = "username";
56 private static final String PASSWORD = "password";
57
58 /**
59 * Retrieves a set of SnmpDeviceInfo containing all the device
60 * configuration pertaining to the SNMP device provider.
61 * @return set of device configurations.
62 *
63 * @throws ConfigException if configuration can't be read
64 */
65 public Set<SnmpDeviceInfo> getDevicesInfo() throws ConfigException {
66 Set<SnmpDeviceInfo> deviceInfos = Sets.newHashSet();
67
68 try {
69 for (JsonNode node : array) {
70 String ip = node.path(IP).asText();
71 IpAddress ipAddr = ip.isEmpty() ? null : IpAddress.valueOf(ip);
72 int port = node.path(PORT).asInt(DEFAULT_TCP_PORT);
73 String name = node.path(NAME).asText();
74 String password = node.path(PASSWORD).asText();
75 deviceInfos.add(new SnmpDeviceInfo(ipAddr, port, name, password));
76
77 }
78 } catch (IllegalArgumentException e) {
79 throw new ConfigException(CONFIG_VALUE_ERROR, e);
80 }
81
82 return deviceInfos;
83 }
84
85 /**
86 * Contains information about a SNMP device retrieved form the net-cfg subsystem.
87 */
88 public class SnmpDeviceInfo {
89 private final IpAddress ip;
90 private final int port;
91 private final String username;
92 private final String password;
93
94 /**
95 * Build an information object containing the given device specifics.
96 * @param ip ip
97 * @param port port
98 * @param username username
99 * @param password password (a.k.a community in SNMP)
100 */
101 public SnmpDeviceInfo(IpAddress ip, int port, String username, String password) {
102 this.ip = ip;
103 this.port = port;
104 this.username = username;
105 this.password = password;
106 }
107
108 /**
109 * Returns IpAddress of the device.
110 * @return ip
111 */
112 public IpAddress ip() {
113 return ip;
114 }
115
116 /**
117 * Returns port of the device.
118 * @return port
119 */
120 public int port() {
121 return port;
122 }
123
124 /**
125 * Returns username of the device.
126 * @return username
127 */
128 public String username() {
129 return username;
130 }
131
132 /**
133 * Returns password of the device.
134 * @return password
135 */
136 public String password() {
137 return password;
138 }
139 }
140
141}
142