blob: d32fb6bea4de8ba7a14c92d84a65beb80722dcd0 [file] [log] [blame]
samuelbc087c02015-07-23 14:11:58 +08001/*
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 */
16package org.onosproject.driver.ovsdb;
17
18import java.util.Collection;
19
20import java.util.Set;
Sho SHIMIZU3be9fbe2015-08-26 14:36:21 -070021import java.util.stream.Collectors;
samuelbc087c02015-07-23 14:11:58 +080022
23import org.onlab.packet.IpAddress;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.behaviour.DefaultTunnelDescription;
26import org.onosproject.net.behaviour.IpTunnelEndPoint;
27import org.onosproject.net.behaviour.TunnelConfig;
28import org.onosproject.net.behaviour.TunnelDescription;
samuelbc087c02015-07-23 14:11:58 +080029import org.onosproject.net.behaviour.TunnelName;
30import org.onosproject.net.driver.AbstractHandlerBehaviour;
31import org.onosproject.net.driver.DriverHandler;
32import org.onosproject.ovsdb.controller.OvsdbClientService;
33import org.onosproject.ovsdb.controller.OvsdbController;
34import org.onosproject.ovsdb.controller.OvsdbNodeId;
35import org.onosproject.ovsdb.controller.OvsdbTunnel;
36
samuelbc087c02015-07-23 14:11:58 +080037/**
38 * OVSDB-based implementation of tunnel config behaviour.
39 */
40public class OvsdbTunnelConfig extends AbstractHandlerBehaviour
41 implements TunnelConfig {
42
samuel2a31ea52015-08-05 18:41:47 +080043 private static final String DEFAULT_ADDRESS = "0.0.0.0";
samuelbc087c02015-07-23 14:11:58 +080044
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 SHIMIZU3be9fbe2015-08-26 14:36:21 -070091 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());
samuelbc087c02015-07-23 14:11:58 +0800103 }
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(":");
samuel6d75dbf2015-08-03 10:06:10 +0800110 String ip = deviceId.toString().substring(fistColon + 1, lastColon);
samuelbc087c02015-07-23 14:11:58 +0800111 String port = deviceId.toString().substring(lastColon + 1);
112 IpAddress ipAddress = IpAddress.valueOf(ip);
Sho SHIMIZU91916b62015-08-26 11:27:43 -0700113 long portL = Long.parseLong(port);
samuelbc087c02015-07-23 14:11:58 +0800114 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}