samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 1 | /* |
Andrea Campanella | 238d96e | 2016-01-20 11:52:02 -0800 | [diff] [blame] | 2 | * Copyright 2016 Open Networking Laboratory |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 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 | */ |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 16 | |
Andrea Campanella | 238d96e | 2016-01-20 11:52:02 -0800 | [diff] [blame] | 17 | package org.onosproject.drivers.ovsdb; |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 18 | |
| 19 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | aab8c67 | 2015-10-27 19:42:12 -0700 | [diff] [blame] | 20 | import org.onosproject.net.DefaultAnnotations; |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 21 | import org.onosproject.net.DeviceId; |
Hyunsun Moon | aab8c67 | 2015-10-27 19:42:12 -0700 | [diff] [blame] | 22 | import org.onosproject.net.behaviour.BridgeName; |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 23 | import org.onosproject.net.behaviour.DefaultTunnelDescription; |
| 24 | import org.onosproject.net.behaviour.IpTunnelEndPoint; |
| 25 | import org.onosproject.net.behaviour.TunnelConfig; |
| 26 | import org.onosproject.net.behaviour.TunnelDescription; |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 27 | import org.onosproject.net.behaviour.TunnelName; |
| 28 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 29 | import org.onosproject.net.driver.DriverHandler; |
| 30 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 31 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 32 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 33 | import org.onosproject.ovsdb.controller.OvsdbTunnel; |
| 34 | |
Andrea Campanella | 238d96e | 2016-01-20 11:52:02 -0800 | [diff] [blame] | 35 | import java.util.Collection; |
| 36 | import java.util.Map; |
| 37 | import java.util.Set; |
| 38 | import java.util.stream.Collectors; |
| 39 | |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 40 | /** |
| 41 | * OVSDB-based implementation of tunnel config behaviour. |
| 42 | */ |
| 43 | public class OvsdbTunnelConfig extends AbstractHandlerBehaviour |
| 44 | implements TunnelConfig { |
| 45 | |
samuel | 2a31ea5 | 2015-08-05 18:41:47 +0800 | [diff] [blame] | 46 | private static final String DEFAULT_ADDRESS = "0.0.0.0"; |
Hyunsun Moon | aab8c67 | 2015-10-27 19:42:12 -0700 | [diff] [blame] | 47 | private static final String OPTION_LOCAL_IP = "local_ip"; |
| 48 | private static final String OPTION_REMOTE_IP = "remote_ip"; |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 49 | |
| 50 | @Override |
Hyunsun Moon | aab8c67 | 2015-10-27 19:42:12 -0700 | [diff] [blame] | 51 | public boolean createTunnelInterface(BridgeName bridgeName, TunnelDescription tunnel) { |
| 52 | Map<String, String> options = ((DefaultAnnotations) tunnel.annotations()).asMap(); |
| 53 | if (tunnel.src() != null) { |
jiangrui | 0df7493 | 2015-11-27 10:04:33 +0800 | [diff] [blame] | 54 | options.put(OPTION_LOCAL_IP, ((IpTunnelEndPoint) tunnel.src()).ip().toString()); |
Hyunsun Moon | aab8c67 | 2015-10-27 19:42:12 -0700 | [diff] [blame] | 55 | } |
| 56 | if (tunnel.dst() != null) { |
jiangrui | 0df7493 | 2015-11-27 10:04:33 +0800 | [diff] [blame] | 57 | options.put(OPTION_REMOTE_IP, ((IpTunnelEndPoint) tunnel.dst()).ip().toString()); |
Hyunsun Moon | aab8c67 | 2015-10-27 19:42:12 -0700 | [diff] [blame] | 58 | } |
| 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 |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 67 | 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 SHIMIZU | 3be9fbe | 2015-08-26 14:36:21 -0700 | [diff] [blame] | 94 | 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()); |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 106 | } |
| 107 | |
jiangrui | 866c2f2 | 2015-10-28 18:58:34 +0800 | [diff] [blame] | 108 | // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP) |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 109 | // is used in the core. So DeviceId need be changed to OvsdbNodeId. |
| 110 | private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) { |
jiangrui | 866c2f2 | 2015-10-28 18:58:34 +0800 | [diff] [blame] | 111 | String[] splits = deviceId.toString().split(":"); |
| 112 | if (splits == null || splits.length < 1) { |
| 113 | return null; |
| 114 | } |
Hyunsun Moon | dbab8de | 2015-10-28 16:45:04 -0700 | [diff] [blame] | 115 | IpAddress ipAddress = IpAddress.valueOf(splits[1]); |
jiangrui | 866c2f2 | 2015-10-28 18:58:34 +0800 | [diff] [blame] | 116 | return new OvsdbNodeId(ipAddress, 0); |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 117 | } |
| 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 | } |