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