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; |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 26 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
Ray Milkey | 0a1a091 | 2018-02-15 11:35:58 -0800 | [diff] [blame] | 27 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId; |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 28 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.snmp.SnmpController; |
| 31 | import org.onosproject.snmp.SnmpDevice; |
| 32 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 33 | import org.osgi.service.component.annotations.Activate; |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 34 | import org.osgi.service.component.annotations.Component; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 35 | import org.osgi.service.component.annotations.Deactivate; |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | import java.io.IOException; |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 40 | import java.net.URI; |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 41 | import java.util.Collection; |
| 42 | import java.util.HashMap; |
| 43 | import java.util.Map; |
| 44 | import java.util.concurrent.ConcurrentHashMap; |
| 45 | |
| 46 | /** |
| 47 | * Default implementation of the SNMP sub-controller. |
| 48 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 49 | @Component(immediate = true, service = SnmpController.class) |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 50 | public class DefaultSnmpController implements SnmpController { |
| 51 | |
| 52 | private final Logger log = LoggerFactory |
| 53 | .getLogger(getClass()); |
| 54 | |
Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [diff] [blame] | 55 | protected ISnmpSessionFactory sessionFactory; |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 56 | |
Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [diff] [blame] | 57 | protected final Map<DeviceId, ISnmpSession> sessionMap = new HashMap<>(); |
| 58 | protected final Map<DeviceId, SnmpDevice> snmpDeviceMap = new ConcurrentHashMap<>(); |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 59 | |
| 60 | @Activate |
| 61 | public void activate(ComponentContext context) { |
| 62 | sessionFactory = new SnmpSessionFactory( |
| 63 | new DefaultSnmpConfigurationFactory(new V2cSnmpConfiguration())); |
| 64 | log.info("Started"); |
| 65 | } |
| 66 | |
| 67 | @Deactivate |
| 68 | public void deactivate() { |
Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [diff] [blame] | 69 | sessionMap.clear(); |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 70 | snmpDeviceMap.clear(); |
| 71 | log.info("Stopped"); |
| 72 | } |
| 73 | |
| 74 | @Override |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 75 | @Deprecated |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 76 | public ISnmpSession getSession(DeviceId deviceId) throws IOException { |
| 77 | if (!sessionMap.containsKey(deviceId)) { |
| 78 | SnmpDevice device = snmpDeviceMap.get(deviceId); |
| 79 | String ipAddress = null; |
| 80 | int port = -1; |
| 81 | if (device != null) { |
| 82 | ipAddress = device.getSnmpHost(); |
| 83 | port = device.getSnmpPort(); |
| 84 | } else { |
| 85 | String[] deviceComponents = deviceId.toString().split(":"); |
| 86 | if (deviceComponents.length > 1) { |
| 87 | ipAddress = deviceComponents[1]; |
| 88 | port = Integer.parseInt(deviceComponents[2]); |
| 89 | |
| 90 | } else { |
| 91 | log.error("Cannot obtain correct information from device id", deviceId); |
| 92 | } |
| 93 | } |
| 94 | Preconditions.checkNotNull(ipAddress, "ip address is empty, cannot start session"); |
| 95 | Preconditions.checkArgument(port != -1, "port is incorrect, cannot start session"); |
| 96 | |
| 97 | ISnmpConfiguration config = new V2cSnmpConfiguration(); |
| 98 | config.setPort(port); |
| 99 | sessionMap.put(deviceId, sessionFactory.createSession(config, ipAddress)); |
| 100 | } |
| 101 | return sessionMap.get(deviceId); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public Collection<SnmpDevice> getDevices() { |
| 106 | return snmpDeviceMap.values(); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public SnmpDevice getDevice(DeviceId did) { |
| 111 | return snmpDeviceMap.get(did); |
| 112 | } |
| 113 | |
| 114 | @Override |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 115 | public SnmpDevice getDevice(URI uri) { |
| 116 | //this assumes that only one device is associated with one deviceId |
| 117 | return snmpDeviceMap.entrySet() |
| 118 | .stream().filter(p -> p.getValue() |
| 119 | .getSnmpHost().equals(uri.toString())) |
| 120 | .map(Map.Entry::getValue).findFirst().orElse(null); |
| 121 | } |
| 122 | |
| 123 | @Override |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 124 | public void removeDevice(DeviceId did) { |
| 125 | snmpDeviceMap.remove(did); |
| 126 | } |
| 127 | |
| 128 | @Override |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 129 | public void addDevice(SnmpDevice device) { |
| 130 | log.info("Adding device {}", device.deviceId()); |
| 131 | snmpDeviceMap.put(device.deviceId(), device); |
| 132 | } |
| 133 | |
| 134 | @Override |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 135 | public DefaultAlarm buildWalkFailedAlarm(DeviceId deviceId) { |
Ray Milkey | 0a1a091 | 2018-02-15 11:35:58 -0800 | [diff] [blame] | 136 | long timeRaised = System.currentTimeMillis(); |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 137 | return new DefaultAlarm.Builder( |
Ray Milkey | 0a1a091 | 2018-02-15 11:35:58 -0800 | [diff] [blame] | 138 | AlarmId.alarmId(deviceId, Long.toString(timeRaised)), |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 139 | deviceId, "SNMP alarm retrieval failed", |
Ray Milkey | 0a1a091 | 2018-02-15 11:35:58 -0800 | [diff] [blame] | 140 | Alarm.SeverityLevel.CRITICAL, timeRaised).build(); |
Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 141 | } |
| 142 | } |