Thomas Vachuska | badb93f | 2014-11-15 23:51:17 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onlab.onos.optical; |
| 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.net.Device; |
| 24 | import org.onlab.onos.net.Link; |
| 25 | import org.onlab.onos.net.device.DeviceEvent; |
| 26 | import org.onlab.onos.net.device.DeviceListener; |
| 27 | import org.onlab.onos.net.device.DeviceService; |
| 28 | import org.onlab.onos.net.link.DefaultLinkDescription; |
| 29 | import org.onlab.onos.net.link.LinkDescription; |
| 30 | import org.onlab.onos.net.link.LinkEvent; |
| 31 | import org.onlab.onos.net.link.LinkListener; |
| 32 | import org.onlab.onos.net.link.LinkProvider; |
| 33 | import org.onlab.onos.net.link.LinkProviderRegistry; |
| 34 | import org.onlab.onos.net.link.LinkProviderService; |
| 35 | import org.onlab.onos.net.link.LinkService; |
| 36 | import org.onlab.onos.net.provider.AbstractProvider; |
| 37 | import org.onlab.onos.net.provider.ProviderId; |
| 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
| 40 | |
| 41 | import static org.onlab.onos.net.Link.Type.OPTICAL; |
| 42 | |
| 43 | /** |
| 44 | * Ancillary provider to activate/deactivate optical links as their respective |
| 45 | * devices go online or offline. |
| 46 | */ |
| 47 | @Component(immediate = true) |
| 48 | public class OpticalLinkProvider extends AbstractProvider implements LinkProvider { |
| 49 | |
| 50 | private static final Logger log = LoggerFactory.getLogger(OpticalLinkProvider.class); |
| 51 | |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 53 | protected LinkProviderRegistry registry; |
| 54 | |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected DeviceService deviceService; |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected LinkService linkService; |
| 60 | |
| 61 | private LinkProviderService providerService; |
| 62 | private DeviceListener deviceListener = new InternalDeviceListener(); |
| 63 | private LinkListener linkListener = new InternalLinkListener(); |
| 64 | |
| 65 | public OpticalLinkProvider() { |
| 66 | super(new ProviderId("optical", "org.onlab.onos.optical")); |
| 67 | } |
| 68 | |
| 69 | @Activate |
| 70 | protected void activate() { |
| 71 | deviceService.addListener(deviceListener); |
| 72 | linkService.addListener(linkListener); |
| 73 | providerService = registry.register(this); |
| 74 | log.info("Started"); |
| 75 | } |
| 76 | |
| 77 | @Deactivate |
| 78 | protected void deactivate() { |
| 79 | deviceService.removeListener(deviceListener); |
| 80 | linkService.removeListener(linkListener); |
| 81 | registry.unregister(this); |
| 82 | log.info("Stopped"); |
| 83 | } |
| 84 | |
| 85 | //Listens to device events and processes their links. |
| 86 | private class InternalDeviceListener implements DeviceListener { |
| 87 | @Override |
| 88 | public void event(DeviceEvent event) { |
| 89 | DeviceEvent.Type type = event.type(); |
| 90 | Device device = event.subject(); |
| 91 | if (type == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED || |
| 92 | type == DeviceEvent.Type.DEVICE_ADDED || |
| 93 | type == DeviceEvent.Type.DEVICE_UPDATED) { |
| 94 | processLinks(device); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | //Listens to link events and processes the link additions. |
| 100 | private class InternalLinkListener implements LinkListener { |
| 101 | @Override |
| 102 | public void event(LinkEvent event) { |
| 103 | if (event.type() == LinkEvent.Type.LINK_ADDED) { |
| 104 | Link link = event.subject(); |
| 105 | if (link.providerId().scheme().equals("cfg")) { |
| 106 | processLink(event.subject()); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | private void processLinks(Device device) { |
| 113 | for (Link link : linkService.getDeviceLinks(device.id())) { |
| 114 | if (link.isDurable() && link.type() == OPTICAL) { |
| 115 | processLink(link); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private void processLink(Link link) { |
| 121 | boolean active = deviceService.isAvailable(link.src().deviceId()) && |
| 122 | deviceService.isAvailable(link.dst().deviceId()); |
| 123 | LinkDescription desc = new DefaultLinkDescription(link.src(), link.dst(), OPTICAL); |
| 124 | if (active) { |
| 125 | providerService.linkDetected(desc); |
| 126 | } else { |
| 127 | providerService.linkVanished(desc); |
| 128 | } |
| 129 | } |
| 130 | } |