blob: 35556cdcb23368d3c9e560cacff7503614bdc24b [file] [log] [blame]
Andrea Campanellac2d754b2016-03-29 17:51:07 -07001/*
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
17package org.onosproject.snmp.ctl;
18
19import com.btisystems.pronx.ems.core.snmp.DefaultSnmpConfigurationFactory;
20import com.btisystems.pronx.ems.core.snmp.ISnmpConfiguration;
21import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
22import com.btisystems.pronx.ems.core.snmp.ISnmpSessionFactory;
23import com.btisystems.pronx.ems.core.snmp.SnmpSessionFactory;
24import com.btisystems.pronx.ems.core.snmp.V2cSnmpConfiguration;
25import com.google.common.base.Preconditions;
26import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Service;
29import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
30import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
31import org.onosproject.net.DeviceId;
32import org.onosproject.snmp.SnmpController;
33import org.onosproject.snmp.SnmpDevice;
34import org.osgi.service.component.ComponentContext;
35import org.osgi.service.component.annotations.Component;
36import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
39import java.io.IOException;
40import java.util.Collection;
41import java.util.HashMap;
42import java.util.Map;
43import java.util.concurrent.ConcurrentHashMap;
44
45/**
46 * Default implementation of the SNMP sub-controller.
47 */
48@Component(immediate = true)
49@Service
50public class DefaultSnmpController implements SnmpController {
51
52 private final Logger log = LoggerFactory
53 .getLogger(getClass());
54
55 private ISnmpSessionFactory sessionFactory;
56
57 private final Map<DeviceId, ISnmpSession> sessionMap = new HashMap<>();
58 protected Map<DeviceId, SnmpDevice> snmpDeviceMap = new ConcurrentHashMap<>();
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() {
69 snmpDeviceMap.clear();
70 log.info("Stopped");
71 }
72
73 @Override
74 public ISnmpSession getSession(DeviceId deviceId) throws IOException {
75 if (!sessionMap.containsKey(deviceId)) {
76 SnmpDevice device = snmpDeviceMap.get(deviceId);
77 String ipAddress = null;
78 int port = -1;
79 if (device != null) {
80 ipAddress = device.getSnmpHost();
81 port = device.getSnmpPort();
82 } else {
83 String[] deviceComponents = deviceId.toString().split(":");
84 if (deviceComponents.length > 1) {
85 ipAddress = deviceComponents[1];
86 port = Integer.parseInt(deviceComponents[2]);
87
88 } else {
89 log.error("Cannot obtain correct information from device id", deviceId);
90 }
91 }
92 Preconditions.checkNotNull(ipAddress, "ip address is empty, cannot start session");
93 Preconditions.checkArgument(port != -1, "port is incorrect, cannot start session");
94
95 ISnmpConfiguration config = new V2cSnmpConfiguration();
96 config.setPort(port);
97 sessionMap.put(deviceId, sessionFactory.createSession(config, ipAddress));
98 }
99 return sessionMap.get(deviceId);
100 }
101
102 @Override
103 public Collection<SnmpDevice> getDevices() {
104 return snmpDeviceMap.values();
105 }
106
107 @Override
108 public SnmpDevice getDevice(DeviceId did) {
109 return snmpDeviceMap.get(did);
110 }
111
112 @Override
113 public void removeDevice(DeviceId did) {
114 snmpDeviceMap.remove(did);
115 }
116
117 @Override
118 public void addDevice(DeviceId did, SnmpDevice device) {
119 snmpDeviceMap.put(did, device);
120 }
121
122 @Override
123 public DefaultAlarm buildWalkFailedAlarm(DeviceId deviceId) {
124 return new DefaultAlarm.Builder(
125 deviceId, "SNMP alarm retrieval failed",
126 Alarm.SeverityLevel.CRITICAL,
127 System.currentTimeMillis()).build();
128 }
129}