Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 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 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 18 | import org.osgi.service.component.annotations.Activate; |
| 19 | import org.osgi.service.component.annotations.Component; |
| 20 | import org.osgi.service.component.annotations.Deactivate; |
| 21 | import org.osgi.service.component.annotations.Reference; |
| 22 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.VlanId; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 27 | import org.onosproject.net.intf.Interface; |
| 28 | import org.onosproject.net.intf.InterfaceEvent; |
| 29 | import org.onosproject.net.intf.InterfaceListener; |
| 30 | import org.onosproject.net.intf.InterfaceService; |
Ray Milkey | b65d784 | 2017-08-03 16:28:24 -0700 | [diff] [blame] | 31 | import org.onosproject.net.neighbour.NeighbourMessageContext; |
| 32 | import org.onosproject.net.neighbour.NeighbourMessageHandler; |
| 33 | import org.onosproject.net.neighbour.NeighbourResolutionService; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 34 | import org.onosproject.net.ConnectPoint; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 35 | import org.onosproject.net.Host; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 36 | import org.onosproject.net.config.NetworkConfigEvent; |
| 37 | import org.onosproject.net.config.NetworkConfigListener; |
| 38 | import org.onosproject.net.config.NetworkConfigService; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 39 | import org.onosproject.net.host.HostService; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 40 | import org.onosproject.vpls.api.VplsData; |
| 41 | import org.onosproject.vpls.api.VplsStore; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 44 | import java.util.Collection; |
| 45 | import java.util.Objects; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 46 | import java.util.Set; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 47 | import java.util.stream.Collectors; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 48 | |
| 49 | import static org.slf4j.LoggerFactory.getLogger; |
| 50 | |
| 51 | /** |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 52 | * Handles neighbour messages for on behalf of the VPLS application. Handlers |
| 53 | * will be changed automatically by interface or network configuration events. |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 54 | */ |
| 55 | @Component(immediate = true) |
| 56 | public class VplsNeighbourHandler { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 57 | private static final String UNKNOWN_CONTEXT = "Unknown context type: {}"; |
| 58 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 59 | private static final String CAN_NOT_FIND_VPLS = |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 60 | "Cannot find VPLS for port {} with VLAN Id {}."; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 61 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 63 | protected CoreService coreService; |
| 64 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 66 | protected InterfaceService interfaceService; |
| 67 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 69 | protected NeighbourResolutionService neighbourService; |
| 70 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 72 | protected VplsStore vplsStore; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 73 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 75 | protected NetworkConfigService configService; |
| 76 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 77 | private VplsInterfaceListener interfaceListener = |
| 78 | new VplsInterfaceListener(); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 79 | |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 80 | protected VplsNeighbourMessageHandler neighbourHandler = |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 81 | new VplsNeighbourMessageHandler(); |
| 82 | |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 83 | protected VplsConfigListener configListener = |
| 84 | new VplsConfigListener(); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 85 | |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 86 | private final Logger log = getLogger(getClass()); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 87 | |
| 88 | private ApplicationId appId; |
| 89 | |
| 90 | |
| 91 | @Activate |
| 92 | protected void activate() { |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 93 | appId = coreService.registerApplication(VplsManager.VPLS_APP); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 94 | interfaceService.addListener(interfaceListener); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 95 | configService.addListener(configListener); |
| 96 | configNeighbourHandler(); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Deactivate |
| 100 | protected void deactivate() { |
| 101 | interfaceService.removeListener(interfaceListener); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 102 | configService.removeListener(configListener); |
| 103 | neighbourService.unregisterNeighbourHandlers(appId); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Registers neighbour handler to all available interfaces. |
| 108 | */ |
| 109 | protected void configNeighbourHandler() { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 110 | neighbourService.unregisterNeighbourHandlers(appId); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 111 | interfaceService |
| 112 | .getInterfaces() |
| 113 | .forEach(intf -> neighbourService.registerNeighbourHandler(intf, |
| 114 | neighbourHandler, |
| 115 | appId)); |
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) { |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 126 | switch (context.type()) { |
| 127 | case REQUEST: |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 128 | handleRequest(context); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 129 | break; |
| 130 | case REPLY: |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 131 | handleReply(context, hostService); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 132 | break; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 133 | default: |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 134 | log.warn(UNKNOWN_CONTEXT, context.type()); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 135 | break; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 141 | * Handles request messages. |
| 142 | * |
| 143 | * @param context the message context |
| 144 | */ |
| 145 | protected void handleRequest(NeighbourMessageContext context) { |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 146 | // Find target VPLS first, then broadcast to all interface of this VPLS |
| 147 | VplsData vplsData = findVpls(context); |
| 148 | if (vplsData != null) { |
| 149 | vplsData.interfaces().stream() |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 150 | .filter(intf -> !context.inPort().equals(intf.connectPoint())) |
| 151 | .forEach(context::forward); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 152 | } else { |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 153 | log.warn(CAN_NOT_FIND_VPLS, context.inPort(), context.vlan()); |
| 154 | context.drop(); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Handles reply messages between VLAN tagged interfaces. |
| 160 | * |
| 161 | * @param context the message context |
| 162 | * @param hostService the host service |
| 163 | */ |
| 164 | protected void handleReply(NeighbourMessageContext context, |
| 165 | HostService hostService) { |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 166 | // Find target VPLS, then reply to the host |
| 167 | VplsData vplsData = findVpls(context); |
| 168 | if (vplsData != null) { |
| 169 | MacAddress dstMac = context.dstMac(); |
| 170 | Set<Host> hosts = hostService.getHostsByMac(dstMac); |
| 171 | hosts = hosts.stream() |
| 172 | .filter(host -> vplsData.interfaces().contains(getHostInterface(host))) |
| 173 | .collect(Collectors.toSet()); |
| 174 | |
| 175 | // reply to all host in same VPLS |
| 176 | hosts.stream() |
| 177 | .map(this::getHostInterface) |
| 178 | .filter(Objects::nonNull) |
| 179 | .forEach(context::forward); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 180 | } else { |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 181 | // this might be happened when we remove an interface from VPLS |
| 182 | // just ignore this message |
| 183 | log.warn(CAN_NOT_FIND_VPLS, context.inPort(), context.vlan()); |
| 184 | context.drop(); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 189 | * Finds the VPLS with given neighbour message context. |
| 190 | * |
| 191 | * @param context the neighbour message context |
| 192 | * @return the VPLS for specific neighbour message context |
| 193 | */ |
| 194 | private VplsData findVpls(NeighbourMessageContext context) { |
| 195 | Collection<VplsData> vplses = vplsStore.getAllVpls(); |
| 196 | for (VplsData vplsData : vplses) { |
| 197 | Set<Interface> interfaces = vplsData.interfaces(); |
| 198 | ConnectPoint port = context.inPort(); |
| 199 | VlanId vlanId = context.vlan(); |
| 200 | boolean match = interfaces.stream() |
| 201 | .anyMatch(iface -> iface.connectPoint().equals(port) && |
| 202 | iface.vlan().equals(vlanId)); |
| 203 | if (match) { |
| 204 | return vplsData; |
| 205 | } |
| 206 | } |
| 207 | return null; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Finds the network interface related to the host. |
| 212 | * |
| 213 | * @param host the host |
| 214 | * @return the interface related to the host |
| 215 | */ |
| 216 | private Interface getHostInterface(Host host) { |
| 217 | Set<Interface> interfaces = interfaceService.getInterfaces(); |
| 218 | return interfaces.stream() |
| 219 | .filter(iface -> iface.connectPoint().equals(host.location()) && |
| 220 | iface.vlan().equals(host.vlan())) |
| 221 | .findFirst() |
| 222 | .orElse(null); |
| 223 | } |
| 224 | |
| 225 | /** |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 226 | * Listener for interface configuration events. |
| 227 | */ |
| 228 | private class VplsInterfaceListener implements InterfaceListener { |
| 229 | |
| 230 | @Override |
| 231 | public void event(InterfaceEvent event) { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 232 | configNeighbourHandler(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Listener for network configuration events. |
| 238 | */ |
| 239 | private class VplsConfigListener implements NetworkConfigListener { |
| 240 | |
| 241 | @Override |
| 242 | public void event(NetworkConfigEvent event) { |
| 243 | configNeighbourHandler(); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 246 | } |