blob: 455c3ab16aed4c8002ef683e4495e453f54466db [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
51 public void createTunnel(TunnelDescription tunnel) {
52 DriverHandler handler = handler();
53 OvsdbClientService ovsdbNode = getOvsdbNode(handler);
54 IpTunnelEndPoint ipSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress
55 .valueOf(DEFAULT_ADDRESS));
56 IpTunnelEndPoint ipDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress
57 .valueOf(DEFAULT_ADDRESS));
58 if (tunnel.src() instanceof IpTunnelEndPoint) {
59 ipSrc = (IpTunnelEndPoint) tunnel.src();
60 }
61 if (tunnel.dst() instanceof IpTunnelEndPoint) {
62 ipDst = (IpTunnelEndPoint) tunnel.dst();
63 }
64 //Even if source point ip or destination point ip equals 0:0:0:0, it is still work-in-progress.
65 ovsdbNode.createTunnel(ipSrc.ip(), ipDst.ip());
66 }
67
68 @Override
Hyunsun Moonaab8c672015-10-27 19:42:12 -070069 public boolean createTunnelInterface(BridgeName bridgeName, TunnelDescription tunnel) {
70 Map<String, String> options = ((DefaultAnnotations) tunnel.annotations()).asMap();
71 if (tunnel.src() != null) {
jiangrui0df74932015-11-27 10:04:33 +080072 options.put(OPTION_LOCAL_IP, ((IpTunnelEndPoint) tunnel.src()).ip().toString());
Hyunsun Moonaab8c672015-10-27 19:42:12 -070073 }
74 if (tunnel.dst() != null) {
jiangrui0df74932015-11-27 10:04:33 +080075 options.put(OPTION_REMOTE_IP, ((IpTunnelEndPoint) tunnel.dst()).ip().toString());
Hyunsun Moonaab8c672015-10-27 19:42:12 -070076 }
77
78 DriverHandler handler = handler();
79 OvsdbClientService ovsdbClient = getOvsdbNode(handler);
80 return ovsdbClient.createTunnel(bridgeName.name(), tunnel.tunnelName().toString(),
81 tunnel.type().toString().toLowerCase(), options);
82 }
83
84 @Override
samuelbc087c02015-07-23 14:11:58 +080085 public void removeTunnel(TunnelDescription tunnel) {
86 DriverHandler handler = handler();
87 OvsdbClientService ovsdbNode = getOvsdbNode(handler);
88 IpTunnelEndPoint ipSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress
89 .valueOf(DEFAULT_ADDRESS));
90 IpTunnelEndPoint ipDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress
91 .valueOf(DEFAULT_ADDRESS));
92 if (tunnel.src() instanceof IpTunnelEndPoint) {
93 ipSrc = (IpTunnelEndPoint) tunnel.src();
94 }
95 if (tunnel.dst() instanceof IpTunnelEndPoint) {
96 ipDst = (IpTunnelEndPoint) tunnel.dst();
97 }
98 //Even if source point ip or destination point ip equals 0:0:0:0, it is still work-in-progress.
99 ovsdbNode.dropTunnel(ipSrc.ip(), ipDst.ip());
100 }
101
102 @Override
103 public void updateTunnel(TunnelDescription tunnel) {
104 // TODO Auto-generated method stub
105
106 }
107
108 @Override
109 public Collection<TunnelDescription> getTunnels() {
110 DriverHandler handler = handler();
111 OvsdbClientService ovsdbNode = getOvsdbNode(handler);
Sho SHIMIZU3be9fbe2015-08-26 14:36:21 -0700112 Set<OvsdbTunnel> tunnels = ovsdbNode.getTunnels();
113
114 return tunnels.stream()
115 .map(x ->
116 new DefaultTunnelDescription(
117 IpTunnelEndPoint.ipTunnelPoint(x.localIp()),
118 IpTunnelEndPoint.ipTunnelPoint(x.remoteIp()),
119 TunnelDescription.Type.VXLAN,
120 TunnelName.tunnelName(x.tunnelName().toString())
121 )
122 )
123 .collect(Collectors.toSet());
samuelbc087c02015-07-23 14:11:58 +0800124 }
125
jiangrui866c2f22015-10-28 18:58:34 +0800126 // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP)
samuelbc087c02015-07-23 14:11:58 +0800127 // is used in the core. So DeviceId need be changed to OvsdbNodeId.
128 private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) {
jiangrui866c2f22015-10-28 18:58:34 +0800129 String[] splits = deviceId.toString().split(":");
130 if (splits == null || splits.length < 1) {
131 return null;
132 }
Hyunsun Moondbab8de2015-10-28 16:45:04 -0700133 IpAddress ipAddress = IpAddress.valueOf(splits[1]);
jiangrui866c2f22015-10-28 18:58:34 +0800134 return new OvsdbNodeId(ipAddress, 0);
samuelbc087c02015-07-23 14:11:58 +0800135 }
136
137 private OvsdbClientService getOvsdbNode(DriverHandler handler) {
138 OvsdbController ovsController = handler.get(OvsdbController.class);
139 DeviceId deviceId = handler.data().deviceId();
140 OvsdbNodeId nodeId = changeDeviceIdToNodeId(deviceId);
141 return ovsController.getOvsdbClient(nodeId);
142 }
143}