blob: c87ca29143861d04a6807e03c1e31475a2bc45a7 [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 SHIMIZU4bdd2592015-08-25 15:33:32 -070018import org.onlab.packet.VlanId;
19import org.onlab.util.ItemNotFoundException;
20import org.onosproject.net.ConnectPoint;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070021import org.onosproject.net.Link;
22import org.onosproject.net.LinkKey;
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070023import org.onosproject.net.behaviour.VlanQuery;
24import org.onosproject.net.driver.DriverHandler;
25import org.onosproject.net.driver.DriverService;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070026import org.onosproject.net.link.LinkEvent;
27import org.onosproject.net.link.LinkListener;
28import org.onosproject.net.newresource.ResourceAdminService;
29import org.onosproject.net.newresource.ResourcePath;
30
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070031import java.util.List;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070032import java.util.concurrent.ExecutorService;
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070033import java.util.stream.Collectors;
34import java.util.stream.IntStream;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070035
36import static com.google.common.base.Preconditions.checkNotNull;
37
38/**
39 * An implementation of LinkListener registering links as resources.
40 */
41final class ResourceLinkListener implements LinkListener {
42
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070043 private static final int TOTAL_VLANS = 1024;
44 private static final List<VlanId> ENTIRE_VLAN_IDS = getEntireVlans();
45
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070046 private final ResourceAdminService adminService;
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070047 private final DriverService driverService;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070048 private final ExecutorService executor;
49
50 /**
51 * Creates an instance with the specified ResourceAdminService and ExecutorService.
52 *
53 * @param adminService instance invoked to register resources
54 * @param executor executor used for processing resource registration
55 */
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070056 ResourceLinkListener(ResourceAdminService adminService, DriverService driverService, ExecutorService executor) {
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070057 this.adminService = checkNotNull(adminService);
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070058 this.driverService = checkNotNull(driverService);
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070059 this.executor = checkNotNull(executor);
60 }
61
62 @Override
63 public void event(LinkEvent event) {
64 Link link = event.subject();
65 switch (event.type()) {
66 case LINK_ADDED:
67 registerLinkResource(link);
68 break;
Sho SHIMIZUd28842c2015-08-20 11:42:43 -070069 case LINK_REMOVED:
70 unregisterLinkResource(link);
71 break;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070072 default:
73 break;
74 }
75 }
76
77 private void registerLinkResource(Link link) {
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070078 executor.submit(() -> {
79 // register the link
80 LinkKey linkKey = LinkKey.linkKey(link);
81 adminService.registerResources(ResourcePath.ROOT, linkKey);
82
83 // register VLAN IDs against the link
84 if (isVlanEnabled(link)) {
85 adminService.registerResources(new ResourcePath(linkKey), ENTIRE_VLAN_IDS);
86 }
87 });
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070088 }
Sho SHIMIZUd28842c2015-08-20 11:42:43 -070089
90 private void unregisterLinkResource(Link link) {
91 LinkKey linkKey = LinkKey.linkKey(link);
92 executor.submit(() -> adminService.unregisterResources(ResourcePath.ROOT, linkKey));
93 }
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070094
95 private boolean isVlanEnabled(Link link) {
96 ConnectPoint src = link.src();
97 ConnectPoint dst = link.dst();
98
99 return isVlanEnabled(src) && isVlanEnabled(dst);
100 }
101
102 private boolean isVlanEnabled(ConnectPoint cp) {
103 try {
104 DriverHandler handler = driverService.createHandler(cp.deviceId());
105 if (handler == null) {
106 return false;
107 }
108
109 VlanQuery query = handler.behaviour(VlanQuery.class);
110 return query != null && query.isEnabled(cp.port());
111 } catch (ItemNotFoundException e) {
112 return false;
113 }
114 }
115
116 private static List<VlanId> getEntireVlans() {
117 return IntStream.range(0, TOTAL_VLANS)
118 .mapToObj(x -> VlanId.vlanId((short) x))
119 .collect(Collectors.toList());
120 }
Sho SHIMIZU47e7b802015-08-18 08:54:30 -0700121}