blob: b15277c914c396b89c84b6d36bf6bee8d4805728 [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 */
16package org.onosproject.snmp.ctl;
17
18import org.onosproject.net.DeviceId;
19import org.onosproject.snmp.SnmpDevice;
20import org.slf4j.Logger;
21
22import java.net.URI;
23import java.net.URISyntaxException;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26import static org.slf4j.LoggerFactory.getLogger;
27
28/**
29 * This is a logical representation of actual SNMP device, carrying all the necessary information to connect and execute
30 * SNMP operations.
31 */
32public class DefaultSnmpDevice implements SnmpDevice {
33
34 private final Logger log = getLogger(DefaultSnmpDevice.class);
35
36
37 private static final int DEFAULT_SNMP_PORT = 161;
38
39 private static final String SCHEME = "snmp";
40
41 private final String snmpHost;
42 private final DeviceId deviceId;
43 private int snmpPort = DEFAULT_SNMP_PORT;
44 private final String username;
45 //Community is a conventional name for password in SNMP.
46 private final String community;
47 private boolean reachable = false;
48
49
50 public DefaultSnmpDevice(String snmpHost, int snmpPort, String username, String community) {
51
52 this.snmpHost = checkNotNull(snmpHost, "SNMP Device IP cannot be null");
Yuta HIGUCHI1edc36b2018-01-24 23:39:06 -080053 this.snmpPort = snmpPort;
Andrea Campanellac2d754b2016-03-29 17:51:07 -070054 this.username = username;
55 this.community = community;
56 this.deviceId = createDeviceId();
57 reachable = true;
58 }
59
60 @Override
61 public String deviceInfo() {
62 return new StringBuilder("host: ").append(snmpHost).append(". port: ")
63 .append(snmpPort).toString();
64 }
65
66 @Override
67 public void disconnect() {
68 log.info("disconnect");
69 reachable = false;
70 }
71
72 @Override
73 public boolean isReachable() {
74 return reachable;
75 }
76
77 @Override
78 public String getSnmpHost() {
79 return snmpHost;
80 }
81
82
83 @Override
84 public int getSnmpPort() {
85 return snmpPort;
86 }
87
88 @Override
89 public String getUsername() {
90 return username;
91 }
92
93 @Override
94 public String getCommunity() {
95 return community;
96 }
97
98 @Override
99 public DeviceId deviceId() {
100 return deviceId;
101 }
102
103 private DeviceId createDeviceId() {
104 String additionalSsp = new StringBuilder(
105 snmpHost).append(":")
106 .append(snmpPort).toString();
107 try {
108 return DeviceId.deviceId(new URI(SCHEME, additionalSsp,
109 null));
110 } catch (URISyntaxException e) {
111 log.error("Syntax Error while creating URI for the device: "
112 + additionalSsp
113 + " couldn't persist the device onto the store", e);
114 throw new IllegalArgumentException("Can't create device ID from " + additionalSsp, e);
115 }
116 }
117}