blob: 859818d6839e6998704b590e241925766ad01750 [file] [log] [blame]
lishuai6c56f5e2015-11-17 16:38:19 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
lishuai6c56f5e2015-11-17 16:38:19 +08003 *
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.vtn.util;
17
jiangruic69a7fd2015-11-19 15:40:01 +080018import java.util.HashMap;
19import java.util.Map;
lishuai6c56f5e2015-11-17 16:38:19 +080020import java.util.Set;
21
22import org.onlab.packet.IpAddress;
jiangruic69a7fd2015-11-19 15:40:01 +080023import org.onosproject.net.DefaultAnnotations;
lishuai6c56f5e2015-11-17 16:38:19 +080024import org.onosproject.net.PortNumber;
25import org.onosproject.net.behaviour.BridgeConfig;
Hyunsun Moon1251e192016-06-07 16:57:05 -070026import org.onosproject.net.behaviour.BridgeDescription;
lishuai6c56f5e2015-11-17 16:38:19 +080027import org.onosproject.net.behaviour.BridgeName;
Hyunsun Moon1251e192016-06-07 16:57:05 -070028import org.onosproject.net.behaviour.DefaultBridgeDescription;
lishuai6c56f5e2015-11-17 16:38:19 +080029import org.onosproject.net.behaviour.DefaultTunnelDescription;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070030import org.onosproject.net.behaviour.InterfaceConfig;
lishuai6c56f5e2015-11-17 16:38:19 +080031import org.onosproject.net.behaviour.TunnelDescription;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070032import org.onosproject.net.behaviour.TunnelEndPoints;
33import org.onosproject.net.behaviour.TunnelKeys;
lishuai6c56f5e2015-11-17 16:38:19 +080034import org.onosproject.net.driver.DriverHandler;
35
36/**
37 * Applies configuration to the device.
38 */
39public final class VtnConfig {
40
lishuai70304d12015-12-14 17:15:26 +080041 public static final String DEFAULT_BRIDGE_NAME = "br-int";
jiangruic69a7fd2015-11-19 15:40:01 +080042 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 Manda8db7d092016-06-04 00:17:24 +053047 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");
jiangruic69a7fd2015-11-19 15:40:01 +080061 }
62 };
lishuai6c56f5e2015-11-17 16:38:19 +080063 /**
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 Moon1251e192016-06-07 16:57:05 -070080 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);
lishuai6c56f5e2015-11-17 16:38:19 +080090 }
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
lishuai6c56f5e2015-11-17 16:38:19 +080097 */
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070098 public static void applyTunnelConfig(DriverHandler handler, IpAddress srcIp) {
jiangruic69a7fd2015-11-19 15:40:01 +080099 DefaultAnnotations.Builder optionBuilder = DefaultAnnotations.builder();
100 for (String key : DEFAULT_TUNNEL_OPTIONS.keySet()) {
101 optionBuilder.set(key, DEFAULT_TUNNEL_OPTIONS.get(key));
102 }
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700103
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);
lishuai6c56f5e2015-11-17 16:38:19 +0800115 }
116
117 /**
118 * Creates or update tunnel in the controller device.
119 *
120 * @param handler DriverHandler
lishuai6c56f5e2015-11-17 16:38:19 +0800121 */
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700122 public static void removeTunnelConfig(DriverHandler handler) {
123 InterfaceConfig interfaceConfig = handler.behaviour(InterfaceConfig.class);
124 interfaceConfig.removeTunnelMode(DEFAULT_TUNNEL);
lishuai6c56f5e2015-11-17 16:38:19 +0800125 }
126
127 /**
128 * Gets ports in the controller device.
129 *
130 * @param handler DriverHandler
Bharat saraswalbedd6ef2015-12-01 01:42:19 +0530131 * @return set of port numbers
lishuai6c56f5e2015-11-17 16:38:19 +0800132 */
133 public static Set<PortNumber> getPortNumbers(DriverHandler handler) {
134 BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class);
135 return bridgeConfig.getPortNumbers();
136 }
137
138}