blob: 7301a46e2e53651a66a276322e93688980858f42 [file] [log] [blame]
lishuai6c56f5e2015-11-17 16:38:19 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
30import org.onosproject.net.behaviour.IpTunnelEndPoint;
31import org.onosproject.net.behaviour.TunnelConfig;
32import org.onosproject.net.behaviour.TunnelDescription;
33import org.onosproject.net.behaviour.TunnelEndPoint;
jiangruic69a7fd2015-11-19 15:40:01 +080034import org.onosproject.net.behaviour.TunnelName;
lishuai6c56f5e2015-11-17 16:38:19 +080035import org.onosproject.net.driver.DriverHandler;
36
37/**
38 * Applies configuration to the device.
39 */
40public final class VtnConfig {
41
lishuai70304d12015-12-14 17:15:26 +080042 public static final String DEFAULT_BRIDGE_NAME = "br-int";
jiangruic69a7fd2015-11-19 15:40:01 +080043 private static final String DEFAULT_TUNNEL = "vxlan-0.0.0.0";
44 private static final Map<String, String> DEFAULT_TUNNEL_OPTIONS = new HashMap<String, String>() {
45 {
46 put("key", "flow");
47 put("remote_ip", "flow");
48 }
49 };
lishuai6c56f5e2015-11-17 16:38:19 +080050 /**
51 * Constructs a vtn config object. Utility classes should not have a
52 * public or default constructor, otherwise IDE will compile unsuccessfully. This
53 * class should not be instantiated.
54 */
55 private VtnConfig() {
56 }
57
58 /**
59 * Creates or update bridge in the controller device.
60 *
61 * @param handler DriverHandler
62 * @param dpid datapath id
63 * @param exPortName external port name
64 */
65 public static void applyBridgeConfig(DriverHandler handler, String dpid, String exPortName) {
66 BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class);
Hyunsun Moon1251e192016-06-07 16:57:05 -070067 BridgeDescription bridgeDesc = DefaultBridgeDescription.builder()
68 .name(DEFAULT_BRIDGE_NAME)
69 .failMode(BridgeDescription.FailMode.SECURE)
70 .datapathId(dpid)
71 .disableInBand()
72 .enableLocalController()
73 .build();
74
75 bridgeConfig.addBridge(bridgeDesc);
76 bridgeConfig.addPort(BridgeName.bridgeName(DEFAULT_BRIDGE_NAME), exPortName);
lishuai6c56f5e2015-11-17 16:38:19 +080077 }
78
79 /**
80 * Creates or update tunnel in the controller device.
81 *
82 * @param handler DriverHandler
83 * @param srcIp the ipAddress of the local controller device
84 * @param dstIp the ipAddress of the remote controller device
85 */
86 public static void applyTunnelConfig(DriverHandler handler, IpAddress srcIp,
87 IpAddress dstIp) {
jiangruic69a7fd2015-11-19 15:40:01 +080088 DefaultAnnotations.Builder optionBuilder = DefaultAnnotations.builder();
89 for (String key : DEFAULT_TUNNEL_OPTIONS.keySet()) {
90 optionBuilder.set(key, DEFAULT_TUNNEL_OPTIONS.get(key));
91 }
lishuai6c56f5e2015-11-17 16:38:19 +080092 TunnelConfig tunnelConfig = handler.behaviour(TunnelConfig.class);
93 TunnelEndPoint tunnelAsSrc = IpTunnelEndPoint.ipTunnelPoint(srcIp);
lishuai6c56f5e2015-11-17 16:38:19 +080094 TunnelDescription tunnel = new DefaultTunnelDescription(
95 tunnelAsSrc,
jiangruic69a7fd2015-11-19 15:40:01 +080096 null,
lishuai6c56f5e2015-11-17 16:38:19 +080097 TunnelDescription.Type.VXLAN,
jiangruic69a7fd2015-11-19 15:40:01 +080098 TunnelName.tunnelName(DEFAULT_TUNNEL),
99 optionBuilder.build());
100 tunnelConfig.createTunnelInterface(BridgeName.bridgeName(DEFAULT_BRIDGE_NAME), tunnel);
lishuai6c56f5e2015-11-17 16:38:19 +0800101 }
102
103 /**
104 * Creates or update tunnel in the controller device.
105 *
106 * @param handler DriverHandler
107 * @param srcIp the ipAddress of the local controller device
108 * @param dstIp the ipAddress of the remote controller device
109 */
110 public static void removeTunnelConfig(DriverHandler handler, IpAddress srcIp,
111 IpAddress dstIp) {
112 TunnelConfig tunnelConfig = handler.behaviour(TunnelConfig.class);
113 TunnelEndPoint tunnelAsSrc = IpTunnelEndPoint.ipTunnelPoint(srcIp);
114 TunnelEndPoint tunnelAsDst = IpTunnelEndPoint.ipTunnelPoint(dstIp);
115 TunnelDescription tunnel = new DefaultTunnelDescription(
116 tunnelAsSrc,
117 tunnelAsDst,
118 TunnelDescription.Type.VXLAN,
119 null);
120 tunnelConfig.removeTunnel(tunnel);
121 }
122
123 /**
124 * Gets ports in the controller device.
125 *
126 * @param handler DriverHandler
Bharat saraswalbedd6ef2015-12-01 01:42:19 +0530127 * @return set of port numbers
lishuai6c56f5e2015-11-17 16:38:19 +0800128 */
129 public static Set<PortNumber> getPortNumbers(DriverHandler handler) {
130 BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class);
131 return bridgeConfig.getPortNumbers();
132 }
133
134}