blob: f2bfa9d74c9fcccda8d04880125d86b21e801b79 [file] [log] [blame]
Yi Tseng28767f02016-09-13 04:27:20 -07001/*
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
17package org.onosproject.vpls;
18
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070019import com.google.common.collect.SetMultimap;
Yi Tseng28767f02016-09-13 04:27:20 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.incubator.net.intf.Interface;
28import org.onosproject.incubator.net.intf.InterfaceEvent;
29import org.onosproject.incubator.net.intf.InterfaceListener;
30import org.onosproject.incubator.net.intf.InterfaceService;
31import org.onosproject.incubator.net.neighbour.NeighbourMessageContext;
32import org.onosproject.incubator.net.neighbour.NeighbourMessageHandler;
33import org.onosproject.incubator.net.neighbour.NeighbourResolutionService;
34import org.onosproject.net.Host;
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070035import org.onosproject.net.config.NetworkConfigEvent;
36import org.onosproject.net.config.NetworkConfigListener;
37import org.onosproject.net.config.NetworkConfigService;
Yi Tseng28767f02016-09-13 04:27:20 -070038import org.onosproject.net.host.HostService;
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070039import org.onosproject.vpls.config.VplsConfigurationService;
Yi Tseng28767f02016-09-13 04:27:20 -070040import org.slf4j.Logger;
41
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070042import java.util.Collection;
43import java.util.Set;
Yi Tseng28767f02016-09-13 04:27:20 -070044
45import static org.slf4j.LoggerFactory.getLogger;
46
47/**
Luca Prete092e8952016-10-26 16:25:56 +020048 * Handles neighbour messages for on behalf of the VPLS application. Handlers
49 * will be changed automatically by interface or network configuration events.
Yi Tseng28767f02016-09-13 04:27:20 -070050 */
51@Component(immediate = true)
52public class VplsNeighbourHandler {
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070053 private static final String UNKNOWN_CONTEXT = "Unknown context type: {}";
54
Luca Prete092e8952016-10-26 16:25:56 +020055 private static final String CAN_NOT_FIND_VPLS =
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070056 "Cannot find VPLS for port {} with VLAN Id {}.";
Yi Tseng28767f02016-09-13 04:27:20 -070057
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 Kimfbf653c2016-09-21 10:05:22 -070067 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected VplsConfigurationService vplsConfigService;
69
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected NetworkConfigService configService;
72
Luca Prete092e8952016-10-26 16:25:56 +020073 private VplsInterfaceListener interfaceListener =
74 new VplsInterfaceListener();
Yi Tseng28767f02016-09-13 04:27:20 -070075
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070076 protected VplsNeighbourMessageHandler neighbourHandler =
Yi Tseng28767f02016-09-13 04:27:20 -070077 new VplsNeighbourMessageHandler();
78
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070079 protected VplsConfigListener configListener =
80 new VplsConfigListener();
Yi Tseng28767f02016-09-13 04:27:20 -070081
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070082 private final Logger log = getLogger(getClass());
Yi Tseng28767f02016-09-13 04:27:20 -070083
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 Kimfbf653c2016-09-21 10:05:22 -070091 configService.addListener(configListener);
92 configNeighbourHandler();
Yi Tseng28767f02016-09-13 04:27:20 -070093 }
94
95 @Deactivate
96 protected void deactivate() {
97 interfaceService.removeListener(interfaceListener);
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070098 configService.removeListener(configListener);
99 neighbourService.unregisterNeighbourHandlers(appId);
Yi Tseng28767f02016-09-13 04:27:20 -0700100 }
101
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700102 private void configNeighbourHandler() {
103 neighbourService.unregisterNeighbourHandlers(appId);
Luca Prete092e8952016-10-26 16:25:56 +0200104 Set<Interface> interfaces = vplsConfigService.allIfaces();
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700105
106 interfaceService.getInterfaces()
107 .stream()
108 .filter(interfaces::contains)
109 .forEach(intf -> {
110 neighbourService.registerNeighbourHandler(intf,
111 neighbourHandler,
112 appId);
113 });
Yi Tseng28767f02016-09-13 04:27:20 -0700114 }
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 Kimfbf653c2016-09-21 10:05:22 -0700127 handleRequest(context);
Yi Tseng28767f02016-09-13 04:27:20 -0700128 break;
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700129
Yi Tseng28767f02016-09-13 04:27:20 -0700130 case REPLY:
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700131 handleReply(context, hostService);
Yi Tseng28767f02016-09-13 04:27:20 -0700132 break;
133
134 default:
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700135 log.warn(UNKNOWN_CONTEXT, context.type());
Yi Tseng28767f02016-09-13 04:27:20 -0700136 break;
137 }
138 }
139 }
140
141 /**
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700142 * Handles request messages.
143 *
144 * @param context the message context
145 */
146 protected void handleRequest(NeighbourMessageContext context) {
147
Luca Prete092e8952016-10-26 16:25:56 +0200148 SetMultimap<String, Interface> vpls =
149 vplsConfigService.ifacesByVplsName(context.vlan(),
150 context.inPort());
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700151
Luca Prete092e8952016-10-26 16:25:56 +0200152 if (vpls != null) {
153 Collection<Interface> vplsInterfaces = vpls.values();
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700154 vplsInterfaces.stream()
155 .filter(intf -> !context.inPort().equals(intf.connectPoint()))
156 .forEach(context::forward);
157
158 } else {
Luca Prete092e8952016-10-26 16:25:56 +0200159 log.debug(CAN_NOT_FIND_VPLS, context.inPort(), context.vlan());
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700160 }
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) {
171
Luca Prete092e8952016-10-26 16:25:56 +0200172 SetMultimap<String, Interface> vpls =
173 vplsConfigService.ifacesByVplsName(context.vlan(),
174 context.inPort());
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700175
176 Set<Host> hosts = hostService.getHostsByMac(context.dstMac());
Luca Prete092e8952016-10-26 16:25:56 +0200177 if (vpls != null) {
178 Collection<Interface> vplsInterfaces = vpls.values();
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700179 hosts.forEach(host -> vplsInterfaces.stream()
180 .filter(intf -> intf.connectPoint().equals(host.location()))
181 .filter(intf -> intf.vlan().equals(host.vlan()))
182 .forEach(context::forward));
183 } else {
Luca Prete092e8952016-10-26 16:25:56 +0200184 log.debug(CAN_NOT_FIND_VPLS, context.inPort(), context.vlan());
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700185 }
186 }
187
188 /**
Yi Tseng28767f02016-09-13 04:27:20 -0700189 * Listener for interface configuration events.
190 */
191 private class VplsInterfaceListener implements InterfaceListener {
192
193 @Override
194 public void event(InterfaceEvent event) {
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700195 configNeighbourHandler();
196 }
197 }
198
199 /**
200 * Listener for network configuration events.
201 */
202 private class VplsConfigListener implements NetworkConfigListener {
203
204 @Override
205 public void event(NetworkConfigEvent event) {
206 configNeighbourHandler();
Yi Tseng28767f02016-09-13 04:27:20 -0700207 }
208 }
209
210}