blob: d706dcae01785cefc1204cc36b7421f9ab3cf5d9 [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/**
48 * Handles neighbour messages for VPLS use case.
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070049 * Handlers 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
55 private static final String CAN_NOT_FIND_NETWORK =
56 "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
Yi Tseng28767f02016-09-13 04:27:20 -070073 private VplsInterfaceListener interfaceListener
74 = new VplsInterfaceListener();
75
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);
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 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
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 Tseng28767f02016-09-13 04:27:20 -0700187 * Listener for interface configuration events.
188 */
189 private class VplsInterfaceListener implements InterfaceListener {
190
191 @Override
192 public void event(InterfaceEvent event) {
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700193 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 Tseng28767f02016-09-13 04:27:20 -0700205 }
206 }
207
208}