Initial opendaylight infrastructure commit!!
[controller.git] / opendaylight / sal / yang-prototype / sal / sal-core-demo / src / main / java / org / opendaylight / controller / sal / demo / DemoConsumerImpl.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.sal.demo;\r
9 \r
10 import java.util.Collection;\r
11 import java.util.HashSet;\r
12 import java.util.Set;\r
13 \r
14 import org.opendaylight.controller.sal.core.api.Consumer;\r
15 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession;\r
16 import org.opendaylight.controller.sal.core.api.notify.NotificationListener;\r
17 import org.opendaylight.controller.sal.core.api.notify.NotificationService;\r
18 import org.opendaylight.controller.yang.common.QName;\r
19 import org.opendaylight.controller.yang.data.api.CompositeNode;\r
20 import org.slf4j.Logger;\r
21 import org.slf4j.LoggerFactory;\r
22 \r
23 \r
24 public class DemoConsumerImpl implements Consumer {\r
25 \r
26     private ConsumerSession session;\r
27     private NotificationService notificationService;\r
28     private final String name;\r
29     private static Logger log = LoggerFactory.getLogger("AlertLogger");\r
30 \r
31     private boolean changeAware;\r
32 \r
33     public DemoConsumerImpl(String name) {\r
34         this.name = name;\r
35     }\r
36 \r
37     private NotificationListener alertLogger = new NotificationListener() {\r
38 \r
39         @Override\r
40         public void onNotification(CompositeNode notification) {\r
41             System.out.println(name\r
42                     + ": Received alert: "\r
43                     + notification.getFirstSimpleByName(\r
44                             DemoUtils.contentNodeName).getValue());\r
45             log.info("AlertLogger: Received notification: " + notification);\r
46         }\r
47 \r
48         @Override\r
49         public Set<QName> getSupportedNotifications() {\r
50             Set<QName> supported = new HashSet<QName>();\r
51             supported.add(DemoUtils.alertNotification);\r
52             return supported;\r
53         }\r
54     };\r
55 \r
56     private NotificationListener changeLogger = new NotificationListener() {\r
57 \r
58         @Override\r
59         public void onNotification(CompositeNode notification) {\r
60             System.out.println(name\r
61                     + ": Received change: "\r
62                     + notification.getFirstSimpleByName(\r
63                             DemoUtils.contentNodeName).getValue());\r
64             log.info("ChangeLogger: Received notification: " + notification);\r
65         }\r
66 \r
67         @Override\r
68         public Set<QName> getSupportedNotifications() {\r
69             Set<QName> supported = new HashSet<QName>();\r
70             supported.add(DemoUtils.alertNotification);\r
71             return supported;\r
72         }\r
73     };\r
74 \r
75     @Override\r
76     public void onSessionInitiated(ConsumerSession session) {\r
77         this.session = session;\r
78         this.notificationService = session\r
79                 .getService(NotificationService.class);\r
80         notificationService.addNotificationListener(\r
81                 DemoUtils.alertNotification, alertLogger);\r
82         if (isChangeAware()) {\r
83             notificationService.addNotificationListener(\r
84                     DemoUtils.changeNotification, changeLogger);\r
85         }\r
86     }\r
87 \r
88     @Override\r
89     public Collection<ConsumerFunctionality> getConsumerFunctionality() {\r
90         Set<ConsumerFunctionality> func = new HashSet<ConsumerFunctionality>();\r
91         func.add(alertLogger);\r
92         return func;\r
93     }\r
94 \r
95     public void closeSession() {\r
96         session.close();\r
97     }\r
98 \r
99     public boolean isChangeAware() {\r
100         return changeAware;\r
101     }\r
102 \r
103     public void setChangeAware(boolean changeAware) {\r
104         this.changeAware = changeAware;\r
105     }\r
106 \r
107 }\r