use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization in project open-smart-grid-platform by OSGP.
the class DeviceManagementService method setOwner.
// === SET OWNER ===
/**
* Sets the owner of the device
*
* @param organisationIdentification The organisation identification who performs the action
* (needed for security)
* @param deviceIdentification The device identification of the device
* @param newOwner The organisation identification of the new owner.
* @throws FunctionalException In case the device or organisation can not be found or the
* organisation is not allowed to perform this action.
*/
public void setOwner(@Identification final String organisationIdentification, @Identification final String deviceIdentification, @Identification final String newOwner) throws FunctionalException {
Organisation organisation = this.findOrganisation(organisationIdentification);
final Device device = this.findDevice(deviceIdentification);
this.isAllowed(organisation, PlatformFunction.SET_OWNER);
organisation = this.findOrganisation(newOwner);
// First remove any other owners.
final List<DeviceAuthorization> owners = this.authorizationRepository.findByDeviceAndFunctionGroup(device, DeviceFunctionGroup.OWNER);
if (!owners.isEmpty()) {
for (final DeviceAuthorization owner : owners) {
this.authorizationRepository.delete(owner);
}
}
// Now add the authorization
final DeviceAuthorization authorization = new DeviceAuthorization(device, organisation, DeviceFunctionGroup.OWNER);
this.authorizationRepository.save(authorization);
}
use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization in project open-smart-grid-platform by OSGP.
the class DeviceManagementService method updateKey.
// === UPDATE KEY ===
public void updateKey(final String organisationIdentification, @Identification final String deviceIdentification, @PublicKey final String publicKey, final Long protocolInfoId) throws FunctionalException {
LOGGER.debug("Updating key for device [{}] on behalf of organisation [{}]", deviceIdentification, organisationIdentification);
final Organisation organisation = this.findOrganisation(organisationIdentification);
this.isAllowed(organisation, PlatformFunction.UPDATE_KEY);
this.organisationDomainService.isOrganisationEnabled(organisation, ComponentType.WS_ADMIN);
final Device device = this.deviceRepository.findByDeviceIdentification(deviceIdentification);
if (device == null) {
// Device not found, create new device
LOGGER.debug("Device [{}] does not exist, creating new device", deviceIdentification);
final Ssld ssld = new Ssld(deviceIdentification);
final DeviceAuthorization authorization = ssld.addAuthorization(organisation, DeviceFunctionGroup.OWNER);
final ProtocolInfo protocolInfo = this.protocolRepository.findById(protocolInfoId).orElseThrow(() -> new EntityNotFoundException("No protocol info record found with ID: " + protocolInfoId));
ssld.updateProtocol(protocolInfo);
this.authorizationRepository.save(authorization);
}
this.enqueueUpdateKeyRequest(organisationIdentification, deviceIdentification, publicKey);
}
use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization in project open-smart-grid-platform by OSGP.
the class DeviceInstallationService method addDevice.
@Transactional(value = "writableTransactionManager")
public void addDevice(@Identification final String organisationIdentification, @Valid final Device newDevice, @Identification final String ownerOrganisationIdentification) throws FunctionalException {
final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
this.domainHelperService.isAllowed(organisation, PlatformFunction.GET_ORGANISATIONS);
this.domainHelperService.isOrganisationEnabled(organisation);
// If the device already exists, throw an exception.
final Device existingDevice = this.writableDeviceRepository.findByDeviceIdentification(newDevice.getDeviceIdentification());
// field
if (existingDevice != null && StringUtils.isNotBlank(existingDevice.getDeviceType())) {
throw new FunctionalException(FunctionalExceptionType.EXISTING_DEVICE, ComponentType.WS_CORE, new ExistingEntityException(Device.class, newDevice.getDeviceIdentification()));
}
Ssld ssld;
if (existingDevice != null) {
// Update existing device
ssld = this.writableSsldRepository.findByDeviceIdentification(newDevice.getDeviceIdentification());
ssld.updateMetaData(newDevice.getAlias(), newDevice.getContainerAddress(), newDevice.getGpsCoordinates());
ssld.getAuthorizations().clear();
} else {
// Create a new SSLD instance.
ssld = new Ssld(newDevice.getDeviceIdentification(), newDevice.getAlias(), newDevice.getContainerAddress(), newDevice.getGpsCoordinates(), null);
}
ssld.setHasSchedule(false);
ssld.setDeviceModel(newDevice.getDeviceModel());
final Organisation ownerOrganisation = this.domainHelperService.findOrganisation(ownerOrganisationIdentification);
ssld = this.writableSsldRepository.save(ssld);
final DeviceAuthorization authorization = ssld.addAuthorization(ownerOrganisation, DeviceFunctionGroup.OWNER);
this.writableAuthorizationRepository.save(authorization);
LOGGER.info("Created new device {} with owner {}", newDevice.getDeviceIdentification(), ownerOrganisationIdentification);
}
use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization in project open-smart-grid-platform by OSGP.
the class DeviceInstallationService method updateDevice.
@Transactional(value = "writableTransactionManager")
public void updateDevice(@Identification final String organisationIdentification, @Valid final Ssld updateDevice) throws FunctionalException {
final Ssld existingDevice = this.writableSsldRepository.findByDeviceIdentification(updateDevice.getDeviceIdentification());
if (existingDevice == null) {
// device does not exist
LOGGER.info("Device does not exist, nothing to update.");
throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICE, ComponentType.WS_CORE, new UnknownEntityException(Device.class, updateDevice.getDeviceIdentification()));
}
final List<DeviceAuthorization> owners = this.writableAuthorizationRepository.findByDeviceAndFunctionGroup(existingDevice, DeviceFunctionGroup.OWNER);
// Check organisation against owner of device
boolean isOwner = false;
for (final DeviceAuthorization owner : owners) {
if (owner.getOrganisation().getOrganisationIdentification().equalsIgnoreCase(organisationIdentification)) {
isOwner = true;
}
}
if (!isOwner) {
LOGGER.info("Device has no owner yet, or organisation is not the owner.");
throw new FunctionalException(FunctionalExceptionType.UNAUTHORIZED, ComponentType.WS_CORE, new NotAuthorizedException(organisationIdentification));
}
// Update the device
existingDevice.updateMetaData(updateDevice.getAlias(), updateDevice.getContainerAddress(), updateDevice.getGpsCoordinates());
existingDevice.setPublicKeyPresent(updateDevice.isPublicKeyPresent());
this.writableSsldRepository.save(existingDevice);
}
use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization in project open-smart-grid-platform by OSGP.
the class DeviceManagementService method findDevices.
/**
* Find all devices
*
* @param organisationIdentification The organisation who performed the action
* @param pageSpecifier The page to be returned
* @param deviceFilter the filter object
* @return A page with devices
* @throws FunctionalException
*/
@Transactional(value = "transactionManager")
public Page<Device> findDevices(@Identification final String organisationIdentification, final PageSpecifier pageSpecifier, final DeviceFilter deviceFilter) throws FunctionalException {
final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
this.domainHelperService.isAllowed(organisation, PlatformFunction.FIND_DEVICES);
this.pagingSettings.updatePagingSettings(pageSpecifier);
Sort.Direction sortDir = Sort.Direction.DESC;
String sortedBy = "creationTime";
if (deviceFilter != null) {
if (!StringUtils.isEmpty(deviceFilter.getSortDir()) && deviceFilter.getSortDir().contains("asc")) {
sortDir = Sort.Direction.ASC;
}
if (!StringUtils.isEmpty(deviceFilter.getSortedBy())) {
sortedBy = deviceFilter.getSortedBy();
}
}
final PageRequest request = PageRequest.of(this.pagingSettings.getPageNumber(), this.pagingSettings.getPageSize(), sortDir, sortedBy);
final Page<Device> devices = this.findDevices(organisationIdentification, deviceFilter, organisation, request);
if (devices == null) {
LOGGER.info("No devices found");
return null;
}
for (final Device device : devices.getContent()) {
for (final DeviceAuthorization deviceAutorization : device.getAuthorizations()) {
device.addOrganisation(deviceAutorization.getOrganisation().getOrganisationIdentification());
}
}
return devices;
}
Aggregations