blob: 2442240f1a79989c237304d3170685f7b074b50e [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;
Laszlo Papp9655b182017-01-31 13:01:45 -080020import org.onosproject.snmp.SnmpDeviceConfig;
Andrea Campanellac2d754b2016-03-29 17:51:07 -070021import org.slf4j.Logger;
Laszlo Papp9655b182017-01-31 13:01:45 -080022import org.snmp4j.Snmp;
23import org.snmp4j.TransportMapping;
24import org.snmp4j.smi.GenericAddress;
25import org.snmp4j.transport.DefaultTcpTransportMapping;
26import org.snmp4j.transport.DefaultUdpTransportMapping;
Andrea Campanellac2d754b2016-03-29 17:51:07 -070027
Laszlo Papp9655b182017-01-31 13:01:45 -080028import java.io.IOException;
Andrea Campanellac2d754b2016-03-29 17:51:07 -070029import java.net.URI;
30import java.net.URISyntaxException;
31
32import static com.google.common.base.Preconditions.checkNotNull;
33import static org.slf4j.LoggerFactory.getLogger;
34
35/**
36 * This is a logical representation of actual SNMP device, carrying all the necessary information to connect and execute
37 * SNMP operations.
38 */
39public class DefaultSnmpDevice implements SnmpDevice {
40
41 private final Logger log = getLogger(DefaultSnmpDevice.class);
42
Andrea Campanellac2d754b2016-03-29 17:51:07 -070043 private static final String SCHEME = "snmp";
44
45 private final String snmpHost;
46 private final DeviceId deviceId;
Laszlo Papp9655b182017-01-31 13:01:45 -080047 private final int snmpPort;
48 private final int notificationPort;
Andrea Campanellac2d754b2016-03-29 17:51:07 -070049 private final String username;
50 //Community is a conventional name for password in SNMP.
51 private final String community;
52 private boolean reachable = false;
Laszlo Papp9655b182017-01-31 13:01:45 -080053 private final String protocol;
54 private final String notificationProtocol;
Andrea Campanellac2d754b2016-03-29 17:51:07 -070055
Laszlo Papp9655b182017-01-31 13:01:45 -080056 private Snmp session;
Andrea Campanellac2d754b2016-03-29 17:51:07 -070057
Laszlo Papp9655b182017-01-31 13:01:45 -080058 public DefaultSnmpDevice(String snmpHost, int snmpPort,
59 String username, String community) {
60 this.protocol = GenericAddress.TYPE_UDP;
Andrea Campanellac2d754b2016-03-29 17:51:07 -070061 this.snmpHost = checkNotNull(snmpHost, "SNMP Device IP cannot be null");
Laszlo Papp9655b182017-01-31 13:01:45 -080062 this.snmpPort = checkNotNull(snmpPort, "SNMP Device port cannot be null");
63 this.notificationProtocol = GenericAddress.TYPE_UDP;
64 this.notificationPort = 0;
Andrea Campanellac2d754b2016-03-29 17:51:07 -070065 this.username = username;
66 this.community = community;
67 this.deviceId = createDeviceId();
Laszlo Papp9655b182017-01-31 13:01:45 -080068 initializeSession();
69 }
70
71 public DefaultSnmpDevice(SnmpDeviceConfig snmpDeviceConfig) {
72 checkNotNull(snmpDeviceConfig.ip(), "SNMP Device IP address cannot be null");
73 this.protocol = snmpDeviceConfig.protocol();
74 this.notificationProtocol = snmpDeviceConfig.notificationProtocol();
75 this.snmpHost = checkNotNull(snmpDeviceConfig.ip().toString(), "SNMP Device IP cannot be null");
76 this.snmpPort = checkNotNull(snmpDeviceConfig.port(), "SNMP Device port cannot be null");
77 this.notificationPort = checkNotNull(snmpDeviceConfig.notificationPort(),
78 "SNMP Device notification port cannot be null");
79 this.username = snmpDeviceConfig.username();
80 this.community = snmpDeviceConfig.password();
81 this.deviceId = createDeviceId();
82 initializeSession();
83 }
84
85 private void initializeSession() {
Andrea Campanellac2d754b2016-03-29 17:51:07 -070086 reachable = true;
Laszlo Papp9655b182017-01-31 13:01:45 -080087 try {
88 TransportMapping transport;
89 if (protocol == GenericAddress.TYPE_TCP) {
90 transport = new DefaultTcpTransportMapping();
91 } else {
92 transport = new DefaultUdpTransportMapping();
93 }
94 transport.listen();
95 session = new Snmp(transport);
96 } catch (IOException e) {
97 log.error("Failed to connect to device: ", e);
98 }
Andrea Campanellac2d754b2016-03-29 17:51:07 -070099 }
100
101 @Override
102 public String deviceInfo() {
103 return new StringBuilder("host: ").append(snmpHost).append(". port: ")
104 .append(snmpPort).toString();
105 }
106
107 @Override
Laszlo Papp9655b182017-01-31 13:01:45 -0800108 public Snmp getSession() {
109 return session;
110 }
111
112 @Override
Andrea Campanellac2d754b2016-03-29 17:51:07 -0700113 public void disconnect() {
114 log.info("disconnect");
115 reachable = false;
116 }
117
118 @Override
119 public boolean isReachable() {
120 return reachable;
121 }
122
123 @Override
124 public String getSnmpHost() {
125 return snmpHost;
126 }
127
128
129 @Override
130 public int getSnmpPort() {
131 return snmpPort;
132 }
133
134 @Override
Laszlo Papp9655b182017-01-31 13:01:45 -0800135 public int getNotificationPort() {
136 return notificationPort;
137 }
138
139 @Override
Andrea Campanellac2d754b2016-03-29 17:51:07 -0700140 public String getUsername() {
141 return username;
142 }
143
144 @Override
145 public String getCommunity() {
146 return community;
147 }
148
149 @Override
150 public DeviceId deviceId() {
151 return deviceId;
152 }
153
Laszlo Papp9655b182017-01-31 13:01:45 -0800154 @Override
155 public String getProtocol() {
156 return protocol;
157 }
158
159 @Override
160 public String getNotificationProtocol() {
161 return notificationProtocol;
162 }
163
Andrea Campanellac2d754b2016-03-29 17:51:07 -0700164 private DeviceId createDeviceId() {
165 String additionalSsp = new StringBuilder(
166 snmpHost).append(":")
167 .append(snmpPort).toString();
168 try {
169 return DeviceId.deviceId(new URI(SCHEME, additionalSsp,
170 null));
171 } catch (URISyntaxException e) {
172 log.error("Syntax Error while creating URI for the device: "
173 + additionalSsp
174 + " couldn't persist the device onto the store", e);
175 throw new IllegalArgumentException("Can't create device ID from " + additionalSsp, e);
176 }
177 }
Laszlo Papp9655b182017-01-31 13:01:45 -0800178
Andrea Campanellac2d754b2016-03-29 17:51:07 -0700179}