Thomas Vachuska | badb93f | 2014-11-15 23:51:17 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | badb93f | 2014-11-15 23:51:17 -0800 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.optical; |
Thomas Vachuska | badb93f | 2014-11-15 23:51:17 -0800 | [diff] [blame] | 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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.Device; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.Link; |
| 27 | import org.onosproject.net.Port; |
| 28 | import org.onosproject.net.device.DeviceEvent; |
| 29 | import org.onosproject.net.device.DeviceListener; |
| 30 | import org.onosproject.net.device.DeviceService; |
| 31 | import org.onosproject.net.link.DefaultLinkDescription; |
| 32 | import org.onosproject.net.link.LinkDescription; |
| 33 | import org.onosproject.net.link.LinkEvent; |
| 34 | import org.onosproject.net.link.LinkListener; |
| 35 | import org.onosproject.net.link.LinkProvider; |
| 36 | import org.onosproject.net.link.LinkProviderRegistry; |
| 37 | import org.onosproject.net.link.LinkProviderService; |
| 38 | import org.onosproject.net.link.LinkService; |
| 39 | import org.onosproject.net.provider.AbstractProvider; |
| 40 | import org.onosproject.net.provider.ProviderId; |
Thomas Vachuska | badb93f | 2014-11-15 23:51:17 -0800 | [diff] [blame] | 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
| 43 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import static org.onosproject.net.Link.Type.OPTICAL; |
Thomas Vachuska | badb93f | 2014-11-15 23:51:17 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Ancillary provider to activate/deactivate optical links as their respective |
| 48 | * devices go online or offline. |
| 49 | */ |
| 50 | @Component(immediate = true) |
| 51 | public class OpticalLinkProvider extends AbstractProvider implements LinkProvider { |
| 52 | |
| 53 | private static final Logger log = LoggerFactory.getLogger(OpticalLinkProvider.class); |
| 54 | |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected LinkProviderRegistry registry; |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected DeviceService deviceService; |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected LinkService linkService; |
| 63 | |
| 64 | private LinkProviderService providerService; |
| 65 | private DeviceListener deviceListener = new InternalDeviceListener(); |
| 66 | private LinkListener linkListener = new InternalLinkListener(); |
| 67 | |
| 68 | public OpticalLinkProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 69 | super(new ProviderId("optical", "org.onosproject.optical")); |
Thomas Vachuska | badb93f | 2014-11-15 23:51:17 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Activate |
| 73 | protected void activate() { |
| 74 | deviceService.addListener(deviceListener); |
| 75 | linkService.addListener(linkListener); |
| 76 | providerService = registry.register(this); |
| 77 | log.info("Started"); |
| 78 | } |
| 79 | |
| 80 | @Deactivate |
| 81 | protected void deactivate() { |
| 82 | deviceService.removeListener(deviceListener); |
| 83 | linkService.removeListener(linkListener); |
| 84 | registry.unregister(this); |
| 85 | log.info("Stopped"); |
| 86 | } |
| 87 | |
| 88 | //Listens to device events and processes their links. |
| 89 | private class InternalDeviceListener implements DeviceListener { |
| 90 | @Override |
| 91 | public void event(DeviceEvent event) { |
| 92 | DeviceEvent.Type type = event.type(); |
| 93 | Device device = event.subject(); |
| 94 | if (type == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED || |
| 95 | type == DeviceEvent.Type.DEVICE_ADDED || |
| 96 | type == DeviceEvent.Type.DEVICE_UPDATED) { |
Thomas Vachuska | 9feadc2 | 2014-11-18 12:42:26 -0800 | [diff] [blame] | 97 | processDeviceLinks(device); |
| 98 | } else if (type == DeviceEvent.Type.PORT_UPDATED) { |
| 99 | processPortLinks(device, event.port()); |
Thomas Vachuska | badb93f | 2014-11-15 23:51:17 -0800 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | //Listens to link events and processes the link additions. |
| 105 | private class InternalLinkListener implements LinkListener { |
| 106 | @Override |
| 107 | public void event(LinkEvent event) { |
| 108 | if (event.type() == LinkEvent.Type.LINK_ADDED) { |
| 109 | Link link = event.subject(); |
| 110 | if (link.providerId().scheme().equals("cfg")) { |
| 111 | processLink(event.subject()); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
Thomas Vachuska | 9feadc2 | 2014-11-18 12:42:26 -0800 | [diff] [blame] | 117 | private void processDeviceLinks(Device device) { |
Thomas Vachuska | badb93f | 2014-11-15 23:51:17 -0800 | [diff] [blame] | 118 | for (Link link : linkService.getDeviceLinks(device.id())) { |
| 119 | if (link.isDurable() && link.type() == OPTICAL) { |
| 120 | processLink(link); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
Thomas Vachuska | 9feadc2 | 2014-11-18 12:42:26 -0800 | [diff] [blame] | 125 | private void processPortLinks(Device device, Port port) { |
| 126 | ConnectPoint connectPoint = new ConnectPoint(device.id(), port.number()); |
| 127 | for (Link link : linkService.getLinks(connectPoint)) { |
| 128 | if (link.isDurable() && link.type() == OPTICAL) { |
| 129 | processLink(link); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
Thomas Vachuska | badb93f | 2014-11-15 23:51:17 -0800 | [diff] [blame] | 134 | private void processLink(Link link) { |
Thomas Vachuska | 9feadc2 | 2014-11-18 12:42:26 -0800 | [diff] [blame] | 135 | DeviceId srcId = link.src().deviceId(); |
| 136 | DeviceId dstId = link.dst().deviceId(); |
| 137 | Port srcPort = deviceService.getPort(srcId, link.src().port()); |
| 138 | Port dstPort = deviceService.getPort(dstId, link.dst().port()); |
| 139 | |
Brian O'Connor | 8b6b172 | 2014-12-08 01:28:50 -0800 | [diff] [blame] | 140 | if (srcPort == null || dstPort == null) { |
| 141 | return; //FIXME remove this in favor of below TODO |
| 142 | } |
| 143 | |
Thomas Vachuska | 9feadc2 | 2014-11-18 12:42:26 -0800 | [diff] [blame] | 144 | boolean active = deviceService.isAvailable(srcId) && |
| 145 | deviceService.isAvailable(dstId) && |
Brian O'Connor | 8b6b172 | 2014-12-08 01:28:50 -0800 | [diff] [blame] | 146 | // TODO: should update be queued if src or dstPort is null? |
| 147 | //srcPort != null && dstPort != null && |
Thomas Vachuska | 9feadc2 | 2014-11-18 12:42:26 -0800 | [diff] [blame] | 148 | srcPort.isEnabled() && dstPort.isEnabled(); |
| 149 | |
Thomas Vachuska | badb93f | 2014-11-15 23:51:17 -0800 | [diff] [blame] | 150 | LinkDescription desc = new DefaultLinkDescription(link.src(), link.dst(), OPTICAL); |
| 151 | if (active) { |
| 152 | providerService.linkDetected(desc); |
| 153 | } else { |
| 154 | providerService.linkVanished(desc); |
| 155 | } |
| 156 | } |
| 157 | } |