Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yms.app.ynh; |
| 18 | |
| 19 | import org.onosproject.event.Event; |
| 20 | import org.onosproject.event.EventListener; |
| 21 | import org.onosproject.event.ListenerRegistry; |
| 22 | import org.onosproject.event.ListenerService; |
| 23 | import org.onosproject.yms.app.ysr.YangSchemaRegistry; |
| 24 | import org.onosproject.yms.app.ytb.DefaultYangTreeBuilder; |
| 25 | import org.onosproject.yms.app.ytb.YangTreeBuilder; |
| 26 | import org.onosproject.yms.ydt.YdtContext; |
| 27 | import org.onosproject.yms.ynh.YangNotification; |
| 28 | import org.onosproject.yms.ynh.YangNotificationEvent; |
| 29 | import org.onosproject.yms.ynh.YangNotificationListener; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import java.util.concurrent.ExecutorService; |
| 34 | import java.util.concurrent.Executors; |
| 35 | |
| 36 | import static org.onlab.util.Tools.groupedThreads; |
| 37 | |
| 38 | /** |
| 39 | * Representation of YANG notification manager. |
| 40 | */ |
| 41 | public 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 | } |