Search in sources :

Example 1 with NetConfig6

use of org.eclipse.kura.net.NetConfig6 in project kura by eclipse.

the class NetworkAdminServiceImpl method updateWifiInterfaceConfig.

@Override
public void updateWifiInterfaceConfig(String interfaceName, boolean autoConnect, WifiAccessPoint accessPoint, List<NetConfig> netConfigs) throws KuraException {
    NetConfigIP4 netConfig4 = null;
    NetConfigIP6 netConfig6 = null;
    WifiConfig wifiConfig = null;
    DhcpServerConfigIP4 dhcpServerConfigIP4 = null;
    FirewallAutoNatConfig natConfig = null;
    boolean hadNetConfig4 = false;
    boolean hadNetConfig6 = false;
    boolean hadWifiConfig = false;
    boolean hadDhcpServerConfigIP4 = false;
    boolean hadNatConfig = false;
    if (netConfigs != null && !netConfigs.isEmpty()) {
        for (NetConfig netConfig : netConfigs) {
            if (!netConfig.isValid()) {
                throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, "NetConfig Configuration is invalid: " + netConfig.toString());
            }
            if (netConfig instanceof NetConfigIP4) {
                s_logger.debug("got new NetConfigIP4");
                netConfig4 = (NetConfigIP4) netConfig;
            } else if (netConfig instanceof NetConfigIP6) {
                s_logger.debug("got new NetConfigIP6");
                netConfig6 = (NetConfigIP6) netConfig;
            } else if (netConfig instanceof WifiConfig) {
                s_logger.debug("got new WifiConfig");
                wifiConfig = (WifiConfig) netConfig;
            } else if (netConfig instanceof DhcpServerConfigIP4) {
                s_logger.debug("got new DhcpServerConfigIP4");
                dhcpServerConfigIP4 = (DhcpServerConfigIP4) netConfig;
            } else if (netConfig instanceof FirewallAutoNatConfig) {
                s_logger.debug("got new NatConfig");
                natConfig = (FirewallAutoNatConfig) netConfig;
            }
        }
    }
    // validation
    if (netConfig4 == null && netConfig6 == null) {
        throw new KuraException(KuraErrorCode.CONFIGURATION_REQUIRED_ATTRIBUTE_MISSING, "Either IPv4 or IPv6 configuration must be defined");
    }
    if (wifiConfig == null) {
        throw new KuraException(KuraErrorCode.CONFIGURATION_REQUIRED_ATTRIBUTE_MISSING, "WiFi configuration must be defined");
    }
    List<String> modifiedInterfaceNames = new ArrayList<String>();
    boolean configurationChanged = false;
    ComponentConfiguration originalNetworkComponentConfiguration = ((SelfConfiguringComponent) this.m_networkConfigurationService).getConfiguration();
    if (originalNetworkComponentConfiguration == null) {
        return;
    }
    try {
        NetworkConfiguration newNetworkConfiguration = new NetworkConfiguration(originalNetworkComponentConfiguration.getConfigurationProperties());
        List<NetInterfaceConfig<? extends NetInterfaceAddressConfig>> netInterfaceConfigs = newNetworkConfiguration.getNetInterfaceConfigs();
        for (NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : netInterfaceConfigs) {
            if (netInterfaceConfig.getName().equals(interfaceName)) {
                // replace existing configs
                List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
                if (netInterfaceAddressConfigs != null && !netInterfaceAddressConfigs.isEmpty()) {
                    for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
                        List<NetConfig> existingNetConfigs = netInterfaceAddressConfig.getConfigs();
                        List<NetConfig> newNetConfigs = new ArrayList<NetConfig>();
                        WifiMode newWifiMode = wifiConfig != null ? wifiConfig.getMode() : null;
                        for (NetConfig netConfig : existingNetConfigs) {
                            s_logger.debug("looking at existing NetConfig for {} with value: {}", interfaceName, netConfig);
                            if (netConfig instanceof NetConfigIP4) {
                                if (netConfig4 == null) {
                                    s_logger.debug("removing NetConfig4 for {}", interfaceName);
                                } else {
                                    hadNetConfig4 = true;
                                    newNetConfigs.add(netConfig4);
                                    if (!netConfig.equals(netConfig4)) {
                                        s_logger.debug("updating NetConfig4 for {}", interfaceName);
                                        s_logger.debug("Is new State DHCP? {}", netConfig4.isDhcp());
                                        configurationChanged = true;
                                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                                            modifiedInterfaceNames.add(interfaceName);
                                        }
                                    } else {
                                        s_logger.debug("not updating NetConfig4 for {} because it is unchanged", interfaceName);
                                    }
                                }
                            } else if (netConfig instanceof NetConfig6) {
                                if (netConfig6 == null) {
                                    s_logger.debug("removing NetConfig6 for {}", interfaceName);
                                } else {
                                    hadNetConfig6 = true;
                                    newNetConfigs.add(netConfig6);
                                    if (!netConfig.equals(netConfig6)) {
                                        s_logger.debug("updating NetConfig6 for {}", interfaceName);
                                        configurationChanged = true;
                                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                                            modifiedInterfaceNames.add(interfaceName);
                                        }
                                    } else {
                                        s_logger.debug("not updating NetConfig6 for {} because it is unchanged", interfaceName);
                                    }
                                }
                            } else if (netConfig instanceof WifiConfig) {
                                if (wifiConfig == null) {
                                    s_logger.debug("removing wifiConfig for {}", interfaceName);
                                } else {
                                    // others
                                    if (newWifiMode.equals(((WifiConfig) netConfig).getMode())) {
                                        hadWifiConfig = true;
                                        newNetConfigs.add(wifiConfig);
                                        s_logger.debug("checking WifiConfig for {} mode", wifiConfig.getMode());
                                        if (!netConfig.equals(wifiConfig)) {
                                            s_logger.debug("updating WifiConfig for {}", interfaceName);
                                            configurationChanged = true;
                                            if (!modifiedInterfaceNames.contains(interfaceName)) {
                                                modifiedInterfaceNames.add(interfaceName);
                                            }
                                        } else {
                                            s_logger.debug("not updating WifiConfig for {} because it is unchanged", interfaceName);
                                        }
                                    } else {
                                        // Keep the old WifiConfig for the non-selected wifi modes
                                        s_logger.debug("adding other WifiConfig: {}", netConfig);
                                        newNetConfigs.add(netConfig);
                                    }
                                }
                            } else if (netConfig instanceof DhcpServerConfigIP4) {
                                if (dhcpServerConfigIP4 == null) {
                                    s_logger.debug("removing DhcpServerConfigIP4 for {}", interfaceName);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                } else {
                                    hadDhcpServerConfigIP4 = true;
                                    newNetConfigs.add(dhcpServerConfigIP4);
                                    if (!netConfig.equals(dhcpServerConfigIP4)) {
                                        s_logger.debug("updating DhcpServerConfigIP4 for {}", interfaceName);
                                        configurationChanged = true;
                                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                                            modifiedInterfaceNames.add(interfaceName);
                                        }
                                    } else {
                                        s_logger.debug("not updating DhcpServerConfigIP4 for {} because it is unchanged", interfaceName);
                                    }
                                }
                            } else if (netConfig instanceof FirewallAutoNatConfig) {
                                if (natConfig == null) {
                                    s_logger.debug("removing FirewallAutoNatConfig for {}", interfaceName);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                } else {
                                    hadNatConfig = true;
                                    newNetConfigs.add(natConfig);
                                    if (!netConfig.equals(natConfig)) {
                                        s_logger.debug("updating FirewallAutoNatConfig for {}", interfaceName);
                                        configurationChanged = true;
                                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                                            modifiedInterfaceNames.add(interfaceName);
                                        }
                                    } else {
                                        s_logger.debug("not updating FirewallNatConfig for {} because it is unchanged", interfaceName);
                                    }
                                }
                            } else {
                                s_logger.debug("Found unsupported configuration: {}", netConfig.toString());
                            }
                        }
                        // add configs that did not match any in the current configuration
                        if (netConfigs != null && !netConfigs.isEmpty()) {
                            for (NetConfig netConfig : netConfigs) {
                                if (netConfig instanceof NetConfigIP4 && !hadNetConfig4) {
                                    s_logger.debug("adding new NetConfig4 to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                                if (netConfig instanceof NetConfigIP6 && !hadNetConfig6) {
                                    s_logger.debug("adding new NetConfig6 to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                                if (netConfig instanceof WifiConfig && !hadWifiConfig) {
                                    s_logger.debug("adding new WifiConfig to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                                if (netConfig instanceof DhcpServerConfigIP4 && !hadDhcpServerConfigIP4) {
                                    s_logger.debug("adding new DhcpServerConfigIP4 to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                                if (netConfig instanceof FirewallAutoNatConfig && !hadNatConfig) {
                                    s_logger.debug("adding new FirewallAutoNatConfig to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                            }
                        }
                        // Update the wifi mode
                        if (newWifiMode != null) {
                            s_logger.debug("setting address config wifiMode to: {}", newWifiMode);
                            ((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setMode(newWifiMode);
                        }
                        // replace with new list
                        for (NetConfig netConfig : newNetConfigs) {
                            s_logger.debug("Current NetConfig: {} :: {}", netConfig.getClass(), netConfig);
                        }
                        ((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(newNetConfigs);
                    }
                }
            }
        }
        if (configurationChanged) {
            submitNetworkConfiguration(modifiedInterfaceNames, newNetworkConfiguration);
        }
    } catch (UnknownHostException e) {
        s_logger.warn("Exception while updating WifiInterfaceConfig", e);
    }
}
Also used : DhcpServerConfigIP4(org.eclipse.kura.net.dhcp.DhcpServerConfigIP4) FirewallAutoNatConfig(org.eclipse.kura.net.firewall.FirewallAutoNatConfig) NetInterfaceConfig(org.eclipse.kura.net.NetInterfaceConfig) UnknownHostException(java.net.UnknownHostException) NetConfig6(org.eclipse.kura.net.NetConfig6) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) ArrayList(java.util.ArrayList) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) SelfConfiguringComponent(org.eclipse.kura.configuration.SelfConfiguringComponent) ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) NetConfigIP6(org.eclipse.kura.net.NetConfigIP6) KuraException(org.eclipse.kura.KuraException) WifiMode(org.eclipse.kura.net.wifi.WifiMode) NetConfig(org.eclipse.kura.net.NetConfig) NetworkConfiguration(org.eclipse.kura.core.net.NetworkConfiguration) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) WifiInterfaceAddressConfigImpl(org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)

Example 2 with NetConfig6

use of org.eclipse.kura.net.NetConfig6 in project kura by eclipse.

the class NetworkAdminServiceImpl method updateEthernetInterfaceConfig.

@Override
public void updateEthernetInterfaceConfig(String interfaceName, boolean autoConnect, int mtu, List<NetConfig> netConfigs) throws KuraException {
    NetConfigIP4 netConfig4 = null;
    NetConfigIP6 netConfig6 = null;
    DhcpServerConfigIP4 dhcpServerConfigIP4 = null;
    FirewallAutoNatConfig natConfig = null;
    boolean hadNetConfig4 = false;
    boolean hadNetConfig6 = false;
    boolean hadDhcpServerConfigIP4 = false;
    boolean hadNatConfig = false;
    if (netConfigs != null && !netConfigs.isEmpty()) {
        for (NetConfig netConfig : netConfigs) {
            if (!netConfig.isValid()) {
                throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, "NetConfig Configuration is invalid: " + netConfig.toString());
            }
            if (netConfig instanceof NetConfigIP4) {
                netConfig4 = (NetConfigIP4) netConfig;
            } else if (netConfig instanceof NetConfigIP6) {
                netConfig6 = (NetConfigIP6) netConfig;
            } else if (netConfig instanceof DhcpServerConfigIP4) {
                dhcpServerConfigIP4 = (DhcpServerConfigIP4) netConfig;
            } else if (netConfig instanceof FirewallAutoNatConfig) {
                natConfig = (FirewallAutoNatConfig) netConfig;
            }
        }
    }
    // validation
    if (netConfig4 == null && netConfig6 == null) {
        throw new KuraException(KuraErrorCode.CONFIGURATION_REQUIRED_ATTRIBUTE_MISSING, "Either IPv4 or IPv6 configuration must be defined");
    }
    List<String> modifiedInterfaceNames = new ArrayList<String>();
    boolean configurationChanged = false;
    ComponentConfiguration originalNetworkComponentConfiguration = ((SelfConfiguringComponent) this.m_networkConfigurationService).getConfiguration();
    if (originalNetworkComponentConfiguration == null) {
        s_logger.debug("Returning for some unknown reason - no existing config???");
        return;
    }
    try {
        NetworkConfiguration newNetworkConfiguration = new NetworkConfiguration(originalNetworkComponentConfiguration.getConfigurationProperties());
        List<NetInterfaceConfig<? extends NetInterfaceAddressConfig>> netInterfaceConfigs = newNetworkConfiguration.getNetInterfaceConfigs();
        for (NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : netInterfaceConfigs) {
            if (netInterfaceConfig.getName().equals(interfaceName)) {
                // handle MTU
                if (mtu != netInterfaceConfig.getMTU()) {
                    AbstractNetInterface<?> absNetInterfaceConfig = (AbstractNetInterface<?>) netInterfaceConfig;
                    s_logger.debug("updating MTU for {}", interfaceName);
                    absNetInterfaceConfig.setMTU(mtu);
                    configurationChanged = true;
                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                        modifiedInterfaceNames.add(interfaceName);
                    }
                }
                // handle autoconnect
                if (autoConnect != netInterfaceConfig.isAutoConnect()) {
                    AbstractNetInterface<?> absNetInterfaceConfig = (AbstractNetInterface<?>) netInterfaceConfig;
                    s_logger.debug("updating autoConnect for {} to be {}", interfaceName, autoConnect);
                    absNetInterfaceConfig.setAutoConnect(autoConnect);
                    configurationChanged = true;
                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                        modifiedInterfaceNames.add(interfaceName);
                    }
                }
                // replace existing configs
                List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
                if (netInterfaceAddressConfigs != null && !netInterfaceAddressConfigs.isEmpty()) {
                    for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
                        List<NetConfig> existingNetConfigs = netInterfaceAddressConfig.getConfigs();
                        List<NetConfig> newNetConfigs = new ArrayList<NetConfig>();
                        for (NetConfig netConfig : existingNetConfigs) {
                            s_logger.debug("looking at existing NetConfig for {} with value: {}", interfaceName, netConfig);
                            if (netConfig instanceof NetConfigIP4) {
                                if (netConfig4 == null) {
                                    s_logger.debug("removing NetConfig4 for {}", interfaceName);
                                } else {
                                    hadNetConfig4 = true;
                                    newNetConfigs.add(netConfig4);
                                    if (!netConfig.equals(netConfig4)) {
                                        s_logger.debug("updating NetConfig4 for {}", interfaceName);
                                        s_logger.debug("Is new State DHCP? {}", netConfig4.isDhcp());
                                        configurationChanged = true;
                                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                                            modifiedInterfaceNames.add(interfaceName);
                                        }
                                    } else {
                                        s_logger.debug("not updating NetConfig4 for {} because it is unchanged", interfaceName);
                                    }
                                }
                            } else if (netConfig instanceof NetConfig6) {
                                if (netConfig6 == null) {
                                    s_logger.debug("removing NetConfig6 for {}", interfaceName);
                                } else {
                                    hadNetConfig6 = true;
                                    newNetConfigs.add(netConfig6);
                                    if (!netConfig.equals(netConfig6)) {
                                        s_logger.debug("updating NetConfig6 for {}", interfaceName);
                                        configurationChanged = true;
                                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                                            modifiedInterfaceNames.add(interfaceName);
                                        }
                                    } else {
                                        s_logger.debug("not updating NetConfig6 for {} because it is unchanged", interfaceName);
                                    }
                                }
                            } else if (netConfig instanceof DhcpServerConfigIP4) {
                                if (dhcpServerConfigIP4 == null) {
                                    s_logger.debug("removing DhcpServerConfigIP4 for {}", interfaceName);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                } else {
                                    hadDhcpServerConfigIP4 = true;
                                    newNetConfigs.add(dhcpServerConfigIP4);
                                    if (!netConfig.equals(dhcpServerConfigIP4)) {
                                        s_logger.debug("updating DhcpServerConfigIP4 for {}", interfaceName);
                                        configurationChanged = true;
                                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                                            modifiedInterfaceNames.add(interfaceName);
                                        }
                                    } else {
                                        s_logger.debug("not updating DhcpServerConfigIP4 for {} because it is unchanged", interfaceName);
                                    }
                                }
                            } else if (netConfig instanceof FirewallAutoNatConfig) {
                                if (natConfig == null) {
                                    s_logger.debug("removing FirewallAutoNatConfig for {}", interfaceName);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                } else {
                                    hadNatConfig = true;
                                    newNetConfigs.add(natConfig);
                                    if (!netConfig.equals(natConfig)) {
                                        s_logger.debug("updating FirewallAutoNatConfig for {}", interfaceName);
                                        configurationChanged = true;
                                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                                            modifiedInterfaceNames.add(interfaceName);
                                        }
                                    } else {
                                        s_logger.debug("not updating FirewallAutoNatConfig for {} because it is unchanged", interfaceName);
                                    }
                                }
                            } else {
                                s_logger.debug("Found unsupported configuration: {}", netConfig.toString());
                            }
                        }
                        // add configs that did not match any in the current configuration
                        if (netConfigs != null && !netConfigs.isEmpty()) {
                            for (NetConfig netConfig : netConfigs) {
                                if (netConfig instanceof NetConfigIP4 && !hadNetConfig4) {
                                    s_logger.debug("adding new NetConfig4 to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                                if (netConfig instanceof NetConfigIP6 && !hadNetConfig6) {
                                    s_logger.debug("adding new NetConfig6 to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                                if (netConfig instanceof DhcpServerConfigIP4 && !hadDhcpServerConfigIP4) {
                                    s_logger.debug("adding new DhcpServerConfigIP4 to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                                if (netConfig instanceof FirewallAutoNatConfig && !hadNatConfig) {
                                    s_logger.debug("adding new FirewallAutoNatConfig to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                            }
                        }
                        for (NetConfig netConfig : newNetConfigs) {
                            s_logger.debug("New NetConfig: {} :: {}", netConfig.getClass().toString(), netConfig.toString());
                        }
                        // replace with new list
                        ((NetInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(newNetConfigs);
                    }
                }
            }
        }
        if (configurationChanged) {
            submitNetworkConfiguration(modifiedInterfaceNames, newNetworkConfiguration);
        }
    } catch (UnknownHostException e) {
        s_logger.warn("Exception while updating EthernetInterfaceConfig", e);
    }
}
Also used : NetInterfaceAddressConfigImpl(org.eclipse.kura.core.net.NetInterfaceAddressConfigImpl) DhcpServerConfigIP4(org.eclipse.kura.net.dhcp.DhcpServerConfigIP4) FirewallAutoNatConfig(org.eclipse.kura.net.firewall.FirewallAutoNatConfig) NetInterfaceConfig(org.eclipse.kura.net.NetInterfaceConfig) UnknownHostException(java.net.UnknownHostException) NetConfig6(org.eclipse.kura.net.NetConfig6) ArrayList(java.util.ArrayList) AbstractNetInterface(org.eclipse.kura.core.net.AbstractNetInterface) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) SelfConfiguringComponent(org.eclipse.kura.configuration.SelfConfiguringComponent) ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) NetConfigIP6(org.eclipse.kura.net.NetConfigIP6) KuraException(org.eclipse.kura.KuraException) NetConfig(org.eclipse.kura.net.NetConfig) NetworkConfiguration(org.eclipse.kura.core.net.NetworkConfiguration) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig)

Example 3 with NetConfig6

use of org.eclipse.kura.net.NetConfig6 in project kura by eclipse.

the class NetworkAdminServiceImpl method updateModemInterfaceConfig.

@Override
public void updateModemInterfaceConfig(String interfaceName, String serialNum, String modemId, int pppNumber, boolean autoConnect, int mtu, List<NetConfig> netConfigs) throws KuraException {
    NetConfigIP4 netConfig4 = null;
    NetConfigIP6 netConfig6 = null;
    ModemConfig modemConfig = null;
    boolean hadNetConfig4 = false;
    boolean hadNetConfig6 = false;
    boolean hadModemConfig = false;
    if (netConfigs != null && !netConfigs.isEmpty()) {
        for (NetConfig netConfig : netConfigs) {
            if (!netConfig.isValid()) {
                throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, "NetConfig Configuration is invalid: " + netConfig.toString());
            }
            if (netConfig instanceof NetConfigIP4) {
                netConfig4 = (NetConfigIP4) netConfig;
            } else if (netConfig instanceof NetConfigIP6) {
                netConfig6 = (NetConfigIP6) netConfig;
            } else if (netConfig instanceof ModemConfig) {
                modemConfig = (ModemConfig) netConfig;
            }
        }
    }
    // validation
    if (netConfig4 == null && netConfig6 == null) {
        throw new KuraException(KuraErrorCode.CONFIGURATION_REQUIRED_ATTRIBUTE_MISSING, "Either IPv4 or IPv6 configuration must be defined");
    }
    if (modemConfig == null) {
        throw new KuraException(KuraErrorCode.CONFIGURATION_REQUIRED_ATTRIBUTE_MISSING, "Modem configuration must be defined");
    }
    List<String> modifiedInterfaceNames = new ArrayList<String>();
    boolean configurationChanged = false;
    ComponentConfiguration originalNetworkComponentConfiguration = ((SelfConfiguringComponent) this.m_networkConfigurationService).getConfiguration();
    if (originalNetworkComponentConfiguration == null) {
        return;
    }
    try {
        NetworkConfiguration newNetworkConfiguration = new NetworkConfiguration(originalNetworkComponentConfiguration.getConfigurationProperties());
        List<NetInterfaceConfig<? extends NetInterfaceAddressConfig>> netInterfaceConfigs = newNetworkConfiguration.getNetInterfaceConfigs();
        for (NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : netInterfaceConfigs) {
            if (netInterfaceConfig.getName().equals(interfaceName)) {
                // handle MTU
                if (mtu != netInterfaceConfig.getMTU()) {
                    AbstractNetInterface<?> absNetInterfaceConfig = (AbstractNetInterface<?>) netInterfaceConfig;
                    s_logger.debug("updating MTU for {}", interfaceName);
                    absNetInterfaceConfig.setMTU(mtu);
                    configurationChanged = true;
                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                        modifiedInterfaceNames.add(interfaceName);
                    }
                }
                if (netInterfaceConfig instanceof ModemInterfaceConfigImpl) {
                    ModemInterfaceConfigImpl modemInterfaceConfig = (ModemInterfaceConfigImpl) netInterfaceConfig;
                    if (modemId == null) {
                        modemId = "";
                    }
                    // handle modem id
                    if (!modemId.equals(modemInterfaceConfig.getModemIdentifier())) {
                        s_logger.debug("updating Modem identifier: {}", modemId);
                        modemInterfaceConfig.setModemIdentifier(modemId);
                        configurationChanged = true;
                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                            modifiedInterfaceNames.add(interfaceName);
                        }
                    }
                    // handle ppp num
                    if (pppNumber != modemInterfaceConfig.getPppNum()) {
                        s_logger.debug("updating PPP number: {}", pppNumber);
                        modemInterfaceConfig.setPppNum(pppNumber);
                        configurationChanged = true;
                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                            modifiedInterfaceNames.add(interfaceName);
                        }
                    }
                }
                // replace existing configs
                List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
                if (netInterfaceAddressConfigs != null && !netInterfaceAddressConfigs.isEmpty()) {
                    for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
                        List<NetConfig> existingNetConfigs = netInterfaceAddressConfig.getConfigs();
                        List<NetConfig> newNetConfigs = new ArrayList<NetConfig>();
                        for (NetConfig netConfig : existingNetConfigs) {
                            s_logger.debug("looking at existing NetConfig for {} with value: {}", interfaceName, netConfig);
                            if (netConfig instanceof NetConfigIP4) {
                                if (netConfig4 == null) {
                                    s_logger.debug("removing NetConfig4 for {}", interfaceName);
                                } else {
                                    hadNetConfig4 = true;
                                    newNetConfigs.add(netConfig4);
                                    if (!netConfig.equals(netConfig4)) {
                                        s_logger.debug("updating NetConfig4 for {}", interfaceName);
                                        s_logger.debug("Is new State DHCP? {}", netConfig4.isDhcp());
                                        configurationChanged = true;
                                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                                            modifiedInterfaceNames.add(interfaceName);
                                        }
                                    } else {
                                        s_logger.debug("not updating NetConfig4 for {} because it is unchanged", interfaceName);
                                    }
                                }
                            } else if (netConfig instanceof NetConfig6) {
                                if (netConfig6 == null) {
                                    s_logger.debug("removing NetConfig6 for {}", interfaceName);
                                } else {
                                    hadNetConfig6 = true;
                                    newNetConfigs.add(netConfig6);
                                    if (!netConfig.equals(netConfig6)) {
                                        s_logger.debug("updating NetConfig6 for {}", interfaceName);
                                        configurationChanged = true;
                                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                                            modifiedInterfaceNames.add(interfaceName);
                                        }
                                    } else {
                                        s_logger.debug("not updating NetConfig6 for {} because it is unchanged", interfaceName);
                                    }
                                }
                            } else if (netConfig instanceof ModemConfig) {
                                if (modemConfig == null) {
                                    s_logger.debug("removing ModemConfig for {}", interfaceName);
                                } else {
                                    hadModemConfig = true;
                                    newNetConfigs.add(modemConfig);
                                    if (!netConfig.equals(modemConfig)) {
                                        s_logger.debug("updating ModemConfig for {}", interfaceName);
                                        configurationChanged = true;
                                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                                            modifiedInterfaceNames.add(interfaceName);
                                        }
                                    } else {
                                        s_logger.debug("not updating ModemConfig for {} because it is unchanged", interfaceName);
                                    }
                                }
                            } else {
                                s_logger.debug("Found unsupported configuration: {}", netConfig.toString());
                            }
                        }
                        // add configs that did not match any in the current configuration
                        if (netConfigs != null && !netConfigs.isEmpty()) {
                            for (NetConfig netConfig : netConfigs) {
                                if (netConfig instanceof NetConfigIP4 && !hadNetConfig4) {
                                    s_logger.debug("adding new NetConfig4 to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                                if (netConfig instanceof NetConfigIP6 && !hadNetConfig6) {
                                    s_logger.debug("adding new NetConfig6 to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                                if (netConfig instanceof ModemConfig && !hadModemConfig) {
                                    s_logger.debug("adding new ModemConfig to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                            }
                        }
                        for (NetConfig netConfig : newNetConfigs) {
                            s_logger.debug("Current NetConfig: {} :: {}", netConfig.getClass(), netConfig);
                        }
                        // replace with new list
                        ((ModemInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(newNetConfigs);
                    }
                }
            }
            newNetworkConfiguration.addNetInterfaceConfig(netInterfaceConfig);
        }
        if (configurationChanged) {
            submitNetworkConfiguration(modifiedInterfaceNames, newNetworkConfiguration);
        }
    } catch (UnknownHostException e) {
        s_logger.warn("Exception while updating ModemInterfaceConfig", e);
    }
}
Also used : ModemInterfaceAddressConfigImpl(org.eclipse.kura.core.net.modem.ModemInterfaceAddressConfigImpl) NetInterfaceConfig(org.eclipse.kura.net.NetInterfaceConfig) UnknownHostException(java.net.UnknownHostException) NetConfig6(org.eclipse.kura.net.NetConfig6) ArrayList(java.util.ArrayList) AbstractNetInterface(org.eclipse.kura.core.net.AbstractNetInterface) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) SelfConfiguringComponent(org.eclipse.kura.configuration.SelfConfiguringComponent) ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) NetConfigIP6(org.eclipse.kura.net.NetConfigIP6) ModemConfig(org.eclipse.kura.net.modem.ModemConfig) ModemInterfaceConfigImpl(org.eclipse.kura.core.net.modem.ModemInterfaceConfigImpl) KuraException(org.eclipse.kura.KuraException) NetConfig(org.eclipse.kura.net.NetConfig) NetworkConfiguration(org.eclipse.kura.core.net.NetworkConfiguration) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig)

Aggregations

UnknownHostException (java.net.UnknownHostException)3 ArrayList (java.util.ArrayList)3 KuraException (org.eclipse.kura.KuraException)3 ComponentConfiguration (org.eclipse.kura.configuration.ComponentConfiguration)3 SelfConfiguringComponent (org.eclipse.kura.configuration.SelfConfiguringComponent)3 NetworkConfiguration (org.eclipse.kura.core.net.NetworkConfiguration)3 NetConfig (org.eclipse.kura.net.NetConfig)3 NetConfig6 (org.eclipse.kura.net.NetConfig6)3 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)3 NetConfigIP6 (org.eclipse.kura.net.NetConfigIP6)3 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)3 NetInterfaceConfig (org.eclipse.kura.net.NetInterfaceConfig)3 AbstractNetInterface (org.eclipse.kura.core.net.AbstractNetInterface)2 DhcpServerConfigIP4 (org.eclipse.kura.net.dhcp.DhcpServerConfigIP4)2 FirewallAutoNatConfig (org.eclipse.kura.net.firewall.FirewallAutoNatConfig)2 NetInterfaceAddressConfigImpl (org.eclipse.kura.core.net.NetInterfaceAddressConfigImpl)1 WifiInterfaceAddressConfigImpl (org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)1 ModemInterfaceAddressConfigImpl (org.eclipse.kura.core.net.modem.ModemInterfaceAddressConfigImpl)1 ModemInterfaceConfigImpl (org.eclipse.kura.core.net.modem.ModemInterfaceConfigImpl)1 ModemConfig (org.eclipse.kura.net.modem.ModemConfig)1