blob: 68fd66120416d5fe5ca1f42d40a0a839d3480b47 [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
Madan Jampanif97edc12015-08-31 14:41:01 -070060 * @param driverService driver service instance
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070061 * @param executor executor used for processing resource registration
62 */
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070063 ResourceLinkListener(ResourceAdminService adminService, DriverService driverService, ExecutorService executor) {
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070064 this.adminService = checkNotNull(adminService);
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070065 this.driverService = checkNotNull(driverService);
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070066 this.executor = checkNotNull(executor);
67 }
68
69 @Override
70 public void event(LinkEvent event) {
71 Link link = event.subject();
72 switch (event.type()) {
73 case LINK_ADDED:
74 registerLinkResource(link);
75 break;
Sho SHIMIZUd28842c2015-08-20 11:42:43 -070076 case LINK_REMOVED:
77 unregisterLinkResource(link);
78 break;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070079 default:
80 break;
81 }
82 }
83
84 private void registerLinkResource(Link link) {
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070085 executor.submit(() -> {
86 // register the link
87 LinkKey linkKey = LinkKey.linkKey(link);
88 adminService.registerResources(ResourcePath.ROOT, linkKey);
89
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080090 ResourcePath linkPath = ResourcePath.discrete(linkKey);
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070091 // register VLAN IDs against the link
Sho SHIMIZU26cd0ad2015-08-25 16:01:33 -070092 if (isEnabled(link, this::isVlanEnabled)) {
93 adminService.registerResources(linkPath, ENTIRE_VLAN_IDS);
94 }
95
96 // register MPLS labels against the link
97 if (isEnabled(link, this::isMplsEnabled)) {
98 adminService.registerResources(linkPath, ENTIRE_MPLS_LABELS);
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070099 }
100 });
Sho SHIMIZU47e7b802015-08-18 08:54:30 -0700101 }
Sho SHIMIZUd28842c2015-08-20 11:42:43 -0700102
103 private void unregisterLinkResource(Link link) {
104 LinkKey linkKey = LinkKey.linkKey(link);
105 executor.submit(() -> adminService.unregisterResources(ResourcePath.ROOT, linkKey));
106 }
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -0700107
Sho SHIMIZU26cd0ad2015-08-25 16:01:33 -0700108 private boolean isEnabled(Link link, Predicate<ConnectPoint> predicate) {
109 return predicate.test(link.src()) && predicate.test(link.dst());
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -0700110 }
111
112 private boolean isVlanEnabled(ConnectPoint cp) {
113 try {
114 DriverHandler handler = driverService.createHandler(cp.deviceId());
115 if (handler == null) {
116 return false;
117 }
118
119 VlanQuery query = handler.behaviour(VlanQuery.class);
120 return query != null && query.isEnabled(cp.port());
121 } catch (ItemNotFoundException e) {
122 return false;
123 }
124 }
125
Sho SHIMIZU26cd0ad2015-08-25 16:01:33 -0700126 private boolean isMplsEnabled(ConnectPoint cp) {
127 try {
128 DriverHandler handler = driverService.createHandler(cp.deviceId());
129 if (handler == null) {
130 return false;
131 }
132
133 MplsQuery query = handler.behaviour(MplsQuery.class);
134 return query != null && query.isEnabled(cp.port());
135 } catch (ItemNotFoundException e) {
136 return false;
137 }
138 }
139
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -0700140 private static List<VlanId> getEntireVlans() {
141 return IntStream.range(0, TOTAL_VLANS)
142 .mapToObj(x -> VlanId.vlanId((short) x))
143 .collect(Collectors.toList());
144 }
Sho SHIMIZU26cd0ad2015-08-25 16:01:33 -0700145
146 private static List<MplsLabel> getEntireMplsLabels() {
147 // potentially many objects are created
148 return IntStream.range(0, TOTAL_MPLS_LABELS)
149 .mapToObj(MplsLabel::mplsLabel)
150 .collect(Collectors.toList());
151 }
Sho SHIMIZU47e7b802015-08-18 08:54:30 -0700152}