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 | |
| 17 | package org.onosproject.snmp.ctl; |
| 18 | |
| 19 | import com.btisystems.pronx.ems.core.snmp.DefaultSnmpConfigurationFactory; |
| 20 | import com.btisystems.pronx.ems.core.snmp.ISnmpConfiguration; |
| 21 | import com.btisystems.pronx.ems.core.snmp.ISnmpSession; |
| 22 | import com.btisystems.pronx.ems.core.snmp.ISnmpSessionFactory; |
| 23 | import com.btisystems.pronx.ems.core.snmp.SnmpSessionFactory; |
| 24 | import com.btisystems.pronx.ems.core.snmp.V2cSnmpConfiguration; |
| 25 | import com.google.common.base.Preconditions; |
senthil | 9d33aa2 | 2021-10-05 09:59:32 +0530 | [diff] [blame^] | 26 | import com.google.common.collect.Maps; |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 27 | import org.onosproject.alarm.Alarm; |
| 28 | import org.onosproject.alarm.AlarmId; |
| 29 | import org.onosproject.alarm.DefaultAlarm; |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.snmp.SnmpController; |
| 32 | import org.onosproject.snmp.SnmpDevice; |
senthil | 9d33aa2 | 2021-10-05 09:59:32 +0530 | [diff] [blame^] | 33 | import org.onosproject.snmp.SnmpException; |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 34 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 35 | import org.osgi.service.component.annotations.Activate; |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 36 | import org.osgi.service.component.annotations.Component; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 37 | import org.osgi.service.component.annotations.Deactivate; |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
senthil | 9d33aa2 | 2021-10-05 09:59:32 +0530 | [diff] [blame^] | 40 | import org.snmp4j.mp.SnmpConstants; |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 41 | |
| 42 | import java.io.IOException; |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 43 | import java.net.URI; |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 44 | import java.util.Collection; |
| 45 | import java.util.HashMap; |
| 46 | import java.util.Map; |
senthil | 9d33aa2 | 2021-10-05 09:59:32 +0530 | [diff] [blame^] | 47 | import java.util.Objects; |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 48 | import java.util.concurrent.ConcurrentHashMap; |
| 49 | |
| 50 | /** |
| 51 | * Default implementation of the SNMP sub-controller. |
| 52 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 53 | @Component(immediate = true, service = SnmpController.class) |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 54 | public class DefaultSnmpController implements SnmpController { |
| 55 | |
| 56 | private final Logger log = LoggerFactory |
| 57 | .getLogger(getClass()); |
| 58 | |
senthil | 9d33aa2 | 2021-10-05 09:59:32 +0530 | [diff] [blame^] | 59 | protected Map<Integer, ISnmpSessionFactory> factoryMap; |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 60 | |
Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [diff] [blame] | 61 | protected final Map<DeviceId, ISnmpSession> sessionMap = new HashMap<>(); |
| 62 | protected final Map<DeviceId, SnmpDevice> snmpDeviceMap = new ConcurrentHashMap<>(); |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 63 | |
| 64 | @Activate |
| 65 | public void activate(ComponentContext context) { |
senthil | 9d33aa2 | 2021-10-05 09:59:32 +0530 | [diff] [blame^] | 66 | factoryMap = Maps.newHashMap(); |
| 67 | factoryMap.put(SnmpConstants.version2c, new SnmpSessionFactory( |
| 68 | new DefaultSnmpConfigurationFactory(new V2cSnmpConfiguration()))); |
| 69 | factoryMap.put(SnmpConstants.version3, new SnmpSessionFactory( |
| 70 | new DefaultSnmpConfigurationFactory(new V3SnmpConfiguration()))); |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 71 | log.info("Started"); |
| 72 | } |
| 73 | |
| 74 | @Deactivate |
| 75 | public void deactivate() { |
Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [diff] [blame] | 76 | sessionMap.clear(); |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 77 | snmpDeviceMap.clear(); |
| 78 | log.info("Stopped"); |
| 79 | } |
| 80 | |
| 81 | @Override |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 82 | @Deprecated |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 83 | public ISnmpSession getSession(DeviceId deviceId) throws IOException { |
| 84 | if (!sessionMap.containsKey(deviceId)) { |
| 85 | SnmpDevice device = snmpDeviceMap.get(deviceId); |
senthil | 9d33aa2 | 2021-10-05 09:59:32 +0530 | [diff] [blame^] | 86 | ISnmpSessionFactory sessionFactory = factoryMap.get(device.getVersion()); |
| 87 | if (Objects.isNull(sessionFactory)) { |
| 88 | log.error("Invalid session factory", deviceId); |
| 89 | throw new SnmpException("Invalid session factory"); |
| 90 | } |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 91 | String ipAddress = null; |
| 92 | int port = -1; |
| 93 | if (device != null) { |
| 94 | ipAddress = device.getSnmpHost(); |
| 95 | port = device.getSnmpPort(); |
| 96 | } else { |
| 97 | String[] deviceComponents = deviceId.toString().split(":"); |
| 98 | if (deviceComponents.length > 1) { |
| 99 | ipAddress = deviceComponents[1]; |
| 100 | port = Integer.parseInt(deviceComponents[2]); |
| 101 | |
| 102 | } else { |
| 103 | log.error("Cannot obtain correct information from device id", deviceId); |
| 104 | } |
| 105 | } |
| 106 | Preconditions.checkNotNull(ipAddress, "ip address is empty, cannot start session"); |
| 107 | Preconditions.checkArgument(port != -1, "port is incorrect, cannot start session"); |
senthil | 9d33aa2 | 2021-10-05 09:59:32 +0530 | [diff] [blame^] | 108 | ISnmpConfiguration config; |
| 109 | if (device.getVersion() == SnmpConstants.version2c) { |
| 110 | config = new V2cSnmpConfiguration(); |
| 111 | config.setCommunity(device.getCommunity()); |
| 112 | } else if (device.getVersion() == SnmpConstants.version3) { |
| 113 | DefaultSnmpv3Device v3Device = (DefaultSnmpv3Device) device; |
| 114 | config = V3SnmpConfiguration.builder() |
| 115 | .setAddress(ipAddress) |
| 116 | .setSecurityName(v3Device.getSecurityName()) |
| 117 | .setSecurityLevel(v3Device.getSecurityLevel()) |
| 118 | .setAuthenticationProtocol(v3Device.getAuthProtocol()) |
| 119 | .setAuthenticationPassword(v3Device.getAuthPassword()) |
| 120 | .setPrivacyProtocol(v3Device.getPrivProtocol()) |
| 121 | .setPrivacyPassword(v3Device.getPrivPassword()) |
| 122 | .setContextName(v3Device.getContextName()) |
| 123 | .build(); |
| 124 | } else { |
| 125 | throw new SnmpException( |
| 126 | String.format("Invalid snmp version %d", device.getVersion())); |
| 127 | } |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 128 | config.setPort(port); |
| 129 | sessionMap.put(deviceId, sessionFactory.createSession(config, ipAddress)); |
| 130 | } |
| 131 | return sessionMap.get(deviceId); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public Collection<SnmpDevice> getDevices() { |
| 136 | return snmpDeviceMap.values(); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public SnmpDevice getDevice(DeviceId did) { |
| 141 | return snmpDeviceMap.get(did); |
| 142 | } |
| 143 | |
| 144 | @Override |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 145 | public SnmpDevice getDevice(URI uri) { |
| 146 | //this assumes that only one device is associated with one deviceId |
| 147 | return snmpDeviceMap.entrySet() |
| 148 | .stream().filter(p -> p.getValue() |
| 149 | .getSnmpHost().equals(uri.toString())) |
| 150 | .map(Map.Entry::getValue).findFirst().orElse(null); |
| 151 | } |
| 152 | |
| 153 | @Override |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 154 | public void removeDevice(DeviceId did) { |
| 155 | snmpDeviceMap.remove(did); |
| 156 | } |
| 157 | |
| 158 | @Override |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 159 | public void addDevice(SnmpDevice device) { |
| 160 | log.info("Adding device {}", device.deviceId()); |
| 161 | snmpDeviceMap.put(device.deviceId(), device); |
| 162 | } |
| 163 | |
| 164 | @Override |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 165 | public DefaultAlarm buildWalkFailedAlarm(DeviceId deviceId) { |
Ray Milkey | 0a1a091 | 2018-02-15 11:35:58 -0800 | [diff] [blame] | 166 | long timeRaised = System.currentTimeMillis(); |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 167 | return new DefaultAlarm.Builder( |
Ray Milkey | 0a1a091 | 2018-02-15 11:35:58 -0800 | [diff] [blame] | 168 | AlarmId.alarmId(deviceId, Long.toString(timeRaised)), |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 169 | deviceId, "SNMP alarm retrieval failed", |
Ray Milkey | 0a1a091 | 2018-02-15 11:35:58 -0800 | [diff] [blame] | 170 | Alarm.SeverityLevel.CRITICAL, timeRaised).build(); |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 171 | } |
| 172 | } |