1 package org.opendaylight.ovsdb.neutron;
3 import java.util.ArrayList;
4 import java.util.Collections;
9 import org.opendaylight.controller.forwardingrulesmanager.FlowConfig;
10 import org.opendaylight.controller.forwardingrulesmanager.IForwardingRulesManager;
11 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
12 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
13 import org.opendaylight.controller.sal.action.ActionType;
14 import org.opendaylight.controller.sal.core.Node;
15 import org.opendaylight.controller.sal.utils.EtherTypes;
16 import org.opendaylight.controller.sal.utils.HexEncode;
17 import org.opendaylight.controller.sal.utils.ServiceHelper;
18 import org.opendaylight.controller.sal.utils.Status;
19 import org.opendaylight.controller.sal.utils.StatusCode;
20 import org.opendaylight.ovsdb.lib.notation.OvsDBMap;
21 import org.opendaylight.ovsdb.lib.notation.OvsDBSet;
22 import org.opendaylight.ovsdb.lib.notation.UUID;
23 import org.opendaylight.ovsdb.lib.table.Bridge;
24 import org.opendaylight.ovsdb.lib.table.Interface;
25 import org.opendaylight.ovsdb.lib.table.Port;
26 import org.opendaylight.ovsdb.lib.table.internal.Table;
27 import org.opendaylight.ovsdb.plugin.IConnectionServiceInternal;
28 import org.opendaylight.ovsdb.plugin.OVSDBConfigService;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * OpenStack Neutron with the OpenVswitch data plan relies on a typical OVS bridge configurations that
34 * consists of br-int (Integration Bridge), br-tun (Tunnel bridge), br-ex (External bridge).
36 * In DevStack like setups, the br-tun is not automatically created on the controller nodes.
37 * Hence this class attempts to bring all the nodes to be elibible for OpenStack operations.
40 public class InternalNetworkManager {
41 static final Logger logger = LoggerFactory.getLogger(InternalNetworkManager.class);
43 private static InternalNetworkManager internalNetwork = new InternalNetworkManager();
44 private InternalNetworkManager() {
47 public static InternalNetworkManager getManager() {
48 return internalNetwork;
51 public String getInternalBridgeUUID (Node node, String bridgeName) {
53 OVSDBConfigService ovsdbTable = (OVSDBConfigService)ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
54 Map<String, Table<?>> bridgeTable = ovsdbTable.getRows(node, Bridge.NAME.getName());
55 if (bridgeTable == null) return null;
56 for (String key : bridgeTable.keySet()) {
57 Bridge bridge = (Bridge)bridgeTable.get(key);
58 if (bridge.getName().equals(bridgeName)) return key;
60 } catch (Exception e) {
61 logger.error("Error getting Bridge Identifier for {} / {}", node, bridgeName, e);
66 public boolean isInternalNetworkNeutronReady(Node node) throws Exception {
67 if (this.getInternalBridgeUUID(node, AdminConfigManager.getManager().getIntegrationBridgeName()) != null) {
74 public boolean isInternalNetworkOverlayReady(Node node) throws Exception {
75 if (this.getInternalBridgeUUID(node, AdminConfigManager.getManager().getTunnelBridgeName()) != null) {
82 public Status createInternalNetworkForOverlay(Node node) throws Exception {
83 if (!isInternalNetworkNeutronReady(node)) {
84 logger.error("Integration Bridge is not available in Node {}", node);
85 return new Status(StatusCode.NOTACCEPTABLE, "Integration Bridge is not avaialble in Node " + node);
87 if (isInternalNetworkOverlayReady(node)) {
88 logger.error("Network Overlay Bridge is already present in Node {}", node);
89 return new Status(StatusCode.NOTACCEPTABLE, "Network Overlay Bridge is already present in Node " + node);
99 options: {peer=patch-tun}
105 OVSDBConfigService ovsdbTable = (OVSDBConfigService)ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
106 Bridge brTun = new Bridge();
107 brTun.setName(AdminConfigManager.getManager().getTunnelBridgeName());
108 // Create br-tun bridge
109 Status status = ovsdbTable.insertRow(node, Bridge.NAME.getName(), null, brTun);
110 if (!status.isSuccess()) return status;
111 String bridgeUUID = status.getDescription();
113 IConnectionServiceInternal connectionService = (IConnectionServiceInternal)ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
114 connectionService.setOFController(node, bridgeUUID);
116 Port port = new Port();
117 port.setName(brTun.getName());
118 status = ovsdbTable.insertRow(node, Port.NAME.getName(), bridgeUUID, port);
120 String patchInt = AdminConfigManager.getManager().getPatchToIntegration();
121 String patchTun = AdminConfigManager.getManager().getPatchToTunnel();
123 status = addPatchPort(node, bridgeUUID, patchInt, patchTun);
124 if (!status.isSuccess()) return status;
126 // Create the corresponding patch-tun port in br-int
127 Map<String, Table<?>> bridges = ovsdbTable.getRows(node, Bridge.NAME.getName());
128 for (String brIntUUID : bridges.keySet()) {
129 Bridge brInt = (Bridge) bridges.get(brIntUUID);
130 if (brInt.getName().equalsIgnoreCase(AdminConfigManager.getManager().getIntegrationBridgeName())) {
131 return addPatchPort(node, brIntUUID, patchTun, patchInt);
138 private Status addPatchPort (Node node, String bridgeUUID, String portName, String patchName) throws Exception {
139 Status status = null;
140 OVSDBConfigService ovsdbTable = (OVSDBConfigService)ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
142 Port patchPort = new Port();
143 patchPort.setName(portName);
144 // Create patch-int port and interface
145 status = ovsdbTable.insertRow(node, Port.NAME.getName(), bridgeUUID, patchPort);
146 if (!status.isSuccess()) return status;
148 String patchPortUUID = status.getDescription();
150 String interfaceUUID = null;
152 while ((interfaceUUID == null) && (timeout > 0)) {
153 patchPort = (Port)ovsdbTable.getRow(node, Port.NAME.getName(), patchPortUUID);
154 OvsDBSet<UUID> interfaces = patchPort.getInterfaces();
155 if (interfaces == null || interfaces.size() == 0) {
156 // Wait for the OVSDB update to sync up the Local cache.
161 interfaceUUID = interfaces.toArray()[0].toString();
164 if (interfaceUUID == null) {
165 return new Status(StatusCode.INTERNALERROR);
168 Interface tunInterface = new Interface();
169 tunInterface.setType("patch");
170 OvsDBMap<String, String> options = new OvsDBMap<String, String>();
171 options.put("peer", patchName);
172 tunInterface.setOptions(options);
173 status = ovsdbTable.updateRow(node, Interface.NAME.getName(), patchPortUUID, interfaceUUID, tunInterface);
178 private void prepareInternalNetwork (NeutronNetwork network, Node node) {
179 // vlan, vxlan, and gre
180 if (network.getProviderNetworkType().equalsIgnoreCase("gre") ||
181 network.getProviderNetworkType().equalsIgnoreCase("vxlan") ||
182 network.getProviderNetworkType().equalsIgnoreCase("vlan")) {
185 if (!this.isInternalNetworkOverlayReady(node)) {
186 this.createInternalNetworkForOverlay(node);
188 } catch (Exception e) {
193 if (!this.isInternalNetworkNeutronReady(node)) {
195 // this.createInternalNetworkForNeutron(node);
197 } catch (Exception e) {
202 this.initializeOFNormalFlowRules(node, AdminConfigManager.getManager().getIntegrationBridgeName());
203 this.initializeLLDPFlowRules(node, AdminConfigManager.getManager().getTunnelBridgeName());
204 this.initializeLLDPFlowRules(node, AdminConfigManager.getManager().getIntegrationBridgeName());
207 private void initializeOFNormalFlowRules(Node node, String bridgeName) {
208 String brIntId = this.getInternalBridgeUUID(node, bridgeName);
209 if (brIntId == null) {
210 logger.error("Failed to initialize Flow Rules for {}", node);
214 OVSDBConfigService ovsdbTable = (OVSDBConfigService)ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
215 Bridge bridge = (Bridge) ovsdbTable.getRow(node, Bridge.NAME.getName(), brIntId);
216 Set<String> dpids = bridge.getDatapath_id();
217 if (dpids == null || dpids.size() == 0) return;
218 Long dpidLong = Long.valueOf(HexEncode.stringToLong((String)dpids.toArray()[0]));
219 Node ofNode = new Node(Node.NodeIDType.OPENFLOW, dpidLong);
220 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper.getInstance(
221 IForwardingRulesManager.class, "default", this);
222 String flowName = ActionType.HW_PATH.toString();
223 if (frm.getStaticFlow(flowName, ofNode) != null) {
224 logger.debug("Static Flow {} already programmed in the node {}", flowName, ofNode);
227 FlowConfig flow = new FlowConfig();
228 flow.setName("IntegrationBridgeNormal");
229 flow.setNode(ofNode);
230 flow.setPriority("1");
231 List<String> normalAction = new ArrayList<String>();
232 normalAction.add(flowName);
233 flow.setActions(normalAction);
234 Status status = frm.addStaticFlow(flow);
235 logger.debug("Flow Programming Status {} for Flow {} on {} / {}", status, flow, ofNode, node);
236 } catch (Exception e) {
237 logger.error("Failed to initialize Flow Rules for {}", node, e);
241 private void initializeLLDPFlowRules(Node node, String bridgeName) {
242 String brIntId = this.getInternalBridgeUUID(node, bridgeName);
243 if (brIntId == null) {
244 logger.error("Failed to initialize Flow Rules for {}", node);
248 OVSDBConfigService ovsdbTable = (OVSDBConfigService)ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
249 Bridge bridge = (Bridge) ovsdbTable.getRow(node, Bridge.NAME.getName(), brIntId);
250 Set<String> dpids = bridge.getDatapath_id();
251 if (dpids == null || dpids.size() == 0) return;
252 Long dpidLong = Long.valueOf(HexEncode.stringToLong((String)dpids.toArray()[0]));
253 Node ofNode = new Node(Node.NodeIDType.OPENFLOW, dpidLong);
254 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper.getInstance(
255 IForwardingRulesManager.class, "default", this);
256 String flowName = "PuntLLDP";
257 if (frm.getStaticFlow(flowName, ofNode) != null) {
258 logger.debug("Static Flow {} already programmed in the node {}", flowName, ofNode);
262 List<String> puntAction = new ArrayList<String>();
263 puntAction.add(ActionType.CONTROLLER.toString());
265 FlowConfig allowLLDP = new FlowConfig();
266 allowLLDP.setInstallInHw(true);
267 allowLLDP.setName(flowName);
268 allowLLDP.setPriority("10");
269 allowLLDP.setNode(ofNode);
270 allowLLDP.setEtherType("0x" + Integer.toHexString(EtherTypes.LLDP.intValue())
272 allowLLDP.setActions(puntAction);
273 Status status = frm.addStaticFlow(allowLLDP);
274 logger.debug("Flow Programming Status {} for Flow {} on {} / {}", status, allowLLDP, ofNode, node);
275 } catch (Exception e) {
276 logger.error("Failed to initialize Flow Rules for {}", node, e);
280 public void prepareInternalNetwork(NeutronNetwork network) {
281 IConnectionServiceInternal connectionService = (IConnectionServiceInternal)ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
282 List<Node> nodes = connectionService.getNodes();
283 for (Node node : nodes) {
284 prepareInternalNetwork(network, node);
288 public void prepareInternalNetwork(Node node) {
289 INeutronNetworkCRUD neutronNetworkService = (INeutronNetworkCRUD)ServiceHelper.getGlobalInstance(INeutronNetworkCRUD.class, this);
290 List <NeutronNetwork> networks = neutronNetworkService.getAllNetworks();
291 for (NeutronNetwork network : networks) {
292 prepareInternalNetwork(network, node);
296 public static List safe( List other ) {
297 return other == null ? Collections.EMPTY_LIST : other;