Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 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 | package org.onosproject.snmp.ctl; |
| 17 | |
| 18 | import org.onosproject.net.DeviceId; |
| 19 | import org.onosproject.snmp.SnmpDevice; |
| 20 | import org.slf4j.Logger; |
| 21 | |
| 22 | import java.net.URI; |
| 23 | import java.net.URISyntaxException; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | import 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 | */ |
| 32 | public 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"); |
| 53 | this.snmpPort = checkNotNull(snmpPort, "SNMP Device port cannot be null"); |
| 54 | 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 | } |