From 03e627fb267317d9e8525abe1ccb79ab6a2f9bd2 Mon Sep 17 00:00:00 2001 From: Mike Hansen Date: Wed, 25 Mar 2020 16:00:13 -0400 Subject: [PATCH] TW-208: ChannelInfoReport from Survey, RSSI metrics enable --- .../opensync/ovsdb/ConnectusOvsdbClient.java | 502 +-- .../wlan/opensync/ovsdb/dao/OvsdbDao.java | 3317 +++++++++-------- 2 files changed, 1931 insertions(+), 1888 deletions(-) diff --git a/opensync-gateway/src/main/java/com/telecominfraproject/wlan/opensync/ovsdb/ConnectusOvsdbClient.java b/opensync-gateway/src/main/java/com/telecominfraproject/wlan/opensync/ovsdb/ConnectusOvsdbClient.java index d1f427a..753beea 100644 --- a/opensync-gateway/src/main/java/com/telecominfraproject/wlan/opensync/ovsdb/ConnectusOvsdbClient.java +++ b/opensync-gateway/src/main/java/com/telecominfraproject/wlan/opensync/ovsdb/ConnectusOvsdbClient.java @@ -36,264 +36,278 @@ import io.netty.handler.ssl.SslContext; @Component public class ConnectusOvsdbClient implements ConnectusOvsdbClientInterface { - private static final Logger LOG = LoggerFactory.getLogger(ConnectusOvsdbClient.class); + private static final Logger LOG = LoggerFactory.getLogger(ConnectusOvsdbClient.class); - @org.springframework.beans.factory.annotation.Value("${connectus.ovsdb.listenPort:6640}") - private int ovsdbListenPort; + @org.springframework.beans.factory.annotation.Value("${connectus.ovsdb.listenPort:6640}") + private int ovsdbListenPort; - @org.springframework.beans.factory.annotation.Value("${connectus.manager.collectionIntervalSec.deviceStats:10}") - private long collectionIntervalSecDeviceStats; + @org.springframework.beans.factory.annotation.Value("${connectus.manager.collectionIntervalSec.deviceStats:10}") + private long collectionIntervalSecDeviceStats; - @Autowired - private SslContext sslContext; + @Autowired + private SslContext sslContext; - @Autowired - private OvsdbPassiveConnectionListener listener; + @Autowired + private OvsdbPassiveConnectionListener listener; - @Autowired - private OvsdbDao ovsdbDao; + @Autowired + private OvsdbDao ovsdbDao; - @Autowired - private OpensyncExternalIntegrationInterface extIntegrationInterface; + @Autowired + private OpensyncExternalIntegrationInterface extIntegrationInterface; - @Autowired - private OvsdbSessionMapInterface ovsdbSessionMapInterface; + @Autowired + private OvsdbSessionMapInterface ovsdbSessionMapInterface; - @PostConstruct - private void postCreate() { - listenForConnections(); - } + @PostConstruct + private void postCreate() { + listenForConnections(); + } - public void listenForConnections() { + public void listenForConnections() { - // This class is only used here, therefore changed it back to an inner class, - // removed the package level class. - // All calling classes related to OVSDB are calling the MonitorCallback - // interface, so the implementation can remain - // hidden. This also gives handy access to the Autowired instances in the - // container class. + // This class is only used here, therefore changed it back to an inner + // class, + // removed the package level class. + // All calling classes related to OVSDB are calling the MonitorCallback + // interface, so the implementation can remain + // hidden. This also gives handy access to the Autowired instances in + // the + // container class. - class ConnectusMonitorCallback implements MonitorCallback { + class ConnectusMonitorCallback implements MonitorCallback { - private String connectedClientId; + private String connectedClientId; - public ConnectusMonitorCallback(String clientId) { - this.connectedClientId = clientId; - } + public ConnectusMonitorCallback(String clientId) { + this.connectedClientId = clientId; + } - @Override - public void update(TableUpdates tableUpdates) { - Set tableNames = tableUpdates.getTableUpdates().keySet(); + @Override + public void update(TableUpdates tableUpdates) { + Set tableNames = tableUpdates.getTableUpdates().keySet(); - for (String name : tableNames) { - LOG.debug("Receive update for table {}", name); + for (String name : tableNames) { + LOG.trace("Receive update for table {}", name); + + Map updates = tableUpdates.getTableUpdates().get(name).getRowUpdates(); + + for (UUID id : updates.keySet()) { - Map updates = tableUpdates.getTableUpdates().get(name).getRowUpdates(); - - for (UUID id : updates.keySet()) { - - LOG.debug("Receive row update for uuid {}", id); - - RowUpdate rowUpdate = updates.get(id); - - Row newRow = rowUpdate.getNew(); - Set newRowColumns = newRow.getColumns().keySet(); - - Row oldRow = rowUpdate.getOld(); - - for (String column : newRowColumns) { - - Value oldVal = null; - if (oldRow != null && oldRow.getColumns().containsKey(column)) oldVal = oldRow.getColumns().get(column); - - Value newVal = newRow.getColumns().get(column); - - LOG.debug("For column {} previous value {} is now {}", column, oldVal, newVal); - } - - } - - } - - // get session information for this client - // OvsdbSession ovsdbSession = ovsdbSessionMapInterface.getSession(connectedClientId); - // if (ovsdbSession == null) { - // throw new IllegalStateException("AP with id " + connectedClientId + " is not connected"); - // } - // - // OvsdbClient ovsdbClient = ovsdbSession.getOvsdbClient(); - // OpensyncAPConfig opensyncAPConfig = extIntegrationInterface.getApConfig(connectedClientId); - - - // TODO: - // example wifiVifStateDbTable get MAC ADDRESS via ovsdbClient from - // Wifi_Associated_Clients given the associated UUID for the client - - // TODO: - // Changes from other Status (Wifi, Inet, etc.) tables? - // Updates to session etc. - // Needs to be reflected in the Cloud - - } - - } - - ConnectionCallback connectionCallback = new ConnectionCallback() { - public void connected(OvsdbClient ovsdbClient) { - String remoteHost = ovsdbClient.getConnectionInfo().getRemoteAddress().getHostAddress(); - int localPort = ovsdbClient.getConnectionInfo().getLocalPort(); - String subjectDn = null; - try { - subjectDn = ((X509Certificate) ovsdbClient.getConnectionInfo().getRemoteCertificate()) - .getSubjectDN().getName(); - - String clientCn = SslUtil.extractCN(subjectDn); - LOG.info("ovsdbClient connecting from {} on port {} clientCn {}", remoteHost, localPort, clientCn); - - ConnectNodeInfo connectNodeInfo = ovsdbDao.getConnectNodeInfo(ovsdbClient); - - // successfully connected - register it in our connectedClients table - // In Plume's environment clientCn is not unique that's why we are augmenting it - // with the serialNumber and using it as a key (equivalent of KDC unique qrCode) - String key = clientCn + "_" + connectNodeInfo.serialNumber; - ConnectusOvsdbClient.this.ovsdbSessionMapInterface.newSession(key, ovsdbClient); - extIntegrationInterface.apConnected(key, connectNodeInfo); - - // push configuration to AP - connectNodeInfo = processConnectRequest(ovsdbClient, clientCn, connectNodeInfo); - LOG.info("ovsdbClient connected from {} on port {} key {} ", remoteHost, localPort, key); - LOG.info("ovsdbClient connectedClients = {}", - ConnectusOvsdbClient.this.ovsdbSessionMapInterface.getNumSessions()); - - // monitor radio config state - ovsdbDao.monitorRadioConfigState(ovsdbClient, new ConnectusMonitorCallback(key)); - // monitor inet state - ovsdbDao.monitorInetState(ovsdbClient, new ConnectusMonitorCallback(key)); - // monitor vif state - ovsdbDao.monitorVIFState(ovsdbClient, new ConnectusMonitorCallback(key)); - // monitor Route state - ovsdbDao.monitorRouteState(ovsdbClient, new ConnectusMonitorCallback(key)); - // monitor Master State - ovsdbDao.monitorMasterState(ovsdbClient, new ConnectusMonitorCallback(key)); - } catch (Exception e) { - LOG.error("ovsdbClient error", e); - // something is wrong with the SSL - ovsdbClient.shutdown(); - return; - } - - } - - public void disconnected(OvsdbClient ovsdbClient) { - String remoteHost = ovsdbClient.getConnectionInfo().getRemoteAddress().getHostAddress(); - int localPort = ovsdbClient.getConnectionInfo().getLocalPort(); - String subjectDn = null; - try { - subjectDn = ((X509Certificate) ovsdbClient.getConnectionInfo().getRemoteCertificate()) - .getSubjectDN().getName(); - } catch (Exception e) { - // do nothing - } - - String clientCn = SslUtil.extractCN(subjectDn); - - // disconnected - deregister ovsdbClient from our connectedClients table - // unfortunately we only know clientCn at this point, but in Plume's environment - // they are not unique - // so we are doing a reverse lookup here, and then if we find the key we will - // remove the entry from the connectedClients. - String key = ConnectusOvsdbClient.this.ovsdbSessionMapInterface.lookupClientId(ovsdbClient); - - if (key != null) { - extIntegrationInterface.apDisconnected(key); - ConnectusOvsdbClient.this.ovsdbSessionMapInterface.removeSession(key); - } - // turn off monitor - ovsdbDao.cancelMonitors(ovsdbClient); - ovsdbClient.shutdown(); - - LOG.info("ovsdbClient disconnected from {} on port {} clientCn {} key {} ", remoteHost, localPort, - clientCn, key); - LOG.info("ovsdbClient connectedClients = {}", - ConnectusOvsdbClient.this.ovsdbSessionMapInterface.getNumSessions()); - } - }; - - listener.startListeningWithSsl(ovsdbListenPort, sslContext, connectionCallback).join(); - - LOG.debug("manager waiting for connection on port {}...", ovsdbListenPort); - } - - private ConnectNodeInfo processConnectRequest(OvsdbClient ovsdbClient, String clientCn, - ConnectNodeInfo connectNodeInfo) { - - LOG.debug("Starting Client connect"); - connectNodeInfo = ovsdbDao.updateConnectNodeInfoOnConnect(ovsdbClient, clientCn, connectNodeInfo); - - String apId = clientCn + "_" + connectNodeInfo.serialNumber; - OpensyncAPConfig opensyncAPConfig = extIntegrationInterface.getApConfig(apId); - - ovsdbDao.configureStats(ovsdbClient); - - // Check if device stats is configured in Wifi_Stats_Config table, provision it - // if needed - if (ovsdbDao.getDeviceStatsReportingInterval(ovsdbClient) != collectionIntervalSecDeviceStats) { - ovsdbDao.updateDeviceStatsReportingInterval(ovsdbClient, collectionIntervalSecDeviceStats); - } - - ovsdbDao.provisionBridgePortInterface(ovsdbClient); - - ovsdbDao.removeAllSsids(ovsdbClient); - - if (opensyncAPConfig != null) { - ovsdbDao.configureWifiRadios(ovsdbClient, opensyncAPConfig.getRadioConfig()); - ovsdbDao.configureSsids(ovsdbClient, opensyncAPConfig.getSsidConfigs()); - } - - ovsdbDao.configureWifiInet(ovsdbClient); - - LOG.debug("Client connect Done"); - return connectNodeInfo; - } - - public Set getConnectedClientIds() { - return ovsdbSessionMapInterface.getConnectedClientIds(); - } - - /** - * @param apId - * @param newRedirectorAddress - * @return updated value of the redirector - */ - public String changeRedirectorAddress(String apId, String newRedirectorAddress) { - OvsdbSession ovsdbSession = ovsdbSessionMapInterface.getSession(apId); - if (ovsdbSession == null) { - throw new IllegalStateException("AP with id " + apId + " is not connected"); - } - - String ret = ovsdbDao.changeRedirectorAddress(ovsdbSession.getOvsdbClient(), apId, newRedirectorAddress); - - return ret; - } - - @Override - public void processConfigChanged(String apId) { - LOG.debug("Starting processConfigChanged for {}", apId); - - OvsdbSession ovsdbSession = ovsdbSessionMapInterface.getSession(apId); - if (ovsdbSession == null) { - throw new IllegalStateException("AP with id " + apId + " is not connected"); - } - - OvsdbClient ovsdbClient = ovsdbSession.getOvsdbClient(); - OpensyncAPConfig opensyncAPConfig = extIntegrationInterface.getApConfig(apId); - - if (opensyncAPConfig != null) { - ovsdbDao.removeAllSsids(ovsdbClient); - ovsdbDao.configureWifiRadios(ovsdbClient, opensyncAPConfig.getRadioConfig()); - ovsdbDao.configureSsids(ovsdbClient, opensyncAPConfig.getSsidConfigs()); - } - - LOG.debug("Finished processConfigChanged for {}", apId); - } + LOG.trace("Receive row update for uuid {}", id); + + RowUpdate rowUpdate = updates.get(id); + + Row newRow = rowUpdate.getNew(); + if (newRow != null) { + Set newRowColumns = newRow.getColumns().keySet(); + Row oldRow = rowUpdate.getOld(); + + for (String column : newRowColumns) { + + Value oldVal = null; + if (oldRow != null && oldRow.getColumns().containsKey(column)) + oldVal = oldRow.getColumns().get(column); + + Value newVal = newRow.getColumns().get(column); + + } + } + + } + + } + + // get session information for this client + // OvsdbSession ovsdbSession = + // ovsdbSessionMapInterface.getSession(connectedClientId); + // if (ovsdbSession == null) { + // throw new IllegalStateException("AP with id " + + // connectedClientId + " is not connected"); + // } + // + // OvsdbClient ovsdbClient = ovsdbSession.getOvsdbClient(); + // OpensyncAPConfig opensyncAPConfig = + // extIntegrationInterface.getApConfig(connectedClientId); + + // TODO: + // example wifiVifStateDbTable get MAC ADDRESS via ovsdbClient + // from + // Wifi_Associated_Clients given the associated UUID for the + // client + + // TODO: + // Changes from other Status (Wifi, Inet, etc.) tables? + // Updates to session etc. + // Needs to be reflected in the Cloud + + } + + } + + ConnectionCallback connectionCallback = new ConnectionCallback() { + public void connected(OvsdbClient ovsdbClient) { + String remoteHost = ovsdbClient.getConnectionInfo().getRemoteAddress().getHostAddress(); + int localPort = ovsdbClient.getConnectionInfo().getLocalPort(); + String subjectDn = null; + try { + subjectDn = ((X509Certificate) ovsdbClient.getConnectionInfo().getRemoteCertificate()) + .getSubjectDN().getName(); + + String clientCn = SslUtil.extractCN(subjectDn); + LOG.info("ovsdbClient connecting from {} on port {} clientCn {}", remoteHost, localPort, clientCn); + + ConnectNodeInfo connectNodeInfo = ovsdbDao.getConnectNodeInfo(ovsdbClient); + + // successfully connected - register it in our + // connectedClients table + // In Plume's environment clientCn is not unique that's why + // we are augmenting it + // with the serialNumber and using it as a key (equivalent + // of KDC unique qrCode) + String key = clientCn + "_" + connectNodeInfo.serialNumber; + ConnectusOvsdbClient.this.ovsdbSessionMapInterface.newSession(key, ovsdbClient); + extIntegrationInterface.apConnected(key, connectNodeInfo); + + // push configuration to AP + connectNodeInfo = processConnectRequest(ovsdbClient, clientCn, connectNodeInfo); + LOG.info("ovsdbClient connected from {} on port {} key {} ", remoteHost, localPort, key); + LOG.info("ovsdbClient connectedClients = {}", + ConnectusOvsdbClient.this.ovsdbSessionMapInterface.getNumSessions()); + + // monitor radio config state + ovsdbDao.monitorRadioConfigState(ovsdbClient, new ConnectusMonitorCallback(key)); + // monitor inet state + ovsdbDao.monitorInetState(ovsdbClient, new ConnectusMonitorCallback(key)); + // monitor vif state + ovsdbDao.monitorVIFState(ovsdbClient, new ConnectusMonitorCallback(key)); + // monitor Route state + ovsdbDao.monitorRouteState(ovsdbClient, new ConnectusMonitorCallback(key)); + // monitor Master State + ovsdbDao.monitorMasterState(ovsdbClient, new ConnectusMonitorCallback(key)); + } catch (Exception e) { + LOG.error("ovsdbClient error", e); + // something is wrong with the SSL + ovsdbClient.shutdown(); + return; + } + + } + + public void disconnected(OvsdbClient ovsdbClient) { + String remoteHost = ovsdbClient.getConnectionInfo().getRemoteAddress().getHostAddress(); + int localPort = ovsdbClient.getConnectionInfo().getLocalPort(); + String subjectDn = null; + try { + subjectDn = ((X509Certificate) ovsdbClient.getConnectionInfo().getRemoteCertificate()) + .getSubjectDN().getName(); + } catch (Exception e) { + // do nothing + } + + String clientCn = SslUtil.extractCN(subjectDn); + + // disconnected - deregister ovsdbClient from our + // connectedClients table + // unfortunately we only know clientCn at this point, but in + // Plume's environment + // they are not unique + // so we are doing a reverse lookup here, and then if we find + // the key we will + // remove the entry from the connectedClients. + String key = ConnectusOvsdbClient.this.ovsdbSessionMapInterface.lookupClientId(ovsdbClient); + + if (key != null) { + extIntegrationInterface.apDisconnected(key); + ConnectusOvsdbClient.this.ovsdbSessionMapInterface.removeSession(key); + } + // turn off monitor + ovsdbDao.cancelMonitors(ovsdbClient); + ovsdbClient.shutdown(); + + LOG.info("ovsdbClient disconnected from {} on port {} clientCn {} key {} ", remoteHost, localPort, + clientCn, key); + LOG.info("ovsdbClient connectedClients = {}", + ConnectusOvsdbClient.this.ovsdbSessionMapInterface.getNumSessions()); + } + }; + + listener.startListeningWithSsl(ovsdbListenPort, sslContext, connectionCallback).join(); + + LOG.debug("manager waiting for connection on port {}...", ovsdbListenPort); + } + + private ConnectNodeInfo processConnectRequest(OvsdbClient ovsdbClient, String clientCn, + ConnectNodeInfo connectNodeInfo) { + + LOG.debug("Starting Client connect"); + connectNodeInfo = ovsdbDao.updateConnectNodeInfoOnConnect(ovsdbClient, clientCn, connectNodeInfo); + + String apId = clientCn + "_" + connectNodeInfo.serialNumber; + OpensyncAPConfig opensyncAPConfig = extIntegrationInterface.getApConfig(apId); + + ovsdbDao.configureStats(ovsdbClient); + + // Check if device stats is configured in Wifi_Stats_Config table, + // provision it + // if needed + if (ovsdbDao.getDeviceStatsReportingInterval(ovsdbClient) != collectionIntervalSecDeviceStats) { + ovsdbDao.updateDeviceStatsReportingInterval(ovsdbClient, collectionIntervalSecDeviceStats); + } + + ovsdbDao.provisionBridgePortInterface(ovsdbClient); + + ovsdbDao.removeAllSsids(ovsdbClient); + + if (opensyncAPConfig != null) { + ovsdbDao.configureWifiRadios(ovsdbClient, opensyncAPConfig.getRadioConfig()); + ovsdbDao.configureSsids(ovsdbClient, opensyncAPConfig.getSsidConfigs()); + } + + ovsdbDao.configureWifiInet(ovsdbClient); + + LOG.debug("Client connect Done"); + return connectNodeInfo; + } + + public Set getConnectedClientIds() { + return ovsdbSessionMapInterface.getConnectedClientIds(); + } + + /** + * @param apId + * @param newRedirectorAddress + * @return updated value of the redirector + */ + public String changeRedirectorAddress(String apId, String newRedirectorAddress) { + OvsdbSession ovsdbSession = ovsdbSessionMapInterface.getSession(apId); + if (ovsdbSession == null) { + throw new IllegalStateException("AP with id " + apId + " is not connected"); + } + + String ret = ovsdbDao.changeRedirectorAddress(ovsdbSession.getOvsdbClient(), apId, newRedirectorAddress); + + return ret; + } + + @Override + public void processConfigChanged(String apId) { + LOG.debug("Starting processConfigChanged for {}", apId); + + OvsdbSession ovsdbSession = ovsdbSessionMapInterface.getSession(apId); + if (ovsdbSession == null) { + throw new IllegalStateException("AP with id " + apId + " is not connected"); + } + + OvsdbClient ovsdbClient = ovsdbSession.getOvsdbClient(); + OpensyncAPConfig opensyncAPConfig = extIntegrationInterface.getApConfig(apId); + + if (opensyncAPConfig != null) { + ovsdbDao.removeAllSsids(ovsdbClient); + ovsdbDao.configureWifiRadios(ovsdbClient, opensyncAPConfig.getRadioConfig()); + ovsdbDao.configureSsids(ovsdbClient, opensyncAPConfig.getSsidConfigs()); + } + + LOG.debug("Finished processConfigChanged for {}", apId); + } } diff --git a/opensync-gateway/src/main/java/com/telecominfraproject/wlan/opensync/ovsdb/dao/OvsdbDao.java b/opensync-gateway/src/main/java/com/telecominfraproject/wlan/opensync/ovsdb/dao/OvsdbDao.java index 5344b4a..3115984 100644 --- a/opensync-gateway/src/main/java/com/telecominfraproject/wlan/opensync/ovsdb/dao/OvsdbDao.java +++ b/opensync-gateway/src/main/java/com/telecominfraproject/wlan/opensync/ovsdb/dao/OvsdbDao.java @@ -50,1743 +50,1772 @@ import com.vmware.ovsdb.service.OvsdbClient; @Component public class OvsdbDao { - private static final Logger LOG = LoggerFactory.getLogger(OvsdbDao.class); - - @org.springframework.beans.factory.annotation.Value("${connectus.ovsdb.managerAddr:3.88.149.10}") - private String managerIpAddr; - - @org.springframework.beans.factory.annotation.Value("${connectus.ovsdb.listenPort:6640}") - private int ovsdbListenPort; - - @org.springframework.beans.factory.annotation.Value("${connectus.mqttBroker.address:testportal.123wlan.com}") - private String mqttBrokerAddress; - - @org.springframework.beans.factory.annotation.Value("${connectus.mqttBroker.listenPort:1883}") - private int mqttBrokerListenPort; - - @org.springframework.beans.factory.annotation.Value("${connectus.ovsdb.timeoutSec:30}") - private int ovsdbTimeoutSec; - - public static final String ovsdbName = "Open_vSwitch"; - public static final String awlanNodeDbTable = "AWLAN_Node"; - public static final String wifiStatsConfigDbTable = "Wifi_Stats_Config"; - - public static final String interfaceDbTable = "Interface"; - public static final String portDbTable = "Port"; - public static final String bridgeDbTable = "Bridge"; - - public static final String wifiRadioConfigDbTable = "Wifi_Radio_Config"; - public static final String wifiRadioStateDbTable = "Wifi_Radio_State"; - - public static final String wifiVifConfigDbTable = "Wifi_VIF_Config"; - public static final String wifiVifStateDbTable = "Wifi_VIF_State"; - - public static final String wifiInetConfigDbTable = "Wifi_Inet_Config"; - public static final String wifiInetStateDbTable = "Wifi_Inet_State"; - - public static final String wifiRouteStateDbTable = "Wifi_Route_State"; - public static final String wifiMasterStateDbTable = "Wifi_Master_State"; - - private static final String wifiRadioStateDbTableMonitorId = "Wifi_Radio_State_Monitor_Id"; - private static final String wifiInetStateDbTableMonitorId = "Wifi_Inet_State_Monitor_Id"; - private static final String wifiVifStateDbTableMonitorId = "Wifi_VIF_State_Monitor_Id"; - private static final String wifiRouteStateDbTableMonitorId = "Wifi_Route_State_Monitor_Id"; - private static final String wifiMasterStateDbTableMonitorId = "Wifi_Master_State_Monitor_Id"; - - public void monitorRouteState(OvsdbClient ovsdbClient, MonitorCallback monitorCallback) { - - List columns = new ArrayList<>(); - columns.add("_uuid"); - columns.add("_version"); - columns.add("dest_addr"); - columns.add("dest_mask"); - columns.add("gateway"); - columns.add("gateway_hwaddr"); - columns.add("if_name"); - - MonitorRequest monitorRequest = new MonitorRequest(); - MonitorRequests monitorRequests = new MonitorRequests( - ImmutableMap.of(wifiRouteStateDbTable, monitorRequest)); - try { - ovsdbClient.monitor(ovsdbName, wifiRouteStateDbTableMonitorId, monitorRequests, monitorCallback); - } catch (OvsdbClientException e) { - LOG.error("Unable to add Monitor to table " + wifiRouteStateDbTable, e); - } - - } - - public void monitorMasterState(OvsdbClient ovsdbClient, MonitorCallback monitorCallback) { - List columns = new ArrayList<>(); - columns.add("_uuid"); - columns.add("_version"); - columns.add("dhcpc"); - columns.add("if_name"); - columns.add("if_type"); - columns.add("inet_addr"); - columns.add("netmask"); - columns.add("network_state"); - columns.add("port_state"); - - MonitorRequest monitorRequest = new MonitorRequest(); - MonitorRequests monitorRequests = new MonitorRequests( - ImmutableMap.of(wifiMasterStateDbTable, monitorRequest)); - - try { - ovsdbClient.monitor(ovsdbName, wifiMasterStateDbTableMonitorId, monitorRequests, monitorCallback); - } catch (OvsdbClientException e) { - LOG.error("Unable to add Monitor to table " + wifiMasterStateDbTable, e); - } - } - public void monitorVIFState(OvsdbClient ovsdbClient, MonitorCallback monitorCallback) { - List columns = new ArrayList<>(); - columns.add("_uuid"); - columns.add("_version"); - columns.add("ap_bridge"); - columns.add("associated_clients"); - columns.add("bridge"); - columns.add("btm"); - columns.add("channel"); - columns.add("dynamic_beacon"); - columns.add("enabled"); - columns.add("group_rekey"); - columns.add("if_name"); - columns.add("mac"); - columns.add("min_hw_mode"); - columns.add("mode"); - columns.add("rrm"); - columns.add("security"); - columns.add("ssid"); - columns.add("ssid_broadcast"); - columns.add("uapsd_enable"); - columns.add("vif_config"); - columns.add("vif_radio_idx"); - - MonitorRequest monitorRequest = new MonitorRequest(); - MonitorRequests monitorRequests = new MonitorRequests( - ImmutableMap.of(wifiVifStateDbTable, monitorRequest)); - - try { - ovsdbClient.monitor(ovsdbName, wifiVifStateDbTableMonitorId, monitorRequests, monitorCallback); - } catch (OvsdbClientException e) { - LOG.error("Unable to add Monitor to table " + wifiVifStateDbTable, e); - } - - } - - public void monitorInetState (OvsdbClient ovsdbClient, MonitorCallback monitorCallback) { - List columns = new ArrayList<>(); - columns.add("_uuid"); - columns.add("_version"); - columns.add("broadcast"); - columns.add("dhcpc"); - columns.add("enabled"); - columns.add("hwaddr"); - columns.add("if_name"); - columns.add("if_type"); - columns.add("if_uuid"); - columns.add("inet_addr"); - columns.add("ip_assign_scheme"); - columns.add("mtu"); - columns.add("netmask"); - columns.add("network"); - - - MonitorRequest monitorRequest = new MonitorRequest(); - MonitorRequests monitorRequests = new MonitorRequests( - ImmutableMap.of(wifiInetStateDbTable, monitorRequest)); - - try { - ovsdbClient.monitor(ovsdbName, wifiInetStateDbTableMonitorId, monitorRequests, monitorCallback); - } catch (OvsdbClientException e) { - LOG.error("Unable to add Monitor to table " + wifiInetStateDbTable, e); - } - - } - - // - //Note: When talking to OVSDB always use future.get(X, TimeUnit.SECONDS); - to prevent DOS attacks with misbehaving clients - // - public void cancelMonitors (OvsdbClient ovsdbClient) { - try { - ovsdbClient.cancelMonitor(wifiRadioStateDbTableMonitorId); - ovsdbClient.cancelMonitor(wifiVifStateDbTableMonitorId); - ovsdbClient.cancelMonitor(wifiInetStateDbTableMonitorId); - ovsdbClient.cancelMonitor(wifiRouteStateDbTableMonitorId); - ovsdbClient.cancelMonitor(wifiMasterStateDbTableMonitorId); - } catch (OvsdbClientException e) { - LOG.debug("Could not cancel Monitor. {}", e.getLocalizedMessage()); - } - } - public void monitorRadioConfigState (OvsdbClient ovsdbClient, MonitorCallback monitorCallback) { - - List columns = new ArrayList<>(); - columns.add("_uuid"); - columns.add("_version"); - columns.add("allowed_channels"); - columns.add("bcn_int"); - columns.add("channel"); - columns.add("channel_mode"); - columns.add("channels"); - columns.add("country"); - columns.add("enabled"); - columns.add("freq_band"); - columns.add("ht_mode"); - columns.add("hw_config"); - columns.add("hw_mode"); - columns.add("hw_params"); - columns.add("if_name"); - columns.add("mac"); - columns.add("radio_config"); - columns.add("tx_chainmask"); - columns.add("tx_power"); - columns.add("vif_states"); - - MonitorRequest monitorRequest = new MonitorRequest(columns, new MonitorSelect(true,true,true,true)); - - Map monitorRequests = new HashMap(); - monitorRequests.put(wifiRadioStateDbTable, monitorRequest); - MonitorRequests requests = new MonitorRequests(monitorRequests) ; - - - - try { - ovsdbClient.monitor(ovsdbName, wifiRadioStateDbTableMonitorId, requests, monitorCallback); - } catch (OvsdbClientException e) { - LOG.error("Unable to add Monitor to table " + wifiRadioStateDbTable, e); - } - - } - - public ConnectNodeInfo getConnectNodeInfo(OvsdbClient ovsdbClient) { - ConnectNodeInfo ret = new ConnectNodeInfo(); - - try { - List operations = new ArrayList<>(); - List conditions = new ArrayList<>(); - List columns = new ArrayList<>(); - columns.add("mqtt_settings"); - columns.add("redirector_addr"); - columns.add("manager_addr"); - columns.add("sku_number"); - columns.add("serial_number"); - columns.add("model"); - columns.add("firmware_version"); - columns.add("platform_version"); - - - operations.add(new Select(awlanNodeDbTable, conditions , columns )); - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - if(LOG.isDebugEnabled()) { - LOG.debug("Select from {}:", awlanNodeDbTable); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - } - - Row row = null; - if (result != null && result.length > 0 && !((SelectResult) result[0]).getRows().isEmpty()) { - row = ((SelectResult)result[0]).getRows().iterator().next(); - } - - ret.mqttSettings = (row!=null)?row.getMapColumn("mqtt_settings"):null; - ret.redirectorAddr = (row!=null)?row.getStringColumn("redirector_addr"):null; - ret.managerAddr = (row!=null)?row.getStringColumn("manager_addr"):null; - - ret.platformVersion = (row!=null)?row.getStringColumn("platform_version"):null; - ret.firmwareVersion = (row!=null)?row.getStringColumn("firmware_version"):null; - - ret.skuNumber = getSingleValueFromSet(row, "sku_number"); - ret.serialNumber = getSingleValueFromSet(row, "serial_number"); - ret.model = getSingleValueFromSet(row, "model"); - - //now populate macAddress, ipV4Address from Wifi_Inet_State - //first look them up for if_name = br-wan - fillInIpAddressAndMac(ovsdbClient, ret, "br-wan"); - if(ret.ipV4Address == null || ret.macAddress==null) { - //when not found - look them up for if_name = br-lan - fillInIpAddressAndMac(ovsdbClient, ret, "br-lan"); - } - - - } catch(OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { - throw new RuntimeException(e); - } - - return ret; - } - - public void fillInIpAddressAndMac(OvsdbClient ovsdbClient, ConnectNodeInfo connectNodeInfo, String ifName) { - try { - List operations = new ArrayList<>(); - List conditions = new ArrayList<>(); - List columns = new ArrayList<>(); - //populate macAddress, ipV4Address from Wifi_Inet_State - - columns.add("inet_addr"); - columns.add("hwaddr"); - - conditions.add(new Condition("if_name", Function.EQUALS, new Atom<>(ifName) )); - - operations.add(new Select(wifiInetStateDbTable, conditions , columns )); - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - - if(LOG.isDebugEnabled()) { - LOG.debug("Select from {}:", wifiInetStateDbTable); + private static final Logger LOG = LoggerFactory.getLogger(OvsdbDao.class); + + @org.springframework.beans.factory.annotation.Value("${connectus.ovsdb.managerAddr:3.88.149.10}") + private String managerIpAddr; + + @org.springframework.beans.factory.annotation.Value("${connectus.ovsdb.listenPort:6640}") + private int ovsdbListenPort; + + @org.springframework.beans.factory.annotation.Value("${connectus.mqttBroker.address:testportal.123wlan.com}") + private String mqttBrokerAddress; + + @org.springframework.beans.factory.annotation.Value("${connectus.mqttBroker.listenPort:1883}") + private int mqttBrokerListenPort; + + @org.springframework.beans.factory.annotation.Value("${connectus.ovsdb.timeoutSec:30}") + private int ovsdbTimeoutSec; + + public static final String ovsdbName = "Open_vSwitch"; + public static final String awlanNodeDbTable = "AWLAN_Node"; + public static final String wifiStatsConfigDbTable = "Wifi_Stats_Config"; + + public static final String interfaceDbTable = "Interface"; + public static final String portDbTable = "Port"; + public static final String bridgeDbTable = "Bridge"; + + public static final String wifiRadioConfigDbTable = "Wifi_Radio_Config"; + public static final String wifiRadioStateDbTable = "Wifi_Radio_State"; + + public static final String wifiVifConfigDbTable = "Wifi_VIF_Config"; + public static final String wifiVifStateDbTable = "Wifi_VIF_State"; + + public static final String wifiInetConfigDbTable = "Wifi_Inet_Config"; + public static final String wifiInetStateDbTable = "Wifi_Inet_State"; + + public static final String wifiRouteStateDbTable = "Wifi_Route_State"; + public static final String wifiMasterStateDbTable = "Wifi_Master_State"; + + private static final String wifiRadioStateDbTableMonitorId = "Wifi_Radio_State_Monitor_Id"; + private static final String wifiInetStateDbTableMonitorId = "Wifi_Inet_State_Monitor_Id"; + private static final String wifiVifStateDbTableMonitorId = "Wifi_VIF_State_Monitor_Id"; + private static final String wifiRouteStateDbTableMonitorId = "Wifi_Route_State_Monitor_Id"; + private static final String wifiMasterStateDbTableMonitorId = "Wifi_Master_State_Monitor_Id"; + + public void monitorRouteState(OvsdbClient ovsdbClient, MonitorCallback monitorCallback) { + + List columns = new ArrayList<>(); + columns.add("_uuid"); + columns.add("_version"); + columns.add("dest_addr"); + columns.add("dest_mask"); + columns.add("gateway"); + columns.add("gateway_hwaddr"); + columns.add("if_name"); + + MonitorRequest monitorRequest = new MonitorRequest(); + MonitorRequests monitorRequests = new MonitorRequests(ImmutableMap.of(wifiRouteStateDbTable, monitorRequest)); + try { + ovsdbClient.monitor(ovsdbName, wifiRouteStateDbTableMonitorId, monitorRequests, monitorCallback); + } catch (OvsdbClientException e) { + LOG.error("Unable to add Monitor to table " + wifiRouteStateDbTable, e); + } + + } + + public void monitorMasterState(OvsdbClient ovsdbClient, MonitorCallback monitorCallback) { + List columns = new ArrayList<>(); + columns.add("_uuid"); + columns.add("_version"); + columns.add("dhcpc"); + columns.add("if_name"); + columns.add("if_type"); + columns.add("inet_addr"); + columns.add("netmask"); + columns.add("network_state"); + columns.add("port_state"); + + MonitorRequest monitorRequest = new MonitorRequest(); + MonitorRequests monitorRequests = new MonitorRequests(ImmutableMap.of(wifiMasterStateDbTable, monitorRequest)); + + try { + ovsdbClient.monitor(ovsdbName, wifiMasterStateDbTableMonitorId, monitorRequests, monitorCallback); + } catch (OvsdbClientException e) { + LOG.error("Unable to add Monitor to table " + wifiMasterStateDbTable, e); + } + } + + public void monitorVIFState(OvsdbClient ovsdbClient, MonitorCallback monitorCallback) { + List columns = new ArrayList<>(); + columns.add("_uuid"); + columns.add("_version"); + columns.add("ap_bridge"); + columns.add("associated_clients"); + columns.add("bridge"); + columns.add("btm"); + columns.add("channel"); + columns.add("dynamic_beacon"); + columns.add("enabled"); + columns.add("group_rekey"); + columns.add("if_name"); + columns.add("mac"); + columns.add("min_hw_mode"); + columns.add("mode"); + columns.add("rrm"); + columns.add("security"); + columns.add("ssid"); + columns.add("ssid_broadcast"); + columns.add("uapsd_enable"); + columns.add("vif_config"); + columns.add("vif_radio_idx"); + + MonitorRequest monitorRequest = new MonitorRequest(); + MonitorRequests monitorRequests = new MonitorRequests(ImmutableMap.of(wifiVifStateDbTable, monitorRequest)); + + try { + ovsdbClient.monitor(ovsdbName, wifiVifStateDbTableMonitorId, monitorRequests, monitorCallback); + } catch (OvsdbClientException e) { + LOG.error("Unable to add Monitor to table " + wifiVifStateDbTable, e); + } + + } + + public void monitorInetState(OvsdbClient ovsdbClient, MonitorCallback monitorCallback) { + List columns = new ArrayList<>(); + columns.add("_uuid"); + columns.add("_version"); + columns.add("broadcast"); + columns.add("dhcpc"); + columns.add("enabled"); + columns.add("hwaddr"); + columns.add("if_name"); + columns.add("if_type"); + columns.add("if_uuid"); + columns.add("inet_addr"); + columns.add("ip_assign_scheme"); + columns.add("mtu"); + columns.add("netmask"); + columns.add("network"); + + MonitorRequest monitorRequest = new MonitorRequest(); + MonitorRequests monitorRequests = new MonitorRequests(ImmutableMap.of(wifiInetStateDbTable, monitorRequest)); + + try { + ovsdbClient.monitor(ovsdbName, wifiInetStateDbTableMonitorId, monitorRequests, monitorCallback); + } catch (OvsdbClientException e) { + LOG.error("Unable to add Monitor to table " + wifiInetStateDbTable, e); + } + + } + + // + // Note: When talking to OVSDB always use future.get(X, TimeUnit.SECONDS); - + // to prevent DOS attacks with misbehaving clients + // + public void cancelMonitors(OvsdbClient ovsdbClient) { + try { + ovsdbClient.cancelMonitor(wifiRadioStateDbTableMonitorId); + ovsdbClient.cancelMonitor(wifiVifStateDbTableMonitorId); + ovsdbClient.cancelMonitor(wifiInetStateDbTableMonitorId); + ovsdbClient.cancelMonitor(wifiRouteStateDbTableMonitorId); + ovsdbClient.cancelMonitor(wifiMasterStateDbTableMonitorId); + } catch (OvsdbClientException e) { + LOG.debug("Could not cancel Monitor. {}", e.getLocalizedMessage()); + } + } + + public void monitorRadioConfigState(OvsdbClient ovsdbClient, MonitorCallback monitorCallback) { + + List columns = new ArrayList<>(); + columns.add("_uuid"); + columns.add("_version"); + columns.add("allowed_channels"); + columns.add("bcn_int"); + columns.add("channel"); + columns.add("channel_mode"); + columns.add("channels"); + columns.add("country"); + columns.add("enabled"); + columns.add("freq_band"); + columns.add("ht_mode"); + columns.add("hw_config"); + columns.add("hw_mode"); + columns.add("hw_params"); + columns.add("if_name"); + columns.add("mac"); + columns.add("radio_config"); + columns.add("tx_chainmask"); + columns.add("tx_power"); + columns.add("vif_states"); + + MonitorRequest monitorRequest = new MonitorRequest(columns, new MonitorSelect(true, true, true, true)); + + Map monitorRequests = new HashMap(); + monitorRequests.put(wifiRadioStateDbTable, monitorRequest); + MonitorRequests requests = new MonitorRequests(monitorRequests); + + try { + ovsdbClient.monitor(ovsdbName, wifiRadioStateDbTableMonitorId, requests, monitorCallback); + } catch (OvsdbClientException e) { + LOG.error("Unable to add Monitor to table " + wifiRadioStateDbTable, e); + } + + } + + public ConnectNodeInfo getConnectNodeInfo(OvsdbClient ovsdbClient) { + ConnectNodeInfo ret = new ConnectNodeInfo(); + + try { + List operations = new ArrayList<>(); + List conditions = new ArrayList<>(); + List columns = new ArrayList<>(); + columns.add("mqtt_settings"); + columns.add("redirector_addr"); + columns.add("manager_addr"); + columns.add("sku_number"); + columns.add("serial_number"); + columns.add("model"); + columns.add("firmware_version"); + columns.add("platform_version"); + + operations.add(new Select(awlanNodeDbTable, conditions, columns)); + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + if (LOG.isDebugEnabled()) { + LOG.debug("Select from {}:", awlanNodeDbTable); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + } + + Row row = null; + if (result != null && result.length > 0 && !((SelectResult) result[0]).getRows().isEmpty()) { + row = ((SelectResult) result[0]).getRows().iterator().next(); + } + + ret.mqttSettings = (row != null) ? row.getMapColumn("mqtt_settings") : null; + ret.redirectorAddr = (row != null) ? row.getStringColumn("redirector_addr") : null; + ret.managerAddr = (row != null) ? row.getStringColumn("manager_addr") : null; + + ret.platformVersion = (row != null) ? row.getStringColumn("platform_version") : null; + ret.firmwareVersion = (row != null) ? row.getStringColumn("firmware_version") : null; + + ret.skuNumber = getSingleValueFromSet(row, "sku_number"); + ret.serialNumber = getSingleValueFromSet(row, "serial_number"); + ret.model = getSingleValueFromSet(row, "model"); + + // now populate macAddress, ipV4Address from Wifi_Inet_State + // first look them up for if_name = br-wan + fillInIpAddressAndMac(ovsdbClient, ret, "br-wan"); + if (ret.ipV4Address == null || ret.macAddress == null) { + // when not found - look them up for if_name = br-lan + fillInIpAddressAndMac(ovsdbClient, ret, "br-lan"); + } + + } catch (OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + + return ret; + } + + public void fillInIpAddressAndMac(OvsdbClient ovsdbClient, ConnectNodeInfo connectNodeInfo, String ifName) { + try { + List operations = new ArrayList<>(); + List conditions = new ArrayList<>(); + List columns = new ArrayList<>(); + // populate macAddress, ipV4Address from Wifi_Inet_State + + columns.add("inet_addr"); + columns.add("hwaddr"); + + conditions.add(new Condition("if_name", Function.EQUALS, new Atom<>(ifName))); + + operations.add(new Select(wifiInetStateDbTable, conditions, columns)); + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + if (LOG.isDebugEnabled()) { + LOG.debug("Select from {}:", wifiInetStateDbTable); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + } + + Row row = null; + if (result != null && result.length > 0 && !((SelectResult) result[0]).getRows().isEmpty()) { + row = ((SelectResult) result[0]).getRows().iterator().next(); + connectNodeInfo.ipV4Address = getSingleValueFromSet(row, "inet_addr"); + connectNodeInfo.macAddress = row.getStringColumn("hwaddr"); + } + + } catch (OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + + } + + public ConnectNodeInfo updateConnectNodeInfoOnConnect(OvsdbClient ovsdbClient, String clientCn, + ConnectNodeInfo incomingConnectNodeInfo) { + ConnectNodeInfo ret = incomingConnectNodeInfo.clone(); + + try { + List operations = new ArrayList<>(); + Map updateColumns = new HashMap<>(); + + // set device_mode = cloud - plume's APs do not use it + // updateColumns.put("device_mode", new Atom("cloud") ); + + // update sku_number if it was empty + if (ret.skuNumber == null || ret.skuNumber.isEmpty()) { + ret.skuNumber = "connectus.ai_" + ret.serialNumber; + updateColumns.put("sku_number", new Atom(ret.skuNumber)); + } + + // Configure the MQTT connection + // ovsh u AWLAN_Node + // mqtt_settings:ins:'["map",[["broker","testportal.123wlan.com"],["topics","/ap/dev-ap-0300/opensync"],["qos","0"],["port","1883"],["remote_log","1"]]]' + Map newMqttSettings = new HashMap<>(); + newMqttSettings.put("broker", mqttBrokerAddress); + newMqttSettings.put("topics", "/ap/" + clientCn + "_" + ret.serialNumber + "/opensync"); + newMqttSettings.put("port", "" + mqttBrokerListenPort); + newMqttSettings.put("compress", "zlib"); + newMqttSettings.put("qos", "0"); + newMqttSettings.put("remote_log", "1"); + + if (ret.mqttSettings == null || !ret.mqttSettings.equals(newMqttSettings)) { + @SuppressWarnings("unchecked") + com.vmware.ovsdb.protocol.operation.notation.Map mgttSettings = com.vmware.ovsdb.protocol.operation.notation.Map + .of(newMqttSettings); + ret.mqttSettings = newMqttSettings; + updateColumns.put("mqtt_settings", mgttSettings); + } + + if (!updateColumns.isEmpty()) { + Row row = new Row(updateColumns); + operations.add(new Update(awlanNodeDbTable, row)); + + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + if (LOG.isDebugEnabled()) { + LOG.debug("Updated {}:", awlanNodeDbTable); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + } + } + + } catch (OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + + return ret; + } + + /** + * @param ovsdbClient + * @return value of reporting_interval column for the stats_type=device from + * the Wifi_Stats_Config table. If value is not provisioned then + * return -1. + */ + public long getDeviceStatsReportingInterval(OvsdbClient ovsdbClient) { + long ret = -1; + try { + List operations = new ArrayList<>(); + List conditions = new ArrayList<>(); + List columns = new ArrayList<>(); + + columns.add("reporting_interval"); + columns.add("stats_type"); + columns.add("radio_type"); + + conditions.add(new Condition("stats_type", Function.EQUALS, new Atom<>("device"))); + + operations.add(new Select(wifiStatsConfigDbTable, conditions, columns)); + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + if (LOG.isDebugEnabled()) { + LOG.debug("Select from {}:", wifiStatsConfigDbTable); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + } + + Row row = null; + if (result != null && result.length > 0 && !((SelectResult) result[0]).getRows().isEmpty()) { + row = ((SelectResult) result[0]).getRows().iterator().next(); + ret = row.getIntegerColumn("reporting_interval"); + LOG.info("Stats collection for stats_type=device is already configured with reporting_interval = {}", + ret); + } + + } catch (OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + + return ret; + } + + /** + * @param ovsdbClient + * @param value + * of reporting_interval column for the stats_type=device from + * the Wifi_Stats_Config table. If value is not provisioned then + * return -1. + */ + public void updateDeviceStatsReportingInterval(OvsdbClient ovsdbClient, long newValue) { + try { + List operations = new ArrayList<>(); + Map updateColumns = new HashMap<>(); + + // turn on stats collection over MQTT: (reporting_interval is in + // seconds?) + // $ ovsh i Wifi_Stats_Config reporting_interval:=10 + // radio_type:="2.4G" stats_type:="device" + + updateColumns.put("reporting_interval", new Atom(10)); + updateColumns.put("radio_type", new Atom("2.4G")); + updateColumns.put("stats_type", new Atom("device")); + + Row row = new Row(updateColumns); + operations.add(new Insert(wifiStatsConfigDbTable, row)); + + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + if (LOG.isDebugEnabled()) { + LOG.debug("Updated {}:", wifiStatsConfigDbTable); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + } + + } catch (OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + + } + + public void performRedirect(OvsdbClient ovsdbClient, String clientCn) { + + List operations = new ArrayList<>(); + List conditions = new ArrayList<>(); + List columns = new ArrayList<>(); + columns.add("manager_addr"); + columns.add("sku_number"); + columns.add("serial_number"); + columns.add("model"); + columns.add("firmware_version"); + + try { + LOG.debug("Starting Redirect"); + + operations.add(new Select(awlanNodeDbTable, conditions, columns)); + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + LOG.debug("Select from AWLAN_Node:"); - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - } + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } - Row row = null; - if (result != null && result.length > 0 && !((SelectResult) result[0]).getRows().isEmpty()) { - row = ((SelectResult)result[0]).getRows().iterator().next(); - connectNodeInfo.ipV4Address = getSingleValueFromSet(row, "inet_addr"); - connectNodeInfo.macAddress = row.getStringColumn("hwaddr"); - } - - - } catch(OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { - throw new RuntimeException(e); - } - - } - - public ConnectNodeInfo updateConnectNodeInfoOnConnect(OvsdbClient ovsdbClient, String clientCn, ConnectNodeInfo incomingConnectNodeInfo) { - ConnectNodeInfo ret = incomingConnectNodeInfo.clone(); - - try { - List operations = new ArrayList<>(); - Map updateColumns = new HashMap<>(); - - //set device_mode = cloud - plume's APs do not use it - //updateColumns.put("device_mode", new Atom("cloud") ); - - //update sku_number if it was empty - if(ret.skuNumber == null || ret.skuNumber.isEmpty()) { - ret.skuNumber = "connectus.ai_"+ ret.serialNumber; - updateColumns.put("sku_number", new Atom(ret.skuNumber) ); - } - - //Configure the MQTT connection - //ovsh u AWLAN_Node mqtt_settings:ins:'["map",[["broker","testportal.123wlan.com"],["topics","/ap/dev-ap-0300/opensync"],["qos","0"],["port","1883"],["remote_log","1"]]]' - Map newMqttSettings = new HashMap<>(); - newMqttSettings.put("broker", mqttBrokerAddress); - newMqttSettings.put("topics", "/ap/"+clientCn+"_"+ret.serialNumber+"/opensync"); - newMqttSettings.put("port", ""+mqttBrokerListenPort); - newMqttSettings.put("compress","zlib"); - newMqttSettings.put("qos", "0"); - newMqttSettings.put("remote_log", "1"); - - if(ret.mqttSettings == null || !ret.mqttSettings.equals(newMqttSettings)) { - @SuppressWarnings("unchecked") - com.vmware.ovsdb.protocol.operation.notation.Map mgttSettings = com.vmware.ovsdb.protocol.operation.notation.Map.of(newMqttSettings); - ret.mqttSettings = newMqttSettings; - updateColumns.put("mqtt_settings", mgttSettings); - } - - if(!updateColumns.isEmpty()) { - Row row = new Row(updateColumns ); - operations.add(new Update(awlanNodeDbTable, row )); + String skuNumber = null; + String serialNumber = null; + String model = null; + String firmwareVersion = null; - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + Row row = null; + if (result != null && result.length > 0 && !((SelectResult) result[0]).getRows().isEmpty()) { + row = ((SelectResult) result[0]).getRows().iterator().next(); + } - if(LOG.isDebugEnabled()) { - LOG.debug("Updated {}:", awlanNodeDbTable); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - } - } - - } catch(OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { - throw new RuntimeException(e); - } - - return ret; - } + firmwareVersion = (row != null) ? row.getStringColumn("firmware_version") : null; + skuNumber = getSingleValueFromSet(row, "sku_number"); + serialNumber = getSingleValueFromSet(row, "serial_number"); + model = getSingleValueFromSet(row, "model"); - /** - * @param ovsdbClient - * @return value of reporting_interval column for the stats_type=device from the Wifi_Stats_Config table. If value is not provisioned then return -1. - */ - public long getDeviceStatsReportingInterval(OvsdbClient ovsdbClient) { - long ret = -1; - try { - List operations = new ArrayList<>(); - List conditions = new ArrayList<>(); - List columns = new ArrayList<>(); + LOG.info("Redirecting AP Node: clientCn {} serialNumber {} model {} firmwareVersion {} skuNumber {}", + clientCn, serialNumber, model, firmwareVersion, skuNumber); - columns.add("reporting_interval"); - columns.add("stats_type"); - columns.add("radio_type"); - - conditions.add(new Condition("stats_type", Function.EQUALS, new Atom<>("device") )); - - operations.add(new Select(wifiStatsConfigDbTable, conditions , columns )); - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - if(LOG.isDebugEnabled()) { - LOG.debug("Select from {}:", wifiStatsConfigDbTable); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - } + // Update table AWLAN_Node - set manager_addr + operations.clear(); + Map updateColumns = new HashMap<>(); - Row row = null; - if (result != null && result.length > 0 && !((SelectResult) result[0]).getRows().isEmpty()) { - row = ((SelectResult)result[0]).getRows().iterator().next(); - ret = row.getIntegerColumn("reporting_interval"); - LOG.info("Stats collection for stats_type=device is already configured with reporting_interval = {}", ret); - } + updateColumns.put("manager_addr", new Atom("ssl:" + managerIpAddr + ":" + ovsdbListenPort)); - } catch(OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { - throw new RuntimeException(e); - } + row = new Row(updateColumns); + operations.add(new Update(awlanNodeDbTable, row)); - return ret; - } + fResult = ovsdbClient.transact(ovsdbName, operations); + result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - /** - * @param ovsdbClient - * @param value of reporting_interval column for the stats_type=device from the Wifi_Stats_Config table. If value is not provisioned then return -1. - */ - public void updateDeviceStatsReportingInterval(OvsdbClient ovsdbClient, long newValue) { - try { - List operations = new ArrayList<>(); - Map updateColumns = new HashMap<>(); + LOG.debug("Updated AWLAN_Node:"); - //turn on stats collection over MQTT: (reporting_interval is in seconds?) - //$ ovsh i Wifi_Stats_Config reporting_interval:=10 radio_type:="2.4G" stats_type:="device" + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } - updateColumns.put("reporting_interval", new Atom(10) ); - updateColumns.put("radio_type", new Atom("2.4G") ); - updateColumns.put("stats_type", new Atom("device") ); + LOG.debug("Redirect Done"); + } catch (ExecutionException | InterruptedException | OvsdbClientException | TimeoutException e) { + LOG.error("Error when redirecting AP Node", e); + throw new RuntimeException(e); + } - Row row = new Row(updateColumns ); - operations.add(new Insert(wifiStatsConfigDbTable, row )); + } - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + public T getSingleValueFromSet(Row row, String columnName) { - if(LOG.isDebugEnabled()) { - LOG.debug("Updated {}:", wifiStatsConfigDbTable); + Set set = (row != null) ? row.getSetColumn(columnName) : null; + T ret = (set != null && !set.isEmpty()) ? set.iterator().next() : null; - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - } + return ret; + } - } catch(OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { - throw new RuntimeException(e); - } + public Map getProvisionedInterfaces(OvsdbClient ovsdbClient) { + Map ret = new HashMap<>(); - } + List operations = new ArrayList<>(); + List conditions = new ArrayList<>(); + List columns = new ArrayList<>(); + columns.add("name"); + columns.add("type"); + columns.add("options"); + columns.add("_uuid"); + columns.add("ofport"); + columns.add("mtu"); + columns.add("ifindex"); + columns.add("link_state"); + columns.add("admin_state"); + try { + LOG.debug("Retrieving Interfaces:"); - public void performRedirect(OvsdbClient ovsdbClient, String clientCn) { + operations.add(new Select(interfaceDbTable, conditions, columns)); + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - List operations = new ArrayList<>(); - List conditions = new ArrayList<>(); - List columns = new ArrayList<>(); - columns.add("manager_addr"); - columns.add("sku_number"); - columns.add("serial_number"); - columns.add("model"); - columns.add("firmware_version"); + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } - try { - LOG.debug("Starting Redirect"); + for (Row row : ((SelectResult) result[0]).getRows()) { - operations.add(new Select(awlanNodeDbTable, conditions , columns )); - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + InterfaceInfo interfaceInfo = new InterfaceInfo(); + interfaceInfo.name = row.getStringColumn("name"); + interfaceInfo.type = row.getStringColumn("type"); + interfaceInfo.uuid = row.getUuidColumn("_uuid"); - LOG.debug("Select from AWLAN_Node:"); + Long tmp = getSingleValueFromSet(row, "ofport"); + interfaceInfo.ofport = tmp != null ? tmp.intValue() : 0; - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } + tmp = getSingleValueFromSet(row, "mtu"); + interfaceInfo.mtu = tmp != null ? tmp.intValue() : 0; - String skuNumber = null; - String serialNumber = null; - String model = null; - String firmwareVersion = null; + tmp = getSingleValueFromSet(row, "ifindex"); + interfaceInfo.ifIndex = tmp != null ? tmp.intValue() : 0; - Row row = null; - if (result != null && result.length > 0 && !((SelectResult) result[0]).getRows().isEmpty()) { - row = ((SelectResult)result[0]).getRows().iterator().next(); - } + String tmpStr = getSingleValueFromSet(row, "link_state"); + interfaceInfo.linkState = tmpStr != null ? tmpStr : ""; - firmwareVersion = (row!=null)?row.getStringColumn("firmware_version"):null; + tmpStr = getSingleValueFromSet(row, "admin_state"); + interfaceInfo.adminState = tmpStr != null ? tmpStr : ""; - skuNumber = getSingleValueFromSet(row, "sku_number"); - serialNumber = getSingleValueFromSet(row, "serial_number"); - model = getSingleValueFromSet(row, "model"); + interfaceInfo.options = row.getMapColumn("options"); - LOG.info("Redirecting AP Node: clientCn {} serialNumber {} model {} firmwareVersion {} skuNumber {}", clientCn, serialNumber, model, firmwareVersion, skuNumber); + ret.put(interfaceInfo.name, interfaceInfo); + } - //Update table AWLAN_Node - set manager_addr - operations.clear(); - Map updateColumns = new HashMap<>(); + LOG.debug("Retrieved Interfaces: {}", ret); - updateColumns.put("manager_addr", new Atom("ssl:" + managerIpAddr + ":" + ovsdbListenPort) ); + } catch (ExecutionException | InterruptedException | OvsdbClientException | TimeoutException e) { + LOG.error("Error in getProvisionedInterfaces", e); - row = new Row(updateColumns ); - operations.add(new Update(awlanNodeDbTable, row )); + throw new RuntimeException(e); + } - fResult = ovsdbClient.transact(ovsdbName, operations); - result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + return ret; + } - LOG.debug("Updated AWLAN_Node:"); + public Map getProvisionedPorts(OvsdbClient ovsdbClient) { + Map ret = new HashMap<>(); - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } + List operations = new ArrayList<>(); + List conditions = new ArrayList<>(); + List columns = new ArrayList<>(); + columns.add("name"); + columns.add("_uuid"); + columns.add("interfaces"); - LOG.debug("Redirect Done"); - } catch (ExecutionException | InterruptedException | OvsdbClientException | TimeoutException e) { - LOG.error("Error when redirecting AP Node", e); - throw new RuntimeException(e); - } + try { + LOG.debug("Retrieving Ports:"); - } + operations.add(new Select(portDbTable, conditions, columns)); + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - public T getSingleValueFromSet(Row row, String columnName){ + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } - Set set = (row!=null)?row.getSetColumn(columnName):null; - T ret = (set!=null && !set.isEmpty())? set.iterator().next(): null; + for (Row row : ((SelectResult) result[0]).getRows()) { - return ret; - } + PortInfo portInfo = new PortInfo(); + portInfo.name = row.getStringColumn("name"); + portInfo.uuid = row.getUuidColumn("_uuid"); + portInfo.interfaceUuids = row.getSetColumn("interfaces"); - public Map getProvisionedInterfaces(OvsdbClient ovsdbClient){ - Map ret = new HashMap<>(); + ret.put(portInfo.name, portInfo); + } - List operations = new ArrayList<>(); - List conditions = new ArrayList<>(); - List columns = new ArrayList<>(); - columns.add("name"); - columns.add("type"); - columns.add("options"); - columns.add("_uuid"); - columns.add("ofport"); - columns.add("mtu"); - columns.add("ifindex"); - columns.add("link_state"); - columns.add("admin_state"); + LOG.debug("Retrieved Ports: {}", ret); - try { - LOG.debug("Retrieving Interfaces:"); + } catch (ExecutionException | InterruptedException | OvsdbClientException | TimeoutException e) { + LOG.error("Error in getProvisionedPorts", e); + throw new RuntimeException(e); + } - operations.add(new Select(interfaceDbTable, conditions , columns )); - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + return ret; + } - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } + public Map getProvisionedBridges(OvsdbClient ovsdbClient) { + Map ret = new HashMap<>(); - for(Row row :((SelectResult)result[0]).getRows()){ + List operations = new ArrayList<>(); + List conditions = new ArrayList<>(); + List columns = new ArrayList<>(); + columns.add("name"); + columns.add("_uuid"); + columns.add("ports"); - InterfaceInfo interfaceInfo = new InterfaceInfo(); - interfaceInfo.name = row.getStringColumn("name"); - interfaceInfo.type = row.getStringColumn("type"); - interfaceInfo.uuid = row.getUuidColumn("_uuid"); + try { + LOG.debug("Retrieving Bridges:"); - Long tmp = getSingleValueFromSet(row, "ofport"); - interfaceInfo.ofport = tmp!=null?tmp.intValue():0; + operations.add(new Select(bridgeDbTable, conditions, columns)); + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - tmp = getSingleValueFromSet(row, "mtu"); - interfaceInfo.mtu = tmp!=null?tmp.intValue():0; + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } - tmp = getSingleValueFromSet(row, "ifindex"); - interfaceInfo.ifIndex = tmp!=null?tmp.intValue():0; + for (Row row : ((SelectResult) result[0]).getRows()) { + + BridgeInfo bridgeInfo = new BridgeInfo(); + bridgeInfo.name = row.getStringColumn("name"); + bridgeInfo.uuid = row.getUuidColumn("_uuid"); + bridgeInfo.portUuids = row.getSetColumn("ports"); + + ret.put(bridgeInfo.name, bridgeInfo); + } + + LOG.debug("Retrieved Bridges: {}", ret); + + } catch (ExecutionException | InterruptedException | OvsdbClientException | TimeoutException e) { + LOG.error("Error in getProvisionedBridges", e); + throw new RuntimeException(e); + } + + return ret; + } + + public Map getProvisionedWifiRadioConfigs(OvsdbClient ovsdbClient) { + Map ret = new HashMap<>(); + + List operations = new ArrayList<>(); + List conditions = new ArrayList<>(); + List columns = new ArrayList<>(); + + columns.add("_uuid"); + columns.add("if_name"); + + columns.add("channel"); + columns.add("channel_mode"); + columns.add("country"); + columns.add("enabled"); + columns.add("ht_mode"); + columns.add("tx_power"); + columns.add("vif_configs"); + columns.add("freq_band"); + columns.add("hw_config"); + + try { + LOG.debug("Retrieving WifiRadioConfig:"); + + operations.add(new Select(wifiRadioConfigDbTable, conditions, columns)); + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + + for (Row row : ((SelectResult) result[0]).getRows()) { + + WifiRadioConfigInfo wifiRadioConfigInfo = new WifiRadioConfigInfo(); + wifiRadioConfigInfo.uuid = row.getUuidColumn("_uuid"); + wifiRadioConfigInfo.ifName = row.getStringColumn("if_name"); + Long channelTmp = getSingleValueFromSet(row, "channel"); + if (channelTmp == null) { + channelTmp = -1L; + } + wifiRadioConfigInfo.channel = channelTmp.intValue(); + wifiRadioConfigInfo.channelMode = row.getStringColumn("channel_mode"); + wifiRadioConfigInfo.country = getSingleValueFromSet(row, "country"); + Boolean tmp = getSingleValueFromSet(row, "enabled"); + wifiRadioConfigInfo.enabled = tmp != null ? tmp : false; + wifiRadioConfigInfo.htMode = getSingleValueFromSet(row, "ht_mode"); + wifiRadioConfigInfo.txPower = getSingleValueFromSet(row, "txPower"); + wifiRadioConfigInfo.vifConfigUuids = row.getSetColumn("vif_configs"); + wifiRadioConfigInfo.freqBand = row.getStringColumn("freq_band"); + wifiRadioConfigInfo.hwConfig = row.getMapColumn("hw_config"); + + ret.put(wifiRadioConfigInfo.ifName, wifiRadioConfigInfo); + } + + LOG.debug("Retrieved WifiRadioConfig: {}", ret); + + } catch (ExecutionException | InterruptedException | OvsdbClientException | TimeoutException e) { + LOG.error("Error in getProvisionedWifiRadioConfigs", e); + throw new RuntimeException(e); + } + + return ret; + } + + public Map getProvisionedWifiVifConfigs(OvsdbClient ovsdbClient) { + Map ret = new HashMap<>(); + + List operations = new ArrayList<>(); + List conditions = new ArrayList<>(); + List columns = new ArrayList<>(); + columns.add("bridge"); + columns.add("_uuid"); + columns.add("btm"); + columns.add("enabled"); + columns.add("ft_psk"); + columns.add("group_rekey"); + columns.add("if_name"); + columns.add("mode"); + columns.add("rrm"); + columns.add("ssid"); + columns.add("ssid_broadcast"); + columns.add("uapsd_enable"); + columns.add("vif_radio_idx"); + columns.add("security"); + + try { + LOG.debug("Retrieving WifiVifConfig:"); + + operations.add(new Select(wifiVifConfigDbTable, conditions, columns)); + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + + for (Row row : ((SelectResult) result[0]).getRows()) { + + WifiVifConfigInfo wifiVifConfigInfo = new WifiVifConfigInfo(); + wifiVifConfigInfo.bridge = row.getStringColumn("bridge"); + wifiVifConfigInfo.uuid = row.getUuidColumn("_uuid"); + wifiVifConfigInfo.btm = row.getIntegerColumn("btm").intValue(); + wifiVifConfigInfo.enabled = row.getBooleanColumn("enabled"); + wifiVifConfigInfo.ftPsk = row.getIntegerColumn("ft_psk").intValue(); + wifiVifConfigInfo.groupRekey = row.getIntegerColumn("group_rekey").intValue(); + wifiVifConfigInfo.ifName = row.getStringColumn("if_name"); + wifiVifConfigInfo.mode = row.getStringColumn("mode"); + wifiVifConfigInfo.rrm = row.getIntegerColumn("rrm").intValue(); + wifiVifConfigInfo.ssid = row.getStringColumn("ssid"); + wifiVifConfigInfo.ssidBroadcast = row.getStringColumn("ssid_broadcast"); + wifiVifConfigInfo.uapsdEnable = row.getBooleanColumn("uapsd_enable"); + wifiVifConfigInfo.vifRadioIdx = row.getIntegerColumn("vif_radio_idx").intValue(); + wifiVifConfigInfo.security = row.getMapColumn("security"); + + ret.put(wifiVifConfigInfo.ifName + '_' + wifiVifConfigInfo.ssid, wifiVifConfigInfo); + } + + LOG.debug("Retrieved WifiVifConfigs: {}", ret); + + } catch (ExecutionException | InterruptedException | OvsdbClientException | TimeoutException e) { + LOG.error("Error in getProvisionedWifiVifConfigs", e); + throw new RuntimeException(e); + } + + return ret; + } + + public Map getProvisionedWifiInetConfigs(OvsdbClient ovsdbClient) { + Map ret = new HashMap<>(); + + List operations = new ArrayList<>(); + List conditions = new ArrayList<>(); + List columns = new ArrayList<>(); + columns.add("NAT"); + columns.add("_uuid"); + columns.add("enabled"); + columns.add("if_name"); + columns.add("if_type"); + columns.add("ip_assign_scheme"); + columns.add("network"); + + try { + LOG.debug("Retrieving WifiInetConfig:"); + + operations.add(new Select(wifiInetConfigDbTable, conditions, columns)); + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + + for (Row row : ((SelectResult) result[0]).getRows()) { + + WifiInetConfigInfo wifiInetConfigInfo = new WifiInetConfigInfo(); + Boolean natTmp = getSingleValueFromSet(row, "NAT"); + wifiInetConfigInfo.nat = natTmp != null ? natTmp : false; + + wifiInetConfigInfo.uuid = row.getUuidColumn("_uuid"); + wifiInetConfigInfo.enabled = row.getBooleanColumn("enabled"); + wifiInetConfigInfo.ifName = row.getStringColumn("if_name"); + wifiInetConfigInfo.ifType = row.getStringColumn("if_type"); + wifiInetConfigInfo.ipAssignScheme = row.getStringColumn("ip_assign_scheme"); + wifiInetConfigInfo.network = row.getBooleanColumn("network"); + + ret.put(wifiInetConfigInfo.ifName, wifiInetConfigInfo); + } + + LOG.debug("Retrieved WifiInetConfigs: {}", ret); + + } catch (ExecutionException | InterruptedException | OvsdbClientException | TimeoutException e) { + LOG.error("Error in getProvisionedWifiInetConfigs", e); + throw new RuntimeException(e); + } + + return ret; + } + + public Map getProvisionedWifiStatsConfigs(OvsdbClient ovsdbClient) { + Map ret = new HashMap<>(); + + List operations = new ArrayList<>(); + List conditions = new ArrayList<>(); + List columns = new ArrayList<>(); + columns.add("channel_list"); + columns.add("radio_type"); + columns.add("reporting_interval"); + columns.add("sampling_interval"); + columns.add("stats_type"); + columns.add("survey_interval_ms"); + columns.add("survey_type"); + columns.add("threshold"); + columns.add("_uuid"); + + try { + LOG.debug("Retrieving WifiStatsConfigs:"); + + operations.add(new Select(wifiStatsConfigDbTable, conditions, columns)); + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + + for (Row row : ((SelectResult) result[0]).getRows()) { + + WifiStatsConfigInfo wifiStatsConfigInfo = new WifiStatsConfigInfo(); + + wifiStatsConfigInfo.channelList = row.getSetColumn("channel_list"); + wifiStatsConfigInfo.radioType = row.getStringColumn("radio_type"); + wifiStatsConfigInfo.reportingInterval = row.getIntegerColumn("reporting_interval").intValue(); + wifiStatsConfigInfo.samplingInterval = row.getIntegerColumn("sampling_interval").intValue(); + wifiStatsConfigInfo.statsType = row.getStringColumn("stats_type"); + wifiStatsConfigInfo.surveyType = getSingleValueFromSet(row, "survey_type"); + Long tmp = getSingleValueFromSet(row, "survey_interval_ms"); + wifiStatsConfigInfo.surveyIntervalMs = tmp != null ? tmp.intValue() : 0; + wifiStatsConfigInfo.threshold = row.getMapColumn("threshold"); + wifiStatsConfigInfo.uuid = row.getUuidColumn("_uuid"); + + ret.put(wifiStatsConfigInfo.radioType + "_" + wifiStatsConfigInfo.statsType + "_" + + wifiStatsConfigInfo.surveyType, wifiStatsConfigInfo); + } + + LOG.debug("Retrieved WifiStatsConfigs: {}", ret); + + } catch (ExecutionException | InterruptedException | OvsdbClientException | TimeoutException e) { + LOG.error("Error in getProvisionedWifiStatsConfigs", e); + + throw new RuntimeException(e); + } + + return ret; + } + + public void provisionSingleBridgePortInterface(OvsdbClient ovsdbClient, String interfaceName, String bridgeName, + String interfaceType, Map interfaceOptions, + Map provisionedInterfaces, Map provisionedPorts, + Map provisionedBridges) + throws OvsdbClientException, TimeoutException, ExecutionException, InterruptedException { + + if (!provisionedInterfaces.containsKey(interfaceName)) { + // Create this interface and link it to the port and the bridge + List operations = new ArrayList<>(); + Map updateColumns = new HashMap<>(); + List conditions = new ArrayList<>(); + + updateColumns.put("name", new Atom<>(interfaceName)); + if (interfaceType != null) { + updateColumns.put("type", new Atom<>(interfaceType)); + } + // updateColumns.put("admin_state", new Atom("up") ); + // updateColumns.put("link_state", new Atom("up") ); + // updateColumns.put("ifindex", new Atom(ifIndex) ); + // updateColumns.put("mtu", new Atom(1500) ); + // updateColumns.put("ofport", new Atom(ofport) ); + + if (interfaceOptions != null) { + @SuppressWarnings("unchecked") + com.vmware.ovsdb.protocol.operation.notation.Map ifOptions = com.vmware.ovsdb.protocol.operation.notation.Map + .of(interfaceOptions); + updateColumns.put("options", ifOptions); + } + + Uuid interfaceUuid = null; + + Row row = new Row(updateColumns); + operations.add(new Insert(interfaceDbTable, row)); + + { + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + LOG.debug("Provisioned Interface for {}", interfaceName); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + if (res instanceof InsertResult) { + interfaceUuid = ((InsertResult) res).getUuid(); + } + } + } + + if (interfaceUuid == null) { + throw new IllegalStateException("Interface entry was not created successfully"); + } + + Uuid portUuid = null; + operations = new ArrayList<>(); + // link the interface to the port, create port if necessary + if (!provisionedPorts.containsKey(interfaceName)) { + // need to create port + updateColumns = new HashMap<>(); + + // portUuid = new Uuid(new UUID(System.currentTimeMillis(), + // System.nanoTime())) ; + updateColumns.put("name", new Atom(interfaceName)); + // updateColumns.put("_uuid", new Atom(portUuid)); + + Set portInterfacesSet = new HashSet<>(); + portInterfacesSet.add(interfaceUuid); + com.vmware.ovsdb.protocol.operation.notation.Set portInterfaces = com.vmware.ovsdb.protocol.operation.notation.Set + .of(portInterfacesSet); + updateColumns.put("interfaces", portInterfaces); + + row = new Row(updateColumns); + operations.add(new Insert(portDbTable, row)); + + { + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + LOG.debug("Provisioned Port for {}", interfaceName); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + if (res instanceof InsertResult) { + portUuid = ((InsertResult) res).getUuid(); + } + } + } + + } else { + // need to update port + PortInfo existingPort = provisionedPorts.get(interfaceName); + portUuid = existingPort.uuid; + + conditions = new ArrayList<>(); + updateColumns = new HashMap<>(); + + conditions.add(new Condition("name", Function.EQUALS, new Atom<>(interfaceName))); + + Set portInterfacesSet = new HashSet<>(); + if (existingPort.interfaceUuids != null) { + portInterfacesSet.addAll(existingPort.interfaceUuids); + } + portInterfacesSet.add(interfaceUuid); + com.vmware.ovsdb.protocol.operation.notation.Set portInterfaces = com.vmware.ovsdb.protocol.operation.notation.Set + .of(portInterfacesSet); + updateColumns.put("interfaces", portInterfaces); + + row = new Row(updateColumns); + operations.add(new Update(portDbTable, row)); + + { + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + LOG.debug("Updated Port for {}", interfaceName); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + } - String tmpStr = getSingleValueFromSet(row, "link_state"); - interfaceInfo.linkState = tmpStr!=null?tmpStr:""; + } - tmpStr = getSingleValueFromSet(row, "admin_state"); - interfaceInfo.adminState = tmpStr!=null?tmpStr:""; + if (portUuid == null) { + throw new IllegalStateException("Port entry was not created successfully"); + } - interfaceInfo.options = row.getMapColumn("options"); + operations = new ArrayList<>(); - ret.put(interfaceInfo.name, interfaceInfo); - } + // link the port to the bridge + if (provisionedBridges.containsKey(bridgeName)) { + BridgeInfo existingBridge = provisionedBridges.get(bridgeName); - LOG.debug("Retrieved Interfaces: {}", ret); + conditions = new ArrayList<>(); + updateColumns = new HashMap<>(); - } catch (ExecutionException | InterruptedException | OvsdbClientException | TimeoutException e) { - LOG.error("Error in getProvisionedInterfaces", e); + conditions.add(new Condition("name", Function.EQUALS, new Atom<>(bridgeName))); - throw new RuntimeException(e); - } + Set bridgePortsSet = new HashSet<>(); + if (existingBridge.portUuids != null) { + bridgePortsSet.addAll(existingBridge.portUuids); + } - return ret; - } + bridgePortsSet.add(portUuid); + com.vmware.ovsdb.protocol.operation.notation.Set bridgePorts = com.vmware.ovsdb.protocol.operation.notation.Set + .of(bridgePortsSet); + updateColumns.put("ports", bridgePorts); - public Map getProvisionedPorts(OvsdbClient ovsdbClient){ - Map ret = new HashMap<>(); + row = new Row(updateColumns); + operations.add(new Update(bridgeDbTable, row)); - List operations = new ArrayList<>(); - List conditions = new ArrayList<>(); - List columns = new ArrayList<>(); - columns.add("name"); - columns.add("_uuid"); - columns.add("interfaces"); + } else { + LOG.warn("provisionedBridges does not have bridge {} - {} - port will be dangling", bridgeName, + provisionedBridges.keySet()); + } - try { - LOG.debug("Retrieving Ports:"); + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - operations.add(new Select(portDbTable, conditions , columns )); - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + if (LOG.isDebugEnabled()) { + LOG.debug("Finished provisioning Interface/port/bridge for {} / {}", interfaceName, bridgeName); - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + } - for(Row row :((SelectResult)result[0]).getRows()){ + } + } - PortInfo portInfo = new PortInfo(); - portInfo.name = row.getStringColumn("name"); - portInfo.uuid = row.getUuidColumn("_uuid"); - portInfo.interfaceUuids = row.getSetColumn("interfaces"); + public static final String homeAp24 = "home-ap-24"; + public static final String homeApL50 = "home-ap-l50"; + public static final String brHome = "br-home"; + public static final String brWan = "br-wan"; - ret.put(portInfo.name, portInfo); - } + public static final String patchW2h = "patch-w2h"; + public static final String patchH2w = "patch-h2w"; - LOG.debug("Retrieved Ports: {}", ret); + public void provisionBridgePortInterface(OvsdbClient ovsdbClient) { + try { + Map provisionedInterfaces = getProvisionedInterfaces(ovsdbClient); + LOG.debug("Existing Interfaces: {}", provisionedInterfaces.keySet()); - } catch (ExecutionException | InterruptedException | OvsdbClientException | TimeoutException e) { - LOG.error("Error in getProvisionedPorts", e); - throw new RuntimeException(e); - } + Map provisionedPorts = getProvisionedPorts(ovsdbClient); + LOG.debug("Existing Ports: {}", provisionedPorts.keySet()); - return ret; - } + Map provisionedBridges = getProvisionedBridges(ovsdbClient); + LOG.debug("Existing Bridges: {}", provisionedBridges.keySet()); - public Map getProvisionedBridges(OvsdbClient ovsdbClient){ - Map ret = new HashMap<>(); + Map patchH2wOptions = new HashMap<>(); + patchH2wOptions.put("peer", "patch-w2h"); - List operations = new ArrayList<>(); - List conditions = new ArrayList<>(); - List columns = new ArrayList<>(); - columns.add("name"); - columns.add("_uuid"); - columns.add("ports"); + Map patchW2hOptions = new HashMap<>(); + patchH2wOptions.put("peer", "patch-h2w"); - try { - LOG.debug("Retrieving Bridges:"); + provisionSingleBridgePortInterface(ovsdbClient, patchH2w, brHome, "patch", patchH2wOptions, + provisionedInterfaces, provisionedPorts, provisionedBridges); + provisionSingleBridgePortInterface(ovsdbClient, patchW2h, brWan, "patch", patchW2hOptions, + provisionedInterfaces, provisionedPorts, provisionedBridges); - operations.add(new Select(bridgeDbTable, conditions , columns )); - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + provisionSingleBridgePortInterface(ovsdbClient, homeApL50, brHome, null, null, provisionedInterfaces, + provisionedPorts, provisionedBridges); + provisionSingleBridgePortInterface(ovsdbClient, homeAp24, brHome, null, null, provisionedInterfaces, + provisionedPorts, provisionedBridges); - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } + } catch (OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { + LOG.error("Error in provisionBridgePortInterface", e); + throw new RuntimeException(e); + } - for(Row row :((SelectResult)result[0]).getRows()){ + } - BridgeInfo bridgeInfo = new BridgeInfo(); - bridgeInfo.name = row.getStringColumn("name"); - bridgeInfo.uuid = row.getUuidColumn("_uuid"); - bridgeInfo.portUuids = row.getSetColumn("ports"); + public void removeOnboardingSsids(OvsdbClient ovsdbClient) { + try { + List operations = new ArrayList<>(); + List conditions = new ArrayList<>(); + conditions.add(new Condition("ssid", Function.EQUALS, new Atom<>("opensync.onboard"))); - ret.put(bridgeInfo.name, bridgeInfo); - } + operations.add(new Delete(wifiVifConfigDbTable, conditions)); - LOG.debug("Retrieved Bridges: {}", ret); - - } catch (ExecutionException | InterruptedException | OvsdbClientException | TimeoutException e) { - LOG.error("Error in getProvisionedBridges", e); - throw new RuntimeException(e); - } - - return ret; - } - - public Map getProvisionedWifiRadioConfigs(OvsdbClient ovsdbClient){ - Map ret = new HashMap<>(); - - List operations = new ArrayList<>(); - List conditions = new ArrayList<>(); - List columns = new ArrayList<>(); - - columns.add("_uuid"); - columns.add("if_name"); - - columns.add("channel"); - columns.add("channel_mode"); - columns.add("country"); - columns.add("enabled"); - columns.add("ht_mode"); - columns.add("tx_power"); - columns.add("vif_configs"); - columns.add("freq_band"); - columns.add("hw_config"); - - try { - LOG.debug("Retrieving WifiRadioConfig:"); - - operations.add(new Select(wifiRadioConfigDbTable, conditions , columns )); - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - - for(Row row :((SelectResult)result[0]).getRows()){ - - WifiRadioConfigInfo wifiRadioConfigInfo = new WifiRadioConfigInfo(); - wifiRadioConfigInfo.uuid = row.getUuidColumn("_uuid"); - wifiRadioConfigInfo.ifName = row.getStringColumn("if_name"); - Long channelTmp = getSingleValueFromSet(row, "channel"); - if (channelTmp==null) { - channelTmp = -1L; - } - wifiRadioConfigInfo.channel = channelTmp.intValue(); - wifiRadioConfigInfo.channelMode = row.getStringColumn("channel_mode"); - wifiRadioConfigInfo.country = getSingleValueFromSet(row, "country"); - Boolean tmp = getSingleValueFromSet(row, "enabled"); - wifiRadioConfigInfo.enabled = tmp!=null?tmp:false; - wifiRadioConfigInfo.htMode = getSingleValueFromSet(row, "ht_mode"); - wifiRadioConfigInfo.txPower = getSingleValueFromSet(row, "txPower"); - wifiRadioConfigInfo.vifConfigUuids = row.getSetColumn("vif_configs"); - wifiRadioConfigInfo.freqBand = row.getStringColumn("freq_band"); - wifiRadioConfigInfo.hwConfig = row.getMapColumn("hw_config"); - - - ret.put(wifiRadioConfigInfo.ifName, wifiRadioConfigInfo); - } - - LOG.debug("Retrieved WifiRadioConfig: {}", ret); - - } catch (ExecutionException | InterruptedException | OvsdbClientException | TimeoutException e) { - LOG.error("Error in getProvisionedWifiRadioConfigs", e); - throw new RuntimeException(e); - } - - return ret; - } - - public Map getProvisionedWifiVifConfigs(OvsdbClient ovsdbClient){ - Map ret = new HashMap<>(); - - List operations = new ArrayList<>(); - List conditions = new ArrayList<>(); - List columns = new ArrayList<>(); - columns.add("bridge"); - columns.add("_uuid"); - columns.add("btm"); - columns.add("enabled"); - columns.add("ft_psk"); - columns.add("group_rekey"); - columns.add("if_name"); - columns.add("mode"); - columns.add("rrm"); - columns.add("ssid"); - columns.add("ssid_broadcast"); - columns.add("uapsd_enable"); - columns.add("vif_radio_idx"); - columns.add("security"); - - try { - LOG.debug("Retrieving WifiVifConfig:"); - - operations.add(new Select(wifiVifConfigDbTable, conditions , columns )); - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - - for(Row row :((SelectResult)result[0]).getRows()){ - - WifiVifConfigInfo wifiVifConfigInfo = new WifiVifConfigInfo(); - wifiVifConfigInfo.bridge = row.getStringColumn("bridge"); - wifiVifConfigInfo.uuid = row.getUuidColumn("_uuid"); - wifiVifConfigInfo.btm = row.getIntegerColumn("btm").intValue(); - wifiVifConfigInfo.enabled = row.getBooleanColumn("enabled"); - wifiVifConfigInfo.ftPsk = row.getIntegerColumn("ft_psk").intValue(); - wifiVifConfigInfo.groupRekey = row.getIntegerColumn("group_rekey").intValue(); - wifiVifConfigInfo.ifName = row.getStringColumn("if_name"); - wifiVifConfigInfo.mode = row.getStringColumn("mode"); - wifiVifConfigInfo.rrm = row.getIntegerColumn("rrm").intValue(); - wifiVifConfigInfo.ssid = row.getStringColumn("ssid"); - wifiVifConfigInfo.ssidBroadcast = row.getStringColumn("ssid_broadcast"); - wifiVifConfigInfo.uapsdEnable = row.getBooleanColumn("uapsd_enable"); - wifiVifConfigInfo.vifRadioIdx = row.getIntegerColumn("vif_radio_idx").intValue(); - wifiVifConfigInfo.security = row.getMapColumn("security"); - - ret.put(wifiVifConfigInfo.ifName + '_' + wifiVifConfigInfo.ssid, wifiVifConfigInfo); - } - - LOG.debug("Retrieved WifiVifConfigs: {}", ret); - - } catch (ExecutionException | InterruptedException | OvsdbClientException | TimeoutException e) { - LOG.error("Error in getProvisionedWifiVifConfigs", e); - throw new RuntimeException(e); - } - - return ret; - } - - public Map getProvisionedWifiInetConfigs(OvsdbClient ovsdbClient){ - Map ret = new HashMap<>(); - - List operations = new ArrayList<>(); - List conditions = new ArrayList<>(); - List columns = new ArrayList<>(); - columns.add("NAT"); - columns.add("_uuid"); - columns.add("enabled"); - columns.add("if_name"); - columns.add("if_type"); - columns.add("ip_assign_scheme"); - columns.add("network"); - - try { - LOG.debug("Retrieving WifiInetConfig:"); - - operations.add(new Select(wifiInetConfigDbTable, conditions , columns )); - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - - for(Row row :((SelectResult)result[0]).getRows()){ - - WifiInetConfigInfo wifiInetConfigInfo = new WifiInetConfigInfo(); - Boolean natTmp = getSingleValueFromSet(row, "NAT"); - wifiInetConfigInfo.nat = natTmp!=null?natTmp:false; - - wifiInetConfigInfo.uuid = row.getUuidColumn("_uuid"); - wifiInetConfigInfo.enabled = row.getBooleanColumn("enabled"); - wifiInetConfigInfo.ifName = row.getStringColumn("if_name"); - wifiInetConfigInfo.ifType = row.getStringColumn("if_type"); - wifiInetConfigInfo.ipAssignScheme = row.getStringColumn("ip_assign_scheme"); - wifiInetConfigInfo.network = row.getBooleanColumn("network"); - - ret.put(wifiInetConfigInfo.ifName, wifiInetConfigInfo); - } - - LOG.debug("Retrieved WifiInetConfigs: {}", ret); - - } catch (ExecutionException | InterruptedException | OvsdbClientException | TimeoutException e) { - LOG.error("Error in getProvisionedWifiInetConfigs", e); - throw new RuntimeException(e); - } - - return ret; - } - - public Map getProvisionedWifiStatsConfigs(OvsdbClient ovsdbClient){ - Map ret = new HashMap<>(); - - List operations = new ArrayList<>(); - List conditions = new ArrayList<>(); - List columns = new ArrayList<>(); - columns.add("channel_list"); - columns.add("radio_type"); - columns.add("reporting_interval"); - columns.add("sampling_interval"); - columns.add("stats_type"); - columns.add("survey_interval_ms"); - columns.add("survey_type"); - columns.add("threshold"); - columns.add("_uuid"); - - try { - LOG.debug("Retrieving WifiStatsConfigs:"); - - operations.add(new Select(wifiStatsConfigDbTable, conditions , columns )); - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - - for(Row row :((SelectResult)result[0]).getRows()){ - - WifiStatsConfigInfo wifiStatsConfigInfo = new WifiStatsConfigInfo(); - - wifiStatsConfigInfo.channelList = row.getSetColumn("channel_list"); - wifiStatsConfigInfo.radioType = row.getStringColumn("radio_type"); - wifiStatsConfigInfo.reportingInterval = row.getIntegerColumn("reporting_interval").intValue(); - wifiStatsConfigInfo.samplingInterval = row.getIntegerColumn("sampling_interval").intValue(); - wifiStatsConfigInfo.statsType = row.getStringColumn("stats_type"); - wifiStatsConfigInfo.surveyType = getSingleValueFromSet(row, "survey_type"); - Long tmp = getSingleValueFromSet(row, "survey_interval_ms"); - wifiStatsConfigInfo.surveyIntervalMs = tmp!=null? tmp.intValue(): 0; - wifiStatsConfigInfo.threshold = row.getMapColumn("threshold"); - wifiStatsConfigInfo.uuid = row.getUuidColumn("_uuid"); - - ret.put(wifiStatsConfigInfo.radioType + "_" + wifiStatsConfigInfo.statsType + "_" + wifiStatsConfigInfo.surveyType, wifiStatsConfigInfo); - } - - LOG.debug("Retrieved WifiStatsConfigs: {}", ret); - - } catch (ExecutionException | InterruptedException | OvsdbClientException | TimeoutException e) { - LOG.error("Error in getProvisionedWifiStatsConfigs", e); - - throw new RuntimeException(e); - } - - return ret; - } - - public void provisionSingleBridgePortInterface(OvsdbClient ovsdbClient, String interfaceName, String bridgeName, String interfaceType, Map interfaceOptions, Map provisionedInterfaces, Map provisionedPorts, - Map provisionedBridges) - throws OvsdbClientException, TimeoutException, ExecutionException, InterruptedException { - - if(!provisionedInterfaces.containsKey(interfaceName)) { - //Create this interface and link it to the port and the bridge - List operations = new ArrayList<>(); - Map updateColumns = new HashMap<>(); - List conditions = new ArrayList<>(); - - - updateColumns.put("name", new Atom<>(interfaceName) ); - if(interfaceType!=null) { - updateColumns.put("type", new Atom<>(interfaceType) ); - } - //updateColumns.put("admin_state", new Atom("up") ); - //updateColumns.put("link_state", new Atom("up") ); - //updateColumns.put("ifindex", new Atom(ifIndex) ); - //updateColumns.put("mtu", new Atom(1500) ); - //updateColumns.put("ofport", new Atom(ofport) ); - - if(interfaceOptions!=null) { - @SuppressWarnings("unchecked") - com.vmware.ovsdb.protocol.operation.notation.Map ifOptions = com.vmware.ovsdb.protocol.operation.notation.Map.of(interfaceOptions); - updateColumns.put("options", ifOptions); - } + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + if (LOG.isDebugEnabled()) { + LOG.debug("Removed onboarding SSIDs from {}:", wifiVifConfigDbTable); - Uuid interfaceUuid = null; + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + } - Row row = new Row(updateColumns ); - operations.add(new Insert(interfaceDbTable, row )); + } catch (OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { + LOG.error("Error in removeOnboardingSsids", e); + throw new RuntimeException(e); + } - { - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + } - LOG.debug("Provisioned Interface for {}", interfaceName); + public void removeAllSsids(OvsdbClient ovsdbClient) { + try { + List operations = new ArrayList<>(); - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - if(res instanceof InsertResult) { - interfaceUuid = ((InsertResult) res).getUuid(); - } - } - } + operations.add(new Delete(wifiVifConfigDbTable)); - if (interfaceUuid == null) { - throw new IllegalStateException("Interface entry was not created successfully"); - } + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - Uuid portUuid = null; - operations = new ArrayList<>(); - //link the interface to the port, create port if necessary - if(!provisionedPorts.containsKey(interfaceName)) { - //need to create port - updateColumns = new HashMap<>(); + if (LOG.isDebugEnabled()) { + LOG.debug("Removed all existing SSIDs from {}:", wifiVifConfigDbTable); - //portUuid = new Uuid(new UUID(System.currentTimeMillis(), System.nanoTime())) ; - updateColumns.put("name", new Atom(interfaceName) ); - //updateColumns.put("_uuid", new Atom(portUuid)); + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + } - Set portInterfacesSet = new HashSet<>(); - portInterfacesSet.add(interfaceUuid); - com.vmware.ovsdb.protocol.operation.notation.Set portInterfaces = com.vmware.ovsdb.protocol.operation.notation.Set.of(portInterfacesSet); - updateColumns.put("interfaces", portInterfaces ); + // Now clean up references in the vif_configs columns + operations = new ArrayList<>(); + Map updateColumns = new HashMap<>(); + Set vifConfigsSet = new HashSet<>(); + com.vmware.ovsdb.protocol.operation.notation.Set vifConfigs = com.vmware.ovsdb.protocol.operation.notation.Set + .of(vifConfigsSet); + updateColumns.put("vif_configs", vifConfigs); - row = new Row(updateColumns); - operations.add(new Insert(portDbTable, row )); + Row row = new Row(updateColumns); + operations.add(new Update(wifiRadioConfigDbTable, row)); - { - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + fResult = ovsdbClient.transact(ovsdbName, operations); + result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - LOG.debug("Provisioned Port for {}", interfaceName); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - if(res instanceof InsertResult) { - portUuid = ((InsertResult) res).getUuid(); - } - } - } - - } else { - //need to update port - PortInfo existingPort = provisionedPorts.get(interfaceName); - portUuid = existingPort.uuid; - - conditions = new ArrayList<>(); - updateColumns = new HashMap<>(); - - conditions.add(new Condition("name", Function.EQUALS, new Atom<>(interfaceName) )); - - Set portInterfacesSet = new HashSet<>(); - if(existingPort.interfaceUuids!=null) { - portInterfacesSet.addAll(existingPort.interfaceUuids); - } - portInterfacesSet.add(interfaceUuid); - com.vmware.ovsdb.protocol.operation.notation.Set portInterfaces = com.vmware.ovsdb.protocol.operation.notation.Set.of(portInterfacesSet); - updateColumns.put("interfaces", portInterfaces ); - - row = new Row(updateColumns); - operations.add(new Update(portDbTable, row )); - - { - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - LOG.debug("Updated Port for {}", interfaceName); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - } - - } - - - if (portUuid == null) { - throw new IllegalStateException("Port entry was not created successfully"); - } - - - operations = new ArrayList<>(); - - //link the port to the bridge - if(provisionedBridges.containsKey(bridgeName)) { - BridgeInfo existingBridge = provisionedBridges.get(bridgeName); - - conditions = new ArrayList<>(); - updateColumns = new HashMap<>(); - - conditions.add(new Condition("name", Function.EQUALS, new Atom<>(bridgeName) )); - - - Set bridgePortsSet = new HashSet<>(); - if(existingBridge.portUuids!=null) { - bridgePortsSet.addAll(existingBridge.portUuids); - } - - bridgePortsSet.add(portUuid); - com.vmware.ovsdb.protocol.operation.notation.Set bridgePorts = com.vmware.ovsdb.protocol.operation.notation.Set.of(bridgePortsSet); - updateColumns.put("ports", bridgePorts ); - - row = new Row(updateColumns); - operations.add(new Update(bridgeDbTable, row )); - - } else { - LOG.warn("provisionedBridges does not have bridge {} - {} - port will be dangling", bridgeName, provisionedBridges.keySet()); - } - - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - if(LOG.isDebugEnabled()) { - LOG.debug("Finished provisioning Interface/port/bridge for {} / {}", interfaceName, bridgeName); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - } - - } - } - - public static final String homeAp24 = "home-ap-24"; - public static final String homeApL50 = "home-ap-l50"; - public static final String brHome = "br-home"; - public static final String brWan = "br-wan"; - - public static final String patchW2h = "patch-w2h"; - public static final String patchH2w = "patch-h2w"; - - public void provisionBridgePortInterface(OvsdbClient ovsdbClient) { - try { - Map provisionedInterfaces = getProvisionedInterfaces(ovsdbClient); - LOG.debug("Existing Interfaces: {}", provisionedInterfaces.keySet()); - - Map provisionedPorts = getProvisionedPorts(ovsdbClient); - LOG.debug("Existing Ports: {}", provisionedPorts.keySet()); - - Map provisionedBridges = getProvisionedBridges(ovsdbClient); - LOG.debug("Existing Bridges: {}", provisionedBridges.keySet()); - - Map patchH2wOptions = new HashMap<>(); - patchH2wOptions.put("peer", "patch-w2h"); - - Map patchW2hOptions = new HashMap<>(); - patchH2wOptions.put("peer", "patch-h2w"); - - provisionSingleBridgePortInterface(ovsdbClient, patchH2w, brHome, "patch", patchH2wOptions, provisionedInterfaces, provisionedPorts, provisionedBridges); - provisionSingleBridgePortInterface(ovsdbClient, patchW2h, brWan, "patch", patchW2hOptions, provisionedInterfaces, provisionedPorts, provisionedBridges); - - provisionSingleBridgePortInterface(ovsdbClient, homeApL50, brHome, null, null, provisionedInterfaces, provisionedPorts, provisionedBridges); - provisionSingleBridgePortInterface(ovsdbClient, homeAp24, brHome, null, null, provisionedInterfaces, provisionedPorts, provisionedBridges); - - } catch(OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { - LOG.error("Error in provisionBridgePortInterface", e); - throw new RuntimeException(e); - } - - } - - public void removeOnboardingSsids(OvsdbClient ovsdbClient) { - try { - List operations = new ArrayList<>(); - List conditions = new ArrayList<>(); - conditions.add(new Condition("ssid", Function.EQUALS, new Atom<>("opensync.onboard") )); - - operations.add(new Delete(wifiVifConfigDbTable, conditions )); - - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - if(LOG.isDebugEnabled()) { - LOG.debug("Removed onboarding SSIDs from {}:", wifiVifConfigDbTable); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - } - - } catch(OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { - LOG.error("Error in removeOnboardingSsids", e); - throw new RuntimeException(e); - } - - } - - public void removeAllSsids(OvsdbClient ovsdbClient) { - try { - List operations = new ArrayList<>(); - - operations.add(new Delete(wifiVifConfigDbTable)); - - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - if(LOG.isDebugEnabled()) { - LOG.debug("Removed all existing SSIDs from {}:", wifiVifConfigDbTable); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - } - - - //Now clean up references in the vif_configs columns - operations = new ArrayList<>(); - Map updateColumns = new HashMap<>(); - Set vifConfigsSet = new HashSet<>(); - com.vmware.ovsdb.protocol.operation.notation.Set vifConfigs = com.vmware.ovsdb.protocol.operation.notation.Set.of(vifConfigsSet); - updateColumns.put("vif_configs", vifConfigs ); - - Row row = new Row(updateColumns); - operations.add(new Update(wifiRadioConfigDbTable, row )); - - fResult = ovsdbClient.transact(ovsdbName, operations); - result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - if(LOG.isDebugEnabled()) { - LOG.debug("Updated WifiRadioConfig "); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - } - - LOG.info("Removed all ssids"); - - } catch(OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { - LOG.error("Error in removeAllSsids", e); - throw new RuntimeException(e); - } - - } - - public void configureWifiRadios(OvsdbClient ovsdbClient, OpensyncAPRadioConfig opensyncAPRadioConfig) { - Map provisionedWifiRadios = getProvisionedWifiRadioConfigs(ovsdbClient); - LOG.debug("Existing WifiRadioConfigs: {}", provisionedWifiRadios.keySet()); - - try { - String country = opensyncAPRadioConfig.getCountry(); - String configName = "wifi0"; - int channel = opensyncAPRadioConfig.getRadioChannel24G(); - Map hwConfig = new HashMap<>(); - configureWifiRadios(ovsdbClient, configName, provisionedWifiRadios, channel, hwConfig, country); - - configName = "wifi1"; - channel = opensyncAPRadioConfig.getRadioChannel5LG(); - hwConfig = new HashMap<>(); - hwConfig.put("dfs_enable", "1"); - hwConfig.put("dfs_ignorecac", "0"); - hwConfig.put("dfs_usenol", "1"); - - configureWifiRadios(ovsdbClient, configName, provisionedWifiRadios, channel, hwConfig, country); - - configName = "wifi2"; - channel = opensyncAPRadioConfig.getRadioChannel5HG(); - hwConfig = new HashMap<>(); - hwConfig.put("dfs_enable", "1"); - hwConfig.put("dfs_ignorecac", "0"); - hwConfig.put("dfs_usenol", "1"); - - configureWifiRadios(ovsdbClient, configName, provisionedWifiRadios, channel, hwConfig, country); - - } catch(OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { - LOG.error("Error in configureWifiRadios", e); - throw new RuntimeException(e); - } - - } - - public void configureWifiRadios(OvsdbClient ovsdbClient, String configName, - Map provisionedWifiRadios, int channel, Map hwConfig, String country ) - throws OvsdbClientException, TimeoutException, ExecutionException, InterruptedException { - - WifiRadioConfigInfo existingConfig = provisionedWifiRadios.get(configName); - - if(existingConfig==null) { - LOG.warn("There is no WifiRadioConfig {}", configName); - return; - } - - List operations = new ArrayList<>(); - Map updateColumns = new HashMap<>(); - List conditions = new ArrayList<>(); - conditions.add(new Condition("if_name", Function.EQUALS, new Atom<>(configName) )); - - updateColumns.put("channel", new Atom(channel) ); - updateColumns.put("country", new Atom<>(country) ); - @SuppressWarnings("unchecked") - com.vmware.ovsdb.protocol.operation.notation.Map hwConfigMap = com.vmware.ovsdb.protocol.operation.notation.Map.of(hwConfig); - updateColumns.put("hw_config", hwConfigMap); - - Row row = new Row(updateColumns ); - operations.add(new Update(wifiRadioConfigDbTable, conditions, row )); - - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - LOG.debug("Provisioned channel {} for {}", channel, configName); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - } - - public void configureSingleSsid(OvsdbClient ovsdbClient, String bridge, String ifName, String ssid, boolean ssidBroadcast, Map security, Map provisionedWifiRadioConfigs, String radioIfName) { - List operations = new ArrayList<>(); - Map updateColumns = new HashMap<>(); - - try { - ///usr/plume/tools/ovsh i Wifi_VIF_Config - // bridge:=br-home btm:=1 enabled:=true ft_psk:=0 group_rekey:=86400 - // if_name:=home-ap-24 mode:=ap rrm:=1 ssid:=ConnectUS-Plume ssid_broadcast:=enabled - // uapsd_enable:=true vif_radio_idx:=2 security:='["map",[["encryption","WPA-PSK"],["key","12345678"],["mode","2"]]]' - - updateColumns.put("bridge", new Atom<>(bridge) ); - updateColumns.put("btm", new Atom<>(1) ); - updateColumns.put("enabled", new Atom<>(true) ); - updateColumns.put("ft_psk", new Atom<>(0) ); - updateColumns.put("group_rekey", new Atom<>(86400) ); - updateColumns.put("if_name", new Atom<>(ifName) ); - updateColumns.put("mode", new Atom<>("ap") ); - updateColumns.put("rrm", new Atom<>(1) ); - updateColumns.put("ssid", new Atom<>(ssid) ); - updateColumns.put("ssid_broadcast", new Atom<>(ssidBroadcast?"enabled":"disabled") ); - updateColumns.put("uapsd_enable", new Atom<>(true) ); - updateColumns.put("vif_radio_idx", new Atom<>(2) ); - - @SuppressWarnings("unchecked") - com.vmware.ovsdb.protocol.operation.notation.Map securityMap = com.vmware.ovsdb.protocol.operation.notation.Map.of(security); - updateColumns.put("security", securityMap); - - Row row = new Row(updateColumns); - operations.add(new Insert(wifiVifConfigDbTable, row )); - - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - LOG.debug("Provisioned SSID {} on {}", ssid, ifName); - - Uuid vifConfigUuid = null; - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - if(res instanceof InsertResult) { - vifConfigUuid = ((InsertResult) res).getUuid(); - } - } - - if (vifConfigUuid == null) { - throw new IllegalStateException("Wifi_VIF_Config entry was not created successfully"); - } - - //update Wifi_Radio_Config here - add vifConfigUuid - ///usr/plume/tools/ovsh u Wifi_Radio_Config vif_configs:='["set",[["uuid","98e42897-b567-4186-84a6-4a4e38a51e9d"],["uuid","4314920e-c4e6-42a6-93e3-261142ed9adf"]]]' --where if_name==wifi0 - updateColumns.clear(); - operations.clear(); - - WifiRadioConfigInfo wifiRadioConfigInfo = provisionedWifiRadioConfigs.get(radioIfName); - if(wifiRadioConfigInfo == null) { - throw new IllegalStateException("missing Wifi_Radio_Config entry "+ radioIfName); - } - - Set vifConfigsSet = new HashSet<>(wifiRadioConfigInfo.vifConfigUuids); - vifConfigsSet.add(vifConfigUuid); - com.vmware.ovsdb.protocol.operation.notation.Set vifConfigs = com.vmware.ovsdb.protocol.operation.notation.Set.of(vifConfigsSet); - updateColumns.put("vif_configs", vifConfigs ); - - List conditions = new ArrayList<>(); - conditions.add(new Condition("if_name", Function.EQUALS, new Atom<>(radioIfName) )); - - row = new Row(updateColumns); - operations.add(new Update(wifiRadioConfigDbTable, conditions, row )); - - fResult = ovsdbClient.transact(ovsdbName, operations); - result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - if(LOG.isDebugEnabled()) { - LOG.debug("Updated WifiRadioConfig {} for SSID {}:", radioIfName, ssid); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - } - - LOG.info("Provisioned SSID {} on interface {} / {}", ssid, ifName, radioIfName); - - } catch(OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { - LOG.error("Error in configureSingleSsid", e); - throw new RuntimeException(e); - } - } - - public void configureSsids(OvsdbClient ovsdbClient, List ssidConfigs) { - if(ssidConfigs==null || ssidConfigs.isEmpty()) { - LOG.debug("No SSIDs to configure"); - return; - } - - Map provisionedWifiVifConfigs = getProvisionedWifiVifConfigs(ovsdbClient); - Map provisionedWifiRadioConfigs = getProvisionedWifiRadioConfigs(ovsdbClient); - LOG.debug("Existing WifiVifConfigs: {}", provisionedWifiVifConfigs.keySet()); - - for(OpensyncAPSsidConfig ssidCfg: ssidConfigs) { - String bridge = brHome; - String ifName = (ssidCfg.getRadioType() == RadioType.is2dot4GHz)?homeAp24:homeApL50; - String radioIfName = (ssidCfg.getRadioType() == RadioType.is2dot4GHz)?"wifi0":"wifi1"; - String ssid = ssidCfg.getSsid(); - boolean ssidBroadcast = ssidCfg.isBroadcast(); - Map security = new HashMap<>(); - security.put("encryption", ssidCfg.getEncryption()); - security.put("key", ssidCfg.getKey()); - security.put("mode", ssidCfg.getMode()); - - if(!provisionedWifiVifConfigs.containsKey(ifName+"_"+ssid)){ - try { - configureSingleSsid(ovsdbClient, bridge, ifName, ssid, ssidBroadcast, security, provisionedWifiRadioConfigs, radioIfName); - } catch (IllegalStateException e) { - //could not provision this SSID, but still can go on - LOG.warn("could not provision SSID {} on {}", ssid, radioIfName); - } - } - - } - - } - - public void configureWifiInet(OvsdbClient ovsdbClient, Map provisionedWifiInetConfigs, String ifName) { - List operations = new ArrayList<>(); - Map updateColumns = new HashMap<>(); - - try { - ///usr/plume/tools/ovsh i Wifi_Inet_Config NAT:=false enabled:=true if_name:=home-ap-24 if_type:=vif ip_assign_scheme:=none network:=true - - updateColumns.put("NAT", new Atom<>(false) ); - updateColumns.put("enabled", new Atom<>(true) ); - updateColumns.put("if_name", new Atom<>(ifName) ); - updateColumns.put("if_type", new Atom<>("vif") ); - updateColumns.put("ip_assign_scheme", new Atom<>("none") ); - updateColumns.put("network", new Atom<>(true) ); - - Row row = new Row(updateColumns); - operations.add(new Insert(wifiInetConfigDbTable, row )); - - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - LOG.debug("Provisioned WifiInetConfig {}", ifName); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - - } catch(OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { - LOG.error("Error in configureWifiInet", e); - throw new RuntimeException(e); - } - - } - - public void configureWifiInetSetNetwork(OvsdbClient ovsdbClient, String ifName) { - List operations = new ArrayList<>(); - Map updateColumns = new HashMap<>(); - List conditions = new ArrayList<>(); - - try { - ///usr/plume/tools/ovsh u Wifi_Inet_Config -w if_name=="br-home" network:=true - - conditions.add(new Condition("if_name", Function.EQUALS, new Atom<>(ifName))); - updateColumns.put("network", new Atom<>(true) ); - - Row row = new Row(updateColumns); - operations.add(new Update(wifiInetConfigDbTable, conditions , row )); - - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - LOG.debug("Enabled network on WifiInetConfig {}", ifName); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - - } catch(OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { - LOG.error("Error in configureWifiInetSetNetwork", e); - throw new RuntimeException(e); - } - - } - - public void configureWifiInet(OvsdbClient ovsdbClient) { - Map provisionedWifiInetConfigs = getProvisionedWifiInetConfigs(ovsdbClient); - LOG.debug("Existing WifiInetConfigs: {}", provisionedWifiInetConfigs.keySet()); - - String ifName = homeAp24; - if(!provisionedWifiInetConfigs.containsKey(ifName)) { - configureWifiInet(ovsdbClient, provisionedWifiInetConfigs, ifName); - } - - ifName = homeApL50; - if(!provisionedWifiInetConfigs.containsKey(ifName)) { - configureWifiInet(ovsdbClient, provisionedWifiInetConfigs, ifName); - } - - if(!provisionedWifiInetConfigs.containsKey(brHome) || !provisionedWifiInetConfigs.get(brHome).network) { - //set network flag on brHome in wifiInetConfig table - configureWifiInetSetNetwork(ovsdbClient, brHome); - } - } - - public void configureStats(OvsdbClient ovsdbClient) { - - Map provisionedWifiStatsConfigs = getProvisionedWifiStatsConfigs(ovsdbClient); - - try { - List operations = new ArrayList<>(); - Map updateColumns = new HashMap<>(); - Row row; - - Set channelSet = new HashSet<>(); - channelSet.add(1); - channelSet.add(6); - channelSet.add(11); - com.vmware.ovsdb.protocol.operation.notation.Set channels = com.vmware.ovsdb.protocol.operation.notation.Set.of(channelSet); - - Map thresholdMap = new HashMap<>(); - thresholdMap.put("max_delay", 600); - thresholdMap.put("util", 10); - - @SuppressWarnings("unchecked") - com.vmware.ovsdb.protocol.operation.notation.Map thresholds = com.vmware.ovsdb.protocol.operation.notation.Map.of(thresholdMap); - - if(!provisionedWifiStatsConfigs.containsKey("2.4G_device_null")) { - // - //updateColumns.put("channel_list", channels ); - updateColumns.put("radio_type", new Atom<>("2.4G") ); - updateColumns.put("reporting_interval", new Atom<>(10) ); - updateColumns.put("sampling_interval", new Atom<>(0) ); - updateColumns.put("stats_type", new Atom<>("device") ); - //updateColumns.put("survey_interval_ms", new Atom<>(10) ); - //updateColumns.put("survey_type", new Atom<>("on-chan") ); - //updateColumns.put("threshold", thresholds ); - - - row = new Row(updateColumns ); - operations.add(new Insert(wifiStatsConfigDbTable, row )); - // - } - - if(!provisionedWifiStatsConfigs.containsKey("5GL_survey_on-chan")) { - // - updateColumns = new HashMap<>(); - //updateColumns.put("channel_list", channels ); - updateColumns.put("radio_type", new Atom<>("5GL") ); - updateColumns.put("reporting_interval", new Atom<>(60) ); - updateColumns.put("sampling_interval", new Atom<>(10) ); - updateColumns.put("stats_type", new Atom<>("survey") ); - updateColumns.put("survey_interval_ms", new Atom<>(0) ); - updateColumns.put("survey_type", new Atom<>("on-chan") ); - //updateColumns.put("threshold", thresholds ); - - - row = new Row(updateColumns ); - operations.add(new Insert(wifiStatsConfigDbTable, row )); - // - } - - if(!provisionedWifiStatsConfigs.containsKey("2.4G_survey_off-chan")) { - // - updateColumns = new HashMap<>(); - updateColumns.put("channel_list", channels ); - updateColumns.put("radio_type", new Atom<>("2.4G") ); - updateColumns.put("reporting_interval", new Atom<>(120) ); - updateColumns.put("sampling_interval", new Atom<>(10) ); - updateColumns.put("stats_type", new Atom<>("survey") ); - updateColumns.put("survey_interval_ms", new Atom<>(50) ); - updateColumns.put("survey_type", new Atom<>("off-chan") ); - updateColumns.put("threshold", thresholds ); - - - row = new Row(updateColumns ); - operations.add(new Insert(wifiStatsConfigDbTable, row )); - // - } - - if(!provisionedWifiStatsConfigs.containsKey("2.4G_neighbor_off-chan")) { - // - updateColumns = new HashMap<>(); - updateColumns.put("channel_list", channels ); - updateColumns.put("radio_type", new Atom<>("2.4G") ); - updateColumns.put("reporting_interval", new Atom<>(120) ); - updateColumns.put("sampling_interval", new Atom<>(0) ); - updateColumns.put("stats_type", new Atom<>("neighbor") ); - updateColumns.put("survey_interval_ms", new Atom<>(0) ); - updateColumns.put("survey_type", new Atom<>("off-chan") ); - //updateColumns.put("threshold", thresholds ); - - - row = new Row(updateColumns ); - operations.add(new Insert(wifiStatsConfigDbTable, row )); - // - } - - if(!provisionedWifiStatsConfigs.containsKey("5GU_neighbor_on-chan")) { - // - updateColumns = new HashMap<>(); - //updateColumns.put("channel_list", channels ); - updateColumns.put("radio_type", new Atom<>("5GU") ); - updateColumns.put("reporting_interval", new Atom<>(60) ); - updateColumns.put("sampling_interval", new Atom<>(0) ); - updateColumns.put("stats_type", new Atom<>("neighbor") ); - updateColumns.put("survey_interval_ms", new Atom<>(0) ); - updateColumns.put("survey_type", new Atom<>("on-chan") ); - //updateColumns.put("threshold", thresholds ); - - - row = new Row(updateColumns ); - operations.add(new Insert(wifiStatsConfigDbTable, row )); - // - } - - if(!provisionedWifiStatsConfigs.containsKey("5GL_client_null")) { - // - updateColumns = new HashMap<>(); - //updateColumns.put("channel_list", channels ); - updateColumns.put("radio_type", new Atom<>("5GL") ); - updateColumns.put("reporting_interval", new Atom<>(60) ); - updateColumns.put("sampling_interval", new Atom<>(10) ); - updateColumns.put("stats_type", new Atom<>("client") ); - //updateColumns.put("survey_interval_ms", new Atom<>(0) ); - //updateColumns.put("survey_type", new Atom<>("on-chan") ); - //updateColumns.put("threshold", thresholds ); - - - row = new Row(updateColumns ); - operations.add(new Insert(wifiStatsConfigDbTable, row )); - // - } - - if(!provisionedWifiStatsConfigs.containsKey("5GU_client_null")) { - // - updateColumns = new HashMap<>(); - //updateColumns.put("channel_list", channels ); - updateColumns.put("radio_type", new Atom<>("5GU") ); - updateColumns.put("reporting_interval", new Atom<>(60) ); - updateColumns.put("sampling_interval", new Atom<>(10) ); - updateColumns.put("stats_type", new Atom<>("client") ); - //updateColumns.put("survey_interval_ms", new Atom<>(0) ); - //updateColumns.put("survey_type", new Atom<>("on-chan") ); - //updateColumns.put("threshold", thresholds ); - - - row = new Row(updateColumns ); - operations.add(new Insert(wifiStatsConfigDbTable, row )); - // - } - - if(!provisionedWifiStatsConfigs.containsKey("2.4G_survey_on-chan")) { - // - updateColumns = new HashMap<>(); - //updateColumns.put("channel_list", channels ); - updateColumns.put("radio_type", new Atom<>("2.4G") ); - updateColumns.put("reporting_interval", new Atom<>(60) ); - updateColumns.put("sampling_interval", new Atom<>(10) ); - updateColumns.put("stats_type", new Atom<>("survey") ); - updateColumns.put("survey_interval_ms", new Atom<>(0) ); - updateColumns.put("survey_type", new Atom<>("on-chan") ); - //updateColumns.put("threshold", thresholds ); - - - row = new Row(updateColumns ); - operations.add(new Insert(wifiStatsConfigDbTable, row )); - // - } - - if(!provisionedWifiStatsConfigs.containsKey("2.4G_client_null")) { - // - updateColumns = new HashMap<>(); - //updateColumns.put("channel_list", channels ); - updateColumns.put("radio_type", new Atom<>("2.4G") ); - updateColumns.put("reporting_interval", new Atom<>(60) ); - updateColumns.put("sampling_interval", new Atom<>(10) ); - updateColumns.put("stats_type", new Atom<>("client") ); - //updateColumns.put("survey_interval_ms", new Atom<>(0) ); - //updateColumns.put("survey_type", new Atom<>("on-chan") ); - //updateColumns.put("threshold", thresholds ); - - - row = new Row(updateColumns ); - operations.add(new Insert(wifiStatsConfigDbTable, row )); - // - } - - if(!provisionedWifiStatsConfigs.containsKey("2.4G_neighbor_on-chan")) { - // - updateColumns = new HashMap<>(); - //updateColumns.put("channel_list", channels ); - updateColumns.put("radio_type", new Atom<>("2.4G") ); - updateColumns.put("reporting_interval", new Atom<>(60) ); - updateColumns.put("sampling_interval", new Atom<>(0) ); - updateColumns.put("stats_type", new Atom<>("neighbor") ); - updateColumns.put("survey_interval_ms", new Atom<>(0) ); - updateColumns.put("survey_type", new Atom<>("on-chan") ); - //updateColumns.put("threshold", thresholds ); - - - row = new Row(updateColumns ); - operations.add(new Insert(wifiStatsConfigDbTable, row )); - // - } - - if(!provisionedWifiStatsConfigs.containsKey("5GU_survey_on-chan")) { - // - updateColumns = new HashMap<>(); - //updateColumns.put("channel_list", channels ); - updateColumns.put("radio_type", new Atom<>("5GU") ); - updateColumns.put("reporting_interval", new Atom<>(60) ); - updateColumns.put("sampling_interval", new Atom<>(10) ); - updateColumns.put("stats_type", new Atom<>("survey") ); - updateColumns.put("survey_interval_ms", new Atom<>(0) ); - updateColumns.put("survey_type", new Atom<>("on-chan") ); - //updateColumns.put("threshold", thresholds ); - - - row = new Row(updateColumns ); - operations.add(new Insert(wifiStatsConfigDbTable, row )); - // - } - - if(!provisionedWifiStatsConfigs.containsKey("5GL_neighbor_on-chan")) { - // - updateColumns = new HashMap<>(); - //updateColumns.put("channel_list", channels ); - updateColumns.put("radio_type", new Atom<>("5GL") ); - updateColumns.put("reporting_interval", new Atom<>(60) ); - updateColumns.put("sampling_interval", new Atom<>(0) ); - updateColumns.put("stats_type", new Atom<>("neighbor") ); - updateColumns.put("survey_interval_ms", new Atom<>(0) ); - updateColumns.put("survey_type", new Atom<>("on-chan") ); - //updateColumns.put("threshold", thresholds ); - - - row = new Row(updateColumns ); - operations.add(new Insert(wifiStatsConfigDbTable, row )); - // - } - - if(!operations.isEmpty()) { - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - if(LOG.isDebugEnabled()) { - LOG.debug("Updated {}:", wifiStatsConfigDbTable); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - } - } - - } catch(OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { - throw new RuntimeException(e); - } - } - - public String changeRedirectorAddress(OvsdbClient ovsdbClient, String apId, String newRedirectorAddress) { - try { - List operations = new ArrayList<>(); - Map updateColumns = new HashMap<>(); - - updateColumns.put("redirector_addr", new Atom<>(newRedirectorAddress)); - - Row row = new Row(updateColumns ); - operations.add(new Update(awlanNodeDbTable, row )); - - CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); - OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); - - if(LOG.isDebugEnabled()) { - LOG.debug("Updated {} redirector_addr = {}", awlanNodeDbTable, newRedirectorAddress); - - for(OperationResult res : result) { - LOG.debug("Op Result {}", res); - } - } - - } catch(OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { - throw new RuntimeException(e); - } - - return newRedirectorAddress; - } + if (LOG.isDebugEnabled()) { + LOG.debug("Updated WifiRadioConfig "); + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + } + + LOG.info("Removed all ssids"); + + } catch (OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { + LOG.error("Error in removeAllSsids", e); + throw new RuntimeException(e); + } + + } + + public void configureWifiRadios(OvsdbClient ovsdbClient, OpensyncAPRadioConfig opensyncAPRadioConfig) { + Map provisionedWifiRadios = getProvisionedWifiRadioConfigs(ovsdbClient); + LOG.debug("Existing WifiRadioConfigs: {}", provisionedWifiRadios.keySet()); + + try { + String country = opensyncAPRadioConfig.getCountry(); + String configName = "wifi0"; + int channel = opensyncAPRadioConfig.getRadioChannel24G(); + Map hwConfig = new HashMap<>(); + configureWifiRadios(ovsdbClient, configName, provisionedWifiRadios, channel, hwConfig, country); + + configName = "wifi1"; + channel = opensyncAPRadioConfig.getRadioChannel5LG(); + hwConfig = new HashMap<>(); + hwConfig.put("dfs_enable", "1"); + hwConfig.put("dfs_ignorecac", "0"); + hwConfig.put("dfs_usenol", "1"); + + configureWifiRadios(ovsdbClient, configName, provisionedWifiRadios, channel, hwConfig, country); + + configName = "wifi2"; + channel = opensyncAPRadioConfig.getRadioChannel5HG(); + hwConfig = new HashMap<>(); + hwConfig.put("dfs_enable", "1"); + hwConfig.put("dfs_ignorecac", "0"); + hwConfig.put("dfs_usenol", "1"); + + configureWifiRadios(ovsdbClient, configName, provisionedWifiRadios, channel, hwConfig, country); + + } catch (OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { + LOG.error("Error in configureWifiRadios", e); + throw new RuntimeException(e); + } + + } + + public void configureWifiRadios(OvsdbClient ovsdbClient, String configName, + Map provisionedWifiRadios, int channel, Map hwConfig, + String country) throws OvsdbClientException, TimeoutException, ExecutionException, InterruptedException { + + WifiRadioConfigInfo existingConfig = provisionedWifiRadios.get(configName); + + if (existingConfig == null) { + LOG.warn("There is no WifiRadioConfig {}", configName); + return; + } + + List operations = new ArrayList<>(); + Map updateColumns = new HashMap<>(); + List conditions = new ArrayList<>(); + conditions.add(new Condition("if_name", Function.EQUALS, new Atom<>(configName))); + + updateColumns.put("channel", new Atom(channel)); + updateColumns.put("country", new Atom<>(country)); + @SuppressWarnings("unchecked") + com.vmware.ovsdb.protocol.operation.notation.Map hwConfigMap = com.vmware.ovsdb.protocol.operation.notation.Map + .of(hwConfig); + updateColumns.put("hw_config", hwConfigMap); + + Row row = new Row(updateColumns); + operations.add(new Update(wifiRadioConfigDbTable, conditions, row)); + + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + LOG.debug("Provisioned channel {} for {}", channel, configName); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + } + + public void configureSingleSsid(OvsdbClient ovsdbClient, String bridge, String ifName, String ssid, + boolean ssidBroadcast, Map security, + Map provisionedWifiRadioConfigs, String radioIfName) { + List operations = new ArrayList<>(); + Map updateColumns = new HashMap<>(); + + try { + /// usr/plume/tools/ovsh i Wifi_VIF_Config + // bridge:=br-home btm:=1 enabled:=true ft_psk:=0 group_rekey:=86400 + // if_name:=home-ap-24 mode:=ap rrm:=1 ssid:=ConnectUS-Plume + /// ssid_broadcast:=enabled + // uapsd_enable:=true vif_radio_idx:=2 + /// security:='["map",[["encryption","WPA-PSK"],["key","12345678"],["mode","2"]]]' + + updateColumns.put("bridge", new Atom<>(bridge)); + updateColumns.put("btm", new Atom<>(1)); + updateColumns.put("enabled", new Atom<>(true)); + updateColumns.put("ft_psk", new Atom<>(0)); + updateColumns.put("group_rekey", new Atom<>(86400)); + updateColumns.put("if_name", new Atom<>(ifName)); + updateColumns.put("mode", new Atom<>("ap")); + updateColumns.put("rrm", new Atom<>(1)); + updateColumns.put("ssid", new Atom<>(ssid)); + updateColumns.put("ssid_broadcast", new Atom<>(ssidBroadcast ? "enabled" : "disabled")); + updateColumns.put("uapsd_enable", new Atom<>(true)); + updateColumns.put("vif_radio_idx", new Atom<>(2)); + + @SuppressWarnings("unchecked") + com.vmware.ovsdb.protocol.operation.notation.Map securityMap = com.vmware.ovsdb.protocol.operation.notation.Map + .of(security); + updateColumns.put("security", securityMap); + + Row row = new Row(updateColumns); + operations.add(new Insert(wifiVifConfigDbTable, row)); + + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + LOG.debug("Provisioned SSID {} on {}", ssid, ifName); + + Uuid vifConfigUuid = null; + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + if (res instanceof InsertResult) { + vifConfigUuid = ((InsertResult) res).getUuid(); + } + } + + if (vifConfigUuid == null) { + throw new IllegalStateException("Wifi_VIF_Config entry was not created successfully"); + } + + // update Wifi_Radio_Config here - add vifConfigUuid + /// usr/plume/tools/ovsh u Wifi_Radio_Config + // vif_configs:='["set",[["uuid","98e42897-b567-4186-84a6-4a4e38a51e9d"],["uuid","4314920e-c4e6-42a6-93e3-261142ed9adf"]]]' + // --where if_name==wifi0 + updateColumns.clear(); + operations.clear(); + + WifiRadioConfigInfo wifiRadioConfigInfo = provisionedWifiRadioConfigs.get(radioIfName); + if (wifiRadioConfigInfo == null) { + throw new IllegalStateException("missing Wifi_Radio_Config entry " + radioIfName); + } + + Set vifConfigsSet = new HashSet<>(wifiRadioConfigInfo.vifConfigUuids); + vifConfigsSet.add(vifConfigUuid); + com.vmware.ovsdb.protocol.operation.notation.Set vifConfigs = com.vmware.ovsdb.protocol.operation.notation.Set + .of(vifConfigsSet); + updateColumns.put("vif_configs", vifConfigs); + + List conditions = new ArrayList<>(); + conditions.add(new Condition("if_name", Function.EQUALS, new Atom<>(radioIfName))); + + row = new Row(updateColumns); + operations.add(new Update(wifiRadioConfigDbTable, conditions, row)); + + fResult = ovsdbClient.transact(ovsdbName, operations); + result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + if (LOG.isDebugEnabled()) { + LOG.debug("Updated WifiRadioConfig {} for SSID {}:", radioIfName, ssid); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + } + + LOG.info("Provisioned SSID {} on interface {} / {}", ssid, ifName, radioIfName); + + } catch (OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { + LOG.error("Error in configureSingleSsid", e); + throw new RuntimeException(e); + } + } + + public void configureSsids(OvsdbClient ovsdbClient, List ssidConfigs) { + if (ssidConfigs == null || ssidConfigs.isEmpty()) { + LOG.debug("No SSIDs to configure"); + return; + } + + Map provisionedWifiVifConfigs = getProvisionedWifiVifConfigs(ovsdbClient); + Map provisionedWifiRadioConfigs = getProvisionedWifiRadioConfigs(ovsdbClient); + LOG.debug("Existing WifiVifConfigs: {}", provisionedWifiVifConfigs.keySet()); + + for (OpensyncAPSsidConfig ssidCfg : ssidConfigs) { + String bridge = brHome; + String ifName = (ssidCfg.getRadioType() == RadioType.is2dot4GHz) ? homeAp24 : homeApL50; + String radioIfName = (ssidCfg.getRadioType() == RadioType.is2dot4GHz) ? "wifi0" : "wifi1"; + String ssid = ssidCfg.getSsid(); + boolean ssidBroadcast = ssidCfg.isBroadcast(); + Map security = new HashMap<>(); + security.put("encryption", ssidCfg.getEncryption()); + security.put("key", ssidCfg.getKey()); + security.put("mode", ssidCfg.getMode()); + + if (!provisionedWifiVifConfigs.containsKey(ifName + "_" + ssid)) { + try { + configureSingleSsid(ovsdbClient, bridge, ifName, ssid, ssidBroadcast, security, + provisionedWifiRadioConfigs, radioIfName); + } catch (IllegalStateException e) { + // could not provision this SSID, but still can go on + LOG.warn("could not provision SSID {} on {}", ssid, radioIfName); + } + } + + } + + } + + public void configureWifiInet(OvsdbClient ovsdbClient, Map provisionedWifiInetConfigs, + String ifName) { + List operations = new ArrayList<>(); + Map updateColumns = new HashMap<>(); + + try { + /// usr/plume/tools/ovsh i Wifi_Inet_Config NAT:=false enabled:=true + /// if_name:=home-ap-24 if_type:=vif ip_assign_scheme:=none + /// network:=true + + updateColumns.put("NAT", new Atom<>(false)); + updateColumns.put("enabled", new Atom<>(true)); + updateColumns.put("if_name", new Atom<>(ifName)); + updateColumns.put("if_type", new Atom<>("vif")); + updateColumns.put("ip_assign_scheme", new Atom<>("none")); + updateColumns.put("network", new Atom<>(true)); + + Row row = new Row(updateColumns); + operations.add(new Insert(wifiInetConfigDbTable, row)); + + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + LOG.debug("Provisioned WifiInetConfig {}", ifName); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + + } catch (OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { + LOG.error("Error in configureWifiInet", e); + throw new RuntimeException(e); + } + + } + + public void configureWifiInetSetNetwork(OvsdbClient ovsdbClient, String ifName) { + List operations = new ArrayList<>(); + Map updateColumns = new HashMap<>(); + List conditions = new ArrayList<>(); + + try { + /// usr/plume/tools/ovsh u Wifi_Inet_Config -w if_name=="br-home" + /// network:=true + + conditions.add(new Condition("if_name", Function.EQUALS, new Atom<>(ifName))); + updateColumns.put("network", new Atom<>(true)); + + Row row = new Row(updateColumns); + operations.add(new Update(wifiInetConfigDbTable, conditions, row)); + + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + LOG.debug("Enabled network on WifiInetConfig {}", ifName); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + + } catch (OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { + LOG.error("Error in configureWifiInetSetNetwork", e); + throw new RuntimeException(e); + } + + } + + public void configureWifiInet(OvsdbClient ovsdbClient) { + Map provisionedWifiInetConfigs = getProvisionedWifiInetConfigs(ovsdbClient); + LOG.debug("Existing WifiInetConfigs: {}", provisionedWifiInetConfigs.keySet()); + + String ifName = homeAp24; + if (!provisionedWifiInetConfigs.containsKey(ifName)) { + configureWifiInet(ovsdbClient, provisionedWifiInetConfigs, ifName); + } + + ifName = homeApL50; + if (!provisionedWifiInetConfigs.containsKey(ifName)) { + configureWifiInet(ovsdbClient, provisionedWifiInetConfigs, ifName); + } + + if (!provisionedWifiInetConfigs.containsKey(brHome) || !provisionedWifiInetConfigs.get(brHome).network) { + // set network flag on brHome in wifiInetConfig table + configureWifiInetSetNetwork(ovsdbClient, brHome); + } + } + + public void configureStats(OvsdbClient ovsdbClient) { + + Map provisionedWifiStatsConfigs = getProvisionedWifiStatsConfigs(ovsdbClient); + + try { + List operations = new ArrayList<>(); + Map updateColumns = new HashMap<>(); + Row row; + + Set channelSet = new HashSet<>(); + channelSet.add(1); + channelSet.add(6); + channelSet.add(11); + com.vmware.ovsdb.protocol.operation.notation.Set channels = com.vmware.ovsdb.protocol.operation.notation.Set + .of(channelSet); + + Map thresholdMap = new HashMap<>(); + thresholdMap.put("max_delay", 600); + thresholdMap.put("util", 10); + + @SuppressWarnings("unchecked") + com.vmware.ovsdb.protocol.operation.notation.Map thresholds = com.vmware.ovsdb.protocol.operation.notation.Map + .of(thresholdMap); + + if (!provisionedWifiStatsConfigs.containsKey("2.4G_device_null")) { + // + // updateColumns.put("channel_list", channels ); + updateColumns.put("radio_type", new Atom<>("2.4G")); + updateColumns.put("reporting_interval", new Atom<>(10)); + updateColumns.put("sampling_interval", new Atom<>(0)); + updateColumns.put("stats_type", new Atom<>("device")); + // updateColumns.put("survey_interval_ms", new Atom<>(10) ); + // updateColumns.put("survey_type", new Atom<>("on-chan") ); + // updateColumns.put("threshold", thresholds ); + + row = new Row(updateColumns); + operations.add(new Insert(wifiStatsConfigDbTable, row)); + // + } + + if (!provisionedWifiStatsConfigs.containsKey("5GL_survey_on-chan")) { + // + updateColumns = new HashMap<>(); + // updateColumns.put("channel_list", channels ); + updateColumns.put("radio_type", new Atom<>("5GL")); + updateColumns.put("reporting_interval", new Atom<>(60)); + updateColumns.put("sampling_interval", new Atom<>(10)); + updateColumns.put("stats_type", new Atom<>("survey")); + updateColumns.put("survey_interval_ms", new Atom<>(0)); + updateColumns.put("survey_type", new Atom<>("on-chan")); + // updateColumns.put("threshold", thresholds ); + + row = new Row(updateColumns); + operations.add(new Insert(wifiStatsConfigDbTable, row)); + // + } + + if (!provisionedWifiStatsConfigs.containsKey("2.4G_survey_off-chan")) { + // + updateColumns = new HashMap<>(); + updateColumns.put("channel_list", channels); + updateColumns.put("radio_type", new Atom<>("2.4G")); + updateColumns.put("reporting_interval", new Atom<>(120)); + updateColumns.put("sampling_interval", new Atom<>(10)); + updateColumns.put("stats_type", new Atom<>("survey")); + updateColumns.put("survey_interval_ms", new Atom<>(50)); + updateColumns.put("survey_type", new Atom<>("off-chan")); + updateColumns.put("threshold", thresholds); + + row = new Row(updateColumns); + operations.add(new Insert(wifiStatsConfigDbTable, row)); + // + } + + if (!provisionedWifiStatsConfigs.containsKey("2.4G_neighbor_off-chan")) { + // + updateColumns = new HashMap<>(); + updateColumns.put("channel_list", channels); + updateColumns.put("radio_type", new Atom<>("2.4G")); + updateColumns.put("reporting_interval", new Atom<>(120)); + updateColumns.put("sampling_interval", new Atom<>(0)); + updateColumns.put("stats_type", new Atom<>("neighbor")); + updateColumns.put("survey_interval_ms", new Atom<>(0)); + updateColumns.put("survey_type", new Atom<>("off-chan")); + // updateColumns.put("threshold", thresholds ); + + row = new Row(updateColumns); + operations.add(new Insert(wifiStatsConfigDbTable, row)); + // + } + + if (!provisionedWifiStatsConfigs.containsKey("5GU_neighbor_on-chan")) { + // + updateColumns = new HashMap<>(); + // updateColumns.put("channel_list", channels ); + updateColumns.put("radio_type", new Atom<>("5GU")); + updateColumns.put("reporting_interval", new Atom<>(60)); + updateColumns.put("sampling_interval", new Atom<>(0)); + updateColumns.put("stats_type", new Atom<>("neighbor")); + updateColumns.put("survey_interval_ms", new Atom<>(0)); + updateColumns.put("survey_type", new Atom<>("on-chan")); + // updateColumns.put("threshold", thresholds ); + + row = new Row(updateColumns); + operations.add(new Insert(wifiStatsConfigDbTable, row)); + // + } + + if (!provisionedWifiStatsConfigs.containsKey("5GL_client_null")) { + // + updateColumns = new HashMap<>(); + // updateColumns.put("channel_list", channels ); + updateColumns.put("radio_type", new Atom<>("5GL")); + updateColumns.put("reporting_interval", new Atom<>(60)); + updateColumns.put("sampling_interval", new Atom<>(10)); + updateColumns.put("stats_type", new Atom<>("client")); + // updateColumns.put("survey_interval_ms", new Atom<>(0) ); + // updateColumns.put("survey_type", new Atom<>("on-chan") ); + // updateColumns.put("threshold", thresholds ); + + row = new Row(updateColumns); + operations.add(new Insert(wifiStatsConfigDbTable, row)); + // + } + + if (!provisionedWifiStatsConfigs.containsKey("5GU_client_null")) { + // + updateColumns = new HashMap<>(); + // updateColumns.put("channel_list", channels ); + updateColumns.put("radio_type", new Atom<>("5GU")); + updateColumns.put("reporting_interval", new Atom<>(60)); + updateColumns.put("sampling_interval", new Atom<>(10)); + updateColumns.put("stats_type", new Atom<>("client")); + // updateColumns.put("survey_interval_ms", new Atom<>(0) ); + // updateColumns.put("survey_type", new Atom<>("on-chan") ); + // updateColumns.put("threshold", thresholds ); + + row = new Row(updateColumns); + operations.add(new Insert(wifiStatsConfigDbTable, row)); + // + } + + if (!provisionedWifiStatsConfigs.containsKey("2.4G_survey_on-chan")) { + // + updateColumns = new HashMap<>(); + // updateColumns.put("channel_list", channels ); + updateColumns.put("radio_type", new Atom<>("2.4G")); + updateColumns.put("reporting_interval", new Atom<>(60)); + updateColumns.put("sampling_interval", new Atom<>(10)); + updateColumns.put("stats_type", new Atom<>("survey")); + updateColumns.put("survey_interval_ms", new Atom<>(0)); + updateColumns.put("survey_type", new Atom<>("on-chan")); + // updateColumns.put("threshold", thresholds ); + + row = new Row(updateColumns); + operations.add(new Insert(wifiStatsConfigDbTable, row)); + // + } + + if (!provisionedWifiStatsConfigs.containsKey("2.4G_client_null")) { + // + updateColumns = new HashMap<>(); + // updateColumns.put("channel_list", channels ); + updateColumns.put("radio_type", new Atom<>("2.4G")); + updateColumns.put("reporting_interval", new Atom<>(60)); + updateColumns.put("sampling_interval", new Atom<>(10)); + updateColumns.put("stats_type", new Atom<>("client")); + // updateColumns.put("survey_interval_ms", new Atom<>(0) ); + // updateColumns.put("survey_type", new Atom<>("on-chan") ); + // updateColumns.put("threshold", thresholds ); + + row = new Row(updateColumns); + operations.add(new Insert(wifiStatsConfigDbTable, row)); + // + } + + if (!provisionedWifiStatsConfigs.containsKey("2.4G_neighbor_on-chan")) { + // + updateColumns = new HashMap<>(); + // updateColumns.put("channel_list", channels ); + updateColumns.put("radio_type", new Atom<>("2.4G")); + updateColumns.put("reporting_interval", new Atom<>(60)); + updateColumns.put("sampling_interval", new Atom<>(0)); + updateColumns.put("stats_type", new Atom<>("neighbor")); + updateColumns.put("survey_interval_ms", new Atom<>(0)); + updateColumns.put("survey_type", new Atom<>("on-chan")); + // updateColumns.put("threshold", thresholds ); + + row = new Row(updateColumns); + operations.add(new Insert(wifiStatsConfigDbTable, row)); + // + } + + if (!provisionedWifiStatsConfigs.containsKey("5GU_survey_on-chan")) { + // + updateColumns = new HashMap<>(); + // updateColumns.put("channel_list", channels ); + updateColumns.put("radio_type", new Atom<>("5GU")); + updateColumns.put("reporting_interval", new Atom<>(60)); + updateColumns.put("sampling_interval", new Atom<>(10)); + updateColumns.put("stats_type", new Atom<>("survey")); + updateColumns.put("survey_interval_ms", new Atom<>(0)); + updateColumns.put("survey_type", new Atom<>("on-chan")); + // updateColumns.put("threshold", thresholds ); + + row = new Row(updateColumns); + operations.add(new Insert(wifiStatsConfigDbTable, row)); + // + } + + if (!provisionedWifiStatsConfigs.containsKey("5GL_neighbor_on-chan")) { + // + updateColumns = new HashMap<>(); + // updateColumns.put("channel_list", channels ); + updateColumns.put("radio_type", new Atom<>("5GL")); + updateColumns.put("reporting_interval", new Atom<>(60)); + updateColumns.put("sampling_interval", new Atom<>(0)); + updateColumns.put("stats_type", new Atom<>("neighbor")); + updateColumns.put("survey_interval_ms", new Atom<>(0)); + updateColumns.put("survey_type", new Atom<>("on-chan")); + // updateColumns.put("threshold", thresholds ); + + row = new Row(updateColumns); + operations.add(new Insert(wifiStatsConfigDbTable, row)); + // + } + + for (String band : new String[] { "2.4G", "5GL", "5G" }) { + updateColumns = new HashMap<>(); + updateColumns.put("radio_type", new Atom<>(band)); + updateColumns.put("reporting_interval", new Atom<>(120)); + updateColumns.put("sampling_interval", new Atom<>(0)); + updateColumns.put("stats_type", new Atom<>("rssi")); + updateColumns.put("survey_interval_ms", new Atom<>(0)); + updateColumns.put("survey_type", new Atom<>("on-chan")); + row = new Row(updateColumns); + + if (provisionedWifiStatsConfigs.containsKey(band + "_rssi_onChannel")) { + operations.add(new Update(wifiStatsConfigDbTable, row)); + } else { + operations.add(new Insert(wifiStatsConfigDbTable, row)); + } + } + + if (!operations.isEmpty()) { + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + if (LOG.isDebugEnabled()) { + LOG.debug("Updated {}:", wifiStatsConfigDbTable); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + } + } + + } catch (OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + } + + public String changeRedirectorAddress(OvsdbClient ovsdbClient, String apId, String newRedirectorAddress) { + try { + List operations = new ArrayList<>(); + Map updateColumns = new HashMap<>(); + + updateColumns.put("redirector_addr", new Atom<>(newRedirectorAddress)); + + Row row = new Row(updateColumns); + operations.add(new Update(awlanNodeDbTable, row)); + + CompletableFuture fResult = ovsdbClient.transact(ovsdbName, operations); + OperationResult[] result = fResult.get(ovsdbTimeoutSec, TimeUnit.SECONDS); + + if (LOG.isDebugEnabled()) { + LOG.debug("Updated {} redirector_addr = {}", awlanNodeDbTable, newRedirectorAddress); + + for (OperationResult res : result) { + LOG.debug("Op Result {}", res); + } + } + + } catch (OvsdbClientException | TimeoutException | ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + + return newRedirectorAddress; + } }