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