blob: a32553ade0e466187ec7c6f9e80027b6050acf09 [file] [log] [blame]
samuelbc087c02015-07-23 14:11:58 +08001/*
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 */
16package org.onosproject.driver.ovsdb;
17
18import java.util.Collection;
Hyunsun Moonaab8c672015-10-27 19:42:12 -070019import java.util.Map;
samuelbc087c02015-07-23 14:11:58 +080020import java.util.Set;
Sho SHIMIZU3be9fbe2015-08-26 14:36:21 -070021import java.util.stream.Collectors;
samuelbc087c02015-07-23 14:11:58 +080022
23import org.onlab.packet.IpAddress;
Hyunsun Moonaab8c672015-10-27 19:42:12 -070024import org.onosproject.net.DefaultAnnotations;
samuelbc087c02015-07-23 14:11:58 +080025import org.onosproject.net.DeviceId;
Hyunsun Moonaab8c672015-10-27 19:42:12 -070026import org.onosproject.net.behaviour.BridgeName;
samuelbc087c02015-07-23 14:11:58 +080027import org.onosproject.net.behaviour.DefaultTunnelDescription;
28import org.onosproject.net.behaviour.IpTunnelEndPoint;
29import org.onosproject.net.behaviour.TunnelConfig;
30import org.onosproject.net.behaviour.TunnelDescription;
samuelbc087c02015-07-23 14:11:58 +080031import org.onosproject.net.behaviour.TunnelName;
32import org.onosproject.net.driver.AbstractHandlerBehaviour;
33import org.onosproject.net.driver.DriverHandler;
34import org.onosproject.ovsdb.controller.OvsdbClientService;
35import org.onosproject.ovsdb.controller.OvsdbController;
36import org.onosproject.ovsdb.controller.OvsdbNodeId;
37import org.onosproject.ovsdb.controller.OvsdbTunnel;
38
samuelbc087c02015-07-23 14:11:58 +080039/**
40 * OVSDB-based implementation of tunnel config behaviour.
41 */
42public class OvsdbTunnelConfig extends AbstractHandlerBehaviour
43 implements TunnelConfig {
44
samuel2a31ea52015-08-05 18:41:47 +080045 private static final String DEFAULT_ADDRESS = "0.0.0.0";
Hyunsun Moonaab8c672015-10-27 19:42:12 -070046 private static final String OPTION_LOCAL_IP = "local_ip";
47 private static final String OPTION_REMOTE_IP = "remote_ip";
samuelbc087c02015-07-23 14:11:58 +080048
49 @Override
50 public void createTunnel(TunnelDescription tunnel) {
51 DriverHandler handler = handler();
52 OvsdbClientService ovsdbNode = getOvsdbNode(handler);
53 IpTunnelEndPoint ipSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress
54 .valueOf(DEFAULT_ADDRESS));
55 IpTunnelEndPoint ipDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress
56 .valueOf(DEFAULT_ADDRESS));
57 if (tunnel.src() instanceof IpTunnelEndPoint) {
58 ipSrc = (IpTunnelEndPoint) tunnel.src();
59 }
60 if (tunnel.dst() instanceof IpTunnelEndPoint) {
61 ipDst = (IpTunnelEndPoint) tunnel.dst();
62 }
63 //Even if source point ip or destination point ip equals 0:0:0:0, it is still work-in-progress.
64 ovsdbNode.createTunnel(ipSrc.ip(), ipDst.ip());
65 }
66
67 @Override
Hyunsun Moonaab8c672015-10-27 19:42:12 -070068 public boolean createTunnelInterface(BridgeName bridgeName, TunnelDescription tunnel) {
69 Map<String, String> options = ((DefaultAnnotations) tunnel.annotations()).asMap();
70 if (tunnel.src() != null) {
71 options.put(OPTION_LOCAL_IP, tunnel.src().toString());
72 }
73 if (tunnel.dst() != null) {
74 options.put(OPTION_REMOTE_IP, tunnel.dst().toString());
75 }
76
77 DriverHandler handler = handler();
78 OvsdbClientService ovsdbClient = getOvsdbNode(handler);
79 return ovsdbClient.createTunnel(bridgeName.name(), tunnel.tunnelName().toString(),
80 tunnel.type().toString().toLowerCase(), options);
81 }
82
83 @Override
samuelbc087c02015-07-23 14:11:58 +080084 public void removeTunnel(TunnelDescription tunnel) {
85 DriverHandler handler = handler();
86 OvsdbClientService ovsdbNode = getOvsdbNode(handler);
87 IpTunnelEndPoint ipSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress
88 .valueOf(DEFAULT_ADDRESS));
89 IpTunnelEndPoint ipDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress
90 .valueOf(DEFAULT_ADDRESS));
91 if (tunnel.src() instanceof IpTunnelEndPoint) {
92 ipSrc = (IpTunnelEndPoint) tunnel.src();
93 }
94 if (tunnel.dst() instanceof IpTunnelEndPoint) {
95 ipDst = (IpTunnelEndPoint) tunnel.dst();
96 }
97 //Even if source point ip or destination point ip equals 0:0:0:0, it is still work-in-progress.
98 ovsdbNode.dropTunnel(ipSrc.ip(), ipDst.ip());
99 }
100
101 @Override
102 public void updateTunnel(TunnelDescription tunnel) {
103 // TODO Auto-generated method stub
104
105 }
106
107 @Override
108 public Collection<TunnelDescription> getTunnels() {
109 DriverHandler handler = handler();
110 OvsdbClientService ovsdbNode = getOvsdbNode(handler);
Sho SHIMIZU3be9fbe2015-08-26 14:36:21 -0700111 Set<OvsdbTunnel> tunnels = ovsdbNode.getTunnels();
112
113 return tunnels.stream()
114 .map(x ->
115 new DefaultTunnelDescription(
116 IpTunnelEndPoint.ipTunnelPoint(x.localIp()),
117 IpTunnelEndPoint.ipTunnelPoint(x.remoteIp()),
118 TunnelDescription.Type.VXLAN,
119 TunnelName.tunnelName(x.tunnelName().toString())
120 )
121 )
122 .collect(Collectors.toSet());
samuelbc087c02015-07-23 14:11:58 +0800123 }
124
jiangrui866c2f22015-10-28 18:58:34 +0800125 // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP)
samuelbc087c02015-07-23 14:11:58 +0800126 // is used in the core. So DeviceId need be changed to OvsdbNodeId.
127 private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) {
jiangrui866c2f22015-10-28 18:58:34 +0800128 String[] splits = deviceId.toString().split(":");
129 if (splits == null || splits.length < 1) {
130 return null;
131 }
Hyunsun Moondbab8de2015-10-28 16:45:04 -0700132 IpAddress ipAddress = IpAddress.valueOf(splits[1]);
jiangrui866c2f22015-10-28 18:58:34 +0800133 return new OvsdbNodeId(ipAddress, 0);
samuelbc087c02015-07-23 14:11:58 +0800134 }
135
136 private OvsdbClientService getOvsdbNode(DriverHandler handler) {
137 OvsdbController ovsController = handler.get(OvsdbController.class);
138 DeviceId deviceId = handler.data().deviceId();
139 OvsdbNodeId nodeId = changeDeviceIdToNodeId(deviceId);
140 return ovsController.getOvsdbClient(nodeId);
141 }
142}