Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 16 | package org.onlab.onos.foo; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.onlab.onos.cluster.ClusterEvent; |
| 24 | import org.onlab.onos.cluster.ClusterEventListener; |
| 25 | import org.onlab.onos.cluster.ClusterService; |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 26 | import org.onlab.onos.cluster.NodeId; |
| 27 | import org.onlab.onos.mastership.MastershipEvent; |
| 28 | import org.onlab.onos.mastership.MastershipListener; |
| 29 | import org.onlab.onos.mastership.MastershipService; |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.device.DeviceEvent; |
| 31 | import org.onlab.onos.net.device.DeviceListener; |
| 32 | import org.onlab.onos.net.device.DeviceService; |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.intent.IntentEvent; |
| 34 | import org.onlab.onos.net.intent.IntentListener; |
| 35 | import org.onlab.onos.net.intent.IntentService; |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | |
| 38 | import static org.slf4j.LoggerFactory.getLogger; |
| 39 | |
| 40 | /** |
| 41 | * Playground app component. |
| 42 | */ |
| 43 | @Component(immediate = true) |
| 44 | public class FooComponent { |
| 45 | |
| 46 | private final Logger log = getLogger(getClass()); |
| 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected ClusterService clusterService; |
| 50 | |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | protected DeviceService deviceService; |
| 53 | |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | protected IntentService intentService; |
| 56 | |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 58 | protected MastershipService mastershipService; |
| 59 | |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 60 | private final ClusterEventListener clusterListener = new InnerClusterListener(); |
| 61 | private final DeviceListener deviceListener = new InnerDeviceListener(); |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 62 | private final IntentListener intentListener = new InnerIntentListener(); |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 63 | private final MastershipListener mastershipListener = new InnerMastershipListener(); |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 64 | |
| 65 | @Activate |
| 66 | public void activate() { |
| 67 | clusterService.addListener(clusterListener); |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 68 | deviceService.addListener(deviceListener); |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 69 | intentService.addListener(intentListener); |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 70 | mastershipService.addListener(mastershipListener); |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 71 | log.info("Started"); |
| 72 | } |
| 73 | |
| 74 | @Deactivate |
| 75 | public void deactivate() { |
| 76 | clusterService.removeListener(clusterListener); |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 77 | deviceService.removeListener(deviceListener); |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 78 | intentService.removeListener(intentListener); |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 79 | mastershipService.removeListener(mastershipListener); |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 80 | 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 | } |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 89 | |
| 90 | private class InnerDeviceListener implements DeviceListener { |
| 91 | @Override |
| 92 | public void event(DeviceEvent event) { |
| 93 | log.info("YEEEEHAAAAW! {}", event); |
| 94 | } |
| 95 | } |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 96 | |
| 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 HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 113 | |
| 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 | } |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |