blob: ec74eb62cf75be6aa5abe318ab1832a89d66dba1 [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;
helenyrwu0407c642016-06-09 12:01:30 -070029import org.onosproject.net.DeviceId;
30import org.onosproject.net.provider.AbstractProvider;
31import org.onosproject.net.provider.ProviderId;
32import org.onosproject.netconf.NetconfController;
33import org.onosproject.netconf.NetconfDevice;
34import org.onosproject.netconf.NetconfDeviceInfo;
35import org.onosproject.netconf.NetconfDeviceListener;
36import org.onosproject.netconf.NetconfDeviceOutputEvent;
37import org.onosproject.netconf.NetconfDeviceOutputEventListener;
38import org.onosproject.netconf.NetconfSession;
39import org.onosproject.netconf.ctl.NetconfDeviceOutputEventListenerImpl;
40import org.slf4j.Logger;
41
helenyrwu45700842016-06-09 12:01:30 -070042import java.io.ByteArrayInputStream;
43import java.io.InputStream;
44import java.nio.charset.StandardCharsets;
helenyrwu0407c642016-06-09 12:01:30 -070045import java.util.Collection;
helenyrwu0407c642016-06-09 12:01:30 -070046import java.util.Map;
47
48import static org.slf4j.LoggerFactory.getLogger;
49
50/**
51 * Provider which uses an Alarm Manager to keep track of device notifications.
52 */
53@Component(immediate = true)
54public class NetconfAlarmProvider extends AbstractProvider implements AlarmProvider {
55
56 public static final String ACTIVE = "active";
57 private final Logger log = getLogger(getClass());
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected AlarmProviderRegistry providerRegistry;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected NetconfController controller;
64
helenyrwu0407c642016-06-09 12:01:30 -070065 protected AlarmProviderService providerService;
66
67 private Map<DeviceId, InternalNotificationListener> idNotificationListenerMap = Maps.newHashMap();
68
69 public NetconfAlarmProvider() {
70 super(new ProviderId("netconf", "org.onosproject.netconf"));
71 }
72
73 private NetconfDeviceListener deviceListener = new InnerDeviceListener();
74
75 @Activate
76 public void activate() {
77 providerService = providerRegistry.register(this);
78 controller.getNetconfDevices().forEach(id -> {
79 NetconfDevice device = controller.getNetconfDevice(id);
80 NetconfSession session = device.getSession();
81 InternalNotificationListener listener = new InternalNotificationListener(device.getDeviceInfo());
82 session.addDeviceOutputListener(listener);
83 idNotificationListenerMap.put(id, listener);
84 });
85 controller.addDeviceListener(deviceListener);
86 log.info("NetconfAlarmProvider Started");
87 }
88
89 @Deactivate
90 public void deactivate() {
91 providerRegistry.unregister(this);
92 idNotificationListenerMap.forEach((id, listener) -> {
93 controller.getNetconfDevice(id)
helenyrwu45700842016-06-09 12:01:30 -070094 .getSession()
95 .removeDeviceOutputListener(listener);
helenyrwu0407c642016-06-09 12:01:30 -070096 });
97 controller.removeDeviceListener(deviceListener);
98 providerService = null;
99 log.info("NetconfAlarmProvider Stopped");
100 }
101
102 @Override
103 public void triggerProbe(DeviceId deviceId) {
104 log.debug("Alarm probe triggered with " + deviceId);
105 }
106
107 private void triggerProbe(DeviceId deviceId, Collection<Alarm> alarms) {
108 providerService.updateAlarmList(deviceId, alarms);
109 triggerProbe(deviceId);
110 }
111
112 private class InternalNotificationListener extends NetconfDeviceOutputEventListenerImpl
113 implements NetconfDeviceOutputEventListener {
114
115 InternalNotificationListener(NetconfDeviceInfo deviceInfo) {
116 super(deviceInfo);
117 }
118
119 @Override
120 public void event(NetconfDeviceOutputEvent event) {
121 if (event.type() == NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION) {
122 DeviceId deviceId = event.getDeviceInfo().getDeviceId();
helenyrwu45700842016-06-09 12:01:30 -0700123 NetconfAlarmTranslator translator = new NetconfAlarmTranslator();
124 String message = event.getMessagePayload();
125 InputStream in = new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8));
126 Collection<Alarm> newAlarms = translator.translateToAlarm(deviceId, in);
127 triggerProbe(deviceId, newAlarms);
helenyrwu0407c642016-06-09 12:01:30 -0700128 }
129 }
130 }
131
132 private class InnerDeviceListener implements NetconfDeviceListener {
133
134 @Override
135 public void deviceAdded(DeviceId deviceId) {
136 NetconfDevice device = controller.getNetconfDevice(deviceId);
137 NetconfSession session = device.getSession();
138 InternalNotificationListener listener = new InternalNotificationListener(device.getDeviceInfo());
139 session.addDeviceOutputListener(listener);
140 idNotificationListenerMap.put(deviceId, listener);
141 }
142
143 @Override
144 public void deviceRemoved(DeviceId deviceId) {
145 idNotificationListenerMap.remove(deviceId);
146 }
147 }
148}