Search in sources :

Example 1 with DeviceClassCacheObject

use of cern.c2mon.server.common.device.DeviceClassCacheObject in project c2mon by c2mon.

the class DeviceClassCacheTest method testGetDeviceClassByName.

@Test
public void testGetDeviceClassByName() {
    DeviceClassCacheObject deviceClass1 = new DeviceClassCacheObject(1L, "test_device_class_name_1", "Test description");
    DeviceClassCacheObject deviceClass2 = new DeviceClassCacheObject(2L, "test_device_class_name_2", "Test description");
    DeviceClassCacheObject deviceClass3 = new DeviceClassCacheObject(3L, "test_device_class_name_3", "Test description");
    deviceClassCache.put(deviceClass1.getId(), deviceClass1);
    deviceClassCache.put(deviceClass2.getId(), deviceClass2);
    deviceClassCache.put(deviceClass3.getId(), deviceClass3);
    long deviceClassId = deviceClassCache.getDeviceClassIdByName("test_device_class_name_1");
    Assert.assertEquals(deviceClassId, (long) deviceClass1.getId());
    deviceClassId = deviceClassCache.getDeviceClassIdByName("test_device_class_name_2");
    Assert.assertEquals(deviceClassId, (long) deviceClass2.getId());
    deviceClassId = deviceClassCache.getDeviceClassIdByName("test_device_class_name_3");
    Assert.assertEquals(deviceClassId, (long) deviceClass3.getId());
    // Test getting an unknown device class
    try {
        deviceClassId = deviceClassCache.getDeviceClassIdByName("unknown_device_class");
        Assert.fail("getDeviceClassByName() did not throw exception");
    } catch (CacheElementNotFoundException e) {
    }
}
Also used : CacheElementNotFoundException(cern.c2mon.server.cache.exception.CacheElementNotFoundException) DeviceClassCacheObject(cern.c2mon.server.common.device.DeviceClassCacheObject) AbstractCacheIntegrationTest(cern.c2mon.server.cache.AbstractCacheIntegrationTest) Test(org.junit.Test)

Example 2 with DeviceClassCacheObject

use of cern.c2mon.server.common.device.DeviceClassCacheObject in project c2mon by c2mon.

the class DeviceClassMapperTest method testInsertDeviceClass.

@Test
public void testInsertDeviceClass() throws ClassNotFoundException {
    DeviceClassCacheObject deviceClass = new DeviceClassCacheObject(402L, "TEST_DEVICE_CLASS_3", "Description of TEST_DEVICE_CLASS_3");
    List<Property> properties = new ArrayList<>();
    properties.add(new Property(10L, "TEST_PROPERTY_1", "Test property 1"));
    properties.add(new Property(11L, "TEST_PROPERTY_2", "Test property 2"));
    List<Property> fields = new ArrayList<>();
    fields.add(new Property(12L, "TEST_FIELD_1", null));
    fields.add(new Property(13L, "TEST_FIELD_2", null));
    properties.add(new Property(14L, "TEST_PROPERTY_WITH_FIELDS", "Test property with fields", fields));
    deviceClass.setProperties(properties);
    deviceClass.setCommands(Arrays.asList(new Command(10L, "TEST_COMMAND_1", "Test command 1"), new Command(11L, "TEST_COMMAND_2", "Test command 2")));
    deviceClassMapper.insertDeviceClass(deviceClass);
    for (Property property : ((DeviceClassCacheObject) deviceClass).getProperties()) {
        deviceClassMapper.insertDeviceClassProperty(deviceClass.getId(), property);
        if (property.getFields() != null) {
            for (Property field : property.getFields()) {
                deviceClassMapper.insertDeviceClassField(property.getId(), field);
            }
        }
    }
    for (Command command : ((DeviceClassCacheObject) deviceClass).getCommands()) {
        deviceClassMapper.insertDeviceClassCommand(deviceClass.getId(), command);
    }
    Assert.assertTrue(deviceClassMapper.isInDb(402L));
    DeviceClassCacheObject fromDb = (DeviceClassCacheObject) deviceClassMapper.getItem(402L);
    Assert.assertNotNull(fromDb);
    List<Property> retrievedProperties = fromDb.getProperties();
    Assert.assertNotNull(retrievedProperties);
    Assert.assertTrue(retrievedProperties.size() == 3);
    assertPropertyListContains(retrievedProperties, new Property(10L, "TEST_PROPERTY_1", "Test property 1"));
    assertPropertyListContains(retrievedProperties, new Property(11L, "TEST_PROPERTY_2", "Test property 2"));
    assertPropertyListContains(retrievedProperties, new Property(14L, "TEST_PROPERTY_WITH_FIELDS", "Test property with fields", fields));
    List<Property> retrievedFields = retrievedProperties.get(0).getFields();
    assertPropertyListContains(retrievedFields, new Property(12L, "TEST_FIELD_1", null));
    assertPropertyListContains(retrievedFields, new Property(13L, "TEST_FIELD_2", null));
    List<Command> commands = fromDb.getCommands();
    Assert.assertNotNull(commands);
    Assert.assertTrue(commands.size() == 2);
    assertCommandListContains(commands, new Command(10L, "TEST_COMMAND_1", "Test command 1"));
    assertCommandListContains(commands, new Command(11L, "TEST_COMMAND_2", "Test command 2"));
}
Also used : Command(cern.c2mon.server.common.device.Command) ArrayList(java.util.ArrayList) DeviceClassCacheObject(cern.c2mon.server.common.device.DeviceClassCacheObject) Property(cern.c2mon.server.common.device.Property) Test(org.junit.Test)

Example 3 with DeviceClassCacheObject

use of cern.c2mon.server.common.device.DeviceClassCacheObject in project c2mon by c2mon.

the class DeviceClassFacadeImpl method createCacheObject.

@Override
public DeviceClass createCacheObject(Long id, Properties properties) throws IllegalAccessException {
    DeviceClassCacheObject deviceClassCacheObject = new DeviceClassCacheObject(id);
    configureCacheObject(deviceClassCacheObject, properties);
    validateConfig(deviceClassCacheObject);
    return deviceClassCacheObject;
}
Also used : DeviceClassCacheObject(cern.c2mon.server.common.device.DeviceClassCacheObject)

Example 4 with DeviceClassCacheObject

use of cern.c2mon.server.common.device.DeviceClassCacheObject in project c2mon by c2mon.

the class DeviceClassMapperTest method testGetItem.

@Test
public void testGetItem() throws ClassNotFoundException {
    DeviceClassCacheObject deviceClass1 = (DeviceClassCacheObject) deviceClassMapper.getItem(400L);
    Assert.assertNotNull(deviceClass1);
    List<Property> properties = deviceClass1.getProperties();
    Assert.assertNotNull(properties);
    Assert.assertTrue(properties.size() == 5);
    assertPropertyListContains(properties, new Property(1L, "cpuLoadInPercent", "The current CPU load in percent"));
    assertPropertyListContains(properties, new Property(2L, "responsiblePerson", "The person responsible for this device"));
    assertPropertyListContains(properties, new Property(3L, "someCalculations", "Some super awesome calculations"));
    assertPropertyListContains(properties, new Property(4L, "numCores", "The number of CPU cores on this device"));
    List<Property> fields = new ArrayList<>();
    fields.add(new Property(1L, "FIELD_CPULOAD", null));
    fields.add(new Property(2L, "FIELD_RESPONSIBLE_PERSON", null));
    fields.add(new Property(3L, "FIELD_SOME_CALCULATIONS", null));
    fields.add(new Property(4L, "FIELD_NUM_CORES", null));
    assertPropertyListContains(properties, new Property(9L, "TEST_PROPERTY_WITH_FIELDS", "Description of TEST_PROPERTY_WITH_FIELDS", fields));
    List<Command> commands = deviceClass1.getCommands();
    Assert.assertNotNull(commands);
    Assert.assertTrue(commands.size() == 2);
    assertCommandListContains(commands, new Command(1L, "TEST_COMMAND_1", "Description of TEST_COMMAND_1"));
    assertCommandListContains(commands, new Command(2L, "TEST_COMMAND_2", "Description of TEST_COMMAND_2"));
    List<Long> deviceIds = deviceClass1.getDeviceIds();
    Assert.assertNotNull(deviceIds);
    Assert.assertFalse(deviceIds.isEmpty());
    Assert.assertTrue(deviceIds.contains(300L));
    Assert.assertTrue(deviceIds.contains(301L));
    DeviceClassCacheObject deviceClass2 = (DeviceClassCacheObject) deviceClassMapper.getItem(401L);
    Assert.assertNotNull(deviceClass2);
    properties = deviceClass2.getProperties();
    Assert.assertNotNull(properties);
    Assert.assertTrue(properties.size() == 4);
    assertPropertyListContains(properties, new Property(5L, "TEST_PROPERTY_1", "Description of TEST_PROPERTY_1"));
    assertPropertyListContains(properties, new Property(6L, "TEST_PROPERTY_2", "Description of TEST_PROPERTY_2"));
    assertPropertyListContains(properties, new Property(7L, "TEST_PROPERTY_3", "Description of TEST_PROPERTY_3"));
    assertPropertyListContains(properties, new Property(8L, "TEST_PROPERTY_4", "Description of TEST_PROPERTY_4"));
    commands = deviceClass2.getCommands();
    Assert.assertNotNull(commands);
    Assert.assertTrue(commands.size() == 2);
    assertCommandListContains(commands, new Command(3L, "TEST_COMMAND_3", "Description of TEST_COMMAND_3"));
    assertCommandListContains(commands, new Command(4L, "TEST_COMMAND_4", "Description of TEST_COMMAND_4"));
    deviceIds = deviceClass2.getDeviceIds();
    Assert.assertNotNull(deviceIds);
    Assert.assertFalse(deviceIds.isEmpty());
    Assert.assertTrue(deviceIds.contains(302L));
    Assert.assertTrue(deviceIds.contains(303L));
}
Also used : Command(cern.c2mon.server.common.device.Command) ArrayList(java.util.ArrayList) DeviceClassCacheObject(cern.c2mon.server.common.device.DeviceClassCacheObject) Property(cern.c2mon.server.common.device.Property) Test(org.junit.Test)

Example 5 with DeviceClassCacheObject

use of cern.c2mon.server.common.device.DeviceClassCacheObject in project c2mon by c2mon.

the class DeviceClassConfigTransactedImpl method doRemoveDeviceClass.

@Override
@Transactional(value = "cacheTransactionManager", propagation = Propagation.REQUIRES_NEW)
public ProcessChange doRemoveDeviceClass(Long id, ConfigurationElementReport elementReport) {
    LOGGER.trace("Removing DeviceClass " + id);
    try {
        deviceClassCache.acquireWriteLockOnKey(id);
        DeviceClassCacheObject deviceClass = (DeviceClassCacheObject) deviceClassCache.get(id);
        // Remove all Devices dependent on this class (using DeviceConfigHandler)
        List<Long> deviceIds = deviceClass.getDeviceIds();
        if (!deviceIds.isEmpty()) {
            LOGGER.trace("Removing Devices dependent on DeviceClass " + id);
            for (Long deviceId : deviceIds) {
                ConfigurationElementReport newReport = new ConfigurationElementReport(Action.REMOVE, Entity.DEVICE, deviceId);
                elementReport.addSubReport(newReport);
                deviceConfigHandler.removeDevice(deviceId, newReport);
            }
        }
        try {
            deviceClassDAO.deleteItem(deviceClass);
            return new ProcessChange();
        } catch (Exception e) {
            LOGGER.error("Exception caught while removing a DeviceClass.", e);
            elementReport.setFailure("Unable to remove DeviceClass with id " + id);
            throw new UnexpectedRollbackException("Unable to remove DeviceClass " + id, e);
        }
    } catch (CacheElementNotFoundException e) {
        LOGGER.warn("Attempting to remove a non-existent DeviceClass - no action taken.");
        elementReport.setWarning("Attempting to remove a non-existent DeviceClass");
        return new ProcessChange();
    } finally {
        if (deviceClassCache.isWriteLockedByCurrentThread(id)) {
            deviceClassCache.releaseWriteLockOnKey(id);
        }
    }
}
Also used : ProcessChange(cern.c2mon.server.configuration.impl.ProcessChange) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) CacheElementNotFoundException(cern.c2mon.server.cache.exception.CacheElementNotFoundException) DeviceClassCacheObject(cern.c2mon.server.common.device.DeviceClassCacheObject) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) ConfigurationException(cern.c2mon.shared.common.ConfigurationException) CacheElementNotFoundException(cern.c2mon.server.cache.exception.CacheElementNotFoundException) ConfigurationElementReport(cern.c2mon.shared.client.configuration.ConfigurationElementReport) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

DeviceClassCacheObject (cern.c2mon.server.common.device.DeviceClassCacheObject)8 Test (org.junit.Test)5 Command (cern.c2mon.server.common.device.Command)3 Property (cern.c2mon.server.common.device.Property)3 AbstractCacheIntegrationTest (cern.c2mon.server.cache.AbstractCacheIntegrationTest)2 CacheElementNotFoundException (cern.c2mon.server.cache.exception.CacheElementNotFoundException)2 ConfigurationException (cern.c2mon.shared.common.ConfigurationException)2 ArrayList (java.util.ArrayList)2 DeviceClass (cern.c2mon.server.common.device.DeviceClass)1 ProcessChange (cern.c2mon.server.configuration.impl.ProcessChange)1 ConfigurationElementReport (cern.c2mon.shared.client.configuration.ConfigurationElementReport)1 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)1 Transactional (org.springframework.transaction.annotation.Transactional)1