/** * Sets the Controller. * * @param bridgeUuid bridge uuid */ private void setController(String bridgeUuid) { String controllerUuid = null; String iPAddress = IpAddress.valueOf( ((InetSocketAddress) channel.localAddress()).getAddress().getHostAddress()) .toString(); String target = "tcp:" + iPAddress + ":" + OvsdbConstant.OFPORT; log.debug("controller IP {}: port {}", iPAddress, OvsdbConstant.OFPORT); DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); Controller controller = (Controller) TableGenerator.createTable(dbSchema, OvsdbTable.CONTROLLER); if (controller != null) { controller.setTarget(target); controllerUuid = getControllerUuid(OvsdbConstant.CONTROLLER, target); if (controllerUuid == null) { insertConfig( OvsdbConstant.CONTROLLER, "_uuid", OvsdbConstant.BRIDGE, "controller", bridgeUuid, controller.getRow()); } else { Bridge bridge = (Bridge) TableGenerator.createTable(dbSchema, OvsdbTable.BRIDGE); Set<UUID> controllerUuids = new HashSet<>(); controllerUuids.add(UUID.uuid(controllerUuid)); bridge.setController(controllerUuids); updateConfig(OvsdbConstant.CONTROLLER, "_uuid", bridgeUuid, bridge.getRow()); } } }
/** * Handles port insert. * * @param tableName ovsdb table interface * @param portRow row of port * @return insert, empty if null */ private Insert handlePortInsertTable(String tableName, Row portRow) { DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); TableSchema portTableSchema = dbSchema.getTableSchema(OvsdbConstant.PORT); ColumnSchema portColumnSchema = portTableSchema.getColumnSchema("name"); String portName = (String) portRow.getColumn(portColumnSchema.name()).data(); Interface inf = (Interface) TableGenerator.createTable(dbSchema, OvsdbTable.INTERFACE); inf.setName(portName); TableSchema intfTableSchema = dbSchema.getTableSchema(OvsdbConstant.INTERFACE); Insert insert = new Insert(intfTableSchema, OvsdbConstant.INTERFACE, inf.getRow()); return insert; }
@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"); }