blob: 117a9a0e84d254994cfdbc0f156f16df35c1c5a7 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
tom0755a362014-09-24 11:54:43 -070016package org.onlab.onos.foo;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.onlab.onos.cluster.ClusterEvent;
24import org.onlab.onos.cluster.ClusterEventListener;
25import org.onlab.onos.cluster.ClusterService;
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080026import org.onlab.onos.cluster.NodeId;
27import org.onlab.onos.mastership.MastershipEvent;
28import org.onlab.onos.mastership.MastershipListener;
29import org.onlab.onos.mastership.MastershipService;
tom0768a022014-09-24 16:16:16 -070030import org.onlab.onos.net.device.DeviceEvent;
31import org.onlab.onos.net.device.DeviceListener;
32import org.onlab.onos.net.device.DeviceService;
tom4e969042014-10-07 00:47:30 -070033import org.onlab.onos.net.intent.IntentEvent;
34import org.onlab.onos.net.intent.IntentListener;
35import org.onlab.onos.net.intent.IntentService;
tom0755a362014-09-24 11:54:43 -070036import org.slf4j.Logger;
37
38import static org.slf4j.LoggerFactory.getLogger;
39
40/**
41 * Playground app component.
42 */
43@Component(immediate = true)
44public class FooComponent {
45
46 private final Logger log = getLogger(getClass());
47
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected ClusterService clusterService;
50
tom0768a022014-09-24 16:16:16 -070051 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected DeviceService deviceService;
53
tom4e969042014-10-07 00:47:30 -070054 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected IntentService intentService;
56
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080057 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected MastershipService mastershipService;
59
tom0768a022014-09-24 16:16:16 -070060 private final ClusterEventListener clusterListener = new InnerClusterListener();
61 private final DeviceListener deviceListener = new InnerDeviceListener();
tom4e969042014-10-07 00:47:30 -070062 private final IntentListener intentListener = new InnerIntentListener();
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080063 private final MastershipListener mastershipListener = new InnerMastershipListener();
tom0755a362014-09-24 11:54:43 -070064
65 @Activate
66 public void activate() {
67 clusterService.addListener(clusterListener);
tom0768a022014-09-24 16:16:16 -070068 deviceService.addListener(deviceListener);
tom4e969042014-10-07 00:47:30 -070069 intentService.addListener(intentListener);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080070 mastershipService.addListener(mastershipListener);
tom0755a362014-09-24 11:54:43 -070071 log.info("Started");
72 }
73
74 @Deactivate
75 public void deactivate() {
76 clusterService.removeListener(clusterListener);
tom0768a022014-09-24 16:16:16 -070077 deviceService.removeListener(deviceListener);
tom4e969042014-10-07 00:47:30 -070078 intentService.removeListener(intentListener);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080079 mastershipService.removeListener(mastershipListener);
tom0755a362014-09-24 11:54:43 -070080 log.info("Stopped");
81 }
82
83 private class InnerClusterListener implements ClusterEventListener {
84 @Override
85 public void event(ClusterEvent event) {
86 log.info("WOOOOT! {}", event);
87 }
88 }
tom0768a022014-09-24 16:16:16 -070089
90 private class InnerDeviceListener implements DeviceListener {
91 @Override
92 public void event(DeviceEvent event) {
93 log.info("YEEEEHAAAAW! {}", event);
94 }
95 }
tom4e969042014-10-07 00:47:30 -070096
97 private class InnerIntentListener implements IntentListener {
98 @Override
99 public void event(IntentEvent event) {
100 String message;
101 if (event.type() == IntentEvent.Type.SUBMITTED) {
102 message = "WOW! It looks like someone has some intentions: {}";
103 } else if (event.type() == IntentEvent.Type.INSTALLED) {
104 message = "AWESOME! So far things are going great: {}";
105 } else if (event.type() == IntentEvent.Type.WITHDRAWN) {
106 message = "HMMM! Ambitions are fading apparently: {}";
107 } else {
108 message = "CRAP!!! Things are not turning out as intended: {}";
109 }
110 log.info(message, event.subject());
111 }
112 }
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800113
114 private class InnerMastershipListener implements MastershipListener {
115 @Override
116 public void event(MastershipEvent event) {
117 final NodeId myId = clusterService.getLocalNode().id();
118 if (myId.equals(event.roleInfo().master())) {
119 log.info("I have control/I wish you luck {}", event);
120 } else {
121 log.info("you have control {}", event);
122 }
123 }
124 }
tom0755a362014-09-24 11:54:43 -0700125}
126
127