use of org.eclipse.kura.configuration.SelfConfiguringComponent in project kura by eclipse.
the class ConfigurableComponentTracker method open.
// ----------------------------------------------------------------
//
// Override APIs
//
// ----------------------------------------------------------------
@SuppressWarnings({ "unchecked" })
@Override
public void open(boolean trackAllServices) {
s_logger.info("Opening ServiceTracker");
super.open(trackAllServices);
try {
s_logger.info("Getting ServiceReferences");
ServiceReference[] refs = this.context.getServiceReferences((String) null, null);
if (refs != null) {
for (ServiceReference ref : refs) {
String servicePid = (String) ref.getProperty(Constants.SERVICE_PID);
String pid = (String) ref.getProperty(ConfigurationService.KURA_SERVICE_PID);
String factoryPid = (String) ref.getProperty(ConfigurationAdmin.SERVICE_FACTORYPID);
if (servicePid != null) {
Object obj = this.context.getService(ref);
try {
if (obj == null) {
s_logger.info("Could not find service for: {}", ref);
} else if (obj instanceof ConfigurableComponent) {
s_logger.info("Adding ConfigurableComponent with pid {}, service pid {} and factory pid " + factoryPid, pid, servicePid);
this.m_confService.registerComponentConfiguration(pid, servicePid, factoryPid);
} else if (obj instanceof SelfConfiguringComponent) {
s_logger.info("Adding SelfConfiguringComponent with pid {} and service pid {}", pid, servicePid);
this.m_confService.registerSelfConfiguringComponent(servicePid);
}
} finally {
this.context.ungetService(ref);
}
}
}
}
} catch (InvalidSyntaxException ise) {
s_logger.error("Error in addingBundle", ise);
}
}
use of org.eclipse.kura.configuration.SelfConfiguringComponent 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);
}
}
use of org.eclipse.kura.configuration.SelfConfiguringComponent in project kura by eclipse.
the class NetworkAdminServiceImpl method manageFirewall.
@Override
public void manageFirewall(String gatewayIface) throws KuraException {
// get desired NAT rules interfaces
LinkedHashSet<NATRule> desiredNatRules = null;
ComponentConfiguration networkComponentConfiguration = ((SelfConfiguringComponent) this.m_networkConfigurationService).getConfiguration();
if (gatewayIface != null && networkComponentConfiguration != null) {
try {
NetworkConfiguration netConfiguration = new NetworkConfiguration(networkComponentConfiguration.getConfigurationProperties());
List<NetInterfaceConfig<? extends NetInterfaceAddressConfig>> netInterfaceConfigs = netConfiguration.getNetInterfaceConfigs();
for (NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : netInterfaceConfigs) {
String ifaceName = netInterfaceConfig.getName();
List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
if (netInterfaceAddressConfigs != null && !netInterfaceAddressConfigs.isEmpty()) {
for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
List<NetConfig> existingNetConfigs = netInterfaceAddressConfig.getConfigs();
if (existingNetConfigs != null && !existingNetConfigs.isEmpty()) {
for (NetConfig netConfig : existingNetConfigs) {
if (netConfig instanceof FirewallAutoNatConfig) {
if (desiredNatRules == null) {
desiredNatRules = new LinkedHashSet<NATRule>();
}
desiredNatRules.add(new NATRule(ifaceName, gatewayIface, true));
}
}
}
}
}
}
} catch (UnknownHostException e) {
s_logger.warn("Exception while updating firewall configuration", e);
}
}
LinuxFirewall firewall = LinuxFirewall.getInstance();
if (desiredNatRules != null) {
firewall.replaceAllNatRules(desiredNatRules);
} else {
firewall.deleteAllAutoNatRules();
}
firewall.enable();
}
use of org.eclipse.kura.configuration.SelfConfiguringComponent in project kura by eclipse.
the class ConfigurationServiceImpl method getSelfConfiguringComponentConfiguration.
private ComponentConfiguration getSelfConfiguringComponentConfiguration(String pid) {
ComponentConfiguration cc = null;
try {
ServiceReference<?>[] refs = this.m_ctx.getBundleContext().getServiceReferences((String) null, null);
if (refs != null) {
for (ServiceReference<?> ref : refs) {
String ppid = (String) ref.getProperty(Constants.SERVICE_PID);
if (pid.equals(ppid)) {
Object obj = this.m_ctx.getBundleContext().getService(ref);
try {
if (obj instanceof SelfConfiguringComponent) {
SelfConfiguringComponent selfConfigComp = null;
selfConfigComp = (SelfConfiguringComponent) obj;
try {
cc = selfConfigComp.getConfiguration();
if (cc.getPid() == null || !cc.getPid().equals(pid)) {
s_logger.error("Invalid pid for returned Configuration of SelfConfiguringComponent with pid: " + pid + ". Ignoring it.");
return null;
}
OCD ocd = cc.getDefinition();
if (ocd != null) {
List<AD> ads = ocd.getAD();
if (ads != null) {
for (AD ad : ads) {
String adId = ad.getId();
String adType = ad.getType().value();
if (adId == null) {
s_logger.error("null required id for AD for returned Configuration of SelfConfiguringComponent with pid: {}", pid);
return null;
}
if (adType == null) {
s_logger.error("null required type for AD id: {} for returned Configuration of SelfConfiguringComponent with pid: {}", adId, pid);
return null;
}
Map<String, Object> props = cc.getConfigurationProperties();
if (props != null) {
final Object value = props.get(adId);
if (value != null) {
final String propType;
if (!value.getClass().isArray()) {
propType = value.getClass().getSimpleName();
} else {
propType = value.getClass().getComponentType().getSimpleName();
}
try {
s_logger.debug("pid: {}, property name: {}, type: {}, value: {}", new Object[] { pid, adId, propType, value });
Scalar.fromValue(propType);
if (!propType.equals(adType)) {
s_logger.error("Type: {} for property named: {} does not match the AD type: {} for returned Configuration of SelfConfiguringComponent with pid: {}", new Object[] { propType, adId, adType, pid });
return null;
}
} catch (IllegalArgumentException e) {
s_logger.error("Invalid class: {} for property named: {} for returned Configuration of SelfConfiguringComponent with pid: " + pid, propType, adId);
return null;
}
}
}
}
}
}
} catch (KuraException e) {
s_logger.error("Error getting Configuration for component: " + pid + ". Ignoring it.", e);
}
} else {
s_logger.error("Component " + obj + " is not a SelfConfiguringComponent. Ignoring it.");
}
} finally {
this.m_ctx.getBundleContext().ungetService(ref);
}
}
}
}
} catch (InvalidSyntaxException e) {
s_logger.error("Error getting Configuration for component: " + pid + ". Ignoring it.", e);
}
return cc;
}
use of org.eclipse.kura.configuration.SelfConfiguringComponent in project kura by eclipse.
the class NetworkAdminServiceImpl method rollbackDefaultConfiguration.
@Override
@Deprecated
public boolean rollbackDefaultConfiguration() throws KuraException {
s_logger.debug("rollbackDefaultConfiguration() :: Recovering default configuration ...");
ArrayList<NetworkRollbackItem> rollbackItems = new ArrayList<NetworkRollbackItem>();
if (this.m_systemService == null) {
return false;
}
String dstDataDirectory = this.m_systemService.getKuraDataDirectory();
if (dstDataDirectory == null) {
return false;
}
int ind = dstDataDirectory.lastIndexOf('/');
String srcDataDirectory = null;
if (ind >= 0) {
srcDataDirectory = "".concat(dstDataDirectory.substring(0, ind + 1).concat(".data"));
}
if (srcDataDirectory == null) {
return false;
}
rollbackItems.add(new NetworkRollbackItem(srcDataDirectory + "/kuranet.conf", dstDataDirectory + "/kuranet.conf"));
// rollbackItems.add(new NetworkRollbackItem(srcDataDirectory + "/firewall", "/etc/init.d/firewall"));
if (OS_VERSION.equals(KuraConstants.Intel_Edison.getImageName() + "_" + KuraConstants.Intel_Edison.getImageVersion() + "_" + KuraConstants.Intel_Edison.getTargetName())) {
rollbackItems.add(new NetworkRollbackItem(srcDataDirectory + "/hostapd.conf", "/etc/hostapd/hostapd.conf"));
rollbackItems.add(new NetworkRollbackItem(srcDataDirectory + "/dhcpd-eth0.conf", "/etc/udhcpd-usb0.conf"));
rollbackItems.add(new NetworkRollbackItem(srcDataDirectory + "/dhcpd-wlan0.conf", "/etc/udhcpd-wlan0.conf"));
} else {
rollbackItems.add(new NetworkRollbackItem(srcDataDirectory + "/hostapd.conf", "/etc/hostapd.conf"));
rollbackItems.add(new NetworkRollbackItem(srcDataDirectory + "/dhcpd-eth0.conf", "/etc/dhcpd-eth0.conf"));
rollbackItems.add(new NetworkRollbackItem(srcDataDirectory + "/dhcpd-wlan0.conf", "/etc/dhcpd-wlan0.conf"));
}
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())) {
// restore Debian interface configuration
rollbackItems.add(new NetworkRollbackItem(srcDataDirectory + "/interfaces", "/etc/network/interfaces"));
} else {
// restore RedHat interface configuration
rollbackItems.add(new NetworkRollbackItem(srcDataDirectory + "/ifcfg-eth0", "/etc/sysconfig/network-scripts/ifcfg-eth0"));
rollbackItems.add(new NetworkRollbackItem(srcDataDirectory + "/ifcfg-eth1", "/etc/sysconfig/network-scripts/ifcfg-eth1"));
rollbackItems.add(new NetworkRollbackItem(srcDataDirectory + "/ifcfg-wlan0", "/etc/sysconfig/network-scripts/ifcfg-wlan0"));
}
for (NetworkRollbackItem rollbackItem : rollbackItems) {
rollbackItem(rollbackItem);
}
s_logger.debug("rollbackDefaultConfiguration() :: setting network configuration ...");
ComponentConfiguration networkComponentConfiguration = ((SelfConfiguringComponent) this.m_networkConfigurationService).getConfiguration();
if (networkComponentConfiguration != null) {
try {
NetworkConfiguration netConfiguration = new NetworkConfiguration(networkComponentConfiguration.getConfigurationProperties());
this.m_networkConfigurationService.setNetworkConfiguration(netConfiguration);
} catch (UnknownHostException e) {
s_logger.error("relback to snapshot_0 has failed - {}", e);
}
}
return true;
}
Aggregations