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; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 38 | import org.onosproject.net.host.HostService; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame^] | 39 | import org.onosproject.vpls.config.VplsConfigurationService; |
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.Collection; |
| 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 | /** |
| 48 | * Handles neighbour messages for VPLS use case. |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame^] | 49 | * Handlers 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 | |
| 55 | private static final String CAN_NOT_FIND_NETWORK = |
| 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) |
| 62 | protected InterfaceService interfaceService; |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected NeighbourResolutionService neighbourService; |
| 66 | |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame^] | 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected VplsConfigurationService vplsConfigService; |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected NetworkConfigService configService; |
| 72 | |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 73 | private VplsInterfaceListener interfaceListener |
| 74 | = new VplsInterfaceListener(); |
| 75 | |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame^] | 76 | protected VplsNeighbourMessageHandler neighbourHandler = |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 77 | new VplsNeighbourMessageHandler(); |
| 78 | |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame^] | 79 | protected VplsConfigListener configListener = |
| 80 | new VplsConfigListener(); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 81 | |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame^] | 82 | private final Logger log = getLogger(getClass()); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 83 | |
| 84 | private ApplicationId appId; |
| 85 | |
| 86 | |
| 87 | @Activate |
| 88 | protected void activate() { |
| 89 | appId = coreService.registerApplication(Vpls.VPLS_APP); |
| 90 | interfaceService.addListener(interfaceListener); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame^] | 91 | configService.addListener(configListener); |
| 92 | configNeighbourHandler(); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Deactivate |
| 96 | protected void deactivate() { |
| 97 | interfaceService.removeListener(interfaceListener); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame^] | 98 | configService.removeListener(configListener); |
| 99 | neighbourService.unregisterNeighbourHandlers(appId); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame^] | 102 | private void configNeighbourHandler() { |
| 103 | neighbourService.unregisterNeighbourHandlers(appId); |
| 104 | Set<Interface> interfaces = vplsConfigService.getAllInterfaces(); |
| 105 | |
| 106 | interfaceService.getInterfaces() |
| 107 | .stream() |
| 108 | .filter(interfaces::contains) |
| 109 | .forEach(intf -> { |
| 110 | neighbourService.registerNeighbourHandler(intf, |
| 111 | neighbourHandler, |
| 112 | appId); |
| 113 | }); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Handler for neighbour messages. |
| 118 | */ |
| 119 | private class VplsNeighbourMessageHandler implements NeighbourMessageHandler { |
| 120 | |
| 121 | @Override |
| 122 | public void handleMessage(NeighbourMessageContext context, |
| 123 | HostService hostService) { |
| 124 | |
| 125 | switch (context.type()) { |
| 126 | case REQUEST: |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame^] | 127 | handleRequest(context); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 128 | break; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame^] | 129 | |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 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; |
| 133 | |
| 134 | default: |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame^] | 135 | log.warn(UNKNOWN_CONTEXT, context.type()); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 136 | break; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame^] | 142 | * Handles request messages. |
| 143 | * |
| 144 | * @param context the message context |
| 145 | */ |
| 146 | protected void handleRequest(NeighbourMessageContext context) { |
| 147 | |
| 148 | SetMultimap<String, Interface> vplsNetwork = |
| 149 | vplsConfigService.getVplsNetwork(context.vlan(), context.inPort()); |
| 150 | |
| 151 | if (vplsNetwork != null) { |
| 152 | Collection<Interface> vplsInterfaces = vplsNetwork.values(); |
| 153 | vplsInterfaces.stream() |
| 154 | .filter(intf -> !context.inPort().equals(intf.connectPoint())) |
| 155 | .forEach(context::forward); |
| 156 | |
| 157 | } else { |
| 158 | log.debug(CAN_NOT_FIND_NETWORK, context.inPort(), context.vlan()); |
| 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) { |
| 170 | |
| 171 | SetMultimap<String, Interface> vplsNetwork = |
| 172 | vplsConfigService.getVplsNetwork(context.vlan(), context.inPort()); |
| 173 | |
| 174 | Set<Host> hosts = hostService.getHostsByMac(context.dstMac()); |
| 175 | if (vplsNetwork != null) { |
| 176 | Collection<Interface> vplsInterfaces = vplsNetwork.values(); |
| 177 | hosts.forEach(host -> vplsInterfaces.stream() |
| 178 | .filter(intf -> intf.connectPoint().equals(host.location())) |
| 179 | .filter(intf -> intf.vlan().equals(host.vlan())) |
| 180 | .forEach(context::forward)); |
| 181 | } else { |
| 182 | log.debug(CAN_NOT_FIND_NETWORK, context.inPort(), context.vlan()); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 187 | * Listener for interface configuration events. |
| 188 | */ |
| 189 | private class VplsInterfaceListener implements InterfaceListener { |
| 190 | |
| 191 | @Override |
| 192 | public void event(InterfaceEvent event) { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame^] | 193 | configNeighbourHandler(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Listener for network configuration events. |
| 199 | */ |
| 200 | private class VplsConfigListener implements NetworkConfigListener { |
| 201 | |
| 202 | @Override |
| 203 | public void event(NetworkConfigEvent event) { |
| 204 | configNeighbourHandler(); |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | } |