Skip to content

Commit

Permalink
Added factoryComponents apis
Browse files Browse the repository at this point in the history
Signed-off-by: SimoneFiorani <[email protected]>
  • Loading branch information
sfiorani committed Nov 17, 2023
1 parent ee2c973 commit aea4b6e
Showing 1 changed file with 132 additions and 0 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 @@ -229,4 +236,129 @@ private boolean isNetworkConfigurationPid(String pid) {
}
return result;
}

/**
* GET method.
*
* The method lists all the network FactoryComponents Pids tracked by
* {@link ConfigurationService}
*
* @return a list of String representing the tracked FactoryComponents
*/
@GET
@RolesAllowed("network.configuration")
@Path("/factoryComponents")
@Produces(MediaType.APPLICATION_JSON)
public PidSet listFactoryComponentsPids() {
return new PidSet(Collections.emptySet());
}

/**
* POST method.
*
* Creates a new network ConfigurableComponent instance by creating a new configuration
* from a
* Configuration Admin factory.
* The {@link FactoryComponentConfigurationDTO} will provide all the information
* needed to generate the instance: it
* links the factory Pid to be used, the target instance Pid, the properties to
* be used when creating the instance
* and if the request should be persisted with a snapshot.
* In case of a request error, an exception is thrown.
*
* @param factoryComponentConfiguration
* provides all the parameters needed to
* generate a new instance from a Factory
* Component
*
*/
@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 method.
*
* For the specified Pid and {@link FactoryComponentDeleteRequest}, the
* {@link ConfigurationService} instance
* deletes the corresponding network ConfigurableComponent instance.
*
* @param pid
* A String representing the pid of the
* instance generated by a Factory
* Component that needs to be
* deleted
* @param factoryComponentDeleteRequest
* A {@link FactoryComponentDeleteRequest}
* containing additional information to
* ease the process of
* instance delete
*/
@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);
}
}

0 comments on commit aea4b6e

Please sign in to comment.