blob: 0b3a99a2eef0de595b5795a59a9b8a001348e156 [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;
samuelbc087c02015-07-23 14:11:58 +080019import java.util.Set;
Sho SHIMIZU3be9fbe2015-08-26 14:36:21 -070020import java.util.stream.Collectors;
samuelbc087c02015-07-23 14:11:58 +080021
22import org.onlab.packet.IpAddress;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.behaviour.DefaultTunnelDescription;
25import org.onosproject.net.behaviour.IpTunnelEndPoint;
26import org.onosproject.net.behaviour.TunnelConfig;
27import org.onosproject.net.behaviour.TunnelDescription;
samuelbc087c02015-07-23 14:11:58 +080028import org.onosproject.net.behaviour.TunnelName;
29import org.onosproject.net.driver.AbstractHandlerBehaviour;
30import org.onosproject.net.driver.DriverHandler;
31import org.onosproject.ovsdb.controller.OvsdbClientService;
32import org.onosproject.ovsdb.controller.OvsdbController;
33import org.onosproject.ovsdb.controller.OvsdbNodeId;
34import org.onosproject.ovsdb.controller.OvsdbTunnel;
35
samuelbc087c02015-07-23 14:11:58 +080036/**
37 * OVSDB-based implementation of tunnel config behaviour.
38 */
39public class OvsdbTunnelConfig extends AbstractHandlerBehaviour
40 implements TunnelConfig {
41
samuel2a31ea52015-08-05 18:41:47 +080042 private static final String DEFAULT_ADDRESS = "0.0.0.0";
samuelbc087c02015-07-23 14:11:58 +080043
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 SHIMIZU3be9fbe2015-08-26 14:36:21 -070090 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());
samuelbc087c02015-07-23 14:11:58 +0800102 }
103
jiangrui866c2f22015-10-28 18:58:34 +0800104 // OvsdbNodeId(IP) is used in the adaptor while DeviceId(ovsdb:IP)
samuelbc087c02015-07-23 14:11:58 +0800105 // is used in the core. So DeviceId need be changed to OvsdbNodeId.
106 private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) {
jiangrui866c2f22015-10-28 18:58:34 +0800107 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);
samuelbc087c02015-07-23 14:11:58 +0800113 }
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}