Search in sources :

Example 1 with GwtModemPdpType

use of org.eclipse.kura.web.shared.model.GwtModemPdpType in project kura by eclipse.

the class GwtNetworkServiceImpl method updateNetInterfaceConfigurations.

@Override
public void updateNetInterfaceConfigurations(GwtXSRFToken xsrfToken, GwtNetInterfaceConfig config) throws GwtKuraException {
    checkXSRFToken(xsrfToken);
    NetworkAdminService nas = ServiceLocator.getInstance().getService(NetworkAdminService.class);
    s_logger.debug("config.getStatus(): {}", GwtSafeHtmlUtils.htmlEscape(config.getStatus()));
    String status = config.getStatus();
    boolean autoConnect = true;
    if (GwtNetIfStatus.netIPv4StatusDisabled.name().equals(status)) {
        autoConnect = false;
    }
    try {
        // Interface status
        NetInterfaceStatus netInterfaceStatus = null;
        if (config.getStatus().equals(GwtNetIfStatus.netIPv4StatusDisabled.name())) {
            netInterfaceStatus = NetInterfaceStatus.netIPv4StatusDisabled;
        } else if (config.getStatus().equals(GwtNetIfStatus.netIPv4StatusEnabledLAN.name())) {
            netInterfaceStatus = NetInterfaceStatus.netIPv4StatusEnabledLAN;
        } else if (config.getStatus().equals(GwtNetIfStatus.netIPv4StatusEnabledWAN.name())) {
            netInterfaceStatus = NetInterfaceStatus.netIPv4StatusEnabledWAN;
        }
        // Set up configs
        List<NetConfig> netConfigs = new ArrayList<NetConfig>();
        // Initialize NetConfigIP4 object
        NetConfigIP4 netConfig4 = new NetConfigIP4(netInterfaceStatus, autoConnect);
        // build the appropriate NetConfig objects for ethernet type
        if (config.getHwTypeEnum() == GwtNetIfType.ETHERNET || config.getHwTypeEnum() == GwtNetIfType.WIFI || config.getHwTypeEnum() == GwtNetIfType.MODEM) {
            s_logger.debug("config.getConfigMode(): {}", config.getConfigMode());
            String regexp = "[\\s,;\\n\\t]+";
            if (GwtNetIfConfigMode.netIPv4ConfigModeDHCP.name().equals(config.getConfigMode())) {
                s_logger.debug("mode is DHCP");
                netConfig4.setDhcp(true);
            } else {
                s_logger.debug("mode is STATIC");
                netConfig4.setDhcp(false);
                if (config.getIpAddress() != null && !config.getIpAddress().isEmpty()) {
                    s_logger.debug("setting address: {}", config.getIpAddress());
                    netConfig4.setAddress((IP4Address) IPAddress.parseHostAddress(config.getIpAddress()));
                }
                if (config.getSubnetMask() != null && !config.getSubnetMask().isEmpty()) {
                    s_logger.debug("setting subnet mask: {}", config.getSubnetMask());
                    netConfig4.setSubnetMask((IP4Address) IPAddress.parseHostAddress(config.getSubnetMask()));
                }
                if (config.getGateway() != null && !config.getGateway().isEmpty()) {
                    s_logger.debug("setting gateway: {}", config.getGateway());
                    netConfig4.setGateway((IP4Address) IPAddress.parseHostAddress(config.getGateway()));
                }
                String[] winServersString = config.getSearchDomains().split(regexp);
                if (winServersString != null && winServersString.length > 0) {
                    IP4Address winServer;
                    List<IP4Address> dnsServers = new ArrayList<IP4Address>();
                    for (String winsEntry : winServersString) {
                        if (!winsEntry.trim().isEmpty()) {
                            s_logger.debug("setting WINs: {}", winsEntry);
                            winServer = (IP4Address) IPAddress.parseHostAddress(winsEntry);
                            dnsServers.add(winServer);
                        }
                    }
                    netConfig4.setDnsServers(dnsServers);
                }
            }
            String[] dnsServersString = config.getDnsServers().split(regexp);
            if (dnsServersString != null && dnsServersString.length > 0) {
                IP4Address dnsServer;
                List<IP4Address> dnsServers = new ArrayList<IP4Address>();
                for (String dnsEntry : dnsServersString) {
                    if (!dnsEntry.trim().isEmpty()) {
                        s_logger.debug("setting DNS: {}", dnsEntry);
                        dnsServer = (IP4Address) IPAddress.parseHostAddress(dnsEntry);
                        dnsServers.add(dnsServer);
                    }
                }
                netConfig4.setDnsServers(dnsServers);
            }
            netConfigs.add(netConfig4);
            // Set up DHCP and NAT
            if (!GwtNetIfConfigMode.netIPv4ConfigModeDHCP.name().equals(config.getConfigMode())) {
                // <--
                List<NetConfig> dhcpConfigs = getDhcpConfig(config);
                if (dhcpConfigs != null) {
                    s_logger.debug("Adding dhcp and/or nat configs to interface update config");
                    netConfigs.addAll(dhcpConfigs);
                }
            }
            if (config.getHwTypeEnum() == GwtNetIfType.ETHERNET) {
                nas.updateEthernetInterfaceConfig(GwtSafeHtmlUtils.htmlEscape(config.getName()), autoConnect, config.getHwMTU(), netConfigs);
            }
        }
        if (config.getHwTypeEnum() == GwtNetIfType.WIFI) {
            if (config instanceof GwtWifiNetInterfaceConfig) {
                GwtWifiConfig gwtWifiConfig = ((GwtWifiNetInterfaceConfig) config).getActiveWifiConfig();
                if (gwtWifiConfig != null) {
                    WifiConfig wifiConfig = getWifiConfig(gwtWifiConfig);
                    String passKey = new String(wifiConfig.getPasskey().getPassword());
                    if (passKey != null && passKey.equals(PLACEHOLDER)) {
                        List<GwtNetInterfaceConfig> result = privateFindNetInterfaceConfigurations();
                        for (GwtNetInterfaceConfig netConfig : result) {
                            if (netConfig instanceof GwtWifiNetInterfaceConfig && config.getName().equals(((GwtWifiNetInterfaceConfig) netConfig).getName())) {
                                GwtWifiNetInterfaceConfig oldWifiConfig = (GwtWifiNetInterfaceConfig) netConfig;
                                GwtWifiConfig oldGwtWifiConfig;
                                if (gwtWifiConfig.getWirelessMode().equals(GwtWifiWirelessMode.netWifiWirelessModeAccessPoint.name())) {
                                    oldGwtWifiConfig = oldWifiConfig.getAccessPointWifiConfig();
                                } else {
                                    oldGwtWifiConfig = oldWifiConfig.getStationWifiConfig();
                                }
                                if (oldGwtWifiConfig != null) {
                                    wifiConfig.setPasskey(oldGwtWifiConfig.getPassword());
                                }
                            }
                        }
                    }
                    netConfigs.add(wifiConfig);
                    nas.updateWifiInterfaceConfig(GwtSafeHtmlUtils.htmlEscape(config.getName()), autoConnect, null, netConfigs);
                }
            }
        } else if (config.getHwTypeEnum() == GwtNetIfType.MODEM) {
            if (config instanceof GwtModemInterfaceConfig) {
                GwtModemInterfaceConfig gwtModemConfig = (GwtModemInterfaceConfig) config;
                ModemConfig modemConfig = new ModemConfig();
                String serialNum = gwtModemConfig.getHwSerial();
                String modemId = gwtModemConfig.getModemId();
                int pppNum = gwtModemConfig.getPppNum();
                // modem enabled/disabled
                if (netInterfaceStatus.equals(NetInterfaceStatus.netIPv4StatusEnabledWAN)) {
                    modemConfig.setEnabled(true);
                } else {
                    modemConfig.setEnabled(false);
                }
                modemConfig.setApn(gwtModemConfig.getApn());
                modemConfig.setPppNumber(gwtModemConfig.getPppNum());
                modemConfig.setDataCompression(gwtModemConfig.getDataCompression());
                modemConfig.setDialString(gwtModemConfig.getDialString());
                modemConfig.setHeaderCompression(gwtModemConfig.getHeaderCompression());
                String passKey = new String(gwtModemConfig.getPassword());
                if (passKey != null && passKey.equals(PLACEHOLDER)) {
                    List<GwtNetInterfaceConfig> result = privateFindNetInterfaceConfigurations();
                    for (GwtNetInterfaceConfig netConfig : result) {
                        if (netConfig instanceof GwtModemInterfaceConfig) {
                            GwtModemInterfaceConfig oldModemConfig = (GwtModemInterfaceConfig) netConfig;
                            if (gwtModemConfig.getName().equals(oldModemConfig.getName())) {
                                modemConfig.setPassword(oldModemConfig.getPassword());
                            }
                        }
                    }
                } else if (passKey != null) {
                    modemConfig.setPassword(passKey);
                }
                modemConfig.setUsername(gwtModemConfig.getUsername());
                modemConfig.setResetTimeout(gwtModemConfig.getResetTimeout());
                modemConfig.setPersist(gwtModemConfig.isPersist());
                modemConfig.setMaxFail(gwtModemConfig.getMaxFail());
                modemConfig.setIdle(gwtModemConfig.getIdle());
                modemConfig.setActiveFilter(gwtModemConfig.getActiveFilter());
                modemConfig.setLcpEchoInterval(gwtModemConfig.getLcpEchoInterval());
                modemConfig.setLcpEchoFailure(gwtModemConfig.getLcpEchoFailure());
                modemConfig.setGpsEnabled(gwtModemConfig.isGpsEnabled());
                GwtModemAuthType authType = gwtModemConfig.getAuthType();
                if (authType != null) {
                    if (authType.equals(GwtModemAuthType.netModemAuthNONE)) {
                        modemConfig.setAuthType(ModemConfig.AuthType.NONE);
                    } else if (authType.equals(GwtModemAuthType.netModemAuthAUTO)) {
                        modemConfig.setAuthType(ModemConfig.AuthType.AUTO);
                    } else if (authType.equals(GwtModemAuthType.netModemAuthCHAP)) {
                        modemConfig.setAuthType(ModemConfig.AuthType.CHAP);
                    } else if (authType.equals(GwtModemAuthType.netModemAuthPAP)) {
                        modemConfig.setAuthType(ModemConfig.AuthType.PAP);
                    }
                }
                GwtModemPdpType pdpType = gwtModemConfig.getPdpType();
                if (pdpType != null) {
                    if (pdpType.equals(GwtModemPdpType.netModemPdpIP)) {
                        modemConfig.setPdpType(ModemConfig.PdpType.IP);
                    } else if (pdpType.equals(GwtModemPdpType.netModemPdpIPv6)) {
                        modemConfig.setPdpType(ModemConfig.PdpType.IPv6);
                    } else if (pdpType.equals(GwtModemPdpType.netModemPdpPPP)) {
                        modemConfig.setPdpType(ModemConfig.PdpType.PPP);
                    } else {
                        modemConfig.setPdpType(ModemConfig.PdpType.UNKNOWN);
                    }
                }
                netConfigs.add(modemConfig);
                nas.updateModemInterfaceConfig(config.getName(), serialNum, modemId, pppNum, autoConnect, -1, netConfigs);
            }
        } else {
        // TODO - more types
        }
    } catch (Exception e) {
        s_logger.warn("Failed", e);
        throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
    }
}
Also used : GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) GwtModemAuthType(org.eclipse.kura.web.shared.model.GwtModemAuthType) NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) IP4Address(org.eclipse.kura.net.IP4Address) GwtWifiConfig(org.eclipse.kura.web.shared.model.GwtWifiConfig) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) GwtWifiConfig(org.eclipse.kura.web.shared.model.GwtWifiConfig) ArrayList(java.util.ArrayList) GwtWifiNetInterfaceConfig(org.eclipse.kura.web.shared.model.GwtWifiNetInterfaceConfig) KuraException(org.eclipse.kura.KuraException) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) UnknownHostException(java.net.UnknownHostException) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) ModemConfig(org.eclipse.kura.net.modem.ModemConfig) GwtModemPdpType(org.eclipse.kura.web.shared.model.GwtModemPdpType) GwtNetInterfaceConfig(org.eclipse.kura.web.shared.model.GwtNetInterfaceConfig) NetConfig(org.eclipse.kura.net.NetConfig) NetworkAdminService(org.eclipse.kura.net.NetworkAdminService) List(java.util.List) ArrayList(java.util.ArrayList) GwtModemInterfaceConfig(org.eclipse.kura.web.shared.model.GwtModemInterfaceConfig)

Aggregations

UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 KuraException (org.eclipse.kura.KuraException)1 IP4Address (org.eclipse.kura.net.IP4Address)1 NetConfig (org.eclipse.kura.net.NetConfig)1 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)1 NetInterfaceStatus (org.eclipse.kura.net.NetInterfaceStatus)1 NetworkAdminService (org.eclipse.kura.net.NetworkAdminService)1 ModemConfig (org.eclipse.kura.net.modem.ModemConfig)1 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)1 GwtKuraException (org.eclipse.kura.web.shared.GwtKuraException)1 GwtModemAuthType (org.eclipse.kura.web.shared.model.GwtModemAuthType)1 GwtModemInterfaceConfig (org.eclipse.kura.web.shared.model.GwtModemInterfaceConfig)1 GwtModemPdpType (org.eclipse.kura.web.shared.model.GwtModemPdpType)1 GwtNetInterfaceConfig (org.eclipse.kura.web.shared.model.GwtNetInterfaceConfig)1 GwtWifiConfig (org.eclipse.kura.web.shared.model.GwtWifiConfig)1 GwtWifiNetInterfaceConfig (org.eclipse.kura.web.shared.model.GwtWifiNetInterfaceConfig)1