blob: e07bd53554aab43fe8d17a51718d548bd35e7c1a [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;
21
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;
28import org.onosproject.net.behaviour.TunnelEndPoint;
29import 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
37import com.google.common.collect.Sets;
38
39/**
40 * OVSDB-based implementation of tunnel config behaviour.
41 */
42public class OvsdbTunnelConfig extends AbstractHandlerBehaviour
43 implements TunnelConfig {
44
45 private static final String DEFAULT_ADDRESS = "0:0:0:0";
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(":");
114 String ip = deviceId.toString().substring(fistColon + 1, lastColon - 1);
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}