Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 19 | package org.onlab.onos.foo; |
| 20 | |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.onlab.onos.cluster.ClusterEvent; |
| 27 | import org.onlab.onos.cluster.ClusterEventListener; |
| 28 | import org.onlab.onos.cluster.ClusterService; |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.device.DeviceEvent; |
| 30 | import org.onlab.onos.net.device.DeviceListener; |
| 31 | import org.onlab.onos.net.device.DeviceService; |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.intent.IntentEvent; |
| 33 | import org.onlab.onos.net.intent.IntentListener; |
| 34 | import org.onlab.onos.net.intent.IntentService; |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * Playground app component. |
| 41 | */ |
| 42 | @Component(immediate = true) |
| 43 | public class FooComponent { |
| 44 | |
| 45 | private final Logger log = getLogger(getClass()); |
| 46 | |
| 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 48 | protected ClusterService clusterService; |
| 49 | |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 51 | protected DeviceService deviceService; |
| 52 | |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 54 | protected IntentService intentService; |
| 55 | |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 56 | private final ClusterEventListener clusterListener = new InnerClusterListener(); |
| 57 | private final DeviceListener deviceListener = new InnerDeviceListener(); |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 58 | private final IntentListener intentListener = new InnerIntentListener(); |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 59 | |
| 60 | @Activate |
| 61 | public void activate() { |
| 62 | clusterService.addListener(clusterListener); |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 63 | deviceService.addListener(deviceListener); |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 64 | intentService.addListener(intentListener); |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 65 | log.info("Started"); |
| 66 | } |
| 67 | |
| 68 | @Deactivate |
| 69 | public void deactivate() { |
| 70 | clusterService.removeListener(clusterListener); |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 71 | deviceService.removeListener(deviceListener); |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 72 | intentService.removeListener(intentListener); |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 73 | log.info("Stopped"); |
| 74 | } |
| 75 | |
| 76 | private class InnerClusterListener implements ClusterEventListener { |
| 77 | @Override |
| 78 | public void event(ClusterEvent event) { |
| 79 | log.info("WOOOOT! {}", event); |
| 80 | } |
| 81 | } |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 82 | |
| 83 | private class InnerDeviceListener implements DeviceListener { |
| 84 | @Override |
| 85 | public void event(DeviceEvent event) { |
| 86 | log.info("YEEEEHAAAAW! {}", event); |
| 87 | } |
| 88 | } |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 89 | |
| 90 | private class InnerIntentListener implements IntentListener { |
| 91 | @Override |
| 92 | public void event(IntentEvent event) { |
| 93 | String message; |
| 94 | if (event.type() == IntentEvent.Type.SUBMITTED) { |
| 95 | message = "WOW! It looks like someone has some intentions: {}"; |
| 96 | } else if (event.type() == IntentEvent.Type.INSTALLED) { |
| 97 | message = "AWESOME! So far things are going great: {}"; |
| 98 | } else if (event.type() == IntentEvent.Type.WITHDRAWN) { |
| 99 | message = "HMMM! Ambitions are fading apparently: {}"; |
| 100 | } else { |
| 101 | message = "CRAP!!! Things are not turning out as intended: {}"; |
| 102 | } |
| 103 | log.info(message, event.subject()); |
| 104 | } |
| 105 | } |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |