Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.net.newresource.impl; |
| 17 | |
Sho SHIMIZU | 26cd0ad | 2015-08-25 16:01:33 -0700 | [diff] [blame] | 18 | import org.onlab.packet.MplsLabel; |
Sho SHIMIZU | 4bdd259 | 2015-08-25 15:33:32 -0700 | [diff] [blame] | 19 | import org.onlab.packet.VlanId; |
| 20 | import org.onlab.util.ItemNotFoundException; |
| 21 | import org.onosproject.net.ConnectPoint; |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 22 | import org.onosproject.net.Link; |
| 23 | import org.onosproject.net.LinkKey; |
Sho SHIMIZU | 26cd0ad | 2015-08-25 16:01:33 -0700 | [diff] [blame] | 24 | import org.onosproject.net.behaviour.MplsQuery; |
Sho SHIMIZU | 4bdd259 | 2015-08-25 15:33:32 -0700 | [diff] [blame] | 25 | import org.onosproject.net.behaviour.VlanQuery; |
| 26 | import org.onosproject.net.driver.DriverHandler; |
| 27 | import org.onosproject.net.driver.DriverService; |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 28 | import org.onosproject.net.link.LinkEvent; |
| 29 | import org.onosproject.net.link.LinkListener; |
| 30 | import org.onosproject.net.newresource.ResourceAdminService; |
| 31 | import org.onosproject.net.newresource.ResourcePath; |
| 32 | |
Sho SHIMIZU | 4bdd259 | 2015-08-25 15:33:32 -0700 | [diff] [blame] | 33 | import java.util.List; |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 34 | import java.util.concurrent.ExecutorService; |
Sho SHIMIZU | 26cd0ad | 2015-08-25 16:01:33 -0700 | [diff] [blame] | 35 | import java.util.function.Predicate; |
Sho SHIMIZU | 4bdd259 | 2015-08-25 15:33:32 -0700 | [diff] [blame] | 36 | import java.util.stream.Collectors; |
| 37 | import java.util.stream.IntStream; |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 38 | |
| 39 | import static com.google.common.base.Preconditions.checkNotNull; |
| 40 | |
| 41 | /** |
| 42 | * An implementation of LinkListener registering links as resources. |
| 43 | */ |
| 44 | final class ResourceLinkListener implements LinkListener { |
| 45 | |
Sho SHIMIZU | 4bdd259 | 2015-08-25 15:33:32 -0700 | [diff] [blame] | 46 | private static final int TOTAL_VLANS = 1024; |
| 47 | private static final List<VlanId> ENTIRE_VLAN_IDS = getEntireVlans(); |
| 48 | |
Sho SHIMIZU | 26cd0ad | 2015-08-25 16:01:33 -0700 | [diff] [blame] | 49 | private static final int TOTAL_MPLS_LABELS = 1048576; |
| 50 | private static final List<MplsLabel> ENTIRE_MPLS_LABELS = getEntireMplsLabels(); |
| 51 | |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 52 | private final ResourceAdminService adminService; |
Sho SHIMIZU | 4bdd259 | 2015-08-25 15:33:32 -0700 | [diff] [blame] | 53 | private final DriverService driverService; |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 54 | 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 Jampani | f97edc1 | 2015-08-31 14:41:01 -0700 | [diff] [blame] | 60 | * @param driverService driver service instance |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 61 | * @param executor executor used for processing resource registration |
| 62 | */ |
Sho SHIMIZU | 4bdd259 | 2015-08-25 15:33:32 -0700 | [diff] [blame] | 63 | ResourceLinkListener(ResourceAdminService adminService, DriverService driverService, ExecutorService executor) { |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 64 | this.adminService = checkNotNull(adminService); |
Sho SHIMIZU | 4bdd259 | 2015-08-25 15:33:32 -0700 | [diff] [blame] | 65 | this.driverService = checkNotNull(driverService); |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 66 | 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 SHIMIZU | d28842c | 2015-08-20 11:42:43 -0700 | [diff] [blame] | 76 | case LINK_REMOVED: |
| 77 | unregisterLinkResource(link); |
| 78 | break; |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 79 | default: |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private void registerLinkResource(Link link) { |
Sho SHIMIZU | 4bdd259 | 2015-08-25 15:33:32 -0700 | [diff] [blame] | 85 | executor.submit(() -> { |
| 86 | // register the link |
| 87 | LinkKey linkKey = LinkKey.linkKey(link); |
| 88 | adminService.registerResources(ResourcePath.ROOT, linkKey); |
| 89 | |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame^] | 90 | ResourcePath linkPath = ResourcePath.discrete(linkKey); |
Sho SHIMIZU | 4bdd259 | 2015-08-25 15:33:32 -0700 | [diff] [blame] | 91 | // register VLAN IDs against the link |
Sho SHIMIZU | 26cd0ad | 2015-08-25 16:01:33 -0700 | [diff] [blame] | 92 | 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 SHIMIZU | 4bdd259 | 2015-08-25 15:33:32 -0700 | [diff] [blame] | 99 | } |
| 100 | }); |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 101 | } |
Sho SHIMIZU | d28842c | 2015-08-20 11:42:43 -0700 | [diff] [blame] | 102 | |
| 103 | private void unregisterLinkResource(Link link) { |
| 104 | LinkKey linkKey = LinkKey.linkKey(link); |
| 105 | executor.submit(() -> adminService.unregisterResources(ResourcePath.ROOT, linkKey)); |
| 106 | } |
Sho SHIMIZU | 4bdd259 | 2015-08-25 15:33:32 -0700 | [diff] [blame] | 107 | |
Sho SHIMIZU | 26cd0ad | 2015-08-25 16:01:33 -0700 | [diff] [blame] | 108 | private boolean isEnabled(Link link, Predicate<ConnectPoint> predicate) { |
| 109 | return predicate.test(link.src()) && predicate.test(link.dst()); |
Sho SHIMIZU | 4bdd259 | 2015-08-25 15:33:32 -0700 | [diff] [blame] | 110 | } |
| 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 SHIMIZU | 26cd0ad | 2015-08-25 16:01:33 -0700 | [diff] [blame] | 126 | 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 SHIMIZU | 4bdd259 | 2015-08-25 15:33:32 -0700 | [diff] [blame] | 140 | 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 SHIMIZU | 26cd0ad | 2015-08-25 16:01:33 -0700 | [diff] [blame] | 145 | |
| 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 SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 152 | } |