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; |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.device.DeviceEvent; |
| 27 | import org.onlab.onos.net.device.DeviceListener; |
| 28 | import org.onlab.onos.net.device.DeviceService; |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.intent.IntentEvent; |
| 30 | import org.onlab.onos.net.intent.IntentListener; |
| 31 | import org.onlab.onos.net.intent.IntentService; |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | |
| 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | |
| 36 | /** |
| 37 | * Playground app component. |
| 38 | */ |
| 39 | @Component(immediate = true) |
| 40 | public class FooComponent { |
| 41 | |
| 42 | private final Logger log = getLogger(getClass()); |
| 43 | |
| 44 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 45 | protected ClusterService clusterService; |
| 46 | |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 48 | protected DeviceService deviceService; |
| 49 | |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 51 | protected IntentService intentService; |
| 52 | |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 53 | private final ClusterEventListener clusterListener = new InnerClusterListener(); |
| 54 | private final DeviceListener deviceListener = new InnerDeviceListener(); |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 55 | private final IntentListener intentListener = new InnerIntentListener(); |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 56 | |
| 57 | @Activate |
| 58 | public void activate() { |
| 59 | clusterService.addListener(clusterListener); |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 60 | deviceService.addListener(deviceListener); |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 61 | intentService.addListener(intentListener); |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 62 | log.info("Started"); |
| 63 | } |
| 64 | |
| 65 | @Deactivate |
| 66 | public void deactivate() { |
| 67 | clusterService.removeListener(clusterListener); |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 68 | deviceService.removeListener(deviceListener); |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 69 | intentService.removeListener(intentListener); |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 70 | log.info("Stopped"); |
| 71 | } |
| 72 | |
| 73 | private class InnerClusterListener implements ClusterEventListener { |
| 74 | @Override |
| 75 | public void event(ClusterEvent event) { |
| 76 | log.info("WOOOOT! {}", event); |
| 77 | } |
| 78 | } |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 79 | |
| 80 | private class InnerDeviceListener implements DeviceListener { |
| 81 | @Override |
| 82 | public void event(DeviceEvent event) { |
| 83 | log.info("YEEEEHAAAAW! {}", event); |
| 84 | } |
| 85 | } |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 86 | |
| 87 | private class InnerIntentListener implements IntentListener { |
| 88 | @Override |
| 89 | public void event(IntentEvent event) { |
| 90 | String message; |
| 91 | if (event.type() == IntentEvent.Type.SUBMITTED) { |
| 92 | message = "WOW! It looks like someone has some intentions: {}"; |
| 93 | } else if (event.type() == IntentEvent.Type.INSTALLED) { |
| 94 | message = "AWESOME! So far things are going great: {}"; |
| 95 | } else if (event.type() == IntentEvent.Type.WITHDRAWN) { |
| 96 | message = "HMMM! Ambitions are fading apparently: {}"; |
| 97 | } else { |
| 98 | message = "CRAP!!! Things are not turning out as intended: {}"; |
| 99 | } |
| 100 | log.info(message, event.subject()); |
| 101 | } |
| 102 | } |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |