Andrea Campanella
Committed by Gerrit Code Review

Removing old SNMPAllarmReleated classes

Change-Id: I27c0e5e30596d77d9099094a1ebda0f50d4b29ac
1 -<?xml version="1.0" encoding="UTF-8"?>
2 -<!--
3 -~ Copyright 2015-present Open Networking Laboratory
4 -~
5 -~ Licensed under the Apache License, Version 2.0 (the "License");
6 -~ you may not use this file except in compliance with the License.
7 -~ You may obtain a copy of the License at
8 -~
9 -~ http://www.apache.org/licenses/LICENSE-2.0
10 -~
11 -~ Unless required by applicable law or agreed to in writing, software
12 -~ distributed under the License is distributed on an "AS IS" BASIS,
13 -~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 -~ See the License for the specific language governing permissions and
15 -~ limitations under the License.
16 --->
17 -<project xmlns="http://maven.apache.org/POM/4.0.0"
18 - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19 - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
20 - <modelVersion>4.0.0</modelVersion>
21 -
22 - <parent>
23 - <groupId>org.onosproject</groupId>
24 - <artifactId>onos-snmp-providers</artifactId>
25 - <version>1.6.0-SNAPSHOT</version>
26 - <relativePath>../pom.xml</relativePath>
27 - </parent>
28 -
29 - <artifactId>onos-snmp-provider-alarm</artifactId>
30 - <packaging>bundle</packaging>
31 -
32 - <description>ONOS SNMP protocol alarm provider</description>
33 -
34 - <dependencies>
35 - <dependency>
36 - <groupId>com.btisystems</groupId>
37 - <artifactId>snmp-core</artifactId>
38 - <version>1.3-SNAPSHOT</version>
39 - </dependency>
40 -
41 - <dependency>
42 - <groupId>com.btisystems.mibbler.mibs</groupId>
43 - <artifactId>bti7000</artifactId>
44 - <version>1.0-SNAPSHOT</version>
45 - </dependency>
46 -
47 - <dependency>
48 - <groupId>com.btisystems.mibbler.mibs</groupId>
49 - <artifactId>net-snmp</artifactId>
50 - <version>1.0-SNAPSHOT</version>
51 - </dependency>
52 -
53 - <dependency>
54 - <groupId>org.osgi</groupId>
55 - <artifactId>org.osgi.compendium</artifactId>
56 - <type>jar</type>
57 - </dependency>
58 -
59 - <dependency>
60 - <groupId>org.onosproject</groupId>
61 - <artifactId>onos-api</artifactId>
62 - </dependency>
63 -
64 -
65 -
66 - <dependency>
67 - <groupId>org.onosproject</groupId>
68 - <artifactId>onlab-junit</artifactId>
69 - <scope>test</scope>
70 - </dependency>
71 -
72 - <dependency>
73 - <groupId>org.onosproject</groupId>
74 - <artifactId>onlab-osgi</artifactId>
75 - <classifier>tests</classifier>
76 - <scope>test</scope>
77 - </dependency>
78 -
79 - <dependency>
80 - <groupId>org.onosproject</groupId>
81 - <artifactId>onos-api</artifactId>
82 - <classifier>tests</classifier>
83 - <scope>test</scope>
84 - </dependency>
85 -
86 - <dependency>
87 - <groupId>junit</groupId>
88 - <artifactId>junit</artifactId>
89 - <scope>test</scope>
90 - </dependency>
91 - <dependency>
92 - <groupId>org.hamcrest</groupId>
93 - <artifactId>hamcrest-core</artifactId>
94 - <scope>test</scope>
95 - </dependency>
96 - <dependency>
97 - <groupId>org.hamcrest</groupId>
98 - <artifactId>hamcrest-library</artifactId>
99 - <scope>test</scope>
100 - </dependency>
101 - <dependency>
102 - <groupId>org.easymock</groupId>
103 - <artifactId>easymock</artifactId>
104 - <scope>test</scope>
105 - </dependency>
106 - </dependencies>
107 -
108 - <build>
109 - <plugins>
110 -
111 - <plugin>
112 - <groupId>org.apache.felix</groupId>
113 - <artifactId>maven-scr-plugin</artifactId>
114 - </plugin>
115 - <plugin>
116 - <groupId>org.apache.felix</groupId>
117 - <artifactId>maven-bundle-plugin</artifactId>
118 - </plugin>
119 - <plugin>
120 - <groupId>org.onosproject</groupId>
121 - <artifactId>onos-maven-plugin</artifactId>
122 - </plugin>
123 - </plugins>
124 - </build>
125 -</project>
1 -/*
2 - * Copyright 2015-present 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 -package org.onosproject.provider.snmp.alarm.impl;
17 -
18 -import com.btisystems.mibbler.mibs.netsnmp.netsnmp.I_Device;
19 -import com.btisystems.mibbler.mibs.netsnmp.netsnmp._OidRegistry;
20 -import com.btisystems.mibbler.mibs.netsnmp.netsnmp.mib_2.interfaces.IfTable;
21 -import com.btisystems.pronx.ems.core.model.ClassRegistry;
22 -import com.btisystems.pronx.ems.core.model.IClassRegistry;
23 -import com.btisystems.pronx.ems.core.model.NetworkDevice;
24 -import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
25 -import java.io.IOException;
26 -import java.util.Arrays;
27 -import java.util.Collection;
28 -import java.util.HashSet;
29 -import java.util.Set;
30 -import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
31 -import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId;
32 -import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
33 -import org.onosproject.net.DeviceId;
34 -import org.slf4j.Logger;
35 -import static org.slf4j.LoggerFactory.getLogger;
36 -import org.snmp4j.smi.OID;
37 -
38 -/**
39 - * Net SNMP specific implementation to provide a list of current alarms.
40 - * @deprecated 1.5.0 Falcon, not compliant with ONOS SB and driver architecture.
41 - */
42 -@Deprecated
43 -public class NetSnmpAlarmProvider implements SnmpDeviceAlarmProvider {
44 - private final Logger log = getLogger(getClass());
45 - protected static final IClassRegistry CLASS_REGISTRY =
46 - new ClassRegistry(_OidRegistry.oidRegistry, I_Device.class);
47 - @Override
48 - public Collection<Alarm> getAlarms(ISnmpSession session, DeviceId deviceId) {
49 - Set<Alarm> alarms = new HashSet<>();
50 -
51 - NetworkDevice networkDevice = new NetworkDevice(CLASS_REGISTRY,
52 - session.getAddress().getHostAddress());
53 - try {
54 - session.walkDevice(networkDevice, Arrays.asList(new OID[]{
55 - CLASS_REGISTRY.getClassToOidMap().get(IfTable.class)}));
56 -
57 - IfTable interfaceTable = (IfTable) networkDevice.getRootObject()
58 - .getEntity(CLASS_REGISTRY.getClassToOidMap().get(IfTable.class));
59 - if (interfaceTable != null) {
60 - interfaceTable.getEntries().values().stream().forEach((ifEntry) -> {
61 - //TODO will raise alarm for each interface as a demo.
62 - // if (ifEntry.getIfAdminStatus() == 1 && ifEntry.getIfOperStatus() == 2){
63 - alarms.add(new DefaultAlarm.Builder(deviceId, "Link Down.",
64 - Alarm.SeverityLevel.CRITICAL, System.currentTimeMillis())
65 - .forSource(AlarmEntityId.alarmEntityId("port:" + ifEntry.getIfDescr())).build());
66 - // }
67 - log.info("Interface: " + ifEntry);
68 - });
69 - }
70 - } catch (IOException ex) {
71 - log.error("Error reading alarms for device {}.", deviceId, ex);
72 - }
73 - return alarms;
74 - }
75 -}
1 -/*
2 - * Copyright 2015-present 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 -package org.onosproject.provider.snmp.alarm.impl;
17 -
18 -import com.btisystems.pronx.ems.core.snmp.DefaultSnmpConfigurationFactory;
19 -import com.btisystems.pronx.ems.core.snmp.ISnmpConfiguration;
20 -import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
21 -import com.btisystems.pronx.ems.core.snmp.ISnmpSessionFactory;
22 -import com.btisystems.pronx.ems.core.snmp.SnmpSessionFactory;
23 -import com.btisystems.pronx.ems.core.snmp.V2cSnmpConfiguration;
24 -import com.google.common.collect.Sets;
25 -import org.apache.felix.scr.annotations.Activate;
26 -import org.apache.felix.scr.annotations.Component;
27 -import org.apache.felix.scr.annotations.Deactivate;
28 -import org.apache.felix.scr.annotations.Modified;
29 -import org.apache.felix.scr.annotations.Reference;
30 -import org.apache.felix.scr.annotations.ReferenceCardinality;
31 -import org.apache.felix.scr.annotations.Service;
32 -import org.onosproject.core.ApplicationId;
33 -import org.onosproject.core.CoreService;
34 -import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
35 -import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEvent;
36 -import org.onosproject.incubator.net.faultmanagement.alarm.AlarmListener;
37 -import org.onosproject.incubator.net.faultmanagement.alarm.AlarmProvider;
38 -import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
39 -import org.onosproject.net.DeviceId;
40 -import org.onosproject.net.device.DeviceEvent;
41 -import org.onosproject.net.device.DeviceListener;
42 -import org.onosproject.net.device.DeviceService;
43 -import org.onosproject.net.provider.AbstractProvider;
44 -import org.onosproject.net.provider.ProviderId;
45 -import org.osgi.service.component.ComponentContext;
46 -import org.slf4j.Logger;
47 -
48 -import java.io.IOException;
49 -import java.util.Collection;
50 -import java.util.HashMap;
51 -import java.util.HashSet;
52 -import java.util.Map;
53 -import java.util.Set;
54 -import java.util.concurrent.ExecutorService;
55 -import java.util.concurrent.Executors;
56 -
57 -import static com.google.common.base.Preconditions.checkNotNull;
58 -import static org.onlab.util.Tools.groupedThreads;
59 -import static org.slf4j.LoggerFactory.getLogger;
60 -
61 -/**
62 - * SNMP alarms provider.
63 - * @deprecated 1.5.0 Falcon, not compliant with ONOS SB and driver architecture.
64 - */
65 -@Deprecated
66 -@Component(immediate = true)
67 -@Service
68 -public class SnmpAlarmProviderService extends AbstractProvider implements AlarmProvider {
69 -
70 - private final Logger log = getLogger(getClass());
71 -
72 - private final InternalDeviceListener internalDeviceListener = new InternalDeviceListener();
73 -
74 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 - protected CoreService coreService;
76 -
77 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 - protected DeviceService deviceService;
79 -
80 - private ApplicationId appId;
81 -
82 - private final ISnmpSessionFactory sessionFactory;
83 -
84 - // TODO convert to standard ONOS listener service approach ?
85 - protected Set<AlarmListener> alarmEventListener = Sets.newHashSet();
86 -
87 - private ExecutorService eventHandlingExecutor;
88 -
89 - // TODO Could be replaced with a service lookup, and bundles per device variant.
90 - Map<String, SnmpDeviceAlarmProvider> providers = new HashMap<>();
91 -
92 - @Deprecated
93 - public SnmpAlarmProviderService() {
94 - super(new ProviderId("snmp", "org.onosproject.provider.alarm"));
95 - log.info("SnmpAlarmProviderService ...");
96 - sessionFactory = new SnmpSessionFactory(
97 - new DefaultSnmpConfigurationFactory(new V2cSnmpConfiguration()));
98 - providers.put("1.3.6.1.4.1.18070.2.2", new Bti7000SnmpAlarmProvider());
99 - providers.put("1.3.6.1.4.1.20408", new NetSnmpAlarmProvider());
100 - }
101 -
102 - @Activate
103 - public void activate(ComponentContext context) {
104 - appId = coreService.registerApplication("org.onosproject.snmp");
105 - eventHandlingExecutor = Executors.newSingleThreadExecutor(
106 - groupedThreads("onos/alarms", "event-handler"));
107 - deviceService.addListener(internalDeviceListener);
108 - log.info("activated SNMP provider with appId = {} and context props {}", appId, context.getProperties());
109 - modified(context);
110 -
111 - log.info("Started");
112 - }
113 -
114 - @Deactivate
115 - public void deactivate() {
116 - log.info("deactivate SNMP provider {}", appId);
117 - deviceService.removeListener(internalDeviceListener);
118 - }
119 -
120 - @Modified
121 - public void modified(ComponentContext context) {
122 - log.info("modified {}", context);
123 -
124 - if (context == null) {
125 - log.info("No configuration file");
126 - }
127 -
128 - }
129 -
130 - @Override
131 - public void triggerProbe(DeviceId deviceId) {
132 - log.info("SNMP walk request for alarms at deviceId={}", deviceId);
133 - if (!isSnmpDevice(deviceId)) {
134 - log.info("Ignore non-snmp device!");
135 - return;
136 - }
137 - String[] deviceComponents = deviceId.toString().split(":");
138 - Set<Alarm> alarms = new HashSet<>(Sets.newHashSet());
139 -
140 - if (deviceComponents.length > 1) {
141 - String ipAddress = deviceComponents[1];
142 - String port = deviceComponents[2];
143 - ISnmpConfiguration config = new V2cSnmpConfiguration();
144 - config.setPort(Integer.parseInt(port));
145 -
146 - try (ISnmpSession session = getSessionFactory().createSession(config, ipAddress)) {
147 - // Each session will be auto-closed.
148 - String deviceOid = session.identifyDevice();
149 - alarms.addAll(getAlarmsForDevice(deviceOid, session, deviceId));
150 - log.info("SNMP walk completed ok for deviceId={}", deviceId);
151 - } catch (IOException | RuntimeException ex) {
152 - log.error("Failed to walk device.", ex.getMessage());
153 - log.debug("Detailed problem was ", ex);
154 - alarms.add(
155 - buildWalkFailedAlarm(deviceId)
156 - );
157 - }
158 - }
159 -
160 - AlarmEvent alarmEvent = new AlarmEvent(alarms, deviceId);
161 -
162 - alarmEventListener.stream().forEach((listener) -> {
163 - listener.event(alarmEvent);
164 - log.info("Successfully event with discovered alarms for deviceId={} to {}", deviceId, listener);
165 - });
166 -
167 - }
168 -
169 - private static DefaultAlarm buildWalkFailedAlarm(DeviceId deviceId) {
170 - return new DefaultAlarm.Builder(
171 - deviceId, "SNMP alarm retrieval failed",
172 - Alarm.SeverityLevel.CRITICAL,
173 - System.currentTimeMillis()).build();
174 - }
175 -
176 - protected ISnmpSessionFactory getSessionFactory() {
177 - return sessionFactory;
178 - }
179 -
180 - private Collection<Alarm> getAlarmsForDevice(String deviceOid, ISnmpSession session,
181 - DeviceId deviceID) throws IOException {
182 - Collection<Alarm> alarms = new HashSet<>();
183 - if (providers.containsKey(deviceOid)) {
184 - alarms.addAll(providers.get(deviceOid).getAlarms(session, deviceID));
185 - }
186 - return alarms;
187 - }
188 -
189 - @Override
190 - public void addAlarmListener(AlarmListener listener) {
191 - alarmEventListener.add(checkNotNull(listener, "Listener cannot be null"));
192 - }
193 -
194 - @Override
195 - public void removeAlarmListener(AlarmListener listener) {
196 - alarmEventListener.remove(checkNotNull(listener, "Listener cannot be null"));
197 - }
198 -
199 - /**
200 - * Internal listener for device service events.
201 - */
202 - private class InternalDeviceListener implements DeviceListener {
203 -
204 - @Override
205 - public void event(DeviceEvent event) {
206 - log.info("InternalDeviceListener has got event from device-service{} with ", event);
207 - eventHandlingExecutor.execute(() -> {
208 - try {
209 - DeviceId deviceId = event.subject().id();
210 - log.info("From device {}", deviceId);
211 - if (!isSnmpDevice(deviceId)) {
212 - log.info("Ignore non-snmp device event for {}", deviceId);
213 - return;
214 - }
215 -
216 - switch (event.type()) {
217 - case DEVICE_ADDED:
218 - case DEVICE_UPDATED:
219 - case DEVICE_AVAILABILITY_CHANGED:
220 - if (deviceService.isAvailable(event.subject().id())) {
221 - triggerProbe(deviceId);
222 - }
223 - break;
224 - case DEVICE_REMOVED:
225 - case DEVICE_SUSPENDED:
226 - default:
227 - // Could potentially remove all alarms when eg DEVICE_REMOVED or DEVICE_SUSPENDED
228 - // however for now ignore and fall through
229 - break;
230 - }
231 - } catch (Exception e) {
232 - log.warn("Failed to process {}", event, e);
233 - }
234 - });
235 - }
236 -
237 - }
238 -
239 - private static boolean isSnmpDevice(DeviceId deviceId) {
240 - return deviceId.uri().getScheme().equalsIgnoreCase("snmp");
241 - }
242 -}
1 -/*
2 - * Copyright 2015-present 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 -package org.onosproject.provider.snmp.alarm.impl;
17 -
18 -import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
19 -import java.util.Collection;
20 -import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
21 -import org.onosproject.net.DeviceId;
22 -
23 -/**
24 - * @deprecated 1.5.0 Falcon, not compliant with ONOS SB and driver architecture.
25 - */
26 -@Deprecated
27 -public interface SnmpDeviceAlarmProvider {
28 - /**
29 - * Implemented by device specific implementations which query the current
30 - * alarms from a device.
31 - * @deprecated 1.5.0 Falcon
32 - * @param snmpSession SNMP Session
33 - * @param deviceId device identifier
34 - * @return device alarms
35 - */
36 - @Deprecated
37 - Collection<Alarm> getAlarms(ISnmpSession snmpSession, DeviceId deviceId);
38 -}
1 -/*
2 - * Copyright 2015-present 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 -package org.onosproject.provider.snmp.alarm.impl;
17 -
18 -import com.btisystems.mibbler.mibs.bti7000.bti7000_13_2_0.btisystems.btiproducts.bti7000.objects.conditions.ActAlarmTable;
19 -import com.btisystems.mibbler.mibs.bti7000.interfaces.btisystems.btiproducts.bti7000.objects.conditions.actalarmtable.IActAlarmEntry;
20 -import com.btisystems.pronx.ems.core.model.NetworkDevice;
21 -import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
22 -import java.io.IOException;
23 -import java.net.InetAddress;
24 -import java.net.UnknownHostException;
25 -import java.util.Arrays;
26 -import java.util.Collection;
27 -import org.easymock.Capture;
28 -import static org.easymock.EasyMock.capture;
29 -import static org.easymock.EasyMock.createMock;
30 -import static org.easymock.EasyMock.eq;
31 -import static org.easymock.EasyMock.expect;
32 -import static org.easymock.EasyMock.isA;
33 -import static org.easymock.EasyMock.replay;
34 -import static org.easymock.EasyMock.verify;
35 -import org.junit.Before;
36 -import org.junit.Test;
37 -import static org.junit.Assert.*;
38 -import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
39 -import org.onosproject.net.DeviceId;
40 -import org.snmp4j.smi.OID;
41 -
42 -
43 -public class Bti7000SnmpAlarmProviderTest {
44 - private Bti7000SnmpAlarmProvider alarmProvider;
45 - private ISnmpSession mockSession;
46 - private ActAlarmTable alarmsTable;
47 -
48 - public Bti7000SnmpAlarmProviderTest() {
49 - }
50 -
51 - @Before
52 - public void setUp() {
53 - mockSession = createMock(ISnmpSession.class);
54 -
55 - alarmProvider = new Bti7000SnmpAlarmProvider();
56 - }
57 -
58 - @Test
59 - public void shouldWalkDevice() throws UnknownHostException, IOException {
60 - expect(mockSession.getAddress()).andReturn(InetAddress.getLoopbackAddress());
61 - expect(mockSession.walkDevice(isA(NetworkDevice.class),
62 - eq(Arrays.asList(new OID[]{
63 - Bti7000SnmpAlarmProvider.CLASS_REGISTRY.getClassToOidMap().get(ActAlarmTable.class)}))))
64 - .andReturn(null);
65 -
66 - replay(mockSession);
67 -
68 - assertNotNull(alarmProvider.getAlarms(mockSession, DeviceId.deviceId("snmp:1.1.1.1")));
69 -
70 - verify(mockSession);
71 - }
72 -
73 - @Test
74 - public void shouldFindAlarms() throws UnknownHostException, IOException {
75 - alarmsTable = new ActAlarmTable();
76 - alarmsTable.createEntry("14.1.3.6.1.4.1.18070.2.2.2.2.20.0.1.13.1.3.6.1.4.1."
77 - + "18070.2.2.1.4.14.1.7.49.46.55.46.50.46.53");
78 - IActAlarmEntry entry = alarmsTable.getEntries().values().iterator().next();
79 - entry.setActAlarmDescription("XFP Missing.");
80 - entry.setActAlarmDateAndTime("07:df:0c:01:03:0d:30:00");
81 - entry.setActAlarmSeverity(1);
82 -
83 - Capture<NetworkDevice> networkDeviceCapture = new Capture<>();
84 -
85 - expect(mockSession.getAddress()).andReturn(InetAddress.getLoopbackAddress());
86 - expect(mockSession.walkDevice(capture(networkDeviceCapture),
87 - eq(Arrays.asList(new OID[]{
88 - Bti7000SnmpAlarmProvider.CLASS_REGISTRY.getClassToOidMap().get(ActAlarmTable.class)}))))
89 - .andAnswer(() -> {
90 - networkDeviceCapture.getValue().getRootObject().setObject(alarmsTable);
91 - return null;
92 - });
93 -
94 - replay(mockSession);
95 -
96 - Collection<Alarm> alarms = alarmProvider.getAlarms(mockSession, DeviceId.deviceId("snmp:1.1.1.1"));
97 - assertEquals(1, alarms.size());
98 - assertEquals("XFP Missing.", alarms.iterator().next().description());
99 - verify(mockSession);
100 - }
101 -
102 - @Test
103 - public void shouldHandleException() throws UnknownHostException, IOException {
104 - expect(mockSession.getAddress()).andReturn(InetAddress.getLoopbackAddress());
105 - expect(mockSession.walkDevice(isA(NetworkDevice.class),
106 - eq(Arrays.asList(new OID[]{
107 - Bti7000SnmpAlarmProvider.CLASS_REGISTRY.getClassToOidMap().get(ActAlarmTable.class)}))))
108 - .andThrow(new IOException());
109 -
110 - replay(mockSession);
111 -
112 - assertNotNull(alarmProvider.getAlarms(mockSession, DeviceId.deviceId("snmp:1.1.1.1")));
113 -
114 - verify(mockSession);
115 - }
116 -
117 -}
1 -/*
2 - * Copyright 2015-present 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 -package org.onosproject.provider.snmp.alarm.impl;
17 -
18 -import com.btisystems.mibbler.mibs.netsnmp.interfaces.mib_2.interfaces.iftable.IIfEntry;
19 -import com.btisystems.mibbler.mibs.netsnmp.netsnmp.mib_2.interfaces.IfTable;
20 -import com.btisystems.pronx.ems.core.model.NetworkDevice;
21 -import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
22 -import java.io.IOException;
23 -import java.net.InetAddress;
24 -import java.net.UnknownHostException;
25 -import java.util.Arrays;
26 -import java.util.Collection;
27 -import org.easymock.Capture;
28 -import static org.easymock.EasyMock.capture;
29 -import static org.easymock.EasyMock.createMock;
30 -import static org.easymock.EasyMock.eq;
31 -import static org.easymock.EasyMock.expect;
32 -import static org.easymock.EasyMock.isA;
33 -import static org.easymock.EasyMock.replay;
34 -import static org.easymock.EasyMock.verify;
35 -import org.junit.Before;
36 -import org.junit.Test;
37 -import static org.junit.Assert.*;
38 -import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
39 -import org.onosproject.net.DeviceId;
40 -import org.snmp4j.smi.OID;
41 -
42 -
43 -public class NetSnmpSnmpAlarmProviderTest {
44 - private NetSnmpAlarmProvider alarmProvider;
45 - private ISnmpSession mockSession;
46 - private IfTable interfaceTable;
47 -
48 - public NetSnmpSnmpAlarmProviderTest() {
49 - }
50 -
51 - @Before
52 - public void setUp() {
53 - mockSession = createMock(ISnmpSession.class);
54 -
55 - alarmProvider = new NetSnmpAlarmProvider();
56 - }
57 -
58 - @Test
59 - public void shouldWalkDevice() throws UnknownHostException, IOException {
60 - expect(mockSession.getAddress()).andReturn(InetAddress.getLoopbackAddress());
61 - expect(mockSession.walkDevice(isA(NetworkDevice.class),
62 - eq(Arrays.asList(new OID[]{
63 - NetSnmpAlarmProvider.CLASS_REGISTRY.getClassToOidMap().get(IfTable.class)}))))
64 - .andReturn(null);
65 -
66 - replay(mockSession);
67 -
68 - assertNotNull(alarmProvider.getAlarms(mockSession, DeviceId.deviceId("snmp:1.1.1.1")));
69 -
70 - verify(mockSession);
71 - }
72 -
73 - @Test
74 - public void shouldFindAlarms() throws UnknownHostException, IOException {
75 - interfaceTable = new IfTable();
76 - interfaceTable.createEntry("1");
77 - IIfEntry entry = interfaceTable.getEntry("1");
78 - entry.setIfDescr("eth1");
79 - entry.setIfAdminStatus(1);
80 - entry.setIfOperStatus(2);
81 -
82 - Capture<NetworkDevice> networkDeviceCapture = new Capture<>();
83 -
84 - expect(mockSession.getAddress()).andReturn(InetAddress.getLoopbackAddress());
85 - expect(mockSession.walkDevice(capture(networkDeviceCapture),
86 - eq(Arrays.asList(new OID[]{
87 - NetSnmpAlarmProvider.CLASS_REGISTRY.getClassToOidMap().get(IfTable.class)}))))
88 - .andAnswer(() -> {
89 - networkDeviceCapture.getValue().getRootObject().setObject(interfaceTable);
90 - return null;
91 - });
92 -
93 - replay(mockSession);
94 -
95 - Collection<Alarm> alarms = alarmProvider.getAlarms(mockSession, DeviceId.deviceId("snmp:1.1.1.1"));
96 - assertEquals(1, alarms.size());
97 - assertEquals("Link Down.", alarms.iterator().next().description());
98 - verify(mockSession);
99 - }
100 -
101 - @Test
102 - public void shouldHandleException() throws UnknownHostException, IOException {
103 - expect(mockSession.getAddress()).andReturn(InetAddress.getLoopbackAddress());
104 - expect(mockSession.walkDevice(isA(NetworkDevice.class),
105 - eq(Arrays.asList(new OID[]{
106 - NetSnmpAlarmProvider.CLASS_REGISTRY.getClassToOidMap().get(IfTable.class)}))))
107 - .andThrow(new IOException());
108 -
109 - replay(mockSession);
110 -
111 - assertNotNull(alarmProvider.getAlarms(mockSession, DeviceId.deviceId("snmp:1.1.1.1")));
112 -
113 - verify(mockSession);
114 - }
115 -
116 -}
1 -/*
2 - * Copyright 2015-present 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 -package org.onosproject.provider.snmp.alarm.impl;
17 -
18 -import com.btisystems.pronx.ems.core.snmp.ISnmpConfiguration;
19 -import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
20 -import com.btisystems.pronx.ems.core.snmp.ISnmpSessionFactory;
21 -import java.io.IOException;
22 -import java.util.HashSet;
23 -import org.easymock.EasyMock;
24 -import static org.easymock.EasyMock.expect;
25 -import static org.easymock.EasyMock.replay;
26 -import static org.easymock.EasyMock.verify;
27 -import org.junit.Assert;
28 -import org.junit.Before;
29 -import org.junit.Test;
30 -import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEvent;
31 -import org.onosproject.net.DeviceId;
32 -
33 -public class SnmpDeviceAlarmProviderTest {
34 - private SnmpAlarmProviderService alarmProvider;
35 - private ISnmpSessionFactory mockSessionFactory;
36 - private ISnmpSession mockSession;
37 - private SnmpDeviceAlarmProvider mockProvider;
38 - private AlarmEvent alarmEvent;
39 -
40 - public SnmpDeviceAlarmProviderTest() {}
41 -
42 - @Before
43 - public void setUp() {
44 - mockSessionFactory = EasyMock.createMock(ISnmpSessionFactory.class);
45 - mockSession = EasyMock.createMock(ISnmpSession.class);
46 - mockProvider = EasyMock.createMock(SnmpDeviceAlarmProvider.class);
47 -
48 - alarmProvider = new SnmpAlarmProviderService() {
49 - @Override
50 - protected ISnmpSessionFactory getSessionFactory() {
51 - return mockSessionFactory;
52 - }
53 - };
54 -
55 - alarmProvider.addAlarmListener((AlarmEvent event) -> {
56 - alarmEvent = event;
57 - });
58 - }
59 -
60 - @Test
61 - public void shouldPopulateAlarmsForNetSnmp() throws IOException {
62 - alarmProvider.providers.put("1.2.3.4", mockProvider);
63 - expect(mockSessionFactory.createSession(EasyMock.isA(ISnmpConfiguration.class),
64 - EasyMock.eq("1.1.1.1"))).andReturn(mockSession);
65 - expect(mockSession.identifyDevice()).andReturn("1.2.3.4");
66 - expect(mockProvider.getAlarms(mockSession, DeviceId.deviceId("snmp:1.1.1.1:161")))
67 - .andReturn(new HashSet<>());
68 -
69 - mockSession.close();
70 - EasyMock.expectLastCall().once();
71 -
72 - replay(mockSessionFactory, mockSession, mockProvider);
73 -
74 - alarmProvider.triggerProbe(DeviceId.deviceId("snmp:1.1.1.1:161"));
75 -
76 - verify(mockSessionFactory, mockSession, mockProvider);
77 - Assert.assertNotNull(alarmEvent);
78 - }
79 -
80 -}