blob: 9d0138b8330f9983d2fd9063f51b08dfd8ada8f8 [file] [log] [blame]
Sho SHIMIZU47e7b802015-08-18 08:54:30 -07001/*
2 * Copyright 2015 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 */
16package org.onosproject.net.newresource.impl;
17
Sho SHIMIZU26cd0ad2015-08-25 16:01:33 -070018import org.onlab.packet.MplsLabel;
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070019import org.onlab.packet.VlanId;
20import org.onlab.util.ItemNotFoundException;
21import org.onosproject.net.ConnectPoint;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070022import org.onosproject.net.Link;
23import org.onosproject.net.LinkKey;
Sho SHIMIZU26cd0ad2015-08-25 16:01:33 -070024import org.onosproject.net.behaviour.MplsQuery;
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070025import org.onosproject.net.behaviour.VlanQuery;
26import org.onosproject.net.driver.DriverHandler;
27import org.onosproject.net.driver.DriverService;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070028import org.onosproject.net.link.LinkEvent;
29import org.onosproject.net.link.LinkListener;
30import org.onosproject.net.newresource.ResourceAdminService;
31import org.onosproject.net.newresource.ResourcePath;
32
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070033import java.util.List;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070034import java.util.concurrent.ExecutorService;
Sho SHIMIZU26cd0ad2015-08-25 16:01:33 -070035import java.util.function.Predicate;
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070036import java.util.stream.Collectors;
37import java.util.stream.IntStream;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070038
39import static com.google.common.base.Preconditions.checkNotNull;
40
41/**
42 * An implementation of LinkListener registering links as resources.
43 */
44final class ResourceLinkListener implements LinkListener {
45
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070046 private static final int TOTAL_VLANS = 1024;
47 private static final List<VlanId> ENTIRE_VLAN_IDS = getEntireVlans();
48
Sho SHIMIZU26cd0ad2015-08-25 16:01:33 -070049 private static final int TOTAL_MPLS_LABELS = 1048576;
50 private static final List<MplsLabel> ENTIRE_MPLS_LABELS = getEntireMplsLabels();
51
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070052 private final ResourceAdminService adminService;
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070053 private final DriverService driverService;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070054 private final ExecutorService executor;
55
56 /**
57 * Creates an instance with the specified ResourceAdminService and ExecutorService.
58 *
59 * @param adminService instance invoked to register resources
60 * @param executor executor used for processing resource registration
61 */
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070062 ResourceLinkListener(ResourceAdminService adminService, DriverService driverService, ExecutorService executor) {
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070063 this.adminService = checkNotNull(adminService);
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070064 this.driverService = checkNotNull(driverService);
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070065 this.executor = checkNotNull(executor);
66 }
67
68 @Override
69 public void event(LinkEvent event) {
70 Link link = event.subject();
71 switch (event.type()) {
72 case LINK_ADDED:
73 registerLinkResource(link);
74 break;
Sho SHIMIZUd28842c2015-08-20 11:42:43 -070075 case LINK_REMOVED:
76 unregisterLinkResource(link);
77 break;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070078 default:
79 break;
80 }
81 }
82
83 private void registerLinkResource(Link link) {
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070084 executor.submit(() -> {
85 // register the link
86 LinkKey linkKey = LinkKey.linkKey(link);
87 adminService.registerResources(ResourcePath.ROOT, linkKey);
88
Sho SHIMIZU26cd0ad2015-08-25 16:01:33 -070089 ResourcePath linkPath = new ResourcePath(linkKey);
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070090 // register VLAN IDs against the link
Sho SHIMIZU26cd0ad2015-08-25 16:01:33 -070091 if (isEnabled(link, this::isVlanEnabled)) {
92 adminService.registerResources(linkPath, ENTIRE_VLAN_IDS);
93 }
94
95 // register MPLS labels against the link
96 if (isEnabled(link, this::isMplsEnabled)) {
97 adminService.registerResources(linkPath, ENTIRE_MPLS_LABELS);
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070098 }
99 });
Sho SHIMIZU47e7b802015-08-18 08:54:30 -0700100 }
Sho SHIMIZUd28842c2015-08-20 11:42:43 -0700101
102 private void unregisterLinkResource(Link link) {
103 LinkKey linkKey = LinkKey.linkKey(link);
104 executor.submit(() -> adminService.unregisterResources(ResourcePath.ROOT, linkKey));
105 }
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -0700106
Sho SHIMIZU26cd0ad2015-08-25 16:01:33 -0700107 private boolean isEnabled(Link link, Predicate<ConnectPoint> predicate) {
108 return predicate.test(link.src()) && predicate.test(link.dst());
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -0700109 }
110
111 private boolean isVlanEnabled(ConnectPoint cp) {
112 try {
113 DriverHandler handler = driverService.createHandler(cp.deviceId());
114 if (handler == null) {
115 return false;
116 }
117
118 VlanQuery query = handler.behaviour(VlanQuery.class);
119 return query != null && query.isEnabled(cp.port());
120 } catch (ItemNotFoundException e) {
121 return false;
122 }
123 }
124
Sho SHIMIZU26cd0ad2015-08-25 16:01:33 -0700125 private boolean isMplsEnabled(ConnectPoint cp) {
126 try {
127 DriverHandler handler = driverService.createHandler(cp.deviceId());
128 if (handler == null) {
129 return false;
130 }
131
132 MplsQuery query = handler.behaviour(MplsQuery.class);
133 return query != null && query.isEnabled(cp.port());
134 } catch (ItemNotFoundException e) {
135 return false;
136 }
137 }
138
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -0700139 private static List<VlanId> getEntireVlans() {
140 return IntStream.range(0, TOTAL_VLANS)
141 .mapToObj(x -> VlanId.vlanId((short) x))
142 .collect(Collectors.toList());
143 }
Sho SHIMIZU26cd0ad2015-08-25 16:01:33 -0700144
145 private static List<MplsLabel> getEntireMplsLabels() {
146 // potentially many objects are created
147 return IntStream.range(0, TOTAL_MPLS_LABELS)
148 .mapToObj(MplsLabel::mplsLabel)
149 .collect(Collectors.toList());
150 }
Sho SHIMIZU47e7b802015-08-18 08:54:30 -0700151}