Skip to content

Commit

Permalink
fix(rest.network.configuration): Fix net interfaces property manageme…
Browse files Browse the repository at this point in the history
…nt again [backport release-5.6.0] (#5538)

fix(rest.network.configuration): Fix net interfaces property management again (#5535)

* Added net.interfaces property management in NetworkConfiguration Rest

Signed-off-by: pierantoniomerlino <[email protected]>

* Changed parsing logic

Signed-off-by: pierantoniomerlino <[email protected]>

* Improved string parsing

Signed-off-by: pierantoniomerlino <[email protected]>

* Added tests for net.interfaces property

Signed-off-by: pierantoniomerlino <[email protected]>

* Update kura/org.eclipse.kura.rest.network.configuration.provider/src/main/java/org/eclipse/kura/internal/rest/network/configuration/NetworkConfigurationRestService.java

Co-authored-by: Mattia Dal Ben <[email protected]>

* Update kura/org.eclipse.kura.rest.network.configuration.provider/src/main/java/org/eclipse/kura/internal/rest/network/configuration/NetworkConfigurationRestService.java

Co-authored-by: Mattia Dal Ben <[email protected]>

---------

Signed-off-by: pierantoniomerlino <[email protected]>
Co-authored-by: Mattia Dal Ben <[email protected]>
(cherry picked from commit da1feb6)

Co-authored-by: Pierantonio Merlino <[email protected]>
  • Loading branch information
eclipse-kura-bot and pierantoniomerlino authored Nov 12, 2024
1 parent 132cd03 commit 616c196
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Import-Package: javax.annotation.security;version="1.2.0",
javax.ws.rs;version="2.0.1",
javax.ws.rs.core;version="2.0.1",
org.apache.commons.io;version="2.4.0",
org.apache.commons.lang3;version="3.12.0",
org.eclipse.kura;version="[1.3,2.0)",
org.eclipse.kura.cloudconnection.request;version="[1.0,2.0)",
org.eclipse.kura.configuration;version="[1.2,2.0)",
Expand Down
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 All @@ -33,6 +35,7 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.kura.KuraErrorCode;
import org.eclipse.kura.KuraException;
import org.eclipse.kura.configuration.ComponentConfiguration;
Expand All @@ -58,8 +61,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 +172,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 +277,28 @@ 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) {
return Optional.ofNullable(StringUtils.substringBetween(key, "net.interface.", ".config."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ Import-Package: com.eclipsesource.json;version="0.9.5",
org.eclipse.kura;version="1.6.0",
org.eclipse.kura.configuration,
org.eclipse.kura.configuration.metatype;version="1.1.0",
org.eclipse.kura.core.configuration;version="[2.0,3.0)",
org.eclipse.kura.core.configuration.metatype;version="[1.0,2.0)",
org.eclipse.kura.core.testutil.json;version="[1.0,2.0)",
org.eclipse.kura.core.testutil.requesthandler;version="[1.1,2.0)",
org.eclipse.kura.core.testutil.requesthandler;version="[1.1,2.0)",
org.eclipse.kura.core.testutil.service;version="[1.0,2.0)",
org.eclipse.kura.core.util;version="1.3.0",
org.eclipse.kura.crypto;version="1.3.0",
Expand Down
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 Down Expand Up @@ -28,7 +28,10 @@
import java.util.concurrent.TimeUnit;

import org.eclipse.kura.KuraException;
import org.eclipse.kura.configuration.ComponentConfiguration;
import org.eclipse.kura.configuration.ConfigurationService;
import org.eclipse.kura.core.configuration.ComponentConfigurationImpl;
import org.eclipse.kura.core.configuration.metatype.ObjectFactory;
import org.eclipse.kura.core.testutil.requesthandler.AbstractRequestHandlerTest;
import org.eclipse.kura.core.testutil.requesthandler.RestTransport;
import org.eclipse.kura.core.testutil.requesthandler.Transport.MethodSpec;
Expand All @@ -38,7 +41,6 @@
import org.eclipse.kura.rest.network.configuration.provider.test.responses.RestNetworkConfigurationJson;
import org.eclipse.kura.util.wire.test.WireTestUtil;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
Expand All @@ -51,7 +53,6 @@
import org.osgi.service.useradmin.User;
import org.osgi.service.useradmin.UserAdmin;

@Ignore
public class NetworkConfigurationRestServiceTest extends AbstractRequestHandlerTest {

private static final String METHOD_SPEC_GET = "GET";
Expand Down Expand Up @@ -155,6 +156,21 @@ public void shouldUpdateWithoutErrors() throws KuraException {
"1234,tcp,0:0:0:0:0:0:0:0/0,,,,,#");
}

@Test
public void shouldUpdateNetInterfacesProperty() throws KuraException {
givenMockUpdateConfiguration();
givenMockNetworkConfigurationService();
givenIdentity("admin", Optional.of("password"), Collections.emptyList());
givenBasicCredentials(Optional.of("admin:password"));

whenRequestIsPerformed(new MethodSpec(METHOD_SPEC_PUT), "/configurableComponents/configurations/_update",
RestNetworkConfigurationJson.NETWORK_CONFIGURATION_UPDATE_REQUEST);

thenRequestSucceeds();
thenValueIsUpdated(NETWORK_CONF_SERVICE_PID, "net.interface.wlan0.config.ip4.status", "netIPv4StatusDisabled");
thenValueIsUpdated(NETWORK_CONF_SERVICE_PID, "net.interfaces", "eth0,wlan0");
}

@Test
public void shouldReturnEmptyArray() throws KuraException {
givenMockUpdateConfiguration();
Expand Down Expand Up @@ -259,6 +275,14 @@ private void givenMockUpdateConfiguration() throws KuraException {
.updateConfiguration(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.anyBoolean());
}

private void givenMockNetworkConfigurationService() throws KuraException {
Map<String, Object> properties = new HashMap<>();
properties.put("net.interfaces", "eth0");
ComponentConfiguration componentConfig = new ComponentConfigurationImpl(NETWORK_CONF_SERVICE_PID,
new ObjectFactory().createTocd(), properties);
when(configurationService.getComponentConfiguration(NETWORK_CONF_SERVICE_PID)).thenReturn(componentConfig);
}

/*
* Then
*/
Expand Down
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 @@ -25,6 +25,7 @@ private RestNetworkConfigurationJson() {
public static final String EMPTY_CONFIGS_REQUEST = "{\"configs\":[]}";
public static final String INVALID_PID_DELETE_REQUEST = "{\"pids\":[\"invalidPid\"]}";
public static final String EMPTY_PIDS_REQUEST = "{\"pids\":[]}";
public static final String NETWORK_CONFIGURATION_UPDATE_REQUEST = "{\"configs\":[{\"pid\":\"org.eclipse.kura.net.admin.NetworkConfigurationService\",properties: {\"net.interface.wlan0.config.ip4.status\":{\"type\":\"STRING\",\"value\":\"netIPv4StatusDisabled\"}}}]}";

/*
* Responses
Expand Down

0 comments on commit 616c196

Please sign in to comment.