Andrea Campanella | c2d754b | 2016-03-29 17:51:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 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; |
| 30 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.snmp.SnmpController; |
| 33 | import org.onosproject.snmp.SnmpDevice; |
| 34 | import org.osgi.service.component.ComponentContext; |
| 35 | import org.osgi.service.component.annotations.Component; |
| 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | import java.io.IOException; |
| 40 | import java.util.Collection; |
| 41 | import java.util.HashMap; |
| 42 | import java.util.Map; |
| 43 | import java.util.concurrent.ConcurrentHashMap; |
| 44 | |
| 45 | /** |
| 46 | * Default implementation of the SNMP sub-controller. |
| 47 | */ |
| 48 | @Component(immediate = true) |
| 49 | @Service |
| 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 |
| 75 | public ISnmpSession getSession(DeviceId deviceId) throws IOException { |
| 76 | if (!sessionMap.containsKey(deviceId)) { |
| 77 | SnmpDevice device = snmpDeviceMap.get(deviceId); |
| 78 | String ipAddress = null; |
| 79 | int port = -1; |
| 80 | if (device != null) { |
| 81 | ipAddress = device.getSnmpHost(); |
| 82 | port = device.getSnmpPort(); |
| 83 | } else { |
| 84 | String[] deviceComponents = deviceId.toString().split(":"); |
| 85 | if (deviceComponents.length > 1) { |
| 86 | ipAddress = deviceComponents[1]; |
| 87 | port = Integer.parseInt(deviceComponents[2]); |
| 88 | |
| 89 | } else { |
| 90 | log.error("Cannot obtain correct information from device id", deviceId); |
| 91 | } |
| 92 | } |
| 93 | Preconditions.checkNotNull(ipAddress, "ip address is empty, cannot start session"); |
| 94 | Preconditions.checkArgument(port != -1, "port is incorrect, cannot start session"); |
| 95 | |
| 96 | ISnmpConfiguration config = new V2cSnmpConfiguration(); |
| 97 | config.setPort(port); |
| 98 | sessionMap.put(deviceId, sessionFactory.createSession(config, ipAddress)); |
| 99 | } |
| 100 | return sessionMap.get(deviceId); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Collection<SnmpDevice> getDevices() { |
| 105 | return snmpDeviceMap.values(); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public SnmpDevice getDevice(DeviceId did) { |
| 110 | return snmpDeviceMap.get(did); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void removeDevice(DeviceId did) { |
| 115 | snmpDeviceMap.remove(did); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void addDevice(DeviceId did, SnmpDevice device) { |
| 120 | snmpDeviceMap.put(did, device); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public DefaultAlarm buildWalkFailedAlarm(DeviceId deviceId) { |
| 125 | return new DefaultAlarm.Builder( |
| 126 | deviceId, "SNMP alarm retrieval failed", |
| 127 | Alarm.SeverityLevel.CRITICAL, |
| 128 | System.currentTimeMillis()).build(); |
| 129 | } |
| 130 | } |