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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations