blob: 8fb9ea0599afa92fb71309ae1734633ddbb05e6d [file] [log] [blame]
kmcpeakeb172d5f2015-12-10 11:30:43 +00001/*
2 * Copyright 2015 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 */
16package org.onosproject.provider.snmp.alarm.impl;
17
18import com.btisystems.pronx.ems.core.snmp.DefaultSnmpConfigurationFactory;
19import com.btisystems.pronx.ems.core.snmp.ISnmpConfiguration;
20import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
21import com.btisystems.pronx.ems.core.snmp.ISnmpSessionFactory;
22import com.btisystems.pronx.ems.core.snmp.SnmpSessionFactory;
23import com.btisystems.pronx.ems.core.snmp.V2cSnmpConfiguration;
kmcpeakeb172d5f2015-12-10 11:30:43 +000024import com.google.common.collect.Sets;
Jonathan Hart51539b82015-10-29 09:53:04 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Modified;
29import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
31import org.apache.felix.scr.annotations.Service;
32import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreService;
34import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
35import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEvent;
36import org.onosproject.incubator.net.faultmanagement.alarm.AlarmListener;
37import org.onosproject.incubator.net.faultmanagement.alarm.AlarmProvider;
38import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
39import org.onosproject.net.DeviceId;
40import org.onosproject.net.device.DeviceEvent;
41import org.onosproject.net.device.DeviceListener;
42import org.onosproject.net.device.DeviceService;
43import org.onosproject.net.provider.AbstractProvider;
44import org.onosproject.net.provider.ProviderId;
45import org.osgi.service.component.ComponentContext;
46import org.slf4j.Logger;
kmcpeakeb172d5f2015-12-10 11:30:43 +000047
Jonathan Hart51539b82015-10-29 09:53:04 -070048import java.io.IOException;
kmcpeakeb172d5f2015-12-10 11:30:43 +000049import java.util.Collection;
50import java.util.HashMap;
51import java.util.HashSet;
52import java.util.Map;
53import java.util.Set;
54import java.util.concurrent.ExecutorService;
55import java.util.concurrent.Executors;
56
Jonathan Hart51539b82015-10-29 09:53:04 -070057import static com.google.common.base.Preconditions.checkNotNull;
kmcpeakeb172d5f2015-12-10 11:30:43 +000058import static org.onlab.util.Tools.groupedThreads;
Jonathan Hart51539b82015-10-29 09:53:04 -070059import static org.slf4j.LoggerFactory.getLogger;
kmcpeakeb172d5f2015-12-10 11:30:43 +000060
61/**
62 * SNMP alarms provider.
63 */
64@Component(immediate = true)
kmcpeake899ea8b2016-01-04 15:00:45 +000065@Service
kmcpeakeb172d5f2015-12-10 11:30:43 +000066public class SnmpAlarmProviderService extends AbstractProvider implements AlarmProvider {
67
68 private final Logger log = getLogger(getClass());
69
kmcpeake899ea8b2016-01-04 15:00:45 +000070 private final InternalDeviceListener internalDeviceListener = new InternalDeviceListener();
71
kmcpeakeb172d5f2015-12-10 11:30:43 +000072 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 protected CoreService coreService;
74
75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected DeviceService deviceService;
77
78 private ApplicationId appId;
79
80 private final ISnmpSessionFactory sessionFactory;
81
82 // TODO convert to standard ONOS listener service approach ?
83 protected Set<AlarmListener> alarmEventListener = Sets.newHashSet();
84
85 private ExecutorService eventHandlingExecutor;
86
87 // TODO Could be replaced with a service lookup, and bundles per device variant.
88 Map<String, SnmpDeviceAlarmProvider> providers = new HashMap<>();
89
90 public SnmpAlarmProviderService() {
91 super(new ProviderId("snmp", "org.onosproject.provider.alarm"));
kmcpeake899ea8b2016-01-04 15:00:45 +000092 log.info("SnmpAlarmProviderService ...");
kmcpeakeb172d5f2015-12-10 11:30:43 +000093 sessionFactory = new SnmpSessionFactory(
94 new DefaultSnmpConfigurationFactory(new V2cSnmpConfiguration()));
95 providers.put("1.3.6.1.4.1.18070.2.2", new Bti7000SnmpAlarmProvider());
96 providers.put("1.3.6.1.4.1.20408", new NetSnmpAlarmProvider());
97 }
98
99 @Activate
100 public void activate(ComponentContext context) {
101 appId = coreService.registerApplication("org.onosproject.snmp");
102 eventHandlingExecutor = Executors.newSingleThreadExecutor(
103 groupedThreads("onos/alarms", "event-handler"));
kmcpeake899ea8b2016-01-04 15:00:45 +0000104 deviceService.addListener(internalDeviceListener);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000105 log.info("activated SNMP provider with appId = {} and context props {}", appId, context.getProperties());
106 modified(context);
107
108 log.info("Started");
109 }
110
111 @Deactivate
112 public void deactivate() {
113 log.info("deactivate SNMP provider {}", appId);
kmcpeake899ea8b2016-01-04 15:00:45 +0000114 deviceService.removeListener(internalDeviceListener);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000115 }
116
117 @Modified
118 public void modified(ComponentContext context) {
119 log.info("modified {}", context);
120
121 if (context == null) {
122 log.info("No configuration file");
123 }
124
125 }
126
127 @Override
128 public void triggerProbe(DeviceId deviceId) {
129 log.info("SNMP walk request for alarms at deviceId={}", deviceId);
130 if (!isSnmpDevice(deviceId)) {
131 log.info("Ignore non-snmp device!");
132 return;
133 }
134 String[] deviceComponents = deviceId.toString().split(":");
135 Set<Alarm> alarms = new HashSet<>(Sets.newHashSet());
136
137 if (deviceComponents.length > 1) {
138 String ipAddress = deviceComponents[1];
139 String port = deviceComponents[2];
140 ISnmpConfiguration config = new V2cSnmpConfiguration();
141 config.setPort(Integer.parseInt(port));
142
143 try (ISnmpSession session = getSessionFactory().createSession(config, ipAddress)) {
144 // Each session will be auto-closed.
Jonathan Hart51539b82015-10-29 09:53:04 -0700145 String deviceOid = session.identifyDevice();
146 alarms.addAll(getAlarmsForDevice(deviceOid, session, deviceId));
kmcpeakeb172d5f2015-12-10 11:30:43 +0000147 log.info("SNMP walk completed ok for deviceId={}", deviceId);
148 } catch (IOException | RuntimeException ex) {
149 log.error("Failed to walk device.", ex.getMessage());
150 log.debug("Detailed problem was ", ex);
151 alarms.add(
152 buildWalkFailedAlarm(deviceId)
153 );
154 }
155 }
156
157 AlarmEvent alarmEvent = new AlarmEvent(alarms, deviceId);
158
159 alarmEventListener.stream().forEach((listener) -> {
160 listener.event(alarmEvent);
161 log.info("Successfully event with discovered alarms for deviceId={} to {}", deviceId, listener);
162 });
163
164 }
165
166 private static DefaultAlarm buildWalkFailedAlarm(DeviceId deviceId) {
167 return new DefaultAlarm.Builder(
168 deviceId, "SNMP alarm retrieval failed",
169 Alarm.SeverityLevel.CRITICAL,
170 System.currentTimeMillis()).build();
171 }
172
173 protected ISnmpSessionFactory getSessionFactory() {
174 return sessionFactory;
175 }
176
Jonathan Hart51539b82015-10-29 09:53:04 -0700177 private Collection<Alarm> getAlarmsForDevice(String deviceOid, ISnmpSession session,
kmcpeakeb172d5f2015-12-10 11:30:43 +0000178 DeviceId deviceID) throws IOException {
179 Collection<Alarm> alarms = new HashSet<>();
Jonathan Hart51539b82015-10-29 09:53:04 -0700180 if (providers.containsKey(deviceOid)) {
181 alarms.addAll(providers.get(deviceOid).getAlarms(session, deviceID));
kmcpeakeb172d5f2015-12-10 11:30:43 +0000182 }
183 return alarms;
184 }
185
186 @Override
187 public void addAlarmListener(AlarmListener listener) {
188 alarmEventListener.add(checkNotNull(listener, "Listener cannot be null"));
189 }
190
191 @Override
192 public void removeAlarmListener(AlarmListener listener) {
193 alarmEventListener.remove(checkNotNull(listener, "Listener cannot be null"));
194 }
195
196 /**
197 * Internal listener for device service events.
198 */
199 private class InternalDeviceListener implements DeviceListener {
200
201 @Override
202 public void event(DeviceEvent event) {
203 log.info("InternalDeviceListener has got event from device-service{} with ", event);
204 eventHandlingExecutor.execute(() -> {
205 try {
206 DeviceId deviceId = event.subject().id();
207 log.info("From device {}", deviceId);
208 if (!isSnmpDevice(deviceId)) {
209 log.info("Ignore non-snmp device event for {}", deviceId);
210 return;
211 }
212
213 switch (event.type()) {
214 case DEVICE_ADDED:
215 case DEVICE_UPDATED:
216 case DEVICE_AVAILABILITY_CHANGED:
217 if (deviceService.isAvailable(event.subject().id())) {
218 triggerProbe(deviceId);
219 }
220 break;
221 case DEVICE_REMOVED:
222 case DEVICE_SUSPENDED:
223 default:
224 // Could potentially remove all alarms when eg DEVICE_REMOVED or DEVICE_SUSPENDED
225 // however for now ignore and fall through
226 break;
227 }
228 } catch (Exception e) {
229 log.warn("Failed to process {}", event, e);
230 }
231 });
232 }
233
234 }
235
236 private static boolean isSnmpDevice(DeviceId deviceId) {
237 return deviceId.uri().getScheme().equalsIgnoreCase("snmp");
238 }
239}