blob: eb0ccb6aaf840993ee94e5834f43d13d529fde08 [file] [log] [blame]
helenyrwu0407c642016-06-09 12:01:30 -07001/*
2 * Copyright 2016-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
17package org.onosproject.provider.netconf.alarm;
18
19import com.google.common.collect.Maps;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
26import org.onosproject.incubator.net.faultmanagement.alarm.AlarmProvider;
27import org.onosproject.incubator.net.faultmanagement.alarm.AlarmProviderService;
helenyrwu0407c642016-06-09 12:01:30 -070028import org.onosproject.incubator.net.faultmanagement.alarm.AlarmProviderRegistry;
helenyrwu82b50d42016-07-15 11:35:32 -070029import org.onosproject.incubator.net.faultmanagement.alarm.AlarmTranslator;
helenyrwu0407c642016-06-09 12:01:30 -070030import org.onosproject.net.DeviceId;
31import org.onosproject.net.provider.AbstractProvider;
32import org.onosproject.net.provider.ProviderId;
33import org.onosproject.netconf.NetconfController;
34import org.onosproject.netconf.NetconfDevice;
35import org.onosproject.netconf.NetconfDeviceInfo;
36import org.onosproject.netconf.NetconfDeviceListener;
37import org.onosproject.netconf.NetconfDeviceOutputEvent;
38import org.onosproject.netconf.NetconfDeviceOutputEventListener;
39import org.onosproject.netconf.NetconfSession;
40import org.onosproject.netconf.ctl.NetconfDeviceOutputEventListenerImpl;
41import org.slf4j.Logger;
42
helenyrwu45700842016-06-09 12:01:30 -070043import java.io.ByteArrayInputStream;
44import java.io.InputStream;
45import java.nio.charset.StandardCharsets;
helenyrwu0407c642016-06-09 12:01:30 -070046import java.util.Collection;
helenyrwu0407c642016-06-09 12:01:30 -070047import java.util.Map;
48
49import static org.slf4j.LoggerFactory.getLogger;
50
51/**
52 * Provider which uses an Alarm Manager to keep track of device notifications.
53 */
54@Component(immediate = true)
55public class NetconfAlarmProvider extends AbstractProvider implements AlarmProvider {
56
57 public static final String ACTIVE = "active";
58 private final Logger log = getLogger(getClass());
helenyrwu82b50d42016-07-15 11:35:32 -070059 private final AlarmTranslator translator = new NetconfAlarmTranslator();
helenyrwu0407c642016-06-09 12:01:30 -070060
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected AlarmProviderRegistry providerRegistry;
63
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected NetconfController controller;
66
helenyrwu0407c642016-06-09 12:01:30 -070067 protected AlarmProviderService providerService;
68
69 private Map<DeviceId, InternalNotificationListener> idNotificationListenerMap = Maps.newHashMap();
70
71 public NetconfAlarmProvider() {
72 super(new ProviderId("netconf", "org.onosproject.netconf"));
73 }
74
75 private NetconfDeviceListener deviceListener = new InnerDeviceListener();
76
77 @Activate
78 public void activate() {
79 providerService = providerRegistry.register(this);
80 controller.getNetconfDevices().forEach(id -> {
81 NetconfDevice device = controller.getNetconfDevice(id);
82 NetconfSession session = device.getSession();
83 InternalNotificationListener listener = new InternalNotificationListener(device.getDeviceInfo());
84 session.addDeviceOutputListener(listener);
85 idNotificationListenerMap.put(id, listener);
86 });
87 controller.addDeviceListener(deviceListener);
88 log.info("NetconfAlarmProvider Started");
89 }
90
91 @Deactivate
92 public void deactivate() {
93 providerRegistry.unregister(this);
94 idNotificationListenerMap.forEach((id, listener) -> {
95 controller.getNetconfDevice(id)
helenyrwu45700842016-06-09 12:01:30 -070096 .getSession()
97 .removeDeviceOutputListener(listener);
helenyrwu0407c642016-06-09 12:01:30 -070098 });
99 controller.removeDeviceListener(deviceListener);
100 providerService = null;
101 log.info("NetconfAlarmProvider Stopped");
102 }
103
104 @Override
105 public void triggerProbe(DeviceId deviceId) {
106 log.debug("Alarm probe triggered with " + deviceId);
107 }
108
109 private void triggerProbe(DeviceId deviceId, Collection<Alarm> alarms) {
110 providerService.updateAlarmList(deviceId, alarms);
111 triggerProbe(deviceId);
112 }
113
114 private class InternalNotificationListener extends NetconfDeviceOutputEventListenerImpl
115 implements NetconfDeviceOutputEventListener {
116
117 InternalNotificationListener(NetconfDeviceInfo deviceInfo) {
118 super(deviceInfo);
119 }
120
121 @Override
122 public void event(NetconfDeviceOutputEvent event) {
123 if (event.type() == NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION) {
124 DeviceId deviceId = event.getDeviceInfo().getDeviceId();
helenyrwu45700842016-06-09 12:01:30 -0700125 String message = event.getMessagePayload();
126 InputStream in = new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8));
127 Collection<Alarm> newAlarms = translator.translateToAlarm(deviceId, in);
128 triggerProbe(deviceId, newAlarms);
helenyrwu0407c642016-06-09 12:01:30 -0700129 }
130 }
131 }
132
133 private class InnerDeviceListener implements NetconfDeviceListener {
134
135 @Override
136 public void deviceAdded(DeviceId deviceId) {
137 NetconfDevice device = controller.getNetconfDevice(deviceId);
138 NetconfSession session = device.getSession();
139 InternalNotificationListener listener = new InternalNotificationListener(device.getDeviceInfo());
140 session.addDeviceOutputListener(listener);
141 idNotificationListenerMap.put(deviceId, listener);
142 }
143
144 @Override
145 public void deviceRemoved(DeviceId deviceId) {
146 idNotificationListenerMap.remove(deviceId);
147 }
148 }
149}