lishuai | 6c56f5e | 2015-11-17 16:38:19 +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.vtn.util; |
| 17 | |
| 18 | import java.util.Set; |
| 19 | |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onosproject.net.PortNumber; |
| 22 | import org.onosproject.net.behaviour.BridgeConfig; |
| 23 | import org.onosproject.net.behaviour.BridgeName; |
| 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.driver.DriverHandler; |
| 30 | |
| 31 | /** |
| 32 | * Applies configuration to the device. |
| 33 | */ |
| 34 | public final class VtnConfig { |
| 35 | |
| 36 | private static final String DEFAULT_BRIDGE_NAME = "br-int"; |
| 37 | |
| 38 | /** |
| 39 | * Constructs a vtn config object. Utility classes should not have a |
| 40 | * public or default constructor, otherwise IDE will compile unsuccessfully. This |
| 41 | * class should not be instantiated. |
| 42 | */ |
| 43 | private VtnConfig() { |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Creates or update bridge in the controller device. |
| 48 | * |
| 49 | * @param handler DriverHandler |
| 50 | * @param dpid datapath id |
| 51 | * @param exPortName external port name |
| 52 | */ |
| 53 | public static void applyBridgeConfig(DriverHandler handler, String dpid, String exPortName) { |
| 54 | BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class); |
| 55 | bridgeConfig.addBridge(BridgeName.bridgeName(DEFAULT_BRIDGE_NAME), dpid, exPortName); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Creates or update tunnel in the controller device. |
| 60 | * |
| 61 | * @param handler DriverHandler |
| 62 | * @param srcIp the ipAddress of the local controller device |
| 63 | * @param dstIp the ipAddress of the remote controller device |
| 64 | */ |
| 65 | public static void applyTunnelConfig(DriverHandler handler, IpAddress srcIp, |
| 66 | IpAddress dstIp) { |
| 67 | TunnelConfig tunnelConfig = handler.behaviour(TunnelConfig.class); |
| 68 | TunnelEndPoint tunnelAsSrc = IpTunnelEndPoint.ipTunnelPoint(srcIp); |
| 69 | TunnelEndPoint tunnelAsDst = IpTunnelEndPoint.ipTunnelPoint(dstIp); |
| 70 | TunnelDescription tunnel = new DefaultTunnelDescription( |
| 71 | tunnelAsSrc, |
| 72 | tunnelAsDst, |
| 73 | TunnelDescription.Type.VXLAN, |
| 74 | null); |
| 75 | tunnelConfig.createTunnel(tunnel); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Creates or update tunnel in the controller device. |
| 80 | * |
| 81 | * @param handler DriverHandler |
| 82 | * @param srcIp the ipAddress of the local controller device |
| 83 | * @param dstIp the ipAddress of the remote controller device |
| 84 | */ |
| 85 | public static void removeTunnelConfig(DriverHandler handler, IpAddress srcIp, |
| 86 | IpAddress dstIp) { |
| 87 | TunnelConfig tunnelConfig = handler.behaviour(TunnelConfig.class); |
| 88 | TunnelEndPoint tunnelAsSrc = IpTunnelEndPoint.ipTunnelPoint(srcIp); |
| 89 | TunnelEndPoint tunnelAsDst = IpTunnelEndPoint.ipTunnelPoint(dstIp); |
| 90 | TunnelDescription tunnel = new DefaultTunnelDescription( |
| 91 | tunnelAsSrc, |
| 92 | tunnelAsDst, |
| 93 | TunnelDescription.Type.VXLAN, |
| 94 | null); |
| 95 | tunnelConfig.removeTunnel(tunnel); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Gets ports in the controller device. |
| 100 | * |
| 101 | * @param handler DriverHandler |
| 102 | */ |
| 103 | public static Set<PortNumber> getPortNumbers(DriverHandler handler) { |
| 104 | BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class); |
| 105 | return bridgeConfig.getPortNumbers(); |
| 106 | } |
| 107 | |
| 108 | } |