samuel
Committed by Gerrit Code Review

[onos-2242]OVSDB-based implementation of tunnel config behaviour.

Change-Id: I854a9e572acbfa698ec40309945d93810fb2b471
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 +
17 +package org.onosproject.net.behaviour;
18 +
19 +import java.util.Objects;
20 +
21 +import com.google.common.annotations.Beta;
22 +import org.onlab.packet.IpAddress;
23 +
24 +import com.google.common.base.MoreObjects;
25 +
26 +/**
27 + * Represent for a tunnel point using ip address.
28 + */
29 +@Beta
30 +public final class IpTunnelEndPoint implements TunnelEndPoint {
31 +
32 + private final IpAddress ip;
33 +
34 + /**
35 + * Public construction is prohibited.
36 + * @param ip ip address
37 + */
38 + private IpTunnelEndPoint(IpAddress ip) {
39 + this.ip = ip;
40 + }
41 +
42 + /**
43 + * Create a IP tunnel end point.
44 + * @param ip IP address
45 + * @return IpTunnelEndPoint
46 + */
47 + public static IpTunnelEndPoint ipTunnelPoint(IpAddress ip) {
48 + return new IpTunnelEndPoint(ip);
49 + }
50 +
51 + /**
52 + * Returns IP address.
53 + * @return IP address
54 + */
55 + public IpAddress ip() {
56 + return ip;
57 + }
58 +
59 + @Override
60 + public int hashCode() {
61 + return Objects.hash(ip);
62 + }
63 +
64 + @Override
65 + public boolean equals(Object obj) {
66 + if (this == obj) {
67 + return true;
68 + }
69 + if (obj instanceof IpTunnelEndPoint) {
70 + final IpTunnelEndPoint other = (IpTunnelEndPoint) obj;
71 + return Objects.equals(this.ip, other.ip);
72 + }
73 + return false;
74 + }
75 +
76 + @Override
77 + public String toString() {
78 + return MoreObjects.toStringHelper(getClass()).add("ip", ip).toString();
79 + }
80 +}
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 +
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 +}