blob: e4b0767c75a339eb6c2cf3a68f22669df4a29dce [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");
Phaneendra Manda8db7d092016-06-04 00:17:24 +053048 put("dst_port", "4790");
49 put("in_nsi", "flow");
50 put("in_nsp", "flow");
51 put("out_nsi", "flow");
52 put("out_nsp", "flow");
53 put("in_nshc1", "flow");
54 put("out_nshc1", "flow");
55 put("in_nshc2", "flow");
56 put("out_nshc2", "flow");
57 put("in_nshc3", "flow");
58 put("out_nshc3", "flow");
59 put("in_nshc4", "flow");
60 put("out_nshc4", "flow");
61 put("exts", "gpe");
jiangruic69a7fd2015-11-19 15:40:01 +080062 }
63 };
lishuai6c56f5e2015-11-17 16:38:19 +080064 /**
65 * Constructs a vtn config object. Utility classes should not have a
66 * public or default constructor, otherwise IDE will compile unsuccessfully. This
67 * class should not be instantiated.
68 */
69 private VtnConfig() {
70 }
71
72 /**
73 * Creates or update bridge in the controller device.
74 *
75 * @param handler DriverHandler
76 * @param dpid datapath id
77 * @param exPortName external port name
78 */
79 public static void applyBridgeConfig(DriverHandler handler, String dpid, String exPortName) {
80 BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class);
Hyunsun Moon1251e192016-06-07 16:57:05 -070081 BridgeDescription bridgeDesc = DefaultBridgeDescription.builder()
82 .name(DEFAULT_BRIDGE_NAME)
83 .failMode(BridgeDescription.FailMode.SECURE)
84 .datapathId(dpid)
85 .disableInBand()
86 .enableLocalController()
87 .build();
88
89 bridgeConfig.addBridge(bridgeDesc);
90 bridgeConfig.addPort(BridgeName.bridgeName(DEFAULT_BRIDGE_NAME), exPortName);
lishuai6c56f5e2015-11-17 16:38:19 +080091 }
92
93 /**
94 * Creates or update tunnel in the controller device.
95 *
96 * @param handler DriverHandler
97 * @param srcIp the ipAddress of the local controller device
98 * @param dstIp the ipAddress of the remote controller device
99 */
100 public static void applyTunnelConfig(DriverHandler handler, IpAddress srcIp,
101 IpAddress dstIp) {
jiangruic69a7fd2015-11-19 15:40:01 +0800102 DefaultAnnotations.Builder optionBuilder = DefaultAnnotations.builder();
103 for (String key : DEFAULT_TUNNEL_OPTIONS.keySet()) {
104 optionBuilder.set(key, DEFAULT_TUNNEL_OPTIONS.get(key));
105 }
lishuai6c56f5e2015-11-17 16:38:19 +0800106 TunnelConfig tunnelConfig = handler.behaviour(TunnelConfig.class);
107 TunnelEndPoint tunnelAsSrc = IpTunnelEndPoint.ipTunnelPoint(srcIp);
lishuai6c56f5e2015-11-17 16:38:19 +0800108 TunnelDescription tunnel = new DefaultTunnelDescription(
109 tunnelAsSrc,
jiangruic69a7fd2015-11-19 15:40:01 +0800110 null,
lishuai6c56f5e2015-11-17 16:38:19 +0800111 TunnelDescription.Type.VXLAN,
jiangruic69a7fd2015-11-19 15:40:01 +0800112 TunnelName.tunnelName(DEFAULT_TUNNEL),
113 optionBuilder.build());
114 tunnelConfig.createTunnelInterface(BridgeName.bridgeName(DEFAULT_BRIDGE_NAME), tunnel);
lishuai6c56f5e2015-11-17 16:38:19 +0800115 }
116
117 /**
118 * Creates or update tunnel in the controller device.
119 *
120 * @param handler DriverHandler
121 * @param srcIp the ipAddress of the local controller device
122 * @param dstIp the ipAddress of the remote controller device
123 */
124 public static void removeTunnelConfig(DriverHandler handler, IpAddress srcIp,
125 IpAddress dstIp) {
126 TunnelConfig tunnelConfig = handler.behaviour(TunnelConfig.class);
127 TunnelEndPoint tunnelAsSrc = IpTunnelEndPoint.ipTunnelPoint(srcIp);
128 TunnelEndPoint tunnelAsDst = IpTunnelEndPoint.ipTunnelPoint(dstIp);
129 TunnelDescription tunnel = new DefaultTunnelDescription(
130 tunnelAsSrc,
131 tunnelAsDst,
132 TunnelDescription.Type.VXLAN,
133 null);
134 tunnelConfig.removeTunnel(tunnel);
135 }
136
137 /**
138 * Gets ports in the controller device.
139 *
140 * @param handler DriverHandler
Bharat saraswalbedd6ef2015-12-01 01:42:19 +0530141 * @return set of port numbers
lishuai6c56f5e2015-11-17 16:38:19 +0800142 */
143 public static Set<PortNumber> getPortNumbers(DriverHandler handler) {
144 BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class);
145 return bridgeConfig.getPortNumbers();
146 }
147
148}