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