@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 String getControllerUuid(String controllerName, String controllerTarget) { DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); OvsdbRowStore rowStore = getRowStore(OvsdbConstant.DATABASENAME, OvsdbConstant.CONTROLLER); if (rowStore == null) { log.debug("The controller uuid is null"); return null; } ConcurrentMap<String, Row> controllerTableRows = rowStore.getRowStore(); if (controllerTableRows != null) { for (String uuid : controllerTableRows.keySet()) { Controller controller = (Controller) TableGenerator.getTable( dbSchema, controllerTableRows.get(uuid), OvsdbTable.CONTROLLER); String target = (String) controller.getTargetColumn().data(); if (target.equalsIgnoreCase(controllerTarget)) { return uuid; } } } return null; }
@Override public String getBridgeUuid(String bridgeName) { DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); OvsdbRowStore rowStore = getRowStore(OvsdbConstant.DATABASENAME, OvsdbConstant.BRIDGE); if (rowStore == null) { log.debug("The bridge uuid is null"); return null; } ConcurrentMap<String, Row> bridgeTableRows = rowStore.getRowStore(); if (bridgeTableRows == null) { log.debug("The bridge uuid is null"); return null; } for (String uuid : bridgeTableRows.keySet()) { Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeTableRows.get(uuid), OvsdbTable.BRIDGE); if (bridge.getName().equals(bridgeName)) { return uuid; } } return null; }
/** * 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()); } } }
// Gets ovsdb port. private OvsdbPort getOvsdbPort(Row row) { DatabaseSchema dbSchema = getDatabaseSchema(OvsdbConstant.DATABASENAME); Interface intf = (Interface) TableGenerator.getTable(dbSchema, row, OvsdbTable.INTERFACE); if (intf == null) { return null; } long ofPort = getOfPort(intf); String portName = intf.getName(); if ((ofPort < 0) || (portName == null)) { return null; } OvsdbPort ovsdbPort = new OvsdbPort(new OvsdbPortNumber(ofPort), new OvsdbPortName(portName)); return ovsdbPort; }
/** * 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; }
//// Gets ovsdb bridge. private OvsdbBridge getOvsdbBridge(Row row) { DatabaseSchema dbSchema = getDatabaseSchema(OvsdbConstant.DATABASENAME); Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, row, OvsdbTable.BRIDGE); if (bridge == null) { return null; } OvsdbSet datapathIdSet = (OvsdbSet) bridge.getDatapathIdColumn().data(); @SuppressWarnings("unchecked") Set<String> datapathIds = datapathIdSet.set(); if (datapathIds == null || datapathIds.size() == 0) { return null; } String datapathId = (String) datapathIds.toArray()[0]; String bridgeName = bridge.getName(); if ((datapathId == null) || (bridgeName == null)) { return null; } OvsdbBridge ovsdbBridge = new OvsdbBridge(new OvsdbBridgeName(bridgeName), new OvsdbDatapathId(datapathId)); return ovsdbBridge; }
@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"); }