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; |
| 19 | |
| 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; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.behaviour.DefaultTunnelDescription; |
| 26 | import org.onosproject.net.behaviour.IpTunnelEndPoint; |
| 27 | import org.onosproject.net.behaviour.TunnelConfig; |
| 28 | import org.onosproject.net.behaviour.TunnelDescription; |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 29 | import org.onosproject.net.behaviour.TunnelName; |
| 30 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 31 | import org.onosproject.net.driver.DriverHandler; |
| 32 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 33 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 34 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 35 | import org.onosproject.ovsdb.controller.OvsdbTunnel; |
| 36 | |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 37 | /** |
| 38 | * OVSDB-based implementation of tunnel config behaviour. |
| 39 | */ |
| 40 | public class OvsdbTunnelConfig extends AbstractHandlerBehaviour |
| 41 | implements TunnelConfig { |
| 42 | |
samuel | 2a31ea5 | 2015-08-05 18:41:47 +0800 | [diff] [blame] | 43 | private static final String DEFAULT_ADDRESS = "0.0.0.0"; |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 44 | |
| 45 | @Override |
| 46 | public void createTunnel(TunnelDescription tunnel) { |
| 47 | DriverHandler handler = handler(); |
| 48 | OvsdbClientService ovsdbNode = getOvsdbNode(handler); |
| 49 | IpTunnelEndPoint ipSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 50 | .valueOf(DEFAULT_ADDRESS)); |
| 51 | IpTunnelEndPoint ipDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 52 | .valueOf(DEFAULT_ADDRESS)); |
| 53 | if (tunnel.src() instanceof IpTunnelEndPoint) { |
| 54 | ipSrc = (IpTunnelEndPoint) tunnel.src(); |
| 55 | } |
| 56 | if (tunnel.dst() instanceof IpTunnelEndPoint) { |
| 57 | ipDst = (IpTunnelEndPoint) tunnel.dst(); |
| 58 | } |
| 59 | //Even if source point ip or destination point ip equals 0:0:0:0, it is still work-in-progress. |
| 60 | ovsdbNode.createTunnel(ipSrc.ip(), ipDst.ip()); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void removeTunnel(TunnelDescription tunnel) { |
| 65 | DriverHandler handler = handler(); |
| 66 | OvsdbClientService ovsdbNode = getOvsdbNode(handler); |
| 67 | IpTunnelEndPoint ipSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 68 | .valueOf(DEFAULT_ADDRESS)); |
| 69 | IpTunnelEndPoint ipDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 70 | .valueOf(DEFAULT_ADDRESS)); |
| 71 | if (tunnel.src() instanceof IpTunnelEndPoint) { |
| 72 | ipSrc = (IpTunnelEndPoint) tunnel.src(); |
| 73 | } |
| 74 | if (tunnel.dst() instanceof IpTunnelEndPoint) { |
| 75 | ipDst = (IpTunnelEndPoint) tunnel.dst(); |
| 76 | } |
| 77 | //Even if source point ip or destination point ip equals 0:0:0:0, it is still work-in-progress. |
| 78 | ovsdbNode.dropTunnel(ipSrc.ip(), ipDst.ip()); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void updateTunnel(TunnelDescription tunnel) { |
| 83 | // TODO Auto-generated method stub |
| 84 | |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public Collection<TunnelDescription> getTunnels() { |
| 89 | DriverHandler handler = handler(); |
| 90 | OvsdbClientService ovsdbNode = getOvsdbNode(handler); |
Sho SHIMIZU | 3be9fbe | 2015-08-26 14:36:21 -0700 | [diff] [blame] | 91 | Set<OvsdbTunnel> tunnels = ovsdbNode.getTunnels(); |
| 92 | |
| 93 | return tunnels.stream() |
| 94 | .map(x -> |
| 95 | new DefaultTunnelDescription( |
| 96 | IpTunnelEndPoint.ipTunnelPoint(x.localIp()), |
| 97 | IpTunnelEndPoint.ipTunnelPoint(x.remoteIp()), |
| 98 | TunnelDescription.Type.VXLAN, |
| 99 | TunnelName.tunnelName(x.tunnelName().toString()) |
| 100 | ) |
| 101 | ) |
| 102 | .collect(Collectors.toSet()); |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // OvsdbNodeId(IP:port) is used in the adaptor while DeviceId(ovsdb:IP:port) |
| 106 | // is used in the core. So DeviceId need be changed to OvsdbNodeId. |
| 107 | private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) { |
| 108 | int lastColon = deviceId.toString().lastIndexOf(":"); |
| 109 | int fistColon = deviceId.toString().indexOf(":"); |
samuel | 6d75dbf | 2015-08-03 10:06:10 +0800 | [diff] [blame] | 110 | String ip = deviceId.toString().substring(fistColon + 1, lastColon); |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 111 | String port = deviceId.toString().substring(lastColon + 1); |
| 112 | IpAddress ipAddress = IpAddress.valueOf(ip); |
Sho SHIMIZU | 91916b6 | 2015-08-26 11:27:43 -0700 | [diff] [blame] | 113 | long portL = Long.parseLong(port); |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 114 | return new OvsdbNodeId(ipAddress, portL); |
| 115 | } |
| 116 | |
| 117 | private OvsdbClientService getOvsdbNode(DriverHandler handler) { |
| 118 | OvsdbController ovsController = handler.get(OvsdbController.class); |
| 119 | DeviceId deviceId = handler.data().deviceId(); |
| 120 | OvsdbNodeId nodeId = changeDeviceIdToNodeId(deviceId); |
| 121 | return ovsController.getOvsdbClient(nodeId); |
| 122 | } |
| 123 | } |