blob: e2bbab550d436400c311c37aaca70bcf3b03f862 [file] [log] [blame]
samuelbc087c02015-07-23 14:11:58 +08001/*
Andrea Campanella238d96e2016-01-20 11:52:02 -08002 * Copyright 2016 Open Networking Laboratory
samuelbc087c02015-07-23 14:11:58 +08003 *
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 */
samuelbc087c02015-07-23 14:11:58 +080016
Andrea Campanella238d96e2016-01-20 11:52:02 -080017package org.onosproject.drivers.ovsdb;
samuelbc087c02015-07-23 14:11:58 +080018
19import org.onlab.packet.IpAddress;
Hyunsun Moonaab8c672015-10-27 19:42:12 -070020import org.onosproject.net.DefaultAnnotations;
samuelbc087c02015-07-23 14:11:58 +080021import org.onosproject.net.DeviceId;
Hyunsun Moonaab8c672015-10-27 19:42:12 -070022import org.onosproject.net.behaviour.BridgeName;
samuelbc087c02015-07-23 14:11:58 +080023import org.onosproject.net.behaviour.DefaultTunnelDescription;
24import org.onosproject.net.behaviour.IpTunnelEndPoint;
25import org.onosproject.net.behaviour.TunnelConfig;
26import org.onosproject.net.behaviour.TunnelDescription;
samuelbc087c02015-07-23 14:11:58 +080027import org.onosproject.net.behaviour.TunnelName;
28import org.onosproject.net.driver.AbstractHandlerBehaviour;
29import org.onosproject.net.driver.DriverHandler;
30import org.onosproject.ovsdb.controller.OvsdbClientService;
31import org.onosproject.ovsdb.controller.OvsdbController;
32import org.onosproject.ovsdb.controller.OvsdbNodeId;
33import org.onosproject.ovsdb.controller.OvsdbTunnel;
34
Andrea Campanella238d96e2016-01-20 11:52:02 -080035import java.util.Collection;
36import java.util.Map;
37import java.util.Set;
38import java.util.stream.Collectors;
39
samuelbc087c02015-07-23 14:11:58 +080040/**
41 * OVSDB-based implementation of tunnel config behaviour.
42 */
43public class OvsdbTunnelConfig extends AbstractHandlerBehaviour
44 implements TunnelConfig {
45
samuel2a31ea52015-08-05 18:41:47 +080046 private static final String DEFAULT_ADDRESS = "0.0.0.0";
Hyunsun Moonaab8c672015-10-27 19:42:12 -070047 private static final String OPTION_LOCAL_IP = "local_ip";
48 private static final String OPTION_REMOTE_IP = "remote_ip";
samuelbc087c02015-07-23 14:11:58 +080049
50 @Override
Hyunsun Moonaab8c672015-10-27 19:42:12 -070051 public boolean createTunnelInterface(BridgeName bridgeName, TunnelDescription tunnel) {
52 Map<String, String> options = ((DefaultAnnotations) tunnel.annotations()).asMap();
53 if (tunnel.src() != null) {
jiangrui0df74932015-11-27 10:04:33 +080054 options.put(OPTION_LOCAL_IP, ((IpTunnelEndPoint) tunnel.src()).ip().toString());
Hyunsun Moonaab8c672015-10-27 19:42:12 -070055 }
56 if (tunnel.dst() != null) {
jiangrui0df74932015-11-27 10:04:33 +080057 options.put(OPTION_REMOTE_IP, ((IpTunnelEndPoint) tunnel.dst()).ip().toString());
Hyunsun Moonaab8c672015-10-27 19:42:12 -070058 }
59
60 DriverHandler handler = handler();
61 OvsdbClientService ovsdbClient = getOvsdbNode(handler);
62 return ovsdbClient.createTunnel(bridgeName.name(), tunnel.tunnelName().toString(),
63 tunnel.type().toString().toLowerCase(), options);
64 }
65
66 @Override
samuelbc087c02015-07-23 14:11:58 +080067 public void removeTunnel(TunnelDescription tunnel) {
68 DriverHandler handler = handler();
69 OvsdbClientService ovsdbNode = getOvsdbNode(handler);
70 IpTunnelEndPoint ipSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress
71 .valueOf(DEFAULT_ADDRESS));
72 IpTunnelEndPoint ipDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress
73 .valueOf(DEFAULT_ADDRESS));
74 if (tunnel.src() instanceof IpTunnelEndPoint) {
75 ipSrc = (IpTunnelEndPoint) tunnel.src();
76 }
77 if (tunnel.dst() instanceof IpTunnelEndPoint) {
78 ipDst = (IpTunnelEndPoint) tunnel.dst();
79 }
80 //Even if source point ip or destination point ip equals 0:0:0:0, it is still work-in-progress.
81 ovsdbNode.dropTunnel(ipSrc.ip(), ipDst.ip());
82 }
83
84 @Override
85 public void updateTunnel(TunnelDescription tunnel) {
86 // TODO Auto-generated method stub
87
88 }
89
90 @Override
91 public Collection<TunnelDescription> getTunnels() {
92 DriverHandler handler = handler();
93 OvsdbClientService ovsdbNode = getOvsdbNode(handler);
Sho SHIMIZU3be9fbe2015-08-26 14:36:21 -070094 Set<OvsdbTunnel> tunnels = ovsdbNode.getTunnels();
95
96 return tunnels.stream()
97 .map(x ->
98 new DefaultTunnelDescription(
99 IpTunnelEndPoint.ipTunnelPoint(x.localIp()),
100 IpTunnelEndPoint.ipTunnelPoint(x.remoteIp()),
101 TunnelDescription.Type.VXLAN,
102 TunnelName.tunnelName(x.tunnelName().toString())
103 )
104 )
105 .collect(Collectors.toSet());
samuelbc087c02015-07-23 14:11:58 +0800106 }
107
jiangrui866c2f22015-10-28 18:58:34 +0800108 // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP)
samuelbc087c02015-07-23 14:11:58 +0800109 // is used in the core. So DeviceId need be changed to OvsdbNodeId.
110 private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) {
jiangrui866c2f22015-10-28 18:58:34 +0800111 String[] splits = deviceId.toString().split(":");
112 if (splits == null || splits.length < 1) {
113 return null;
114 }
Hyunsun Moondbab8de2015-10-28 16:45:04 -0700115 IpAddress ipAddress = IpAddress.valueOf(splits[1]);
jiangrui866c2f22015-10-28 18:58:34 +0800116 return new OvsdbNodeId(ipAddress, 0);
samuelbc087c02015-07-23 14:11:58 +0800117 }
118
119 private OvsdbClientService getOvsdbNode(DriverHandler handler) {
120 OvsdbController ovsController = handler.get(OvsdbController.class);
121 DeviceId deviceId = handler.data().deviceId();
122 OvsdbNodeId nodeId = changeDeviceIdToNodeId(deviceId);
123 return ovsController.getOvsdbClient(nodeId);
124 }
125}