blob: e82a583c03c7115dbbeb272383a11fd0c575c2ef [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
Andrea Campanella58454b92016-04-01 15:19:00 -070055 protected ISnmpSessionFactory sessionFactory;
Andrea Campanellac2d754b2016-03-29 17:51:07 -070056
Andrea Campanella58454b92016-04-01 15:19:00 -070057 protected final Map<DeviceId, ISnmpSession> sessionMap = new HashMap<>();
58 protected final Map<DeviceId, SnmpDevice> snmpDeviceMap = new ConcurrentHashMap<>();
Andrea Campanellac2d754b2016-03-29 17:51:07 -070059
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 Campanella58454b92016-04-01 15:19:00 -070069 sessionMap.clear();
Andrea Campanellac2d754b2016-03-29 17:51:07 -070070 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}