Search in sources :

Example 1 with NetInterface

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

the class LifeCyclePayloadBuilder method buildDeviceProfile.

public KuraDeviceProfile buildDeviceProfile() {
    SystemService systemService = this.m_cloudServiceImpl.getSystemService();
    SystemAdminService sysAdminService = this.m_cloudServiceImpl.getSystemAdminService();
    NetworkService networkService = this.m_cloudServiceImpl.getNetworkService();
    PositionService positionService = this.m_cloudServiceImpl.getPositionService();
    // 
    // get the network information
    StringBuilder sbConnectionIp = null;
    StringBuilder sbConnectionInterface = null;
    try {
        List<NetInterface<? extends NetInterfaceAddress>> nis = networkService.getActiveNetworkInterfaces();
        if (!nis.isEmpty()) {
            sbConnectionIp = new StringBuilder();
            sbConnectionInterface = new StringBuilder();
            for (NetInterface<? extends NetInterfaceAddress> ni : nis) {
                List<? extends NetInterfaceAddress> nias = ni.getNetInterfaceAddresses();
                if (nias != null && !nias.isEmpty()) {
                    sbConnectionInterface.append(buildConnectionInterface(ni)).append(",");
                    sbConnectionIp.append(buildConnectionIp(ni)).append(",");
                }
            }
            // Remove trailing comma
            sbConnectionIp.deleteCharAt(sbConnectionIp.length() - 1);
            sbConnectionInterface.deleteCharAt(sbConnectionInterface.length() - 1);
        }
    } catch (Exception se) {
        s_logger.warn("Error while getting ConnetionIP and ConnectionInterface", se);
    }
    String connectionIp = sbConnectionIp != null ? sbConnectionIp.toString() : "UNKNOWN";
    String connectionInterface = sbConnectionInterface != null ? sbConnectionInterface.toString() : "UNKNOWN";
    // 
    // get the network information
    // String primaryNetInterface = systemService.getPrimaryNetworkInterfaceName();
    // String connectionIp = UNKNOWN;
    // String connectionInterface = UNKNOWN;
    // try {
    // List<NetInterface<? extends NetInterfaceAddress>> nis = networkService.getActiveNetworkInterfaces();
    // if (!nis.isEmpty()) {
    // 
    // // look for the primary network interface first
    // for (NetInterface<? extends NetInterfaceAddress> ni : nis) {
    // if (ni.getName().equals(primaryNetInterface)) {
    // List<? extends NetInterfaceAddress> nias = ni.getNetInterfaceAddresses();
    // if (nias != null && !nias.isEmpty()) {
    // connectionInterface = buildConnectionInterface(ni);
    // connectionIp = buildConnectionIp(ni);
    // break;
    // }
    // }
    // }
    // 
    // // if not resolved, loop through all network interfaces until we find one with an address
    // if (UNKNOWN.equals(connectionIp) || UNKNOWN.equals(connectionInterface)) {
    // s_logger.warn("Unresolved connectionIp for primary Network Interface. Looping through all interfaces...");
    // for (NetInterface<? extends NetInterfaceAddress> ni : nis) {
    // List<? extends NetInterfaceAddress> nias = ni.getNetInterfaceAddresses();
    // if (nias != null && !nias.isEmpty()) {
    // connectionInterface = buildConnectionInterface(ni);
    // connectionIp = buildConnectionIp(ni);
    // break;
    // }
    // }
    // }
    // }
    // 
    // if (UNKNOWN.equals(connectionIp) || UNKNOWN.equals(connectionInterface)) {
    // s_logger.warn("Unresolved NetworkService reference or IP address. Defaulting to JVM Networking
    // Information.");
    // InetAddress addr = NetUtil.getCurrentInetAddress();
    // if (addr != null) {
    // connectionIp = addr.getHostAddress();
    // NetworkInterface netInterface = NetworkInterface.getByInetAddress(addr);
    // if (netInterface != null) {
    // connectionInterface = NetUtil.hardwareAddressToString(netInterface.getHardwareAddress());
    // }
    // }
    // }
    // }
    // catch (Exception se) {
    // s_logger.warn("Error while getting ConnetionIP and ConnectionInterface", se);
    // }
    // 
    // get the position information
    double latitude = 0.0;
    double longitude = 0.0;
    double altitude = 0.0;
    if (positionService != null) {
        NmeaPosition position = positionService.getNmeaPosition();
        if (position != null) {
            latitude = position.getLatitude();
            longitude = position.getLongitude();
            altitude = position.getAltitude();
        } else {
            s_logger.warn("Unresolved PositionService reference.");
        }
    }
    // 
    // build the profile
    KuraDeviceProfile KuraDeviceProfile = new KuraDeviceProfile(sysAdminService.getUptime(), systemService.getDeviceName(), systemService.getModelName(), systemService.getModelId(), systemService.getPartNumber(), systemService.getSerialNumber(), systemService.getFirmwareVersion(), systemService.getBiosVersion(), systemService.getOsName(), systemService.getOsVersion(), systemService.getJavaVmName(), systemService.getJavaVmVersion() + " " + systemService.getJavaVmInfo(), systemService.getJavaVendor() + " " + systemService.getJavaVersion(), systemService.getKuraVersion(), connectionInterface, connectionIp, latitude, longitude, altitude, String.valueOf(systemService.getNumberOfProcessors()), String.valueOf(systemService.getTotalMemory()), systemService.getOsArch(), systemService.getOsgiFwName(), systemService.getOsgiFwVersion());
    return KuraDeviceProfile;
}
Also used : SystemAdminService(org.eclipse.kura.system.SystemAdminService) NmeaPosition(org.eclipse.kura.position.NmeaPosition) PositionService(org.eclipse.kura.position.PositionService) SystemService(org.eclipse.kura.system.SystemService) NetInterface(org.eclipse.kura.net.NetInterface) KuraDeviceProfile(org.eclipse.kura.core.message.KuraDeviceProfile) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) NetworkService(org.eclipse.kura.net.NetworkService)

Example 2 with NetInterface

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

the class SystemServiceImpl method getPrimaryMacAddress.

@Override
public String getPrimaryMacAddress() {
    String primaryNetworkInterfaceName = getPrimaryNetworkInterfaceName();
    String macAddress = null;
    InetAddress ip;
    if (OS_MAC_OSX.equals(getOsName())) {
        SafeProcess proc = null;
        try {
            s_logger.info("executing: ifconfig and looking for " + primaryNetworkInterfaceName);
            proc = ProcessUtil.exec("ifconfig");
            BufferedReader br = null;
            try {
                proc.waitFor();
                br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                String line = null;
                while ((line = br.readLine()) != null) {
                    if (line.startsWith(primaryNetworkInterfaceName)) {
                        // get the next line and save the MAC
                        line = br.readLine();
                        if (line == null) {
                            throw new IOException("Null imput!");
                        }
                        if (!line.trim().startsWith("ether")) {
                            line = br.readLine();
                        }
                        String[] splitLine = line.split(" ");
                        if (splitLine.length > 0) {
                            return splitLine[1].toUpperCase();
                        }
                    }
                }
            } catch (InterruptedException e) {
                s_logger.error("Exception while executing ifconfig!", e);
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException ex) {
                        s_logger.error("I/O Exception while closing BufferedReader!");
                    }
                }
            }
        } catch (Exception e) {
            s_logger.error("Failed to get network interfaces", e);
        } finally {
            if (proc != null) {
                ProcessUtil.destroy(proc);
            }
        }
    } else if (getOsName().contains("Windows")) {
        try {
            s_logger.info("executing: InetAddress.getLocalHost " + primaryNetworkInterfaceName);
            ip = InetAddress.getLocalHost();
            Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
            while (networks.hasMoreElements()) {
                NetworkInterface network = networks.nextElement();
                if ("eth0".equals(network.getName())) {
                    ip = network.getInetAddresses().nextElement();
                    break;
                }
            }
            NetworkInterface network = NetworkInterface.getByInetAddress(ip);
            byte[] mac = network.getHardwareAddress();
            macAddress = NetUtil.hardwareAddressToString(mac);
            s_logger.info("macAddress " + macAddress);
        } catch (UnknownHostException e) {
            s_logger.error(e.getLocalizedMessage());
        } catch (SocketException e) {
            s_logger.error(e.getLocalizedMessage());
        }
    } else {
        try {
            List<NetInterface<? extends NetInterfaceAddress>> interfaces = this.m_networkService.getNetworkInterfaces();
            if (interfaces != null) {
                for (NetInterface<? extends NetInterfaceAddress> iface : interfaces) {
                    if (iface.getName() != null && getPrimaryNetworkInterfaceName().equals(iface.getName())) {
                        macAddress = NetUtil.hardwareAddressToString(iface.getHardwareAddress());
                        break;
                    }
                }
            }
        } catch (KuraException e) {
            s_logger.error("Failed to get network interfaces", e);
        }
    }
    return macAddress;
}
Also used : SocketException(java.net.SocketException) Enumeration(java.util.Enumeration) InputStreamReader(java.io.InputStreamReader) UnknownHostException(java.net.UnknownHostException) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) ComponentException(org.osgi.service.component.ComponentException) SocketException(java.net.SocketException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) KuraException(org.eclipse.kura.KuraException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NetInterface(org.eclipse.kura.net.NetInterface) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) KuraException(org.eclipse.kura.KuraException) SafeProcess(org.eclipse.kura.core.util.SafeProcess) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List) InetAddress(java.net.InetAddress)

Example 3 with NetInterface

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

the class NetworkServiceImpl method getActiveNetworkInterfaces.

@Override
public List<NetInterface<? extends NetInterfaceAddress>> getActiveNetworkInterfaces() throws KuraException {
    List<NetInterface<? extends NetInterfaceAddress>> interfaces = getNetworkInterfaces();
    if (interfaces != null) {
        for (int i = 0; i < interfaces.size(); i++) {
            NetInterface<? extends NetInterfaceAddress> iface = interfaces.get(i);
            if (!LinuxNetworkUtil.hasAddress(iface.getName())) {
                s_logger.debug("removing interface {} because it is not up", iface.getName());
                interfaces.remove(i);
                i--;
            }
        }
    }
    return interfaces;
}
Also used : NetInterface(org.eclipse.kura.net.NetInterface) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint)

Example 4 with NetInterface

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

the class NetworkServiceImpl method getNetworkInterfaces.

@Override
public List<NetInterface<? extends NetInterfaceAddress>> getNetworkInterfaces() throws KuraException {
    s_logger.trace("getNetworkInterfaces()");
    List<NetInterface<? extends NetInterfaceAddress>> netInterfaces = new ArrayList<NetInterface<? extends NetInterfaceAddress>>();
    List<String> interfaceNames = getAllNetworkInterfaceNames();
    for (String interfaceName : interfaceNames) {
        try {
            NetInterface<? extends NetInterfaceAddress> netInterface = getNetworkInterface(interfaceName);
            if (netInterface != null) {
                netInterfaces.add(netInterface);
            }
        } catch (KuraException e) {
            s_logger.error("Can't get network interface info for {} :: exception - {}", interfaceName, e.toString());
        }
    }
    // Return an entry for non-connected modems (those w/o a ppp interface)
    Iterator<String> it = this.m_addedModems.iterator();
    while (it.hasNext()) {
        String modemId = it.next();
        UsbModemDevice usbModem = this.m_usbModems.get(modemId);
        if (usbModem != null) {
            // only add if there is not already a ppp interface for this modem
            boolean addModem = true;
            for (NetInterface<?> netInterface : netInterfaces) {
                UsbDevice usbDevice = netInterface.getUsbDevice();
                if (usbDevice != null) {
                    if (usbDevice.getUsbPort().equals(usbModem.getUsbPort())) {
                        addModem = false;
                        break;
                    }
                }
            }
            if (addModem) {
                netInterfaces.add(getModemInterface(usbModem.getUsbPort(), false, usbModem));
            }
        } else {
            // for Serial modem
            if (this.m_serialModem != null) {
                // only add if there is not already a ppp interface for this modem
                boolean addModem = true;
                for (NetInterface<?> netInterface : netInterfaces) {
                    String iface = netInterface.getName();
                    if (iface != null && iface.startsWith("ppp")) {
                        ModemInterface<ModemInterfaceAddress> pppModemInterface = getModemInterface(iface, false, this.m_serialModem);
                        ModemInterface<ModemInterfaceAddress> serialModemInterface = getModemInterface(this.m_serialModem.getProductName(), false, this.m_serialModem);
                        if (pppModemInterface != null && serialModemInterface != null) {
                            String pppModel = pppModemInterface.getModel();
                            String serialModel = serialModemInterface.getModel();
                            if (pppModel != null && pppModel.equals(serialModel)) {
                                addModem = false;
                                break;
                            }
                        }
                    }
                }
                if (addModem) {
                    netInterfaces.add(getModemInterface(this.m_serialModem.getProductName(), false, this.m_serialModem));
                }
            }
        }
    }
    return netInterfaces;
}
Also used : ArrayList(java.util.ArrayList) UsbDevice(org.eclipse.kura.usb.UsbDevice) UsbModemDevice(org.eclipse.kura.usb.UsbModemDevice) NetInterface(org.eclipse.kura.net.NetInterface) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) KuraException(org.eclipse.kura.KuraException) ModemInterfaceAddress(org.eclipse.kura.net.modem.ModemInterfaceAddress)

Example 5 with NetInterface

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

the class NetworkConfigurationServiceImpl method getNetworkConfiguration.

// @Override
// FIXME:MC Introducing a short lived cache will make startup much faster.
@Override
public synchronized NetworkConfiguration getNetworkConfiguration() throws KuraException {
    NetworkConfiguration networkConfiguration = new NetworkConfiguration();
    // Get the current values
    List<NetInterface<? extends NetInterfaceAddress>> allNetworkInterfaces = this.m_networkService.getNetworkInterfaces();
    Map<String, NetInterface<? extends NetInterfaceAddress>> allNetworkInterfacesMap = new HashMap<String, NetInterface<? extends NetInterfaceAddress>>();
    Map<String, NetInterface<? extends NetInterfaceAddress>> activeNetworkInterfacesMap = new HashMap<String, NetInterface<? extends NetInterfaceAddress>>();
    for (NetInterface<? extends NetInterfaceAddress> netInterface : allNetworkInterfaces) {
        allNetworkInterfacesMap.put(netInterface.getName(), netInterface);
        if (netInterface.isUp()) {
            activeNetworkInterfacesMap.put(netInterface.getName(), netInterface);
        }
    }
    // Create the NetInterfaceConfig objects
    if (allNetworkInterfacesMap.keySet() != null) {
        for (NetInterface<? extends NetInterfaceAddress> netInterface : allNetworkInterfacesMap.values()) {
            String interfaceName = netInterface.getName();
            try {
                // ignore mon interface
                if (interfaceName.startsWith("mon.")) {
                    continue;
                }
                // ignore redpine vlan interface
                if (interfaceName.startsWith("rpine")) {
                    continue;
                }
                // ignore usb0 for beaglebone
                if (interfaceName.startsWith("usb0") && System.getProperty("target.device").equals("beaglebone")) {
                    continue;
                }
                NetInterfaceType type = netInterface.getType();
                if (type == NetInterfaceType.UNKNOWN) {
                    if (interfaceName.matches(UNCONFIGURED_MODEM_REGEX)) {
                        // If the interface name is in a form such as "1-3.4", assume it is a modem
                        type = NetInterfaceType.MODEM;
                    } else {
                        SupportedSerialModemInfo serialModemInfo = SupportedSerialModemsInfo.getModem();
                        if (serialModemInfo != null && serialModemInfo.getModemName().equals(interfaceName)) {
                            type = NetInterfaceType.MODEM;
                        }
                    }
                }
                s_logger.debug("Getting config for {} type: {}", interfaceName, type);
                switch(type) {
                    case LOOPBACK:
                        LoopbackInterface<? extends NetInterfaceAddress> activeLoopInterface = (LoopbackInterface<? extends NetInterfaceAddress>) netInterface;
                        LoopbackInterfaceConfigImpl loopbackInterfaceConfig = null;
                        loopbackInterfaceConfig = new LoopbackInterfaceConfigImpl(activeLoopInterface);
                        networkConfiguration.addNetInterfaceConfig(loopbackInterfaceConfig);
                        break;
                    case ETHERNET:
                        EthernetInterface<? extends NetInterfaceAddress> activeEthInterface = (EthernetInterface<? extends NetInterfaceAddress>) netInterface;
                        EthernetInterfaceConfigImpl ethernetInterfaceConfig = null;
                        ethernetInterfaceConfig = new EthernetInterfaceConfigImpl(activeEthInterface);
                        networkConfiguration.addNetInterfaceConfig(ethernetInterfaceConfig);
                        break;
                    case WIFI:
                        WifiInterfaceImpl<? extends NetInterfaceAddress> activeWifiInterface = (WifiInterfaceImpl<? extends NetInterfaceAddress>) netInterface;
                        WifiInterfaceConfigImpl wifiInterfaceConfig = null;
                        wifiInterfaceConfig = new WifiInterfaceConfigImpl(activeWifiInterface);
                        networkConfiguration.addNetInterfaceConfig(wifiInterfaceConfig);
                        break;
                    case MODEM:
                        ModemInterfaceImpl<? extends NetInterfaceAddress> activeModemInterface = (ModemInterfaceImpl<? extends NetInterfaceAddress>) netInterface;
                        addPropertiesInModemInterface(activeModemInterface);
                        ModemInterfaceConfigImpl modemInterfaceConfig = null;
                        modemInterfaceConfig = new ModemInterfaceConfigImpl(activeModemInterface);
                        networkConfiguration.addNetInterfaceConfig(modemInterfaceConfig);
                        break;
                    case UNKNOWN:
                        s_logger.debug("Found interface of unknown type in current configuration: {}. Ignoring it.", interfaceName);
                        break;
                    default:
                        s_logger.debug("Unsupported type: {} - not adding to configuration. Ignoring it.", type);
                }
            } catch (Exception e) {
                s_logger.warn("Error fetching information for network interface: {}", interfaceName, e);
            }
        }
    }
    // populate the NetInterfaceConfigs
    for (NetworkConfigurationVisitor visitor : this.m_readVisitors) {
        networkConfiguration.accept(visitor);
    }
    return networkConfiguration;
}
Also used : HashMap(java.util.HashMap) SupportedSerialModemInfo(org.eclipse.kura.linux.net.modem.SupportedSerialModemInfo) WifiInterfaceImpl(org.eclipse.kura.core.net.WifiInterfaceImpl) KuraException(org.eclipse.kura.KuraException) ModemInterfaceImpl(org.eclipse.kura.core.net.modem.ModemInterfaceImpl) WifiInterfaceConfigImpl(org.eclipse.kura.core.net.WifiInterfaceConfigImpl) EthernetInterfaceConfigImpl(org.eclipse.kura.core.net.EthernetInterfaceConfigImpl) NetInterface(org.eclipse.kura.net.NetInterface) NetInterfaceType(org.eclipse.kura.net.NetInterfaceType) LoopbackInterface(org.eclipse.kura.net.LoopbackInterface) ModemInterfaceConfigImpl(org.eclipse.kura.core.net.modem.ModemInterfaceConfigImpl) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) NetworkConfiguration(org.eclipse.kura.core.net.NetworkConfiguration) NetworkConfigurationVisitor(org.eclipse.kura.core.net.NetworkConfigurationVisitor) LoopbackInterfaceConfigImpl(org.eclipse.kura.core.net.LoopbackInterfaceConfigImpl) EthernetInterface(org.eclipse.kura.net.EthernetInterface)

Aggregations

NetInterface (org.eclipse.kura.net.NetInterface)9 NetInterfaceAddress (org.eclipse.kura.net.NetInterfaceAddress)9 KuraException (org.eclipse.kura.KuraException)5 ArrayList (java.util.ArrayList)4 SocketException (java.net.SocketException)3 WifiAccessPoint (org.eclipse.kura.net.wifi.WifiAccessPoint)3 EthernetInterfaceImpl (org.eclipse.kura.core.net.EthernetInterfaceImpl)2 NetInterfaceAddressImpl (org.eclipse.kura.core.net.NetInterfaceAddressImpl)2 IPAddress (org.eclipse.kura.net.IPAddress)2 TestTarget (org.eclipse.kura.test.annotation.TestTarget)2 Test (org.junit.Test)2 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 InetAddress (java.net.InetAddress)1 NetworkInterface (java.net.NetworkInterface)1 UnknownHostException (java.net.UnknownHostException)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1