use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization in project open-smart-grid-platform by OSGP.
the class DeviceManagementService method addDeviceAuthorization.
public void addDeviceAuthorization(@Identification final String ownerOrganisationIdentification, @Identification final String organisationIdentification, @Identification final String deviceIdentification, @NotNull final DeviceFunctionGroup group) throws FunctionalException {
// Check input data and authorization
final Organisation organisation = this.findOrganisation(organisationIdentification);
final Organisation ownerOrganisation = this.findOrganisation(ownerOrganisationIdentification);
final Device device = this.findDevice(deviceIdentification);
this.isAllowed(ownerOrganisation, device, DeviceFunction.SET_DEVICE_AUTHORIZATION);
// Check if group is already set on device
for (final DeviceAuthorization authorization : device.getAuthorizations()) {
if (authorization.getOrganisation() == organisation && authorization.getFunctionGroup() == group) {
LOGGER.info("Organisation {} already has authorization for group {} on device {}", organisationIdentification, group, deviceIdentification);
// Ignore the request, the authorization is already available
return;
}
}
// All checks pass, add new authorization
final DeviceAuthorization authorization = device.addAuthorization(organisation, group);
this.deviceRepository.save(device);
this.authorizationRepository.save(authorization);
LOGGER.info("Organisation {} now has authorization for function group {} on device {}", organisationIdentification, group, deviceIdentification);
}
use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization in project open-smart-grid-platform by OSGP.
the class DeviceManagementService method removeDevice.
// === REMOVE DEVICE ===
/**
* Removes a device.
*
* @param organisationIdentification The organisation identification who performs the action
* @param deviceIdentification The device identification of the device
* @throws FunctionalException In case the device or organisation can not be found or the
* organisation is not allowed to perform this action.
*/
public void removeDevice(@Identification final String organisationIdentification, @Identification final String deviceIdentification) throws FunctionalException {
final Organisation organisation = this.findOrganisation(organisationIdentification);
final Device device = this.findDevice(deviceIdentification);
this.isAllowed(organisation, device, DeviceFunction.REMOVE_DEVICE);
// First remove all authorizations
final List<DeviceAuthorization> authorisations = this.authorizationRepository.findByDevice(device);
for (final DeviceAuthorization authorisation : authorisations) {
this.authorizationRepository.delete(authorisation);
}
// Remove all events
final List<Event> events = this.eventRepository.findByDeviceIdentification(deviceIdentification);
for (final Event event : events) {
this.eventRepository.delete(event);
}
// Then remove the device.
this.deviceRepository.delete(device);
}
use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization in project open-smart-grid-platform by OSGP.
the class DeviceConverterHelper method initJaxb.
Device initJaxb(final T source) {
final org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.Device destination = new org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.Device();
destination.setAlias(source.getAlias());
destination.setActivated(source.isActivated());
destination.setDeviceLifecycleStatus(org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.DeviceLifecycleStatus.valueOf(source.getDeviceLifecycleStatus().name()));
destination.setContainerAddress(this.mapper.map(source.getContainerAddress(), org.opensmartgridplatform.adapter.ws.schema.core.common.Address.class));
destination.setDeviceIdentification(source.getDeviceIdentification());
destination.setDeviceType(source.getDeviceType());
destination.setTechnicalInstallationDate(this.mapper.map(source.getTechnicalInstallationDate(), XMLGregorianCalendar.class));
if (!Objects.isNull(source.getGpsCoordinates())) {
final GpsCoordinates gpsCoordinates = source.getGpsCoordinates();
if (gpsCoordinates.getLatitude() != null) {
destination.setGpsLatitude(Float.toString(gpsCoordinates.getLatitude()));
}
if (gpsCoordinates.getLongitude() != null) {
destination.setGpsLongitude(Float.toString(gpsCoordinates.getLongitude()));
}
}
destination.setNetworkAddress(source.getNetworkAddress() == null ? null : source.getNetworkAddress().toString());
destination.setOwner(source.getOwner() == null ? "" : source.getOwner().getName());
destination.getOrganisations().addAll(source.getOrganisations());
destination.setInMaintenance(source.isInMaintenance());
final List<org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.DeviceAuthorization> deviceAuthorizations = new ArrayList<>();
for (final DeviceAuthorization deviceAuthorisation : source.getAuthorizations()) {
final org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.DeviceAuthorization newDeviceAuthorization = new org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.DeviceAuthorization();
newDeviceAuthorization.setFunctionGroup(deviceAuthorisation.getFunctionGroup().name());
newDeviceAuthorization.setOrganisation(deviceAuthorisation.getOrganisation().getOrganisationIdentification());
deviceAuthorizations.add(newDeviceAuthorization);
}
destination.getDeviceAuthorizations().addAll(deviceAuthorizations);
if (source.getDeviceModel() != null) {
final DeviceModel deviceModel = new DeviceModel();
deviceModel.setDescription(source.getDeviceModel().getDescription());
if (source.getDeviceModel().getManufacturer() != null) {
final Manufacturer manufacturer = new Manufacturer();
manufacturer.setManufacturerId(source.getDeviceModel().getManufacturer().getCode());
manufacturer.setName(source.getDeviceModel().getManufacturer().getName());
manufacturer.setUsePrefix(source.getDeviceModel().getManufacturer().isUsePrefix());
deviceModel.setManufacturer(manufacturer);
}
deviceModel.setModelCode(source.getDeviceModel().getModelCode());
destination.setDeviceModel(deviceModel);
}
destination.setLastCommunicationTime(this.mapper.map(source.getLastSuccessfulConnectionTimestamp(), XMLGregorianCalendar.class));
if (source instanceof LightMeasurementDevice) {
final LightMeasurementDevice sourceLmd = (LightMeasurementDevice) source;
final org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.LightMeasurementDevice destinationLmd = new org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.LightMeasurementDevice();
destinationLmd.setDescription(sourceLmd.getDescription());
destinationLmd.setCode(sourceLmd.getCode());
destinationLmd.setColor(sourceLmd.getColor());
destinationLmd.setDigitalInput(sourceLmd.getDigitalInput());
destinationLmd.setLastCommunicationTime(this.mapper.map(sourceLmd.getLastCommunicationTime(), XMLGregorianCalendar.class));
destination.setLightMeasurementDevice(destinationLmd);
}
return destination;
}
use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization in project open-smart-grid-platform by OSGP.
the class DeviceManagementService method setMaintenanceStatus.
@Transactional(value = "writableTransactionManager")
public void setMaintenanceStatus(@Identification final String organisationIdentification, final String deviceIdentification, final boolean status) throws FunctionalException {
final Device existingDevice = this.writableDeviceRepository.findByDeviceIdentification(deviceIdentification);
if (existingDevice == null) {
// device does not exist
LOGGER.info("Device does not exist, cannot set maintenance status.");
throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICE, ComponentType.WS_CORE, new UnknownEntityException(Device.class, deviceIdentification));
} else {
// Check to see if the organisation is CONFIGURATION or OWNER
// authorized
boolean isAuthorized = false;
for (final DeviceAuthorization authorizations : existingDevice.getAuthorizations()) {
if (organisationIdentification.equals(authorizations.getOrganisation().getOrganisationIdentification()) && (DeviceFunctionGroup.OWNER.equals(authorizations.getFunctionGroup()) || DeviceFunctionGroup.CONFIGURATION.equals(authorizations.getFunctionGroup()))) {
isAuthorized = true;
existingDevice.updateInMaintenance(status);
this.writableDeviceRepository.save(existingDevice);
break;
}
}
if (!isAuthorized) {
// unauthorized, throwing exception.
throw new FunctionalException(FunctionalExceptionType.UNAUTHORIZED, ComponentType.WS_CORE, new NotAuthorizedException(organisationIdentification));
}
}
}
use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization in project open-smart-grid-platform by OSGP.
the class DeviceManagementService method updateDevice.
@Transactional(value = "writableTransactionManager")
public void updateDevice(@Identification final String organisationIdentification, final String deviceToUpdateIdentification, @Valid final Ssld updateDevice) throws FunctionalException {
final Device existingDevice = this.writableDeviceRepository.findByDeviceIdentification(deviceToUpdateIdentification);
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, deviceToUpdateIdentification));
}
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.setActivated(updateDevice.isActivated());
if (updateDevice.getDeviceLifecycleStatus() != null) {
existingDevice.setDeviceLifecycleStatus(updateDevice.getDeviceLifecycleStatus());
}
if (updateDevice.getTechnicalInstallationDate() != null) {
existingDevice.setTechnicalInstallationDate(updateDevice.getTechnicalInstallationDate());
}
final Ssld ssld = this.writableSsldRepository.findById(existingDevice.getId()).orElseThrow(() -> new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICE, ComponentType.WS_CORE));
ssld.updateOutputSettings(updateDevice.receiveOutputSettings());
ssld.setEans(updateDevice.getEans());
for (final Ean ean : updateDevice.getEans()) {
ean.setDevice(ssld);
}
this.writableSsldRepository.save(ssld);
}
Aggregations