blob: 131dcb8718fbcab5a31809ff8b3c1d14f45fb4f0 [file] [log] [blame]
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +05303 *
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.yms.app.ynh;
18
19import org.onosproject.event.Event;
20import org.onosproject.event.EventListener;
21import org.onosproject.event.ListenerRegistry;
22import org.onosproject.event.ListenerService;
23import org.onosproject.yms.app.ysr.YangSchemaRegistry;
24import org.onosproject.yms.app.ytb.DefaultYangTreeBuilder;
25import org.onosproject.yms.app.ytb.YangTreeBuilder;
26import org.onosproject.yms.ydt.YdtContext;
27import org.onosproject.yms.ynh.YangNotification;
28import org.onosproject.yms.ynh.YangNotificationEvent;
29import org.onosproject.yms.ynh.YangNotificationListener;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import java.util.concurrent.ExecutorService;
34import java.util.concurrent.Executors;
35
36import static org.onlab.util.Tools.groupedThreads;
37
38/**
39 * Representation of YANG notification manager.
40 */
41public class YangNotificationManager
42 extends ListenerRegistry<YangNotificationEvent, YangNotificationListener>
43 implements YangNotificationExtendedService {
44
45 private static final String YANG_NOTIFICATION = "yangnotification";
sonu guptaeff184b2016-11-24 12:43:49 +053046
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +053047 private final Logger log = LoggerFactory.getLogger(getClass());
sonu guptaeff184b2016-11-24 12:43:49 +053048
49 private ExecutorService executor;
50
51 /**
52 * YANG notification abstract listener. This listener will listens
53 * abstractly to all the notification from the application to which it
54 * has subscribed.
55 */
56 private YnhAbstractListener listener;
57
58 /**
59 * Maintains schema registry.
60 */
61 private YangSchemaRegistry schemaRegistry;
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +053062
63 /**
64 * Creates an instance of YANG notification manager.
65 *
66 * @param registry YANG schema registry
67 */
68 public YangNotificationManager(YangSchemaRegistry registry) {
69 listener = new YnhAbstractListener();
sonu guptaeff184b2016-11-24 12:43:49 +053070 executor = Executors.newSingleThreadExecutor(groupedThreads(
71 "onos/yms", "event-handler-%d", log));
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +053072 schemaRegistry = registry;
73 }
74
75 @Override
76 public void registerAsListener(ListenerService manager) {
77 manager.addListener(listener);
78 }
79
80 @Override
81 public YangNotification getFilteredSubject(YangNotification subject,
82 YangNotification filter) {
83 return null;
84 // TODO
85 }
86
87 /**
88 * Representation of YANG notification handler's abstract listener. It
89 * listens for events from application(s).
90 */
91 private class YnhAbstractListener<E extends Event> implements
92 EventListener<E> {
sonu guptaeff184b2016-11-24 12:43:49 +053093
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +053094 @Override
95 public void event(Event event) {
96 executor.execute(() -> {
97 try {
sonu guptaeff184b2016-11-24 12:43:49 +053098 log.info("Event received in ynh " + event.type());
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +053099 /*
100 * Obtain YANG data tree corresponding to notification with
101 * logical root node as yangnotification, followed by
102 * module/sub-module, followed by notification.
103 */
104 YangTreeBuilder builder = new DefaultYangTreeBuilder();
105 YdtContext context = builder.getYdtForNotification(
106 event, YANG_NOTIFICATION, schemaRegistry);
107 /*
108 * Create YANG notification from obtained data tree and
109 * send it to registered protocols.
110 */
111 YangNotification notification =
112 new YangNotification(context);
113 process(new YangNotificationEvent(notification));
114 } catch (Exception e) {
115 log.warn("Failed to process {}", event, e);
116 }
117 });
118 }
119 }
120}