Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 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"; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 46 | |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 47 | private final Logger log = LoggerFactory.getLogger(getClass()); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 48 | |
| 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 Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 62 | |
| 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 70 | executor = Executors.newSingleThreadExecutor(groupedThreads( |
| 71 | "onos/yms", "event-handler-%d", log)); |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 72 | 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 93 | |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 94 | @Override |
| 95 | public void event(Event event) { |
| 96 | executor.execute(() -> { |
| 97 | try { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 98 | log.info("Event received in ynh " + event.type()); |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 99 | /* |
| 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 | } |