Skip to content

Commit

Permalink
feat(rest.network.configuration.provider): Added factoryComponents AP…
Browse files Browse the repository at this point in the history
…Is (#4994)

* Added factoryComponents apis

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

* Added check on possible null entries in non-generic profiles

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

* Added test for factoryPids

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

---------

Signed-off-by: SimoneFiorani <[email protected]>
  • Loading branch information
sfiorani authored Nov 17, 2023
1 parent ee2c973 commit d94ccbf
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.security.RolesAllowed;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
Expand All @@ -31,13 +33,18 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.eclipse.kura.KuraErrorCode;
import org.eclipse.kura.KuraException;
import org.eclipse.kura.configuration.ComponentConfiguration;
import org.eclipse.kura.configuration.ConfigurationService;
import org.eclipse.kura.crypto.CryptoService;
import org.eclipse.kura.request.handler.jaxrs.DefaultExceptionHandler;
import org.eclipse.kura.rest.configuration.api.ComponentConfigurationDTO;
import org.eclipse.kura.rest.configuration.api.ComponentConfigurationList;
import org.eclipse.kura.rest.configuration.api.CreateFactoryComponentConfigurationsRequest;
import org.eclipse.kura.rest.configuration.api.DTOUtil;
import org.eclipse.kura.rest.configuration.api.DeleteFactoryComponentRequest;
import org.eclipse.kura.rest.configuration.api.FactoryComponentConfigurationDTO;
import org.eclipse.kura.rest.configuration.api.FailureHandler;
import org.eclipse.kura.rest.configuration.api.PidSet;
import org.eclipse.kura.rest.configuration.api.UpdateComponentConfigurationRequest;
Expand Down Expand Up @@ -71,15 +78,6 @@ public void setCryptoService(CryptoService cryptoService) {
this.cryptoService = cryptoService;
}

/**
* GET method.
*
* Lists the tracked network configurable component Pids
*
* @return a List of String objects representing the Pids of network factory components
* tracked by the
* {@link ConfigurationService}
*/
@GET
@RolesAllowed("network.configuration")
@Path("/configurableComponents")
Expand All @@ -91,17 +89,6 @@ public PidSet listNetworkConfigurableComponentsPids() {
return new PidSet(pids);
}

/**
* GET method.
*
* Lists all the network component configurations of all the ConfigurableComponents
* tracked by the
* {@link ConfigurationService}
*
* @return a list of {@link ComponentConfigurationDTO} that map all the
* configuration parameters tracked for the
* network configurable components tracked.
*/
@GET
@RolesAllowed("network.configuration")
@Path("/configurableComponents/configurations")
Expand All @@ -110,26 +97,17 @@ public ComponentConfigurationList listNetworkConfiguration() {
final List<ComponentConfiguration> ccs = new ArrayList<>();
try {
for (String config : NETWORK_CONFIGURATION_PIDS) {
ccs.add(this.configurationService.getComponentConfiguration(config));
ComponentConfiguration configuration = this.configurationService.getComponentConfiguration(config);
if (null != configuration) {
ccs.add(configuration);
}
}
} catch (final Exception e) {
throw DefaultExceptionHandler.toWebApplicationException(e);
}
return DTOUtil.toComponentConfigurationList(ccs, this.cryptoService, false).replacePasswordsWithPlaceholder();
}

/**
* POST method.
*
* Lists the network component configurations of all the network ConfigurableComponents tracked
* by the
* {@link ConfigurationService} that match the filter specified
*
* @param filter
* A String representing an OSGi filter
* @return a list of {@link ComponentConfigurationDTO}s for the components that
* match the specified filter
*/
@POST
@RolesAllowed("network.configuration")
@Path("/configurableComponents/configurations/byPid")
Expand All @@ -152,16 +130,6 @@ public ComponentConfigurationList listNetworkComponentConfigurations(final PidSe
.replacePasswordsWithPlaceholder();
}

/**
* POST method.
*
* This method provides the default network Component Configuration for the component identified by
* the specified PID in the body request
*
* @param componentPid
* @return The default {@link ComponentConfiguration} or a null object if the
* component is not tracked
*/
@POST
@RolesAllowed("network.configuration")
@Path("/configurableComponents/configurations/byPid/_default")
Expand All @@ -188,13 +156,6 @@ public ComponentConfigurationList listDefaultNetworkComponentConfiguration(final
return new ComponentConfigurationList(requestResult);
}

/**
* POST method.
*
* This method let the user update the configuration of multiple network configurable components
*
* @param request
*/
@PUT
@RolesAllowed("network.configuration")
@Path("/configurableComponents/configurations/_update")
Expand All @@ -220,6 +181,89 @@ public Response updateNetworkComponentConfigurations(UpdateComponentConfiguratio
return Response.ok().build();
}

@GET
@RolesAllowed("network.configuration")
@Path("/factoryComponents")
@Produces(MediaType.APPLICATION_JSON)
public PidSet listFactoryComponentsPids() {
return new PidSet(Collections.emptySet());
}

@POST
@RolesAllowed("network.configuration")
@Path("/factoryComponents")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response createNetworkFactoryComponents(CreateFactoryComponentConfigurationsRequest configs) {
configs.validate();

final FailureHandler handler = new FailureHandler();

for (final FactoryComponentConfigurationDTO config : configs.getConfigs()) {
handler.runFallibleSubtask("create:" + config.getPid(), () -> {

throw new KuraException(KuraErrorCode.INVALID_PARAMETER,
"Factory pid doesn't correspond to a network component factory");

});
}

if (configs.isTakeSnapshot()) {
handler.runFallibleSubtask(SUBTASK_SNAPSHOT_TAG, () -> this.configurationService.snapshot());
}

handler.checkStatus();
return Response.ok().build();
}

@DELETE
@RolesAllowed("network.configuration")
@Path("/factoryComponents/byPid")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response deleteFactoryConfigurations(final DeleteFactoryComponentRequest request) {
request.validate();

final FailureHandler handler = new FailureHandler();

for (final String pid : request.getPids()) {
handler.runFallibleSubtask("delete:" + pid, () -> {
throw new KuraException(KuraErrorCode.INVALID_PARAMETER,
"Pid doesn't correspond to a network factory component");
});
}

if (request.isTakeSnapshot()) {
handler.runFallibleSubtask(SUBTASK_SNAPSHOT_TAG, () -> this.configurationService.snapshot());
}

handler.checkStatus();
return Response.ok().build();
}

@GET
@RolesAllowed("network.configuration")
@Path("/factoryComponents/ocd")
@Produces(MediaType.APPLICATION_JSON)
public ComponentConfigurationList getFactoryComponentOcds() {

return DTOUtil.toComponentConfigurationList(Collections.emptyList(), this.cryptoService, false);
}

@POST
@RolesAllowed("network.configuration")
@Path("/factoryComponents/ocd/byFactoryPid")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public ComponentConfigurationList getFactoryComponentOcdsByPid(final PidSet factoryPids) {
factoryPids.validate();
return DTOUtil.toComponentConfigurationList(Collections.emptyList(), this.cryptoService, false);
}

/*
* Utils
*/

private boolean isNetworkConfigurationPid(String pid) {
boolean result = false;
for (String filter : NETWORK_CONFIGURATION_PIDS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class NetworkConfigurationRestServiceTest extends AbstractRequestHandlerT
private static final String METHOD_SPEC_GET = "GET";
private static final String METHOD_SPEC_POST = "POST";
private static final String METHOD_SPEC_PUT = "PUT";
private static final String METHOD_SPEC_DELETE = "DELETE";
private static final String REST_APP_ID = "networkConfiguration/v1";

private static final String NETWORK_CONF_SERVICE_PID = "org.eclipse.kura.net.admin.NetworkConfigurationService";
Expand Down Expand Up @@ -152,6 +153,64 @@ public void shouldUpdateWithoutErrors() throws KuraException {
"1234,tcp,0:0:0:0:0:0:0:0/0,,,,,#");
}

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

whenRequestIsPerformed(new MethodSpec(METHOD_SPEC_GET), "/factoryComponents");

thenResponseBodyEqualsJson(RestNetworkConfigurationJson.EMPTY_PIDS_RESPONSE);
}

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

whenRequestIsPerformed(new MethodSpec(METHOD_SPEC_POST), "/factoryComponents",
RestNetworkConfigurationJson.EMPTY_CONFIGS_REQUEST);

thenResponseCodeIs(200);
}

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

whenRequestIsPerformed(new MethodSpec(METHOD_SPEC_DELETE, "DEL"), "/factoryComponents/byPid",
RestNetworkConfigurationJson.INVALID_PID_DELETE_REQUEST);

thenResponseBodyEqualsJson(RestNetworkConfigurationJson.INVALID_PID_DELETE_RESPONSE);
}

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

whenRequestIsPerformed(new MethodSpec(METHOD_SPEC_GET), "/factoryComponents/ocd");

thenResponseBodyEqualsJson(RestNetworkConfigurationJson.EMPTY_CONFIGS_RESPONSE);
}

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

whenRequestIsPerformed(new MethodSpec(METHOD_SPEC_POST), "/factoryComponents/ocd/byFactoryPid",
RestNetworkConfigurationJson.EMPTY_PIDS_REQUEST);

thenResponseBodyEqualsJson(RestNetworkConfigurationJson.EMPTY_CONFIGS_RESPONSE);
}

/*
* Given
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ private RestNetworkConfigurationJson() {
*/
public static final String FIREWALL_IP6_BYPID_REQUEST = "{\"pids\":[\"org.eclipse.kura.net.admin.ipv6.FirewallConfigurationServiceIPv6\"]}";
public static final String FIREWALL_IP6_UPDATE_REQUEST = "{\"configs\":[{\"pid\":\"org.eclipse.kura.net.admin.ipv6.FirewallConfigurationServiceIPv6\",properties: {\"firewall.ipv6.open.ports\":{\"type\":\"STRING\",\"value\":\"1234,tcp,0:0:0:0:0:0:0:0/0,,,,,#\"}}}]}";
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\":[]}";

/*
* Responses
*/
public static final String ALL_CONFIGURATIONS_RESPONSE = "{\"configs\":[{\"pid\":\"CONF_COMP_PID_0\",\"definition\":{\"name\":\"OCD_MOCK_NAME_0\",\"description\":\"OCD_MOCK_DESC_0\",\"id\":\"OCD_MOCK_ID_0\"},\"properties\":{}},{\"pid\":\"CONF_COMP_PID_1\",\"definition\":{\"name\":\"OCD_MOCK_NAME_1\",\"description\":\"OCD_MOCK_DESC_1\",\"id\":\"OCD_MOCK_ID_1\"},\"properties\":{}},{\"pid\":\"CONF_COMP_PID_2\",\"definition\":{\"name\":\"OCD_MOCK_NAME_2\",\"description\":\"OCD_MOCK_DESC_2\",\"id\":\"OCD_MOCK_ID_2\"},\"properties\":{}}]}";
public static final String SINGLE_CONFIG_RESPONSE = "{\"configs\":[{\"pid\":\"CONF_COMP_PID_0\",\"definition\":{\"name\":\"OCD_MOCK_NAME_0\",\"description\":\"OCD_MOCK_DESC_0\",\"id\":\"OCD_MOCK_ID_0\"},\"properties\":{}}]}";
public static final String EMPTY_PIDS_RESPONSE = "{\"pids\":[]}";
public static final String INVALID_PID_DELETE_RESPONSE = "{\"failures\":[{\"id\":\"delete:invalidPid\",\"message\":\"Invalid parameter. Pid doesn't correspond to a network factory component\"}]}";
public static final String EMPTY_CONFIGS_RESPONSE = "{\"configs\":[]}";
}

0 comments on commit d94ccbf

Please sign in to comment.