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; |
| 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; |
| 28 | import org.onosproject.net.behaviour.TunnelEndPoint; |
| 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 | |
| 37 | import com.google.common.collect.Sets; |
| 38 | |
| 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"; |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 46 | |
| 47 | @Override |
| 48 | public void createTunnel(TunnelDescription tunnel) { |
| 49 | DriverHandler handler = handler(); |
| 50 | OvsdbClientService ovsdbNode = getOvsdbNode(handler); |
| 51 | IpTunnelEndPoint ipSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 52 | .valueOf(DEFAULT_ADDRESS)); |
| 53 | IpTunnelEndPoint ipDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 54 | .valueOf(DEFAULT_ADDRESS)); |
| 55 | if (tunnel.src() instanceof IpTunnelEndPoint) { |
| 56 | ipSrc = (IpTunnelEndPoint) tunnel.src(); |
| 57 | } |
| 58 | if (tunnel.dst() instanceof IpTunnelEndPoint) { |
| 59 | ipDst = (IpTunnelEndPoint) tunnel.dst(); |
| 60 | } |
| 61 | //Even if source point ip or destination point ip equals 0:0:0:0, it is still work-in-progress. |
| 62 | ovsdbNode.createTunnel(ipSrc.ip(), ipDst.ip()); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void removeTunnel(TunnelDescription tunnel) { |
| 67 | DriverHandler handler = handler(); |
| 68 | OvsdbClientService ovsdbNode = getOvsdbNode(handler); |
| 69 | IpTunnelEndPoint ipSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 70 | .valueOf(DEFAULT_ADDRESS)); |
| 71 | IpTunnelEndPoint ipDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 72 | .valueOf(DEFAULT_ADDRESS)); |
| 73 | if (tunnel.src() instanceof IpTunnelEndPoint) { |
| 74 | ipSrc = (IpTunnelEndPoint) tunnel.src(); |
| 75 | } |
| 76 | if (tunnel.dst() instanceof IpTunnelEndPoint) { |
| 77 | ipDst = (IpTunnelEndPoint) tunnel.dst(); |
| 78 | } |
| 79 | //Even if source point ip or destination point ip equals 0:0:0:0, it is still work-in-progress. |
| 80 | ovsdbNode.dropTunnel(ipSrc.ip(), ipDst.ip()); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void updateTunnel(TunnelDescription tunnel) { |
| 85 | // TODO Auto-generated method stub |
| 86 | |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Collection<TunnelDescription> getTunnels() { |
| 91 | DriverHandler handler = handler(); |
| 92 | OvsdbClientService ovsdbNode = getOvsdbNode(handler); |
| 93 | Set<OvsdbTunnel> ovsdbSet = ovsdbNode.getTunnels(); |
| 94 | Collection<TunnelDescription> tunnels = Sets.newHashSet(); |
| 95 | ovsdbSet.forEach(o -> { |
| 96 | TunnelEndPoint ipSrc = IpTunnelEndPoint.ipTunnelPoint(o.localIp()); |
| 97 | TunnelEndPoint ipDst = IpTunnelEndPoint.ipTunnelPoint(o.remoteIp()); |
| 98 | TunnelName name = TunnelName.tunnelName(o.tunnelName().toString()); |
| 99 | TunnelDescription des = new DefaultTunnelDescription( |
| 100 | ipSrc, |
| 101 | ipDst, |
| 102 | TunnelDescription.Type.VXLAN, |
| 103 | name); |
| 104 | tunnels.add(des); |
| 105 | }); |
| 106 | return tunnels; |
| 107 | } |
| 108 | |
| 109 | // OvsdbNodeId(IP:port) is used in the adaptor while DeviceId(ovsdb:IP:port) |
| 110 | // is used in the core. So DeviceId need be changed to OvsdbNodeId. |
| 111 | private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) { |
| 112 | int lastColon = deviceId.toString().lastIndexOf(":"); |
| 113 | int fistColon = deviceId.toString().indexOf(":"); |
samuel | 6d75dbf | 2015-08-03 10:06:10 +0800 | [diff] [blame] | 114 | String ip = deviceId.toString().substring(fistColon + 1, lastColon); |
samuel | bc087c0 | 2015-07-23 14:11:58 +0800 | [diff] [blame] | 115 | String port = deviceId.toString().substring(lastColon + 1); |
| 116 | IpAddress ipAddress = IpAddress.valueOf(ip); |
| 117 | long portL = Long.valueOf(port).longValue(); |
| 118 | return new OvsdbNodeId(ipAddress, portL); |
| 119 | } |
| 120 | |
| 121 | private OvsdbClientService getOvsdbNode(DriverHandler handler) { |
| 122 | OvsdbController ovsController = handler.get(OvsdbController.class); |
| 123 | DeviceId deviceId = handler.data().deviceId(); |
| 124 | OvsdbNodeId nodeId = changeDeviceIdToNodeId(deviceId); |
| 125 | return ovsController.getOvsdbClient(nodeId); |
| 126 | } |
| 127 | } |