Skip to content

Commit

Permalink
Added net.interfaces property management in NetworkConfiguration Rest
Browse files Browse the repository at this point in the history
Signed-off-by: pierantoniomerlino <[email protected]>
  • Loading branch information
pierantoniomerlino committed Nov 8, 2024
1 parent 2c67224 commit 274263c
Showing 1 changed file with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021, 2023 Eurotech and/or its affiliates and others
* Copyright (c) 2021, 2024 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -17,8 +17,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -58,8 +60,8 @@ public class NetworkConfigurationRestService {

private static final Logger logger = LoggerFactory.getLogger(NetworkConfigurationRestService.class);
private static final String KURA_PERMISSION_REST_CONFIGURATION_ROLE = "kura.permission.rest.network.configuration";
private static final List<String> NETWORK_CONFIGURATION_PIDS = Arrays.asList(
"org.eclipse.kura.net.admin.NetworkConfigurationService",
private static final String NETWORK_CONFIGURATION_SERVICE_PID = "org.eclipse.kura.net.admin.NetworkConfigurationService";
private static final List<String> NETWORK_CONFIGURATION_PIDS = Arrays.asList(NETWORK_CONFIGURATION_SERVICE_PID,
"org.eclipse.kura.net.admin.FirewallConfigurationService",
"org.eclipse.kura.net.admin.ipv6.FirewallConfigurationServiceIPv6");
private static final String SUBTASK_SNAPSHOT_TAG = "snapshot";
Expand Down Expand Up @@ -169,6 +171,7 @@ public Response updateNetworkComponentConfigurations(UpdateComponentConfiguratio
failureHandler.runFallibleSubtask("update:" + componentConfig.getPid(), () -> {
final Map<String, Object> configurationProperties = DTOUtil
.dtosToConfigurationProperties(componentConfig.getProperties());
updateNetInterfaces(configurationProperties, componentConfig.getPid());
this.configurationService.updateConfiguration(componentConfig.getPid(), configurationProperties,
false);
});
Expand Down Expand Up @@ -273,4 +276,33 @@ private boolean isNetworkConfigurationPid(String pid) {
}
return result;
}

private void updateNetInterfaces(Map<String, Object> configurationProperties, String pid) throws KuraException {
if (pid.equals(NETWORK_CONFIGURATION_SERVICE_PID)) {
Set<String> interfaceNames = new HashSet<>();
configurationProperties.keySet().forEach(key -> {
Optional<String> interfaceName = parseInterfaceName(key);
interfaceName.ifPresent(interfaceNames::add);
});
Map<String, Object> properties = this.configurationService.getComponentConfiguration(pid)
.getConfigurationProperties();
String netInterfaces = (String) properties.get("net.interfaces");
List<String> netInterfacesList = Arrays.asList(netInterfaces.split(","));
for (String name : interfaceNames) {
if (!netInterfacesList.contains(name)) {
netInterfaces += "," + name;
}
}
configurationProperties.put("net.interfaces", netInterfaces);
}
}

private Optional<String> parseInterfaceName(String key) {
Optional<String> interfaceName = Optional.empty();
String[] splittedKey = key.split("\\.");
if (splittedKey.length >= 3) {
interfaceName = Optional.of(splittedKey[2]);
}
return interfaceName;
}
}

0 comments on commit 274263c

Please sign in to comment.