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