kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | package org.onosproject.provider.snmp.device.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | |
| 22 | import org.slf4j.Logger; |
| 23 | |
| 24 | /** |
| 25 | * This is a logical representation of actual SNMP device, carrying all the necessary information to connect and execute |
| 26 | * SNMP operations. |
| 27 | */ |
| 28 | public class SnmpDevice { |
| 29 | |
| 30 | private final Logger log = getLogger(SnmpDevice.class); |
| 31 | |
| 32 | |
| 33 | private static final int DEFAULT_SNMP_PORT = 161; |
| 34 | |
| 35 | private final String snmpHost; |
| 36 | private int snmpPort = DEFAULT_SNMP_PORT; |
| 37 | private final String community; |
| 38 | private boolean reachable = false; |
| 39 | |
| 40 | private DeviceState deviceState = DeviceState.INVALID; |
| 41 | |
| 42 | protected SnmpDevice(String snmpHost, int snmpPort, String community) { |
| 43 | |
| 44 | this.snmpHost = checkNotNull(snmpHost, "SNMP Device IP cannot be null"); |
| 45 | this.snmpPort = checkNotNull(snmpPort, "SNMP Device snmp port cannot be null"); |
| 46 | this.community = community; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * This will try to connect to SNMP device. |
| 51 | * |
| 52 | */ |
| 53 | public void init() { |
| 54 | |
| 55 | reachable = true; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * This would return host IP and host Port, used by this particular SNMP Device. |
| 60 | * |
| 61 | * @return Device Information. |
| 62 | */ |
| 63 | public String deviceInfo() { |
| 64 | return new StringBuilder("host: ").append(snmpHost).append(". port: ") |
| 65 | .append(snmpPort).toString(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * This will terminate the device connection. |
| 70 | */ |
| 71 | public void disconnect() { |
| 72 | log.info("disconnect"); |
| 73 | reachable = false; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * This api is intended to know whether the device is connected or not. |
| 78 | * |
| 79 | * @return true if connected |
| 80 | */ |
| 81 | public boolean isReachable() { |
| 82 | return reachable; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * This will return the IP used connect ssh on the device. |
| 87 | * |
| 88 | * @return SNMP Device IP |
| 89 | */ |
| 90 | public String getSnmpHost() { |
| 91 | return snmpHost; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * This will return the SSH Port used connect the device. |
| 96 | * |
| 97 | * @return SSH Port number |
| 98 | */ |
| 99 | public int getSnmpPort() { |
| 100 | return snmpPort; |
| 101 | } |
| 102 | |
| 103 | public String getCommunity() { |
| 104 | return community; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Retrieve current state of the device. |
| 109 | * |
| 110 | * @return Current Device State |
| 111 | */ |
| 112 | public DeviceState getDeviceState() { |
| 113 | return deviceState; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * This is set the state information for the device. |
| 118 | * |
| 119 | * @param deviceState Next Device State |
| 120 | */ |
| 121 | public void setDeviceState(DeviceState deviceState) { |
| 122 | this.deviceState = deviceState; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Check whether the device is in Active state. |
| 127 | * |
| 128 | * @return true if the device is Active |
| 129 | */ |
| 130 | public boolean isActive() { |
| 131 | return deviceState == DeviceState.ACTIVE; |
| 132 | } |
| 133 | |
| 134 | } |