@Override public String getInterfaceUuid(String portUuid, String portName) { DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); Row portRow = getRow(OvsdbConstant.DATABASENAME, OvsdbConstant.PORT, portUuid); Port port = (Port) TableGenerator.getTable(dbSchema, portRow, OvsdbTable.PORT); if (port != null) { OvsdbSet setInterfaces = (OvsdbSet) port.getInterfacesColumn().data(); @SuppressWarnings("unchecked") Set<UUID> interfaces = setInterfaces.set(); if (interfaces == null || interfaces.size() == 0) { log.warn("The interface uuid is null"); return null; } for (UUID uuid : interfaces) { Row intfRow = getRow(OvsdbConstant.DATABASENAME, OvsdbConstant.INTERFACE, uuid.value()); Interface intf = (Interface) TableGenerator.getTable(dbSchema, intfRow, OvsdbTable.INTERFACE); if (intf != null && portName.equalsIgnoreCase(intf.getName())) { return uuid.value(); } } } return null; }
@Override public String getPortUuid(String portName, String bridgeUuid) { DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); Row bridgeRow = getRow(OvsdbConstant.DATABASENAME, OvsdbConstant.BRIDGE, bridgeUuid); Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE); if (bridge != null) { OvsdbSet setPorts = (OvsdbSet) bridge.getPortsColumn().data(); @SuppressWarnings("unchecked") Set<UUID> ports = setPorts.set(); if (ports == null || ports.size() == 0) { log.warn("The port uuid is null"); return null; } for (UUID uuid : ports) { Row portRow = getRow(OvsdbConstant.DATABASENAME, OvsdbConstant.PORT, uuid.value()); Port port = (Port) TableGenerator.getTable(dbSchema, portRow, OvsdbTable.PORT); if (port != null && portName.equalsIgnoreCase(port.getName())) { return uuid.value(); } } } return null; }
@Override public void createPort(String bridgeName, String portName) { String bridgeUuid = getBridgeUuid(bridgeName); if (bridgeUuid == null) { log.error("Can't find bridge {} in {}", bridgeName, nodeId.getIpAddress()); return; } DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); String portUuid = getPortUuid(portName, bridgeUuid); Port port = (Port) TableGenerator.createTable(dbSchema, OvsdbTable.PORT); port.setName(portName); if (portUuid == null) { insertConfig( OvsdbConstant.PORT, "_uuid", OvsdbConstant.BRIDGE, "ports", bridgeUuid, port.getRow()); } else { updateConfig(OvsdbConstant.PORT, "_uuid", portUuid, port.getRow()); } return; }
@Override public void createTunnel(IpAddress srcIp, IpAddress dstIp) { String bridgeUuid = getBridgeUuid(OvsdbConstant.INTEGRATION_BRIDGE); if (bridgeUuid == null) { log.warn( "Could not find bridge {} and Could not create tunnel. ", OvsdbConstant.INTEGRATION_BRIDGE); return; } DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); String portName = getTunnelName(OvsdbConstant.TYPEVXLAN, dstIp); String portUuid = getPortUuid(portName, bridgeUuid); Port port = (Port) TableGenerator.createTable(dbSchema, OvsdbTable.PORT); if (port != null) { port.setName(portName); } if (portUuid == null) { portUuid = insertConfig( OvsdbConstant.PORT, "_uuid", OvsdbConstant.BRIDGE, "ports", bridgeUuid, port.getRow()); } else { updateConfig(OvsdbConstant.PORT, "_uuid", portUuid, port.getRow()); } // When a tunnel is created, A row is inserted into port table and // interface table of the ovsdb node. // and the following step is to get the interface uuid from local store // in controller node. // but it need spend some time synchronising data between node and // controller. // so loop to judge if interfaceUUid is null is necessary. String interfaceUuid = null; for (int i = 0; i < 10; i++) { interfaceUuid = getInterfaceUuid(portUuid, portName); if (interfaceUuid == null) { try { Thread.sleep(500); } catch (InterruptedException e) { log.warn("Interrupted while waiting to get interfaceUuid"); Thread.currentThread().interrupt(); } } else { break; } } if (interfaceUuid != null) { Interface tunInterface = (Interface) TableGenerator.createTable(dbSchema, OvsdbTable.INTERFACE); if (tunInterface != null) { tunInterface.setType(OvsdbConstant.TYPEVXLAN); Map<String, String> options = Maps.newHashMap(); options.put("key", "flow"); options.put("local_ip", srcIp.toString()); options.put("remote_ip", dstIp.toString()); tunInterface.setOptions(options); updateConfig(OvsdbConstant.INTERFACE, "_uuid", interfaceUuid, tunInterface.getRow()); log.info("Tunnel added success", tunInterface); } } return; }
@Override public void createBridge(String bridgeName) { log.debug("create bridge {}", bridgeName); DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); if (dbSchema == null) { log.warn("The schema is null"); return; } Bridge bridge = (Bridge) TableGenerator.createTable(dbSchema, OvsdbTable.BRIDGE); if (bridge == null) { log.debug("Can not create bridge"); return; } Set<String> failModes = new HashSet<>(); failModes.add("secure"); bridge.setFailMode(failModes); Set<String> protocols = new HashSet<>(); protocols.add(OvsdbConstant.OPENFLOW13); bridge.setProtocols(protocols); String ovsUuid = getOvsUuid(OvsdbConstant.DATABASENAME); if (ovsUuid == null) { log.warn("The Open_vSwitch is null"); return; } String bridgeUuid = getBridgeUuid(bridgeName); if (bridgeUuid == null) { log.debug("Create a new bridge"); bridge.setName(bridgeName); bridgeUuid = insertConfig( OvsdbConstant.BRIDGE, "_uuid", OvsdbConstant.DATABASENAME, "bridges", ovsUuid, bridge.getRow()); if (bridgeUuid != null) { Port port = (Port) TableGenerator.createTable(dbSchema, OvsdbTable.PORT); if (port != null) { log.debug("the port is not null"); port.setName(bridgeName); insertConfig(OvsdbConstant.PORT, "_uuid", "Bridge", "ports", bridgeUuid, port.getRow()); } } } else { log.info("Update a bridge"); updateConfig(OvsdbConstant.BRIDGE, "_uuid", bridgeUuid, bridge.getRow()); } setController(bridgeUuid); log.info("Create bridge success"); }