samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 1 | /* |
| 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 | package org.onosproject.driver.ovsdb; |
| 17 | |
| 18 | import java.util.Collection; |
Hyunsun Moon | aab8c67 | 2015-10-27 19:42:12 -0700 | [diff] [blame] | 19 | import java.util.Map; |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 20 | import java.util.Set; |
Sho SHIMIZU | 3be9fbe | 2015-08-26 14:36:21 -0700 | [diff] [blame] | 21 | import java.util.stream.Collectors; |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 22 | |
| 23 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | aab8c67 | 2015-10-27 19:42:12 -0700 | [diff] [blame] | 24 | import org.onosproject.net.DefaultAnnotations; |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
Hyunsun Moon | aab8c67 | 2015-10-27 19:42:12 -0700 | [diff] [blame] | 26 | import org.onosproject.net.behaviour.BridgeName; |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 27 | import org.onosproject.net.behaviour.DefaultTunnelDescription; |
| 28 | import org.onosproject.net.behaviour.IpTunnelEndPoint; |
| 29 | import org.onosproject.net.behaviour.TunnelConfig; |
| 30 | import org.onosproject.net.behaviour.TunnelDescription; |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 31 | import org.onosproject.net.behaviour.TunnelName; |
| 32 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 33 | import org.onosproject.net.driver.DriverHandler; |
| 34 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 35 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 36 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 37 | import org.onosproject.ovsdb.controller.OvsdbTunnel; |
| 38 | |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 39 | /** |
| 40 | * OVSDB-based implementation of tunnel config behaviour. |
| 41 | */ |
| 42 | public class OvsdbTunnelConfig extends AbstractHandlerBehaviour |
| 43 | implements TunnelConfig { |
| 44 | |
samuel | 2a31ea5 | 2015-08-05 18:41:47 +0800 | [diff] [blame] | 45 | private static final String DEFAULT_ADDRESS = "0.0.0.0"; |
Hyunsun Moon | aab8c67 | 2015-10-27 19:42:12 -0700 | [diff] [blame] | 46 | private static final String OPTION_LOCAL_IP = "local_ip"; |
| 47 | private static final String OPTION_REMOTE_IP = "remote_ip"; |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 48 | |
| 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 Moon | aab8c67 | 2015-10-27 19:42:12 -0700 | [diff] [blame] | 68 | 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 |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 84 | 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 SHIMIZU | 3be9fbe | 2015-08-26 14:36:21 -0700 | [diff] [blame] | 111 | 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()); |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 123 | } |
| 124 | |
jiangrui | 866c2f2 | 2015-10-28 18:58:34 +0800 | [diff] [blame] | 125 | // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP) |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 126 | // is used in the core. So DeviceId need be changed to OvsdbNodeId. |
| 127 | private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) { |
jiangrui | 866c2f2 | 2015-10-28 18:58:34 +0800 | [diff] [blame] | 128 | String[] splits = deviceId.toString().split(":"); |
| 129 | if (splits == null || splits.length < 1) { |
| 130 | return null; |
| 131 | } |
Hyunsun Moon | dbab8de | 2015-10-28 16:45:04 -0700 | [diff] [blame^] | 132 | IpAddress ipAddress = IpAddress.valueOf(splits[1]); |
jiangrui | 866c2f2 | 2015-10-28 18:58:34 +0800 | [diff] [blame] | 133 | return new OvsdbNodeId(ipAddress, 0); |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 134 | } |
| 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 | } |