blob: c042abb82e36b793a440746f30bdbb26c1ea3220 [file] [log] [blame]
Thomas Vachuskabadb93f2014-11-15 23:51:17 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuskabadb93f2014-11-15 23:51:17 -08003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.optical;
Thomas Vachuskabadb93f2014-11-15 23:51:17 -080017
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.Device;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.Link;
27import org.onosproject.net.Port;
28import org.onosproject.net.device.DeviceEvent;
29import org.onosproject.net.device.DeviceListener;
30import org.onosproject.net.device.DeviceService;
31import org.onosproject.net.link.DefaultLinkDescription;
32import org.onosproject.net.link.LinkDescription;
33import org.onosproject.net.link.LinkEvent;
34import org.onosproject.net.link.LinkListener;
35import org.onosproject.net.link.LinkProvider;
36import org.onosproject.net.link.LinkProviderRegistry;
37import org.onosproject.net.link.LinkProviderService;
38import org.onosproject.net.link.LinkService;
39import org.onosproject.net.provider.AbstractProvider;
40import org.onosproject.net.provider.ProviderId;
Thomas Vachuskabadb93f2014-11-15 23:51:17 -080041import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
43
Brian O'Connorabafb502014-12-02 22:26:20 -080044import static org.onosproject.net.Link.Type.OPTICAL;
Thomas Vachuskabadb93f2014-11-15 23:51:17 -080045
46/**
47 * Ancillary provider to activate/deactivate optical links as their respective
48 * devices go online or offline.
Naoki Shiota7964fce2016-05-23 11:24:26 -070049 *
50 * @deprecated in Goldeneye (1.6.0)
Thomas Vachuskabadb93f2014-11-15 23:51:17 -080051 */
Naoki Shiota7964fce2016-05-23 11:24:26 -070052@Deprecated
Thomas Vachuskabadb93f2014-11-15 23:51:17 -080053@Component(immediate = true)
54public 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'Connorabafb502014-12-02 22:26:20 -080072 super(new ProviderId("optical", "org.onosproject.optical"));
Thomas Vachuskabadb93f2014-11-15 23:51:17 -080073 }
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 Vachuska9feadc22014-11-18 12:42:26 -0800100 processDeviceLinks(device);
101 } else if (type == DeviceEvent.Type.PORT_UPDATED) {
102 processPortLinks(device, event.port());
Thomas Vachuskabadb93f2014-11-15 23:51:17 -0800103 }
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();
Jon Halla3fcf672017-03-28 16:53:22 -0700113 if ("cfg".equals(link.providerId().scheme())) {
Thomas Vachuskabadb93f2014-11-15 23:51:17 -0800114 processLink(event.subject());
115 }
116 }
117 }
118 }
119
Thomas Vachuska9feadc22014-11-18 12:42:26 -0800120 private void processDeviceLinks(Device device) {
Thomas Vachuskabadb93f2014-11-15 23:51:17 -0800121 for (Link link : linkService.getDeviceLinks(device.id())) {
Ray Milkey8521f812017-05-24 09:49:26 -0700122 if (link.isExpected() && link.type() == OPTICAL) {
Thomas Vachuskabadb93f2014-11-15 23:51:17 -0800123 processLink(link);
124 }
125 }
126 }
127
Thomas Vachuska9feadc22014-11-18 12:42:26 -0800128 private void processPortLinks(Device device, Port port) {
129 ConnectPoint connectPoint = new ConnectPoint(device.id(), port.number());
130 for (Link link : linkService.getLinks(connectPoint)) {
Ray Milkey8521f812017-05-24 09:49:26 -0700131 if (link.isExpected() && link.type() == OPTICAL) {
Thomas Vachuska9feadc22014-11-18 12:42:26 -0800132 processLink(link);
133 }
134 }
135 }
136
Thomas Vachuskabadb93f2014-11-15 23:51:17 -0800137 private void processLink(Link link) {
Thomas Vachuska9feadc22014-11-18 12:42:26 -0800138 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'Connor8b6b1722014-12-08 01:28:50 -0800143 if (srcPort == null || dstPort == null) {
144 return; //FIXME remove this in favor of below TODO
145 }
146
Thomas Vachuska9feadc22014-11-18 12:42:26 -0800147 boolean active = deviceService.isAvailable(srcId) &&
148 deviceService.isAvailable(dstId) &&
Brian O'Connor8b6b1722014-12-08 01:28:50 -0800149 // TODO: should update be queued if src or dstPort is null?
150 //srcPort != null && dstPort != null &&
Thomas Vachuska9feadc22014-11-18 12:42:26 -0800151 srcPort.isEnabled() && dstPort.isEnabled();
152
Thomas Vachuskabadb93f2014-11-15 23:51:17 -0800153 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}