Search in sources :

Example 16 with NetConfigIP4

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

the class EthernetMonitorServiceImpl method monitor.

private void monitor(String interfaceName) {
    synchronized (s_lock) {
        try {
            List<? extends NetInterfaceAddressConfig> new_niacs = null;
            List<? extends NetInterfaceAddressConfig> cur_niacs = null;
            InterfaceState currentInterfaceState = null;
            boolean interfaceEnabled = false;
            boolean isDhcpClient = false;
            IP4Address staticGateway = null;
            boolean dhcpServerEnabled = false;
            // IPAddress dhcpServerSubnet = null;
            // short dhcpServerPrefix = -1;
            boolean postStatusChangeEvent = false;
            EthernetInterfaceConfigImpl currentInterfaceConfig = this.m_networkConfiguration.get(interfaceName);
            EthernetInterfaceConfigImpl newInterfaceConfig = this.m_newNetworkConfiguration.get(interfaceName);
            // FIXME:MC it should be possible to refactor this under the InterfaceState to avoid dual checks
            if (!LinuxNetworkUtil.isUp(interfaceName)) {
                LinuxNetworkUtil.bringUpDeletingAddress(interfaceName);
            }
            // If a new configuration exists, compare it to the existing configuration
            if (newInterfaceConfig != null) {
                // Get all configurations for the interface
                new_niacs = newInterfaceConfig.getNetInterfaceAddresses();
                if (currentInterfaceConfig != null) {
                    cur_niacs = currentInterfaceConfig.getNetInterfaceAddresses();
                }
                if (isConfigChanged(new_niacs, cur_niacs)) {
                    s_logger.info("Found a new Ethernet network configuration for {}", interfaceName);
                    // Disable the interface to be reconfigured below
                    disableInterface(interfaceName);
                    // Set the current config to the new config
                    this.m_networkConfiguration.put(interfaceName, newInterfaceConfig);
                    currentInterfaceConfig = newInterfaceConfig;
                    // Post a status change event - not to be confusd with the Config Change that I am consuming
                    postStatusChangeEvent = true;
                }
                this.m_newNetworkConfiguration.remove(interfaceName);
            }
            // Monitor for status changes and ensure dhcp server is running when enabled
            interfaceEnabled = isEthernetEnabled(currentInterfaceConfig);
            InterfaceState prevInterfaceState = this.m_interfaceState.get(interfaceName);
            // FIXME:MC Deprecate this constructor and prefer the one with the explicit parameters
            // (String interfaceName, boolean up, boolean link, IPAddress ipAddress)
            // It will save a call to determine the iface type and it will keep InterfaceState
            // as a state object as it should be. Maybe introduce an InterfaceStateBuilder.
            currentInterfaceState = new InterfaceState(NetInterfaceType.ETHERNET, interfaceName);
            if (!currentInterfaceState.equals(prevInterfaceState)) {
                postStatusChangeEvent = true;
            }
            // Find if DHCP server or DHCP client mode is enabled
            if (currentInterfaceConfig != null) {
                NetInterfaceStatus netInterfaceStatus = getStatus(currentInterfaceConfig);
                cur_niacs = currentInterfaceConfig.getNetInterfaceAddresses();
                if (cur_niacs != null && cur_niacs.size() > 0) {
                    for (NetInterfaceAddressConfig niac : cur_niacs) {
                        List<NetConfig> netConfigs = niac.getConfigs();
                        if (netConfigs != null && netConfigs.size() > 0) {
                            for (NetConfig netConfig : netConfigs) {
                                if (netConfig instanceof DhcpServerConfig4) {
                                    // only enable if Enabled for LAN
                                    if (netInterfaceStatus.equals(NetInterfaceStatus.netIPv4StatusEnabledLAN)) {
                                        dhcpServerEnabled = ((DhcpServerConfig4) netConfig).isEnabled();
                                    // dhcpServerSubnet = ((DhcpServerConfig4) netConfig).getSubnet();
                                    // dhcpServerPrefix = ((DhcpServerConfig4) netConfig).getPrefix();
                                    } else {
                                        s_logger.trace("Not enabling DHCP server for {} since it is set to {}", interfaceName, netInterfaceStatus);
                                    }
                                } else if (netConfig instanceof NetConfigIP4) {
                                    isDhcpClient = ((NetConfigIP4) netConfig).isDhcp();
                                    staticGateway = ((NetConfigIP4) netConfig).getGateway();
                                }
                            }
                        }
                    }
                } else {
                    s_logger.debug("No current net interface addresses for {}", interfaceName);
                }
            } else {
                s_logger.debug("Current interface config is null for {}", interfaceName);
            }
            // Enable/disable based on configuration and current status
            boolean interfaceStateChanged = false;
            if (interfaceEnabled) {
                if (currentInterfaceState.isUp()) {
                    if (!currentInterfaceState.isLinkUp()) {
                        s_logger.debug("link is down - disabling {}", interfaceName);
                        disableInterface(interfaceName);
                        interfaceStateChanged = true;
                    }
                } else {
                    // State is currently down
                    if (currentInterfaceState.isLinkUp()) {
                        s_logger.debug("link is up - enabling {}", interfaceName);
                        this.m_netAdminService.enableInterface(interfaceName, isDhcpClient);
                        interfaceStateChanged = true;
                    }
                }
            } else {
                if (currentInterfaceState.isUp()) {
                    s_logger.debug("{} is currently up - disable interface", interfaceName);
                    disableInterface(interfaceName);
                    interfaceStateChanged = true;
                }
            }
            // FIXME: reload the configuration IFF one of above enable/disable happened
            if (interfaceStateChanged) {
                currentInterfaceState = new InterfaceState(NetInterfaceType.ETHERNET, interfaceName);
            }
            // Manage the DHCP server and validate routes
            if (currentInterfaceState != null && currentInterfaceState.isUp() && currentInterfaceState.isLinkUp()) {
                NetInterfaceStatus netInterfaceStatus = getStatus(currentInterfaceConfig);
                if (netInterfaceStatus == NetInterfaceStatus.netIPv4StatusEnabledWAN) {
                    // This should be the default gateway - make sure it is
                    boolean found = false;
                    RouteConfig[] routes = this.m_routeService.getRoutes();
                    if (routes != null && routes.length > 0) {
                        for (RouteConfig route : routes) {
                            if (route.getInterfaceName().equals(interfaceName) && route.getDestination().equals(IPAddress.parseHostAddress("0.0.0.0")) && !route.getGateway().equals(IPAddress.parseHostAddress("0.0.0.0"))) {
                                found = true;
                                break;
                            }
                        }
                    }
                    if (!found) {
                        if (isDhcpClient || staticGateway != null) {
                            // disable the interface and reenable - something didn't happen at initialization as it
                            // was supposed to
                            s_logger.error("WAN interface " + interfaceName + " did not have a route setting it as the default gateway, restarting it");
                            this.m_netAdminService.disableInterface(interfaceName);
                            this.m_netAdminService.enableInterface(interfaceName, isDhcpClient);
                        }
                    }
                } else if (netInterfaceStatus == NetInterfaceStatus.netIPv4StatusEnabledLAN) {
                    if (isDhcpClient) {
                        RouteService rs = RouteServiceImpl.getInstance();
                        RouteConfig rconf = rs.getDefaultRoute(interfaceName);
                        if (rconf != null) {
                            s_logger.debug("{} is configured for LAN/DHCP - removing GATEWAY route ...", rconf.getInterfaceName());
                            rs.removeStaticRoute(rconf.getDestination(), rconf.getGateway(), rconf.getNetmask(), rconf.getInterfaceName());
                        }
                    }
                }
                if (dhcpServerEnabled && !DhcpServerManager.isRunning(interfaceName)) {
                    s_logger.debug("Starting DHCP server for {}", interfaceName);
                    this.m_netAdminService.manageDhcpServer(interfaceName, true);
                }
            } else if (DhcpServerManager.isRunning(interfaceName)) {
                s_logger.debug("Stopping DHCP server for {}", interfaceName);
                this.m_netAdminService.manageDhcpServer(interfaceName, false);
            }
            // post event if there were any changes
            if (postStatusChangeEvent) {
                s_logger.debug("Posting NetworkStatusChangeEvent for {}: {}", interfaceName, currentInterfaceState);
                this.m_eventAdmin.postEvent(new NetworkStatusChangeEvent(interfaceName, currentInterfaceState, null));
                this.m_interfaceState.put(interfaceName, currentInterfaceState);
            }
            // If the interface is disabled in Denali, stop the monitor
            if (!interfaceEnabled) {
                s_logger.debug("{} is disabled - stopping monitor", interfaceName);
                stopMonitor(interfaceName);
            }
        } catch (Exception e) {
            s_logger.warn("Error during Ethernet Monitor", e);
        }
    }
}
Also used : IP4Address(org.eclipse.kura.net.IP4Address) NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) RouteConfig(org.eclipse.kura.net.route.RouteConfig) DhcpServerConfig4(org.eclipse.kura.net.dhcp.DhcpServerConfig4) KuraException(org.eclipse.kura.KuraException) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) EthernetInterfaceConfigImpl(org.eclipse.kura.core.net.EthernetInterfaceConfigImpl) NetworkStatusChangeEvent(org.eclipse.kura.net.admin.event.NetworkStatusChangeEvent) NetConfig(org.eclipse.kura.net.NetConfig) RouteService(org.eclipse.kura.linux.net.route.RouteService) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig)

Example 17 with NetConfigIP4

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

the class IfcfgConfigReader method getConfig.

private void getConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig, Properties kuraExtendedProps) throws KuraException {
    String interfaceName = netInterfaceConfig.getName();
    s_logger.debug("Getting config for {}", interfaceName);
    NetInterfaceType type = netInterfaceConfig.getType();
    if (type == NetInterfaceType.ETHERNET || type == NetInterfaceType.WIFI || type == NetInterfaceType.LOOPBACK) {
        NetInterfaceStatus netInterfaceStatus = null;
        StringBuilder sb = new StringBuilder().append("net.interface.").append(netInterfaceConfig.getName()).append(".config.ip4.status");
        if (kuraExtendedProps != null && kuraExtendedProps.getProperty(sb.toString()) != null) {
            netInterfaceStatus = NetInterfaceStatus.valueOf(kuraExtendedProps.getProperty(sb.toString()));
        } else {
            netInterfaceStatus = NetInterfaceStatus.netIPv4StatusDisabled;
        }
        s_logger.debug("Setting NetInterfaceStatus to " + netInterfaceStatus + " for " + netInterfaceConfig.getName());
        boolean autoConnect = false;
        // int mtu = -1; // MTU is not currently used
        boolean dhcp = false;
        IP4Address address = null;
        String ipAddress = null;
        String prefixString = null;
        String netmask = null;
        String gateway = null;
        File ifcfgFile = null;
        if (OS_VERSION.equals(KuraConstants.Mini_Gateway.getImageName() + "_" + KuraConstants.Mini_Gateway.getImageVersion()) || OS_VERSION.equals(KuraConstants.Raspberry_Pi.getImageName()) || OS_VERSION.equals(KuraConstants.BeagleBone.getImageName()) || OS_VERSION.equals(KuraConstants.Intel_Edison.getImageName() + "_" + KuraConstants.Intel_Edison.getImageVersion() + "_" + KuraConstants.Intel_Edison.getTargetName()) || OS_VERSION.equals(KuraConstants.ReliaGATE_50_21_Ubuntu.getImageName() + "_" + KuraConstants.ReliaGATE_50_21_Ubuntu.getImageVersion())) {
            ifcfgFile = new File(DEBIAN_NET_CONFIGURATION_DIRECTORY + "interfaces");
        } else {
            ifcfgFile = new File(REDHAT_NET_CONFIGURATION_DIRECTORY + "ifcfg-" + interfaceName);
        }
        if (ifcfgFile.exists()) {
            Properties kuraProps;
            // found our match so load the properties
            if (OS_VERSION.equals(KuraConstants.Mini_Gateway.getImageName() + "_" + KuraConstants.Mini_Gateway.getImageVersion()) || OS_VERSION.equals(KuraConstants.Raspberry_Pi.getImageName()) || OS_VERSION.equals(KuraConstants.BeagleBone.getImageName()) || OS_VERSION.equals(KuraConstants.Intel_Edison.getImageName() + "_" + KuraConstants.Intel_Edison.getImageVersion() + "_" + KuraConstants.Intel_Edison.getTargetName()) || OS_VERSION.equals(KuraConstants.ReliaGATE_50_21_Ubuntu.getImageName() + "_" + KuraConstants.ReliaGATE_50_21_Ubuntu.getImageVersion())) {
                kuraProps = parseDebianConfigFile(ifcfgFile, interfaceName);
            } else {
                kuraProps = parseRedhatConfigFile(ifcfgFile, interfaceName);
            }
            if (kuraProps != null) {
                String onBoot = kuraProps.getProperty("ONBOOT");
                if ("yes".equals(onBoot)) {
                    s_logger.debug("Setting autoConnect to true");
                    autoConnect = true;
                } else {
                    s_logger.debug("Setting autoConnect to false");
                    autoConnect = false;
                }
                // override MTU with what is in config if it is present
                /*
                     * IAB: MTU is not currently used
                     * String stringMtu = kuraProps.getProperty("MTU");
                     * if (stringMtu == null) {
                     * try {
                     * mtu = LinuxNetworkUtil.getCurrentMtu(interfaceName);
                     * } catch (KuraException e) {
                     * // just assume ???
                     * if (interfaceName.equals("lo")) {
                     * mtu = 16436;
                     * } else {
                     * mtu = 1500;
                     * }
                     * }
                     * } else {
                     * mtu = Short.parseShort(stringMtu);
                     * }
                     */
                // get the bootproto
                String bootproto = kuraProps.getProperty("BOOTPROTO");
                if (bootproto == null) {
                    bootproto = "static";
                }
                // get the defroute
                String defroute = kuraProps.getProperty("DEFROUTE");
                if (defroute == null) {
                    defroute = "no";
                }
                // actual properties
                if (netInterfaceStatus == NetInterfaceStatus.netIPv4StatusDisabled) {
                    if (autoConnect) {
                        if (defroute.equals("no")) {
                            netInterfaceStatus = NetInterfaceStatus.netIPv4StatusEnabledLAN;
                        } else {
                            netInterfaceStatus = NetInterfaceStatus.netIPv4StatusEnabledWAN;
                        }
                    }
                }
                // check for dhcp or static configuration
                try {
                    ipAddress = kuraProps.getProperty("IPADDR");
                    prefixString = kuraProps.getProperty("PREFIX");
                    netmask = kuraProps.getProperty("NETMASK");
                    kuraProps.getProperty("BROADCAST");
                    try {
                        gateway = kuraProps.getProperty("GATEWAY");
                        s_logger.debug("got gateway for " + interfaceName + ": " + gateway);
                    } catch (Exception e) {
                        s_logger.warn("missing gateway stanza for " + interfaceName);
                    }
                    if (bootproto.equals("dhcp")) {
                        s_logger.debug("currently set for DHCP");
                        dhcp = true;
                        ipAddress = null;
                        netmask = null;
                    } else {
                        s_logger.debug("currently set for static address");
                        dhcp = false;
                    }
                } catch (Exception e) {
                    throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "malformatted config file: " + ifcfgFile.toString(), e);
                }
                if (ipAddress != null && !ipAddress.isEmpty()) {
                    try {
                        address = (IP4Address) IPAddress.parseHostAddress(ipAddress);
                    } catch (UnknownHostException e) {
                        s_logger.warn("Error parsing address: " + ipAddress, e);
                    }
                }
                // make sure at least prefix or netmask is present if static
                if (autoConnect && !dhcp && prefixString == null && netmask == null) {
                    throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "malformatted config file: " + ifcfgFile.toString() + " must contain NETMASK and/or PREFIX");
                }
            }
            List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
            if (netInterfaceAddressConfigs == null) {
                throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "InterfaceAddressConfig list is null");
            } else if (netInterfaceAddressConfigs.size() == 0) {
                throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "InterfaceAddressConfig list has no entries");
            }
            for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
                List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
                if (netConfigs == null) {
                    netConfigs = new ArrayList<NetConfig>();
                    if (netInterfaceAddressConfig instanceof NetInterfaceAddressConfigImpl) {
                        ((NetInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
                        if (dhcp) {
                            // Replace with DNS provided by DHCP server
                            // (displayed as read-only in Denali)
                            List<? extends IPAddress> dhcpDnsServers = getDhcpDnsServers(interfaceName, netInterfaceAddressConfig.getAddress());
                            if (dhcpDnsServers != null) {
                                ((NetInterfaceAddressConfigImpl) netInterfaceAddressConfig).setDnsServers(dhcpDnsServers);
                            }
                        }
                    } else if (netInterfaceAddressConfig instanceof WifiInterfaceAddressConfigImpl) {
                        ((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
                        if (dhcp) {
                            // Replace with DNS provided by DHCP server
                            // (displayed as read-only in Denali)
                            List<? extends IPAddress> dhcpDnsServers = getDhcpDnsServers(interfaceName, netInterfaceAddressConfig.getAddress());
                            if (dhcpDnsServers != null) {
                                ((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setDnsServers(dhcpDnsServers);
                            }
                        }
                    }
                }
                NetConfigIP4 netConfig = new NetConfigIP4(netInterfaceStatus, autoConnect);
                setNetConfigIP4(netConfig, autoConnect, dhcp, address, gateway, prefixString, netmask, kuraProps);
                s_logger.debug("NetConfig: {}", netConfig);
                netConfigs.add(netConfig);
            }
        }
    }
}
Also used : NetInterfaceAddressConfigImpl(org.eclipse.kura.core.net.NetInterfaceAddressConfigImpl) UnknownHostException(java.net.UnknownHostException) NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) IP4Address(org.eclipse.kura.net.IP4Address) Properties(java.util.Properties) KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) NetInterfaceType(org.eclipse.kura.net.NetInterfaceType) KuraException(org.eclipse.kura.KuraException) NetConfig(org.eclipse.kura.net.NetConfig) ArrayList(java.util.ArrayList) List(java.util.List) IPAddress(org.eclipse.kura.net.IPAddress) File(java.io.File) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) WifiInterfaceAddressConfigImpl(org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)

Example 18 with NetConfigIP4

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

the class IfcfgConfigWriter method debianWriteUtility.

private String debianWriteUtility(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig, String interfaceName) {
    List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
    StringBuffer sb = new StringBuffer();
    s_logger.debug("There are " + netInterfaceAddressConfigs.size() + " NetInterfaceAddressConfigs in this configuration");
    for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
        List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
        if (netConfigs != null) {
            for (NetConfig netConfig : netConfigs) {
                if (netConfig instanceof NetConfigIP4) {
                    s_logger.debug("Writing netconfig " + netConfig.getClass().toString() + " for " + interfaceName);
                    // ONBOOT
                    if (((NetConfigIP4) netConfig).isAutoConnect()) {
                        if (OS_VERSION.equals(KuraConstants.Raspberry_Pi.getImageName()) && netInterfaceConfig.getType() == NetInterfaceType.WIFI && ((NetConfigIP4) netConfig).isDhcp()) {
                            sb.append("#!kura!auto " + interfaceName + "\n");
                        } else {
                            sb.append("auto " + interfaceName + "\n");
                        }
                    }
                    // BOOTPROTO
                    if (OS_VERSION.equals(KuraConstants.Raspberry_Pi.getImageName()) && netInterfaceConfig.getType() == NetInterfaceType.WIFI && ((NetConfigIP4) netConfig).isDhcp()) {
                        sb.append("# Commented out to prevent wpa_supplicant from starting dhclient\n");
                        sb.append("#!kura!iface " + interfaceName + " inet ");
                    } else {
                        sb.append("iface " + interfaceName + " inet ");
                    }
                    if (((NetConfigIP4) netConfig).isDhcp()) {
                        s_logger.debug("new config is DHCP");
                        sb.append("dhcp\n");
                    } else {
                        s_logger.debug("new config is STATIC");
                        sb.append("static\n");
                    }
                    if (!((NetConfigIP4) netConfig).isDhcp()) {
                        // IPADDR
                        sb.append("\taddress ").append(((NetConfigIP4) netConfig).getAddress().getHostAddress()).append("\n");
                        // NETMASK
                        sb.append("\tnetmask ").append(((NetConfigIP4) netConfig).getSubnetMask().getHostAddress()).append("\n");
                        // Gateway
                        if (((NetConfigIP4) netConfig).getGateway() != null) {
                            sb.append("\tgateway ").append(((NetConfigIP4) netConfig).getGateway().getHostAddress()).append("\n");
                        }
                    } else {
                        // DEFROUTE
                        if (((NetConfigIP4) netConfig).getStatus() == NetInterfaceStatus.netIPv4StatusEnabledLAN) {
                            sb.append("post-up route del default dev ");
                            sb.append(interfaceName);
                            sb.append("\n");
                        }
                    }
                    // DNS
                    List<? extends IPAddress> dnsAddresses = ((NetConfigIP4) netConfig).getDnsServers();
                    if (!dnsAddresses.isEmpty()) {
                        boolean setDns = false;
                        for (int i = 0; i < dnsAddresses.size(); i++) {
                            if (!dnsAddresses.get(i).getHostAddress().equals("127.0.0.1")) {
                                if (!setDns) {
                                    /*
                                         * IAB:
                                         * If DNS servers are listed, those entries will be appended to the
                                         * /etc/resolv.conf
                                         * file on every ifdown/ifup sequence resulting in multiple entries for the same
                                         * servers.
                                         * (Tested on 10-20, 10-10, and Raspberry Pi).
                                         * Commenting out dns-nameservers in the /etc/network interfaces file allows DNS
                                         * servers
                                         * to be picked up by the IfcfgConfigReader and be displayed on the Web UI but
                                         * the
                                         * /etc/resolv.conf file will only be updated by Kura.
                                         */
                                    sb.append("\t#dns-nameservers ");
                                    setDns = true;
                                }
                                sb.append(dnsAddresses.get(i).getHostAddress() + " ");
                            }
                        }
                        sb.append("\n");
                    }
                }
            }
        } else {
            s_logger.debug("netConfigs is null");
        }
        // WIFI
        if (netInterfaceAddressConfig instanceof WifiInterfaceAddressConfig) {
            s_logger.debug("new config is a WifiInterfaceAddressConfig");
        }
    }
    return sb.toString();
}
Also used : NetConfig(org.eclipse.kura.net.NetConfig) WifiInterfaceAddressConfig(org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4)

Example 19 with NetConfigIP4

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

the class IfcfgConfigWriter method writeKuraExtendedConfig.

public static void writeKuraExtendedConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig) throws KuraException {
    NetInterfaceStatus netInterfaceStatus = null;
    boolean gotNetConfigIP4 = false;
    List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
    if (netInterfaceAddressConfigs != null && netInterfaceAddressConfigs.size() > 0) {
        for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
            List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
            if (netConfigs != null && netConfigs.size() > 0) {
                for (int i = 0; i < netConfigs.size(); i++) {
                    NetConfig netConfig = netConfigs.get(i);
                    if (netConfig instanceof NetConfigIP4) {
                        netInterfaceStatus = ((NetConfigIP4) netConfig).getStatus();
                        gotNetConfigIP4 = true;
                    }
                }
            }
        }
    }
    if (!gotNetConfigIP4) {
        netInterfaceStatus = NetInterfaceStatus.netIPv4StatusDisabled;
    }
    s_logger.debug("Setting NetInterfaceStatus to " + netInterfaceStatus + " for " + netInterfaceConfig.getName());
    // set it all
    Properties kuraExtendedProps = KuranetConfig.getProperties();
    if (kuraExtendedProps == null) {
        s_logger.debug("kuraExtendedProps was null");
        kuraExtendedProps = new Properties();
    }
    StringBuilder sb = new StringBuilder().append("net.interface.").append(netInterfaceConfig.getName()).append(".config.ip4.status");
    kuraExtendedProps.put(sb.toString(), netInterfaceStatus.toString());
    // write it
    if (kuraExtendedProps != null && !kuraExtendedProps.isEmpty()) {
        try {
            KuranetConfig.storeProperties(kuraExtendedProps);
        } catch (IOException e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
        }
    }
}
Also used : NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) KuraException(org.eclipse.kura.KuraException) NetConfig(org.eclipse.kura.net.NetConfig) IOException(java.io.IOException) Properties(java.util.Properties) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4)

Example 20 with NetConfigIP4

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

the class IfcfgConfigWriter method parseNetInterfaceAddressConfig.

private Properties parseNetInterfaceAddressConfig(NetInterfaceAddressConfig netInterfaceAddressConfig) {
    Properties props = new Properties();
    List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
    if (netConfigs != null) {
        for (NetConfig netConfig : netConfigs) {
            if (netConfig instanceof NetConfigIP4) {
                NetConfigIP4 netConfigIP4 = (NetConfigIP4) netConfig;
                // ONBOOT
                props.setProperty("ONBOOT", netConfigIP4.isAutoConnect() ? "yes" : "no");
                // BOOTPROTO
                props.setProperty("BOOTPROTO", netConfigIP4.isDhcp() ? "dhcp" : "static");
                if (!netConfigIP4.isDhcp()) {
                    // IPADDR
                    if (netConfigIP4.getAddress() != null) {
                        props.setProperty("IPADDR", netConfigIP4.getAddress().getHostAddress());
                    }
                    // NETMASK
                    if (netConfigIP4.getSubnetMask() != null) {
                        props.setProperty("NETMASK", netConfigIP4.getSubnetMask().getHostAddress());
                    }
                    // GATEWAY
                    if (netConfigIP4.getGateway() != null) {
                        props.setProperty("GATEWAY", netConfigIP4.getGateway().getHostAddress());
                        props.setProperty("DEFROUTE", "yes");
                    } else {
                        props.setProperty("DEFROUTE", "no");
                    }
                } else {
                    if (((NetConfigIP4) netConfig).getStatus() == NetInterfaceStatus.netIPv4StatusEnabledWAN) {
                        props.setProperty("DEFROUTE", "yes");
                    } else {
                        props.setProperty("DEFROUTE", "no");
                    }
                }
                // DNS
                List<? extends IPAddress> dnsAddresses = ((NetConfigIP4) netConfig).getDnsServers();
                if (!dnsAddresses.isEmpty()) {
                    for (int i = 0; i < dnsAddresses.size(); i++) {
                        if (!dnsAddresses.get(i).getHostAddress().equals("127.0.0.1")) {
                            props.setProperty("DNS" + Integer.toString(i + 1), dnsAddresses.get(i).getHostAddress());
                        }
                    }
                }
            }
        }
    } else {
        s_logger.debug("netConfigs is null");
    }
    return props;
}
Also used : NetConfig(org.eclipse.kura.net.NetConfig) Properties(java.util.Properties) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4)

Aggregations

NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)29 NetConfig (org.eclipse.kura.net.NetConfig)25 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)20 KuraException (org.eclipse.kura.KuraException)19 ArrayList (java.util.ArrayList)13 IP4Address (org.eclipse.kura.net.IP4Address)11 NetInterfaceStatus (org.eclipse.kura.net.NetInterfaceStatus)11 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)11 WifiMode (org.eclipse.kura.net.wifi.WifiMode)11 UnknownHostException (java.net.UnknownHostException)10 IOException (java.io.IOException)9 WifiInterfaceAddressConfig (org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig)9 IPAddress (org.eclipse.kura.net.IPAddress)7 FirewallAutoNatConfig (org.eclipse.kura.net.firewall.FirewallAutoNatConfig)7 ModemConfig (org.eclipse.kura.net.modem.ModemConfig)7 NetConfigIP6 (org.eclipse.kura.net.NetConfigIP6)6 List (java.util.List)5 WifiInterfaceAddressConfigImpl (org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)5 NetworkConfiguration (org.eclipse.kura.core.net.NetworkConfiguration)4 NetInterfaceType (org.eclipse.kura.net.NetInterfaceType)4