blob: 79ae52396f3373701a39ef48af5a904242e6436b [file] [log] [blame]
sanghoshin94872a12015-10-16 18:04:34 +09001/*
2* Copyright 2015 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
sangho0c2a3da2016-02-16 13:39:07 +090017package org.onosproject.openstacknetworking.switching;
sanghoshin94872a12015-10-16 18:04:34 +090018
19import org.onlab.packet.Ethernet;
sanghoshin94872a12015-10-16 18:04:34 +090020import org.onlab.packet.Ip4Address;
sanghoshin94872a12015-10-16 18:04:34 +090021import org.onosproject.core.ApplicationId;
sanghoshinf25d2e02015-11-11 23:07:17 +090022import org.onosproject.net.Device;
sanghoshin94872a12015-10-16 18:04:34 +090023import org.onosproject.net.DeviceId;
24import org.onosproject.net.Port;
25import org.onosproject.net.PortNumber;
sanghoshinf25d2e02015-11-11 23:07:17 +090026import org.onosproject.net.behaviour.ExtensionTreatmentResolver;
27import org.onosproject.net.device.DeviceService;
28import org.onosproject.net.driver.DefaultDriverData;
29import org.onosproject.net.driver.DefaultDriverHandler;
30import org.onosproject.net.driver.Driver;
31import org.onosproject.net.driver.DriverHandler;
32import org.onosproject.net.driver.DriverService;
sanghoshin94872a12015-10-16 18:04:34 +090033import org.onosproject.net.flow.DefaultTrafficSelector;
34import org.onosproject.net.flow.DefaultTrafficTreatment;
35import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
sanghoshinf25d2e02015-11-11 23:07:17 +090037import org.onosproject.net.flow.instructions.ExtensionTreatment;
38import org.onosproject.net.flow.instructions.ExtensionPropertyException;
39import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
sanghoshin94872a12015-10-16 18:04:34 +090040import org.onosproject.net.flowobjective.DefaultForwardingObjective;
41import org.onosproject.net.flowobjective.FlowObjectiveService;
42import org.onosproject.net.flowobjective.ForwardingObjective;
sangho93447f12016-02-24 00:33:22 +090043import org.onosproject.openstackinterface.OpenstackInterfaceService;
44import org.onosproject.openstackinterface.OpenstackNetwork;
45import org.onosproject.openstackinterface.OpenstackPort;
sangho0c2a3da2016-02-16 13:39:07 +090046import org.onosproject.openstacknetworking.OpenstackPortInfo;
sanghoshin94872a12015-10-16 18:04:34 +090047import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
49
sanghoshinf25d2e02015-11-11 23:07:17 +090050import java.util.Collection;
Daniel Park5df65182016-01-09 00:12:03 +090051import java.util.Map;
sanghoshinf25d2e02015-11-11 23:07:17 +090052
sanghoshin94872a12015-10-16 18:04:34 +090053/**
sanghoshin46297d22015-11-03 17:51:24 +090054 * Populates switching flow rules.
sanghoshin94872a12015-10-16 18:04:34 +090055 */
56public class OpenstackSwitchingRulePopulator {
57
58 private static Logger log = LoggerFactory
59 .getLogger(OpenstackSwitchingRulePopulator.class);
daniel2a2bd7b2015-12-02 13:53:58 +090060 private static final int SWITCHING_RULE_PRIORITY = 30000;
61 private static final int EAST_WEST_ROUTING_RULE_PRIORITY = 29000;
62 private static final int TUNNELTAG_RULE_PRIORITY = 30000;
sanghoshin94872a12015-10-16 18:04:34 +090063
64 private FlowObjectiveService flowObjectiveService;
sanghoshinf25d2e02015-11-11 23:07:17 +090065 private DriverService driverService;
66 private DeviceService deviceService;
sanghoshin94872a12015-10-16 18:04:34 +090067 private ApplicationId appId;
68
sanghoshinf25d2e02015-11-11 23:07:17 +090069 private Collection<OpenstackNetwork> openstackNetworkList;
70 private Collection<OpenstackPort> openstackPortList;
71
sanghoshin94872a12015-10-16 18:04:34 +090072 /**
Ray Milkey87b16b02015-10-30 11:50:00 -070073 * Creates OpenstackSwitchingRulPopulator.
74 *
75 * @param appId application id
sanghoshin94872a12015-10-16 18:04:34 +090076 * @param flowObjectiveService FlowObjectiveService reference
sanghoshinf25d2e02015-11-11 23:07:17 +090077 * @param deviceService DeviceService reference
Hyunsun Moon0dba61f2016-03-03 14:05:21 -080078 * @param openstackService openstack interface service
sanghoshinf25d2e02015-11-11 23:07:17 +090079 * @param driverService DriverService reference
sanghoshin94872a12015-10-16 18:04:34 +090080 */
81 public OpenstackSwitchingRulePopulator(ApplicationId appId,
sanghoshinf25d2e02015-11-11 23:07:17 +090082 FlowObjectiveService flowObjectiveService,
83 DeviceService deviceService,
sangho93447f12016-02-24 00:33:22 +090084 OpenstackInterfaceService openstackService,
sanghoshinf25d2e02015-11-11 23:07:17 +090085 DriverService driverService) {
sanghoshin94872a12015-10-16 18:04:34 +090086 this.flowObjectiveService = flowObjectiveService;
sanghoshinf25d2e02015-11-11 23:07:17 +090087 this.deviceService = deviceService;
88 this.driverService = driverService;
sanghoshin94872a12015-10-16 18:04:34 +090089 this.appId = appId;
sanghoshinf25d2e02015-11-11 23:07:17 +090090
sangho0c2a3da2016-02-16 13:39:07 +090091 openstackNetworkList = openstackService.networks();
92 openstackPortList = openstackService.ports();
sanghoshin94872a12015-10-16 18:04:34 +090093 }
94
daniel2a2bd7b2015-12-02 13:53:58 +090095
sanghoshin94872a12015-10-16 18:04:34 +090096 /**
sanghoshinf25d2e02015-11-11 23:07:17 +090097 * Populates flow rules for the VM created.
sanghoshin94872a12015-10-16 18:04:34 +090098 *
sanghoshinf25d2e02015-11-11 23:07:17 +090099 * @param device device to populate rules to
100 * @param port port for the VM created
sanghoshin94872a12015-10-16 18:04:34 +0900101 */
sangho93447f12016-02-24 00:33:22 +0900102 public void populateSwitchingRules(Device device, Port port) {
Hyunsun Moon74ec4062015-12-09 10:58:32 -0800103 populateFlowRulesForTunnelTag(device, port);
sanghoshinf25d2e02015-11-11 23:07:17 +0900104 populateFlowRulesForTrafficToSameCnode(device, port);
105 populateFlowRulesForTrafficToDifferentCnode(device, port);
sanghoshin94872a12015-10-16 18:04:34 +0900106 }
107
108 /**
daniel2a2bd7b2015-12-02 13:53:58 +0900109 * Populate the flow rules for tagging tunnelId according to which inport is came from.
110 *
111 * @param device device to put the rules
112 * @param port port info of the VM
113 */
Hyunsun Moon74ec4062015-12-09 10:58:32 -0800114 private void populateFlowRulesForTunnelTag(Device device, Port port) {
daniel2a2bd7b2015-12-02 13:53:58 +0900115 Ip4Address vmIp = getFixedIpAddressForPort(port.annotations().value("portName"));
116 String portName = port.annotations().value("portName");
117 String vni = getVniForPort(portName);
118
119 if (vmIp != null) {
Hyunsun Moon74ec4062015-12-09 10:58:32 -0800120 setFlowRuleForTunnelTag(device.id(), port, vni);
daniel2a2bd7b2015-12-02 13:53:58 +0900121 }
122 }
123
Daniel Park5df65182016-01-09 00:12:03 +0900124 private void setFlowRuleForTunnelTag(DeviceId deviceId, Port port, String vni) {
sanghoshin075e3e72015-11-25 16:34:29 +0900125
Daniel Park5df65182016-01-09 00:12:03 +0900126 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
127 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
128
129 sBuilder.matchEthType(Ethernet.TYPE_IPV4)
130 .matchInPort(port.number());
131
132 tBuilder.setTunnelId(Long.parseLong(vni));
133
134 ForwardingObjective fo = DefaultForwardingObjective.builder()
135 .withSelector(sBuilder.build())
136 .withTreatment(tBuilder.build())
137 .withPriority(TUNNELTAG_RULE_PRIORITY)
138 .withFlag(ForwardingObjective.Flag.SPECIFIC)
139 .fromApp(appId)
140 .add();
141
142 flowObjectiveService.forward(deviceId, fo);
sanghoshin65723ae2015-11-17 22:07:21 +0900143 }
144
145 /**
sanghoshinf25d2e02015-11-11 23:07:17 +0900146 * Populates the flow rules for traffic to VMs in the same Cnode as the sender.
sanghoshin94872a12015-10-16 18:04:34 +0900147 *
sanghoshinf25d2e02015-11-11 23:07:17 +0900148 * @param device device to put the rules
149 * @param port port info of the VM
sanghoshin94872a12015-10-16 18:04:34 +0900150 */
sanghoshinf25d2e02015-11-11 23:07:17 +0900151 private void populateFlowRulesForTrafficToSameCnode(Device device, Port port) {
152 Ip4Address vmIp = getFixedIpAddressForPort(port.annotations().value("portName"));
daniel2a2bd7b2015-12-02 13:53:58 +0900153 String portName = port.annotations().value("portName");
154 String vni = getVniForPort(portName);
daniel2a2bd7b2015-12-02 13:53:58 +0900155
sanghoshinf25d2e02015-11-11 23:07:17 +0900156 if (vmIp != null) {
Daniel Park5df65182016-01-09 00:12:03 +0900157 setFlowRuleForVMsInSameCnode(vmIp, device.id(), port, vni);
sanghoshinf25d2e02015-11-11 23:07:17 +0900158 }
sanghoshin94872a12015-10-16 18:04:34 +0900159 }
160
161 /**
Daniel Park5df65182016-01-09 00:12:03 +0900162 * Sets the flow rules for traffic between VMs in the same Cnode.
163 *
164 * @param ip4Address VM IP address
165 * @param id device ID to put rules
166 * @param port VM port
167 * @param vni VM VNI
168 */
169 private void setFlowRuleForVMsInSameCnode(Ip4Address ip4Address, DeviceId id,
170 Port port, String vni) {
171
172 //For L2 Switching Case
173 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
174 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
175
176 sBuilder.matchEthType(Ethernet.TYPE_IPV4)
177 .matchIPDst(ip4Address.toIpPrefix())
178 .matchTunnelId(Long.parseLong(vni));
179
180 tBuilder.setOutput(port.number());
181
182 ForwardingObjective fo = DefaultForwardingObjective.builder()
183 .withSelector(sBuilder.build())
184 .withTreatment(tBuilder.build())
185 .withPriority(SWITCHING_RULE_PRIORITY)
186 .withFlag(ForwardingObjective.Flag.SPECIFIC)
187 .fromApp(appId)
188 .add();
189
190 flowObjectiveService.forward(id, fo);
191 }
192
193 /**
sanghoshinf25d2e02015-11-11 23:07:17 +0900194 * Populates the flow rules for traffic to VMs in different Cnode using
195 * Nicira extention.
sanghoshin94872a12015-10-16 18:04:34 +0900196 *
sanghoshinf25d2e02015-11-11 23:07:17 +0900197 * @param device device to put rules
198 * @param port port information of the VM
sanghoshin94872a12015-10-16 18:04:34 +0900199 */
sanghoshinf25d2e02015-11-11 23:07:17 +0900200 private void populateFlowRulesForTrafficToDifferentCnode(Device device, Port port) {
201 String portName = port.annotations().value("portName");
202 String channelId = device.annotations().value("channelId");
203 Ip4Address hostIpAddress = Ip4Address.valueOf(channelId.split(":")[0]);
204 Ip4Address fixedIp = getFixedIpAddressForPort(portName);
sanghoshinf25d2e02015-11-11 23:07:17 +0900205 String vni = getVniForPort(portName);
206 deviceService.getAvailableDevices().forEach(d -> {
207 if (!d.equals(device)) {
208 deviceService.getPorts(d.id()).forEach(p -> {
209 String pName = p.annotations().value("portName");
210 if (!p.equals(port) && vni.equals(getVniForPort(pName))) {
211 String cidx = d.annotations().value("channelId");
212 Ip4Address hostIpx = Ip4Address.valueOf(cidx.split(":")[0]);
sanghoshinf25d2e02015-11-11 23:07:17 +0900213 Ip4Address fixedIpx = getFixedIpAddressForPort(pName);
sanghoshin46c6e3e2015-12-18 18:42:17 +0900214 if (port.isEnabled() ||
215 port.annotations().value("portName").startsWith(
216 OpenstackSwitchingManager.PORTNAME_PREFIX_ROUTER)) {
Daniel Park5df65182016-01-09 00:12:03 +0900217 setVxLanFlowRule(vni, device.id(), hostIpx, fixedIpx);
218 setVxLanFlowRule(vni, d.id(), hostIpAddress, fixedIp);
sanghoshin65723ae2015-11-17 22:07:21 +0900219 }
sanghoshinf25d2e02015-11-11 23:07:17 +0900220 }
221 });
222 }
223 });
sanghoshin94872a12015-10-16 18:04:34 +0900224 }
225
226 /**
Daniel Park5df65182016-01-09 00:12:03 +0900227 * Sets the flow rules between traffic from VMs in different Cnode.
228 *
229 * @param vni VNI
230 * @param deviceId device ID
231 * @param hostIp host IP of the VM
232 * @param vmIp fixed IP of the VM
233 */
234 private void setVxLanFlowRule(String vni, DeviceId deviceId, Ip4Address hostIp,
235 Ip4Address vmIp) {
236 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
237 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
238
239 sBuilder.matchEthType(Ethernet.TYPE_IPV4)
240 .matchTunnelId(Long.parseLong(vni))
241 .matchIPDst(vmIp.toIpPrefix());
242 tBuilder.extension(buildNiciraExtenstion(deviceId, hostIp), deviceId)
243 .setOutput(getTunnelPort(deviceId));
244
245 ForwardingObjective fo = DefaultForwardingObjective.builder()
246 .withSelector(sBuilder.build())
247 .withTreatment(tBuilder.build())
248 .withPriority(SWITCHING_RULE_PRIORITY)
249 .withFlag(ForwardingObjective.Flag.SPECIFIC)
250 .fromApp(appId)
251 .add();
252
253 flowObjectiveService.forward(deviceId, fo);
254 }
255
256 /**
257 * Returns OpenstackPort object for the Port reference given.
258 *
259 * @param port Port object
260 * @return OpenstackPort reference, or null when not found
261 */
262 public OpenstackPort openstackPort(Port port) {
263 String uuid = port.annotations().value("portName").substring(3);
264 return openstackPortList.stream().filter(p -> p.id().startsWith(uuid))
265 .findAny().orElse(null);
266 }
267
268 /**
269 * Remove flows rules for the removed VM.
270 *
271 * @param removedPort removedport info
272 * @param openstackPortInfoMap openstackPortInfoMap
273 */
sangho93447f12016-02-24 00:33:22 +0900274 public void removeSwitchingRules(Port removedPort, Map<String,
sangho3623cb62016-01-15 22:06:38 +0900275 OpenstackPortInfo> openstackPortInfoMap) {
Daniel Park5df65182016-01-09 00:12:03 +0900276 OpenstackPortInfo openstackPortInfo = openstackPortInfoMap
277 .get(removedPort.annotations().value("portName"));
278
279 DeviceId deviceId = openstackPortInfo.deviceId();
280 Ip4Address vmIp = openstackPortInfo.ip();
281 PortNumber portNumber = removedPort.number();
282 long vni = openstackPortInfo.vni();
283
284 removeFlowRuleForTunnelTag(deviceId, portNumber);
285 removeFlowRuleForVMsInSameCnode(deviceId, vmIp, vni);
286 removeFlowRuleForVMsInDiffrentCnode(removedPort, deviceId, vmIp, vni, openstackPortInfoMap);
287 }
288
289 /**
290 * Removes flow rules for tagging tunnelId.
291 *
292 * @param deviceId device id
293 * @param portNumber port number
294 */
295 private void removeFlowRuleForTunnelTag(DeviceId deviceId, PortNumber portNumber) {
296 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
297 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
298
299 sBuilder.matchEthType(Ethernet.TYPE_IPV4)
300 .matchInPort(portNumber);
301
302 ForwardingObjective fo = DefaultForwardingObjective.builder()
303 .withSelector(sBuilder.build())
304 .withTreatment(tBuilder.build())
305 .withPriority(TUNNELTAG_RULE_PRIORITY)
306 .withFlag(ForwardingObjective.Flag.SPECIFIC)
307 .fromApp(appId)
308 .remove();
309
310 flowObjectiveService.forward(deviceId, fo);
311 }
312
313 /**
314 * Removes the flow rules for traffic between VMs in the same Cnode.
315 *
316 * @param deviceId device id on which removed VM was run
317 * @param vmIp ip of the removed VM
318 * @param vni vni which removed VM was belonged
319 */
320 private void removeFlowRuleForVMsInSameCnode(DeviceId deviceId, Ip4Address vmIp, long vni) {
321 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
322
323 sBuilder.matchEthType(Ethernet.TYPE_IPV4)
324 .matchIPDst(vmIp.toIpPrefix())
325 .matchTunnelId(vni);
326
327 ForwardingObjective fo = DefaultForwardingObjective.builder()
328 .withSelector(sBuilder.build())
329 .withTreatment(DefaultTrafficTreatment.builder().build())
330 .withFlag(ForwardingObjective.Flag.SPECIFIC)
331 .withPriority(SWITCHING_RULE_PRIORITY)
332 .fromApp(appId)
333 .remove();
334
335 flowObjectiveService.forward(deviceId, fo);
336 }
337
338 /**
339 * Removes the flow rules for traffic between VMs in the different Cnode.
340 *
341 * @param removedPort removedport info
342 * @param deviceId device id on which removed VM was run
343 * @param vmIp ip of the removed VM
344 * @param vni vni which removed VM was belonged
345 * @param openstackPortInfoMap openstackPortInfoMap
346 */
347 private void removeFlowRuleForVMsInDiffrentCnode(Port removedPort, DeviceId deviceId, Ip4Address vmIp,
348 long vni, Map<String, OpenstackPortInfo> openstackPortInfoMap) {
349
350 final boolean anyPortRemainedInSameCnode
351 = checkIfAnyPortRemainedInSameCnode(removedPort, deviceId, vni, openstackPortInfoMap);
352
353 openstackPortInfoMap.forEach((port, portInfo) -> {
354 if (portInfo.vni() == vni && !portInfo.deviceId().equals(deviceId)) {
355 removeVxLanFlowRule(portInfo.deviceId(), vmIp, vni);
356 if (!anyPortRemainedInSameCnode) {
357 removeVxLanFlowRule(deviceId, portInfo.ip(), vni);
358 }
359 }
360 });
361 }
362
363 /**
364 * Removes the flow rules between traffic from VMs in different Cnode.
365 *
366 * @param deviceId device id
367 * @param vmIp ip
368 * @param vni vni which removed VM was belonged
369 */
370 private void removeVxLanFlowRule(DeviceId deviceId, Ip4Address vmIp, long vni) {
371 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
372
373 sBuilder.matchEthType(Ethernet.TYPE_IPV4)
374 .matchTunnelId(vni)
375 .matchIPDst(vmIp.toIpPrefix());
376
377 ForwardingObjective fo = DefaultForwardingObjective.builder()
378 .withSelector(sBuilder.build())
379 .withTreatment(DefaultTrafficTreatment.builder().build())
380 .withFlag(ForwardingObjective.Flag.SPECIFIC)
381 .withPriority(SWITCHING_RULE_PRIORITY)
382 .fromApp(appId)
383 .remove();
384
385 flowObjectiveService.forward(deviceId, fo);
386 }
387
388 /**
389 * Checks if there is any port remained with same vni at the device, on which the removed VM was run.
390 *
391 * @param removedPort removedport info
392 * @param deviceId device id on which removed VM was run
393 * @param vni vni which removed VM was belonged
394 * @param openstackPortInfoMap openstackPortInfoMap
395 * @return true if there is, false otherwise
396 */
397 private boolean checkIfAnyPortRemainedInSameCnode(Port removedPort, DeviceId deviceId, long vni,
398 Map<String, OpenstackPortInfo> openstackPortInfoMap) {
399
400 for (Map.Entry<String, OpenstackPortInfo> entry : openstackPortInfoMap.entrySet()) {
401 if (!removedPort.annotations().value("portName").equals(entry.getKey())) {
402 if (entry.getValue().vni() == vni && entry.getValue().deviceId().equals(deviceId)) {
403 return true;
404 }
405 }
406 }
407 return false;
408 }
409
410 /**
sanghoshinf25d2e02015-11-11 23:07:17 +0900411 * Returns the VNI of the VM of the port.
sanghoshin94872a12015-10-16 18:04:34 +0900412 *
sanghoshinf25d2e02015-11-11 23:07:17 +0900413 * @param portName VM port
414 * @return VNI
sanghoshin94872a12015-10-16 18:04:34 +0900415 */
sanghoshinf25d2e02015-11-11 23:07:17 +0900416 private String getVniForPort(String portName) {
417 String uuid = portName.substring(3);
418 OpenstackPort port = openstackPortList.stream()
419 .filter(p -> p.id().startsWith(uuid))
420 .findAny().orElse(null);
421 if (port == null) {
daniel2a2bd7b2015-12-02 13:53:58 +0900422 log.debug("No port information for port {}", portName);
sanghoshinf25d2e02015-11-11 23:07:17 +0900423 return null;
424 }
sanghoshin94872a12015-10-16 18:04:34 +0900425
sanghoshinf25d2e02015-11-11 23:07:17 +0900426 OpenstackNetwork network = openstackNetworkList.stream()
427 .filter(n -> n.id().equals(port.networkId()))
428 .findAny().orElse(null);
429 if (network == null) {
Satish K2e2d6352015-11-27 12:02:15 +0530430 log.warn("No VNI information for network {}", port.networkId());
sanghoshinf25d2e02015-11-11 23:07:17 +0900431 return null;
432 }
sanghoshin94872a12015-10-16 18:04:34 +0900433
sanghoshinf25d2e02015-11-11 23:07:17 +0900434 return network.segmentId();
sanghoshin94872a12015-10-16 18:04:34 +0900435 }
436
437 /**
sanghoshinf25d2e02015-11-11 23:07:17 +0900438 * Returns the Fixed IP address of the VM.
sanghoshin94872a12015-10-16 18:04:34 +0900439 *
sanghoshinf25d2e02015-11-11 23:07:17 +0900440 * @param portName VM port info
441 * @return IP address of the VM
sanghoshin94872a12015-10-16 18:04:34 +0900442 */
sanghoshinf25d2e02015-11-11 23:07:17 +0900443 private Ip4Address getFixedIpAddressForPort(String portName) {
sanghoshin94872a12015-10-16 18:04:34 +0900444
sanghoshinf25d2e02015-11-11 23:07:17 +0900445 String uuid = portName.substring(3);
446 OpenstackPort port = openstackPortList.stream()
447 .filter(p -> p.id().startsWith(uuid))
448 .findFirst().orElse(null);
sanghoshin94872a12015-10-16 18:04:34 +0900449
sanghoshinf25d2e02015-11-11 23:07:17 +0900450 if (port == null) {
451 log.error("There is no port information for port name {}", portName);
452 return null;
453 }
sanghoshin94872a12015-10-16 18:04:34 +0900454
sanghoshinf25d2e02015-11-11 23:07:17 +0900455 if (port.fixedIps().isEmpty()) {
456 log.error("There is no fixed IP info in the port information");
457 return null;
458 }
459
460 return (Ip4Address) port.fixedIps().values().toArray()[0];
461 }
462
sanghoshinf25d2e02015-11-11 23:07:17 +0900463 private ExtensionTreatment buildNiciraExtenstion(DeviceId id, Ip4Address hostIp) {
464 Driver driver = driverService.getDriver(id);
465 DriverHandler driverHandler = new DefaultDriverHandler(new DefaultDriverData(driver, id));
466 ExtensionTreatmentResolver resolver = driverHandler.behaviour(ExtensionTreatmentResolver.class);
467
468 ExtensionTreatment extensionInstruction =
469 resolver.getExtensionInstruction(
470 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type());
471
472 try {
473 extensionInstruction.setPropertyValue("tunnelDst", hostIp);
474 } catch (ExtensionPropertyException e) {
475 log.error("Error setting Nicira extension setting {}", e);
476 }
477
478 return extensionInstruction;
479 }
480
Daniel Park5df65182016-01-09 00:12:03 +0900481 private PortNumber getTunnelPort(DeviceId deviceId) {
482 Port port = deviceService.getPorts(deviceId).stream()
sanghoshin46c6e3e2015-12-18 18:42:17 +0900483 .filter(p -> p.annotations().value("portName").equals(
484 OpenstackSwitchingManager.PORTNAME_PREFIX_TUNNEL))
sanghoshinf25d2e02015-11-11 23:07:17 +0900485 .findAny().orElse(null);
486
487 if (port == null) {
488 log.error("No TunnelPort was created.");
489 return null;
490 }
491 return port.number();
492 }
sanghoshin94872a12015-10-16 18:04:34 +0900493}