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