Skip to content

Commit

Permalink
Configuration REST API first implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Claudio Mezzasalma <claudio.mezzasalma@eurotech.com>
  • Loading branch information
Claudio Mezzasalma committed Oct 18, 2019
1 parent 838417b commit 50d4d4e
Show file tree
Hide file tree
Showing 18 changed files with 1,050 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*******************************************************************************
* Copyright (c) 2019 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.commons.configuration;

import java.util.Map;

import org.eclipse.kapua.service.config.ServiceComponentConfiguration;
import org.eclipse.kapua.commons.configuration.metatype.TocdImpl;
import org.eclipse.kapua.model.config.metatype.KapuaTocd;

/**
* Service component configuration entity implementation.
*
* @since 1.0
*/
public class ServiceComponentConfigurationImpl implements ServiceComponentConfiguration {

private String id;
private String name;
private TocdImpl definition;
private Map<String, Object> properties;

/**
* Constructor
*/
public ServiceComponentConfigurationImpl() {
}

/**
* Constructor
*
* @param id
*/
public ServiceComponentConfigurationImpl(String id) {
this.id = id;
}

@Override
public String getId() {
return id;
}

@Override
public void setId(String id) {
this.id = id;
}

@Override
public String getName() {
return name;
}

@Override
public void setName(String name) {
this.name = name;
}

@Override
public void setDefinition(KapuaTocd definition) {
this.definition = (TocdImpl) definition;
}

@Override
public KapuaTocd getDefinition() {
return definition;
}

@Override
public Map<String, Object> getProperties() {
return properties;
}

@Override
public void setProperties(Map<String, Object> properties) {
this.properties = properties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*******************************************************************************
* Copyright (c) 2019 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.commons.configuration;

import org.eclipse.kapua.service.config.ServiceComponentConfiguration;
import org.eclipse.kapua.service.config.ServiceConfiguration;
import org.eclipse.kapua.service.config.ServiceConfigurationFactory;
import org.eclipse.kapua.locator.KapuaProvider;

/**
* Service configuration entity service factory implementation.
*
* @since 1.0
*/
@KapuaProvider
public class ServiceConfigurationFactoryImpl implements ServiceConfigurationFactory {

@Override
public ServiceComponentConfiguration newComponentConfigurationInstance(String componentConfigurationId) {
return new ServiceComponentConfigurationImpl(componentConfigurationId);
}

@Override
public ServiceConfiguration newConfigurationInstance() {
return new ServiceConfigurationImpl();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* Copyright (c) 2019 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.commons.configuration;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.kapua.service.config.ServiceComponentConfiguration;
import org.eclipse.kapua.service.config.ServiceConfiguration;

/**
* Service configuration entity implementation.
*
* @since 1.0
*/
public class ServiceConfigurationImpl implements ServiceConfiguration {

private static final long serialVersionUID = -2167999497954676423L;

private List<ServiceComponentConfiguration> configurations;

public ServiceConfigurationImpl() {
configurations = new ArrayList<>();
}

@Override
public List<ServiceComponentConfiguration> getComponentConfigurations() {
return configurations;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*******************************************************************************
* Copyright (c) 2019 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.app.api.resources.v1.resources;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.kapua.app.api.resources.v1.resources.model.ScopeId;
import org.eclipse.kapua.commons.configuration.metatype.EmptyTocd;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.model.config.metatype.KapuaTocd;
import org.eclipse.kapua.service.KapuaService;
import org.eclipse.kapua.service.account.Account;
import org.eclipse.kapua.service.account.AccountService;
import org.eclipse.kapua.service.config.KapuaConfigurableService;
import org.eclipse.kapua.service.config.ServiceComponentConfiguration;
import org.eclipse.kapua.service.config.ServiceConfiguration;
import org.eclipse.kapua.service.config.ServiceConfigurationFactory;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;

@Api(value = "Service Configurations", authorizations = {@Authorization(value = "kapuaAccessToken")})
@Path("{scopeId}/configurations")
public class ServiceConfigurations extends AbstractKapuaResource {

private final KapuaLocator locator = KapuaLocator.getInstance();
private final AccountService accountService = locator.getService(AccountService.class);
private final ServiceConfigurationFactory serviceConfigurationFactory = locator.getFactory(ServiceConfigurationFactory.class);

@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@ApiOperation(nickname = "serviceConfigurationGet", value = "Gets multiple services configurations", notes = "Returns the current configuration of multiple services from an account")
public ServiceConfiguration get(@ApiParam(value = "The ScopeId in which to search results.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId) throws Exception {
List<KapuaConfigurableService> configurableServices = locator.getServices().stream().filter(service -> service instanceof KapuaConfigurableService).map(kapuaService -> (KapuaConfigurableService)kapuaService).collect(Collectors.toList());
ServiceConfiguration serviceConfiguration = serviceConfigurationFactory.newConfigurationInstance();
for (KapuaConfigurableService configurableService : configurableServices) {
KapuaTocd metadata = configurableService.getConfigMetadata(scopeId);
Map<String, Object> values = configurableService.getConfigValues(scopeId);
if (metadata != null && !(metadata instanceof EmptyTocd)) {
ServiceComponentConfiguration serviceComponentConfiguration = serviceConfigurationFactory.newComponentConfigurationInstance(metadata.getId());
serviceComponentConfiguration.setDefinition(metadata);
serviceComponentConfiguration.setName(metadata.getName());
serviceComponentConfiguration.setProperties(values);
serviceConfiguration.getComponentConfigurations().add(serviceComponentConfiguration);
}
}
return serviceConfiguration;
}

@PUT
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@ApiOperation(nickname = "serviceConfigurationUpdate", value = "Updates multiple services configuration", notes = "Updates the configuration of multiple services in an account")
public Response update(
@ApiParam(value = "The ScopeId in which to search results.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId,
@ApiParam(value = "The values for the configurations", required = true) ServiceConfiguration serviceConfiguration
) throws Exception {
for (ServiceComponentConfiguration serviceComponentConfiguration : serviceConfiguration.getComponentConfigurations()) {
Class<KapuaService> configurableServiceClass = (Class<KapuaService>) Class.forName(serviceComponentConfiguration.getId()).asSubclass(KapuaService.class);
if (KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) {
KapuaConfigurableService configurableService = (KapuaConfigurableService) locator.getService(configurableServiceClass);
Account account = accountService.find(scopeId);
configurableService.setConfigValues(scopeId, account.getScopeId(), serviceComponentConfiguration.getProperties());
}
}
return Response.noContent().build();
}

@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("{serviceId}")
@ApiOperation(nickname = "serviceConfigurationComponentGet", value = "Gets the configuration of a service on an account", notes = "Returns the configuration of a single service in an account")
public ServiceComponentConfiguration getComponent(
@ApiParam(value = "The ScopeId in which to search results.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId,
@ApiParam(value = "The full class name of the service.", required = true) @PathParam("serviceId") String serviceId
) throws Exception {
Class<KapuaService> configurableServiceClass = (Class<KapuaService>) Class.forName(serviceId).asSubclass(KapuaService.class);
if (KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) {
KapuaConfigurableService configurableService = (KapuaConfigurableService)locator.getService(configurableServiceClass);
KapuaTocd metadata = configurableService.getConfigMetadata(scopeId);
Map<String, Object> values = configurableService.getConfigValues(scopeId);
if (metadata != null && !(metadata instanceof EmptyTocd)) {
ServiceComponentConfiguration serviceComponentConfiguration = serviceConfigurationFactory.newComponentConfigurationInstance(metadata.getId());
serviceComponentConfiguration.setDefinition(metadata);
serviceComponentConfiguration.setName(metadata.getName());
serviceComponentConfiguration.setProperties(values);
return serviceComponentConfiguration;
}
}
return null;
}

@PUT
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("{serviceId}")
@ApiOperation(nickname = "serviceConfigurationComponentUpdate", value = "Updates the configuration of a service on an account", notes = "Updates a service component configuration")
public Response updateComponent(
@ApiParam(value = "The ScopeId in which to search results.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId,
@ApiParam(value = "The full class name of the service.", required = true) @PathParam("serviceId") String serviceId,
@ApiParam(value = "The values for the configurations", required = true) ServiceComponentConfiguration serviceComponentConfiguration
) throws Exception {
Class<KapuaService> configurableServiceClass = (Class<KapuaService>) Class.forName(serviceId).asSubclass(KapuaService.class);
if (KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) {
KapuaConfigurableService configurableService = (KapuaConfigurableService) locator.getService(configurableServiceClass);
Account account = accountService.find(scopeId);
configurableService.setConfigValues(scopeId, account.getScopeId(), serviceComponentConfiguration.getProperties());
}
return Response.noContent().build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
import org.eclipse.kapua.service.authorization.role.RolePermissionXmlRegistry;
import org.eclipse.kapua.service.authorization.role.RoleQuery;
import org.eclipse.kapua.service.authorization.role.RoleXmlRegistry;
import org.eclipse.kapua.service.config.ServiceComponentConfiguration;
import org.eclipse.kapua.service.config.ServiceConfiguration;
import org.eclipse.kapua.service.config.ServiceConfigurationXmlRegistry;
import org.eclipse.kapua.service.datastore.client.model.InsertResponse;
import org.eclipse.kapua.service.datastore.model.ChannelInfo;
import org.eclipse.kapua.service.datastore.model.ChannelInfoListResult;
Expand Down Expand Up @@ -485,8 +488,12 @@ public JaxbContextResolver() {
EventStoreRecordCreator.class,
EventStoreRecordListResult.class,
EventStoreRecordQuery.class,
EventStoreXmlRegistry.class
EventStoreXmlRegistry.class,

// Service Config
ServiceConfigurationXmlRegistry.class,
ServiceConfiguration.class,
ServiceComponentConfiguration.class
}, properties);
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down
1 change: 1 addition & 0 deletions rest-api/web/src/main/resources/locator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
<api>org.eclipse.kapua.transport.TransportClientFactory</api>

<api>org.eclipse.kapua.model.config.metatype.KapuaMetatypeFactory</api>
<api>org.eclipse.kapua.service.config.ServiceConfigurationFactory</api>

<api>org.eclipse.kapua.commons.service.event.store.api.EventStoreFactory</api>
</provided>
Expand Down
4 changes: 4 additions & 0 deletions rest-api/web/src/main/resources/shiro.ini
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ securityManager.rememberMeManager.cookie.maxAge = 0
/v1/*/accounts.json = kapuaAuthcAccessToken
/v1/*/accounts/** = kapuaAuthcAccessToken

# Configurations
/v1/*/configurations = kapuaAuthcAccessToken
/v1/*/configurations/** = kapuaAuthcAccessToken

# Datastore
/v1/*/data/clients.xml = kapuaAuthcAccessToken
/v1/*/data/clients.json = kapuaAuthcAccessToken
Expand Down
Loading

0 comments on commit 50d4d4e

Please sign in to comment.