blob: b39180a405f9cca3f8bed1695a9d5680271d6f5d [file] [log] [blame]
Andrea Campanella59b549d2017-04-14 21:58:16 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Andrea Campanella59b549d2017-04-14 21:58:16 +02003 *
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 Papp9655b182017-01-31 13:01:45 -080017package org.onosproject.snmp;
Andrea Campanella59b549d2017-04-14 21:58:16 +020018
19import com.google.common.annotations.Beta;
20import org.apache.commons.lang3.tuple.Pair;
21import org.onlab.packet.IpAddress;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.config.Config;
24
25/**
26 * Configuration to push devices to the SNMP provider.
27 */
28@Beta
29public class SnmpDeviceConfig extends Config<DeviceId> {
30
Laszlo Papp9655b182017-01-31 13:01:45 -080031 private static final String PROTOCOL = "protocol";
32 private static final String NOTIFICATION_PROTOCOL = "notificationProtocol";
Andrea Campanella59b549d2017-04-14 21:58:16 +020033 private static final String IP = "ip";
34 private static final String PORT = "port";
Laszlo Papp9655b182017-01-31 13:01:45 -080035 private static final int DEFAULT_PORT = 161;
36 private static final String NOTIFICATION_PORT = "notificationPort";
Andrea Campanella59b549d2017-04-14 21:58:16 +020037 private static final String USERNAME = "username";
38 private static final String PASSWORD = "password";
39
40 @Override
41 public boolean isValid() {
42 return hasOnlyFields(IP, PORT, USERNAME, PASSWORD) &&
43 ip() != null;
44 }
45
46 /**
Laszlo Papp9655b182017-01-31 13:01:45 -080047 * Gets the protocol of the SNMP device.
48 *
49 * @return protocol
50 */
51 public String protocol() {
52 return get(PROTOCOL, "udp");
53 }
54
55 /**
56 * Gets the notification protocol of the SNMP device.
57 *
58 * @return notification protocol
59 */
60 public String notificationProtocol() {
61 return get(NOTIFICATION_PROTOCOL, "udp");
62 }
63
64 /**
Andrea Campanella59b549d2017-04-14 21:58:16 +020065 * Gets the Ip of the SNMP device.
66 *
67 * @return ip
68 */
69 public IpAddress ip() {
70 return IpAddress.valueOf(get(IP, extractIpPort().getKey()));
71 }
72
73 /**
74 * Gets the port of the SNMP device.
75 *
76 * @return port
77 */
78 public int port() {
79 return get(PORT, extractIpPort().getValue());
80 }
81
82 /**
Laszlo Papp9655b182017-01-31 13:01:45 -080083 * Gets the notification port of the SNMP device.
84 *
85 * @return notification port
86 */
87 public int notificationPort() {
88 return get(NOTIFICATION_PORT, 0);
89 }
90
91 /**
Andrea Campanella59b549d2017-04-14 21:58:16 +020092 * Gets the username of the SNMP device.
93 *
94 * @return username
95 */
96 public String username() {
97 return get(USERNAME, "");
98 }
99
100 /**
101 * Gets the password of the SNMP device.
102 *
103 * @return password
104 */
105 public String password() {
106 return get(PASSWORD, "");
107 }
108
109
110 private Pair<String, Integer> extractIpPort() {
Laszlo Papp9655b182017-01-31 13:01:45 -0800111 String info = subject.uri().getSchemeSpecificPart();
112 int portSeparator = info.lastIndexOf(':');
113 if (portSeparator == -1) {
114 return Pair.of(info, DEFAULT_PORT);
Andrea Campanella59b549d2017-04-14 21:58:16 +0200115 }
Laszlo Papp9655b182017-01-31 13:01:45 -0800116
117 String ip = info.substring(0, portSeparator);
118 int port = Integer.parseInt(info.substring(portSeparator + 1));
119 return Pair.of(ip, port);
Andrea Campanella59b549d2017-04-14 21:58:16 +0200120 }
Laszlo Papp9655b182017-01-31 13:01:45 -0800121}