blob: 293f5cd99acb9c8069913d3afbeb5efb89b46187 [file] [log] [blame]
Andrea Campanella59b549d2017-04-14 21:58:16 +02001/*
2 * Copyright 2017-present Open Networking Laboratory
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 */
16
17package org.onosproject.provider.snmp.device.impl;
18
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
31 private static final String IP = "ip";
32 private static final String PORT = "port";
33 private static final String USERNAME = "username";
34 private static final String PASSWORD = "password";
35
36 @Override
37 public boolean isValid() {
38 return hasOnlyFields(IP, PORT, USERNAME, PASSWORD) &&
39 ip() != null;
40 }
41
42 /**
43 * Gets the Ip of the SNMP device.
44 *
45 * @return ip
46 */
47 public IpAddress ip() {
48 return IpAddress.valueOf(get(IP, extractIpPort().getKey()));
49 }
50
51 /**
52 * Gets the port of the SNMP device.
53 *
54 * @return port
55 */
56 public int port() {
57 return get(PORT, extractIpPort().getValue());
58 }
59
60 /**
61 * Gets the username of the SNMP device.
62 *
63 * @return username
64 */
65 public String username() {
66 return get(USERNAME, "");
67 }
68
69 /**
70 * Gets the password of the SNMP device.
71 *
72 * @return password
73 */
74 public String password() {
75 return get(PASSWORD, "");
76 }
77
78
79 private Pair<String, Integer> extractIpPort() {
80 String info = subject.toString();
81 if (info.startsWith(SnmpDeviceProvider.SCHEME)) {
82 //+1 is due to length of colon separator
83 String ip = info.substring(info.indexOf(":") + 1, info.lastIndexOf(":"));
84 int port = Integer.parseInt(info.substring(info.lastIndexOf(":") + 1));
85 return Pair.of(ip, port);
86 }
87 return null;
88 }
89}