blob: 33417796e5f240b134e1efcc0f03c61dc8f5eac5 [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;
26import org.onosproject.net.behaviour.BridgeName;
27import org.onosproject.net.behaviour.DefaultTunnelDescription;
28import org.onosproject.net.behaviour.IpTunnelEndPoint;
29import org.onosproject.net.behaviour.TunnelConfig;
30import org.onosproject.net.behaviour.TunnelDescription;
31import org.onosproject.net.behaviour.TunnelEndPoint;
jiangruic69a7fd2015-11-19 15:40:01 +080032import org.onosproject.net.behaviour.TunnelName;
lishuai6c56f5e2015-11-17 16:38:19 +080033import org.onosproject.net.driver.DriverHandler;
34
35/**
36 * Applies configuration to the device.
37 */
38public final class VtnConfig {
39
lishuai70304d12015-12-14 17:15:26 +080040 public static final String DEFAULT_BRIDGE_NAME = "br-int";
jiangruic69a7fd2015-11-19 15:40:01 +080041 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 };
lishuai6c56f5e2015-11-17 16:38:19 +080048 /**
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) {
jiangruic69a7fd2015-11-19 15:40:01 +080077 DefaultAnnotations.Builder optionBuilder = DefaultAnnotations.builder();
78 for (String key : DEFAULT_TUNNEL_OPTIONS.keySet()) {
79 optionBuilder.set(key, DEFAULT_TUNNEL_OPTIONS.get(key));
80 }
lishuai6c56f5e2015-11-17 16:38:19 +080081 TunnelConfig tunnelConfig = handler.behaviour(TunnelConfig.class);
82 TunnelEndPoint tunnelAsSrc = IpTunnelEndPoint.ipTunnelPoint(srcIp);
lishuai6c56f5e2015-11-17 16:38:19 +080083 TunnelDescription tunnel = new DefaultTunnelDescription(
84 tunnelAsSrc,
jiangruic69a7fd2015-11-19 15:40:01 +080085 null,
lishuai6c56f5e2015-11-17 16:38:19 +080086 TunnelDescription.Type.VXLAN,
jiangruic69a7fd2015-11-19 15:40:01 +080087 TunnelName.tunnelName(DEFAULT_TUNNEL),
88 optionBuilder.build());
89 tunnelConfig.createTunnelInterface(BridgeName.bridgeName(DEFAULT_BRIDGE_NAME), tunnel);
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
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
Bharat saraswalbedd6ef2015-12-01 01:42:19 +0530116 * @return set of port numbers
lishuai6c56f5e2015-11-17 16:38:19 +0800117 */
118 public static Set<PortNumber> getPortNumbers(DriverHandler handler) {
119 BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class);
120 return bridgeConfig.getPortNumbers();
121 }
122
123}