Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | */ |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 16 | package org.onosproject.vpls; |
| 17 | |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 18 | import com.google.common.collect.SetMultimap; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.incubator.net.intf.Interface; |
| 27 | import org.onosproject.incubator.net.intf.InterfaceEvent; |
| 28 | import org.onosproject.incubator.net.intf.InterfaceListener; |
| 29 | import org.onosproject.incubator.net.intf.InterfaceService; |
| 30 | import org.onosproject.incubator.net.neighbour.NeighbourMessageContext; |
| 31 | import org.onosproject.incubator.net.neighbour.NeighbourMessageHandler; |
| 32 | import org.onosproject.incubator.net.neighbour.NeighbourResolutionService; |
| 33 | import org.onosproject.net.Host; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 34 | import org.onosproject.net.config.NetworkConfigEvent; |
| 35 | import org.onosproject.net.config.NetworkConfigListener; |
| 36 | import org.onosproject.net.config.NetworkConfigService; |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 37 | import org.onosproject.net.device.DeviceService; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 38 | import org.onosproject.net.host.HostService; |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 39 | import org.onosproject.vpls.config.VplsConfigService; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 42 | import java.util.Set; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 43 | |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
| 45 | |
| 46 | /** |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 47 | * Handles neighbour messages for on behalf of the VPLS application. Handlers |
| 48 | * will be changed automatically by interface or network configuration events. |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 49 | */ |
| 50 | @Component(immediate = true) |
| 51 | public class VplsNeighbourHandler { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 52 | private static final String UNKNOWN_CONTEXT = "Unknown context type: {}"; |
| 53 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 54 | private static final String CAN_NOT_FIND_VPLS = |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 55 | "Cannot find VPLS for port {} with VLAN Id {}."; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 58 | protected CoreService coreService; |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 61 | protected DeviceService deviceService; |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 64 | protected InterfaceService interfaceService; |
| 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected NeighbourResolutionService neighbourService; |
| 68 | |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 70 | protected VplsConfigService vplsConfigService; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected NetworkConfigService configService; |
| 74 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 75 | private VplsInterfaceListener interfaceListener = |
| 76 | new VplsInterfaceListener(); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 77 | |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 78 | protected VplsNeighbourMessageHandler neighbourHandler = |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 79 | new VplsNeighbourMessageHandler(); |
| 80 | |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 81 | protected VplsConfigListener configListener = |
| 82 | new VplsConfigListener(); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 83 | |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 84 | private final Logger log = getLogger(getClass()); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 85 | |
| 86 | private ApplicationId appId; |
| 87 | |
| 88 | |
| 89 | @Activate |
| 90 | protected void activate() { |
| 91 | appId = coreService.registerApplication(Vpls.VPLS_APP); |
| 92 | interfaceService.addListener(interfaceListener); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 93 | configService.addListener(configListener); |
| 94 | configNeighbourHandler(); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Deactivate |
| 98 | protected void deactivate() { |
| 99 | interfaceService.removeListener(interfaceListener); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 100 | configService.removeListener(configListener); |
| 101 | neighbourService.unregisterNeighbourHandlers(appId); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 104 | private void configNeighbourHandler() { |
| 105 | neighbourService.unregisterNeighbourHandlers(appId); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 106 | Set<Interface> interfaces = vplsConfigService.allIfaces(); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 107 | |
| 108 | interfaceService.getInterfaces() |
| 109 | .stream() |
| 110 | .filter(interfaces::contains) |
| 111 | .forEach(intf -> { |
| 112 | neighbourService.registerNeighbourHandler(intf, |
| 113 | neighbourHandler, |
| 114 | appId); |
| 115 | }); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Handler for neighbour messages. |
| 120 | */ |
| 121 | private class VplsNeighbourMessageHandler implements NeighbourMessageHandler { |
| 122 | |
| 123 | @Override |
| 124 | public void handleMessage(NeighbourMessageContext context, |
| 125 | HostService hostService) { |
| 126 | |
| 127 | switch (context.type()) { |
| 128 | case REQUEST: |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 129 | handleRequest(context); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 130 | break; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 131 | |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 132 | case REPLY: |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 133 | handleReply(context, hostService); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 134 | break; |
| 135 | |
| 136 | default: |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 137 | log.warn(UNKNOWN_CONTEXT, context.type()); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 138 | break; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 144 | * Handles request messages. |
| 145 | * |
| 146 | * @param context the message context |
| 147 | */ |
| 148 | protected void handleRequest(NeighbourMessageContext context) { |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 149 | SetMultimap<String, Interface> interfaces = |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 150 | vplsConfigService.ifacesByVplsName(context.vlan(), |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 151 | context.inPort()); |
| 152 | if (interfaces != null) { |
| 153 | interfaces.values().stream() |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 154 | .filter(intf -> !context.inPort().equals(intf.connectPoint())) |
| 155 | .forEach(context::forward); |
| 156 | |
| 157 | } else { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 158 | log.debug(CAN_NOT_FIND_VPLS, context.inPort(), context.vlan()); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Handles reply messages between VLAN tagged interfaces. |
| 164 | * |
| 165 | * @param context the message context |
| 166 | * @param hostService the host service |
| 167 | */ |
| 168 | protected void handleReply(NeighbourMessageContext context, |
| 169 | HostService hostService) { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 170 | Set<Host> hosts = hostService.getHostsByMac(context.dstMac()); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 171 | SetMultimap<String, Interface> interfaces = |
| 172 | vplsConfigService.ifacesByVplsName(context.vlan(), |
| 173 | context.inPort()); |
| 174 | if (interfaces != null) { |
| 175 | hosts.forEach(host -> interfaces.values().stream() |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 176 | .filter(intf -> intf.connectPoint().equals(host.location())) |
| 177 | .filter(intf -> intf.vlan().equals(host.vlan())) |
| 178 | .forEach(context::forward)); |
| 179 | } else { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 180 | log.debug(CAN_NOT_FIND_VPLS, context.inPort(), context.vlan()); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 185 | * Listener for interface configuration events. |
| 186 | */ |
| 187 | private class VplsInterfaceListener implements InterfaceListener { |
| 188 | |
| 189 | @Override |
| 190 | public void event(InterfaceEvent event) { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 191 | configNeighbourHandler(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Listener for network configuration events. |
| 197 | */ |
| 198 | private class VplsConfigListener implements NetworkConfigListener { |
| 199 | |
| 200 | @Override |
| 201 | public void event(NetworkConfigEvent event) { |
| 202 | configNeighbourHandler(); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 205 | } |