lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 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; |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 26 | import org.onosproject.net.behaviour.BridgeDescription; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 27 | import org.onosproject.net.behaviour.BridgeName; |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 28 | import org.onosproject.net.behaviour.DefaultBridgeDescription; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 29 | import org.onosproject.net.behaviour.DefaultTunnelDescription; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 30 | import org.onosproject.net.behaviour.InterfaceConfig; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 31 | import org.onosproject.net.behaviour.TunnelDescription; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 32 | import org.onosproject.net.behaviour.TunnelEndPoints; |
| 33 | import org.onosproject.net.behaviour.TunnelKeys; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 34 | import org.onosproject.net.driver.DriverHandler; |
| 35 | |
| 36 | /** |
| 37 | * Applies configuration to the device. |
| 38 | */ |
| 39 | public final class VtnConfig { |
| 40 | |
lishuai | 70304d1 | 2015-12-14 17:15:26 +0800 | [diff] [blame] | 41 | public static final String DEFAULT_BRIDGE_NAME = "br-int"; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 42 | private static final String DEFAULT_TUNNEL = "vxlan-0.0.0.0"; |
| 43 | private static final Map<String, String> DEFAULT_TUNNEL_OPTIONS = new HashMap<String, String>() { |
| 44 | { |
| 45 | put("key", "flow"); |
| 46 | put("remote_ip", "flow"); |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 47 | put("dst_port", "4790"); |
| 48 | put("in_nsi", "flow"); |
| 49 | put("in_nsp", "flow"); |
| 50 | put("out_nsi", "flow"); |
| 51 | put("out_nsp", "flow"); |
| 52 | put("in_nshc1", "flow"); |
| 53 | put("out_nshc1", "flow"); |
| 54 | put("in_nshc2", "flow"); |
| 55 | put("out_nshc2", "flow"); |
| 56 | put("in_nshc3", "flow"); |
| 57 | put("out_nshc3", "flow"); |
| 58 | put("in_nshc4", "flow"); |
| 59 | put("out_nshc4", "flow"); |
| 60 | put("exts", "gpe"); |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 61 | } |
| 62 | }; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 63 | /** |
| 64 | * Constructs a vtn config object. Utility classes should not have a |
| 65 | * public or default constructor, otherwise IDE will compile unsuccessfully. This |
| 66 | * class should not be instantiated. |
| 67 | */ |
| 68 | private VtnConfig() { |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Creates or update bridge in the controller device. |
| 73 | * |
| 74 | * @param handler DriverHandler |
| 75 | * @param dpid datapath id |
| 76 | * @param exPortName external port name |
| 77 | */ |
| 78 | public static void applyBridgeConfig(DriverHandler handler, String dpid, String exPortName) { |
| 79 | BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 80 | BridgeDescription bridgeDesc = DefaultBridgeDescription.builder() |
| 81 | .name(DEFAULT_BRIDGE_NAME) |
| 82 | .failMode(BridgeDescription.FailMode.SECURE) |
| 83 | .datapathId(dpid) |
| 84 | .disableInBand() |
| 85 | .enableLocalController() |
| 86 | .build(); |
| 87 | |
| 88 | bridgeConfig.addBridge(bridgeDesc); |
| 89 | bridgeConfig.addPort(BridgeName.bridgeName(DEFAULT_BRIDGE_NAME), exPortName); |
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 |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 97 | */ |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 98 | public static void applyTunnelConfig(DriverHandler handler, IpAddress srcIp) { |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 99 | DefaultAnnotations.Builder optionBuilder = DefaultAnnotations.builder(); |
| 100 | for (String key : DEFAULT_TUNNEL_OPTIONS.keySet()) { |
| 101 | optionBuilder.set(key, DEFAULT_TUNNEL_OPTIONS.get(key)); |
| 102 | } |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 103 | |
| 104 | InterfaceConfig interfaceConfig = handler.behaviour(InterfaceConfig.class); |
| 105 | TunnelDescription tunnel = DefaultTunnelDescription.builder() |
| 106 | .deviceId(DEFAULT_BRIDGE_NAME) |
| 107 | .ifaceName(DEFAULT_TUNNEL) |
| 108 | .type(TunnelDescription.Type.VXLAN) |
| 109 | .local(TunnelEndPoints.ipTunnelEndpoint(srcIp)) |
| 110 | .remote(TunnelEndPoints.flowTunnelEndpoint()) |
| 111 | .key(TunnelKeys.flowTunnelKey()) |
| 112 | .otherConfigs(optionBuilder.build()) |
| 113 | .build(); |
| 114 | interfaceConfig.addTunnelMode(DEFAULT_TUNNEL, tunnel); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Creates or update tunnel in the controller device. |
| 119 | * |
| 120 | * @param handler DriverHandler |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 121 | */ |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 122 | public static void removeTunnelConfig(DriverHandler handler) { |
| 123 | InterfaceConfig interfaceConfig = handler.behaviour(InterfaceConfig.class); |
| 124 | interfaceConfig.removeTunnelMode(DEFAULT_TUNNEL); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Gets ports in the controller device. |
| 129 | * |
| 130 | * @param handler DriverHandler |
Bharat saraswal | bedd6ef | 2015-12-01 01:42:19 +0530 | [diff] [blame] | 131 | * @return set of port numbers |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 132 | */ |
| 133 | public static Set<PortNumber> getPortNumbers(DriverHandler handler) { |
| 134 | BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class); |
| 135 | return bridgeConfig.getPortNumbers(); |
| 136 | } |
| 137 | |
| 138 | } |