blob: 59da18cdb5c49ef35b5b4d959e5b03f71e75a380 [file] [log] [blame]
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +05301/*
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.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";
46 private final Logger log = LoggerFactory.getLogger(getClass());
47 private final ExecutorService executor;
48 private final YnhAbstractListener listener;
49 private final YangSchemaRegistry schemaRegistry;
50
51 /**
52 * Creates an instance of YANG notification manager.
53 *
54 * @param registry YANG schema registry
55 */
56 public YangNotificationManager(YangSchemaRegistry registry) {
57 listener = new YnhAbstractListener();
58 executor = Executors.newSingleThreadExecutor(
59 groupedThreads("onos/yms", "event-handler-%d", log));
60 schemaRegistry = registry;
61 }
62
63 @Override
64 public void registerAsListener(ListenerService manager) {
65 manager.addListener(listener);
66 }
67
68 @Override
69 public YangNotification getFilteredSubject(YangNotification subject,
70 YangNotification filter) {
71 return null;
72 // TODO
73 }
74
75 /**
76 * Representation of YANG notification handler's abstract listener. It
77 * listens for events from application(s).
78 */
79 private class YnhAbstractListener<E extends Event> implements
80 EventListener<E> {
81 @Override
82 public void event(Event event) {
83 executor.execute(() -> {
84 try {
85 /*
86 * Obtain YANG data tree corresponding to notification with
87 * logical root node as yangnotification, followed by
88 * module/sub-module, followed by notification.
89 */
90 YangTreeBuilder builder = new DefaultYangTreeBuilder();
91 YdtContext context = builder.getYdtForNotification(
92 event, YANG_NOTIFICATION, schemaRegistry);
93 /*
94 * Create YANG notification from obtained data tree and
95 * send it to registered protocols.
96 */
97 YangNotification notification =
98 new YangNotification(context);
99 process(new YangNotificationEvent(notification));
100 } catch (Exception e) {
101 log.warn("Failed to process {}", event, e);
102 }
103 });
104 }
105 }
106}