Search in sources :

Example 1 with EventMessage

use of de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.EventMessage in project FAAAST-Service by FraunhoferIOSB.

the class MessageBusInternal method run.

/**
 * Take an EventMessage from the queue. Iterate over all subscribers and
 * check which filter applies and call the subscription handler
 */
@Override
public void run() {
    running.set(true);
    try {
        while (running.get()) {
            EventMessage message = messageQueue.take();
            Class<? extends EventMessage> messageType = message.getClass();
            for (SubscriptionInfo subscription : subscriptions.values()) {
                if (subscription.getSubscribedEvents().stream().anyMatch(x -> x.isAssignableFrom(messageType)) && subscription.getFilter().test(message.getElement())) {
                    subscription.getHandler().accept(message);
                }
            }
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}
Also used : MessageBus(de.fraunhofer.iosb.ilt.faaast.service.messagebus.MessageBus) EventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.EventMessage) SubscriptionInfo(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.SubscriptionInfo) CoreConfig(de.fraunhofer.iosb.ilt.faaast.service.config.CoreConfig) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ServiceContext(de.fraunhofer.iosb.ilt.faaast.service.ServiceContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockingQueue(java.util.concurrent.BlockingQueue) SubscriptionId(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.SubscriptionId) EventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.EventMessage) SubscriptionInfo(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.SubscriptionInfo)

Example 2 with EventMessage

use of de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.EventMessage in project FAAAST-Service by FraunhoferIOSB.

the class MessageBusInternalTest method testSubscribeUnsubscribe.

@Test
public void testSubscribeUnsubscribe() throws InterruptedException {
    MessageBusInternal messageBus = new MessageBusInternal();
    messageBus.start();
    CountDownLatch condition = new CountDownLatch(1);
    final AtomicReference<EventMessage> response = new AtomicReference<>();
    messageBus.unsubscribe(messageBus.subscribe(SubscriptionInfo.create(ValueChangeEventMessage.class, x -> {
        response.set(x);
        condition.countDown();
    })));
    try {
        messageBus.publish(valueChangeMessage);
    } catch (Exception e) {
        Assert.fail();
    }
    Assert.assertFalse(condition.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS));
    messageBus.stop();
}
Also used : ErrorEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.error.ErrorEventMessage) ChangeEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ChangeEventMessage) ValueChangeEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ValueChangeEventMessage) EventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.EventMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with EventMessage

use of de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.EventMessage in project FAAAST-Service by FraunhoferIOSB.

the class OpcUaEndpointTest method testUpdatePropertyValue.

/**
 * Test method for changing a property based on an event from the MessageBus. Sets an event
 * on the MessageBus and checks the new value in the server.
 *
 * @throws SecureIdentityException If the operation fails
 * @throws ServiceException If the operation fails
 * @throws IOException If the operation fails
 * @throws StatusException If the operation fails
 */
@Test
public void testUpdatePropertyValue() throws SecureIdentityException, ServiceException, IOException, StatusException, Exception {
    UaClient client = new UaClient(ENDPOINT_URL);
    client.setSecurityMode(SecurityMode.NONE);
    TestUtils.initialize(client);
    client.connect();
    System.out.println("testUpdatePropertyValue: client connected");
    aasns = client.getAddressSpace().getNamespaceTable().getIndex(VariableIds.AASAssetAdministrationShellType_AssetInformation_AssetKind.getNamespaceUri());
    List<RelativePath> relPath = new ArrayList<>();
    List<RelativePathElement> browsePath = new ArrayList<>();
    browsePath.add(new RelativePathElement(Identifiers.HierarchicalReferences, false, true, new QualifiedName(aasns, TestDefines.AAS_ENVIRONMENT_NAME)));
    browsePath.add(new RelativePathElement(Identifiers.HierarchicalReferences, false, true, new QualifiedName(aasns, TestDefines.SUBMODEL_OPER_DATA_NODE_NAME)));
    browsePath.add(new RelativePathElement(Identifiers.HierarchicalReferences, false, true, new QualifiedName(aasns, TestDefines.ROTATION_SPEED_NAME)));
    browsePath.add(new RelativePathElement(Identifiers.HasProperty, false, true, new QualifiedName(aasns, TestDefines.PROPERTY_VALUE_NAME)));
    relPath.add(new RelativePath(browsePath.toArray(RelativePathElement[]::new)));
    BrowsePathResult[] bpres = client.getAddressSpace().translateBrowsePathsToNodeIds(Identifiers.ObjectsFolder, relPath.toArray(RelativePath[]::new));
    Assert.assertNotNull("testWriteProperty Browse Result Null", bpres);
    Assert.assertTrue("testWriteProperty Browse Result: size doesn't match", bpres.length == 1);
    Assert.assertTrue("testWriteProperty Browse Result Good", bpres[0].getStatusCode().isGood());
    BrowsePathTarget[] targets = bpres[0].getTargets();
    Assert.assertNotNull("testWriteProperty ValueType Null", targets);
    Assert.assertTrue("testWriteProperty ValueType empty", targets.length > 0);
    DataValue value = client.readValue(targets[0].getTargetId());
    Assert.assertEquals(StatusCode.GOOD, value.getStatusCode());
    Integer oldValue = 4370;
    Assert.assertEquals("intial value not equal", oldValue, value.getValue().getValue());
    CountDownLatch condition = new CountDownLatch(1);
    final AtomicReference<EventMessage> response = new AtomicReference<>();
    service.getMessageBus().subscribe(SubscriptionInfo.create(ValueChangeEventMessage.class, x -> {
        response.set(x);
        condition.countDown();
    }));
    Integer newValue = 9999;
    List<Key> keys = new ArrayList<>();
    keys.add(new DefaultKey.Builder().idType(KeyType.IRI).type(KeyElements.SUBMODEL).value(TestDefines.SUBMODEL_OPER_DATA_NAME).build());
    keys.add(new DefaultKey.Builder().idType(KeyType.ID_SHORT).type(KeyElements.PROPERTY).value(TestDefines.ROTATION_SPEED_NAME).build());
    Reference propRef = new DefaultReference.Builder().keys(keys).build();
    ValueChangeEventMessage valueChangeMessage = new ValueChangeEventMessage();
    valueChangeMessage.setElement(propRef);
    // PropertyValue propertyValue = new PropertyValue();
    // propertyValue.setValue(new IntValue(oldValue));
    valueChangeMessage.setOldValue(PropertyValue.of(Datatype.Int, oldValue.toString()));
    // propertyValue.setValue(new IntValue(newValue));
    valueChangeMessage.setNewValue(PropertyValue.of(Datatype.Int, newValue.toString()));
    service.getMessageBus().publish(valueChangeMessage);
    Thread.sleep(100);
    // check MessageBus
    condition.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
    Assert.assertEquals(valueChangeMessage, response.get());
    // read new value
    value = client.readValue(targets[0].getTargetId());
    Assert.assertEquals(StatusCode.GOOD, value.getStatusCode());
    Assert.assertEquals("new value not equal", newValue, value.getValue().getValue());
    System.out.println("disconnect client");
    client.disconnect();
}
Also used : BrowsePathTarget(com.prosysopc.ua.stack.core.BrowsePathTarget) NodeId(com.prosysopc.ua.stack.builtintypes.NodeId) DefaultRelationshipElement(io.adminshell.aas.v3.model.impl.DefaultRelationshipElement) ValueChangeEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ValueChangeEventMessage) LoggerFactory(org.slf4j.LoggerFactory) ByteString(com.prosysopc.ua.stack.builtintypes.ByteString) ReferenceDescription(com.prosysopc.ua.stack.core.ReferenceDescription) TestUtils(de.fraunhofer.iosb.ilt.faaast.service.endpoint.opcua.helper.TestUtils) DefaultIdentifier(io.adminshell.aas.v3.model.impl.DefaultIdentifier) DefaultProperty(io.adminshell.aas.v3.model.impl.DefaultProperty) SubscriptionInfo(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.SubscriptionInfo) ServiceResultException(com.prosysopc.ua.stack.common.ServiceResultException) TestDefines(de.fraunhofer.iosb.ilt.faaast.service.endpoint.opcua.helper.TestDefines) TestService(de.fraunhofer.iosb.ilt.faaast.service.endpoint.opcua.helper.TestService) DefaultKey(io.adminshell.aas.v3.model.impl.DefaultKey) SecureIdentityException(com.prosysopc.ua.SecureIdentityException) AfterClass(org.junit.AfterClass) IdentifierType(io.adminshell.aas.v3.model.IdentifierType) Reference(io.adminshell.aas.v3.model.Reference) ElementCreateEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ElementCreateEventMessage) ServiceException(com.prosysopc.ua.ServiceException) AASRelationshipElementType(opc.i4aas.AASRelationshipElementType) LocalizedText(com.prosysopc.ua.stack.builtintypes.LocalizedText) EventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.EventMessage) Key(io.adminshell.aas.v3.model.Key) CountDownLatch(java.util.concurrent.CountDownLatch) Base64(java.util.Base64) List(java.util.List) StatusCode(com.prosysopc.ua.stack.builtintypes.StatusCode) AASKeyElementsDataType(opc.i4aas.AASKeyElementsDataType) CoreConfig(de.fraunhofer.iosb.ilt.faaast.service.config.CoreConfig) AASKeyTypeDataType(opc.i4aas.AASKeyTypeDataType) DataValue(com.prosysopc.ua.stack.builtintypes.DataValue) ModelingKind(io.adminshell.aas.v3.model.ModelingKind) RelativePath(com.prosysopc.ua.stack.core.RelativePath) KeyElements(io.adminshell.aas.v3.model.KeyElements) ServerState(com.prosysopc.ua.stack.core.ServerState) AASModelingKindDataType(opc.i4aas.AASModelingKindDataType) BeforeClass(org.junit.BeforeClass) StatusException(com.prosysopc.ua.StatusException) AtomicReference(java.util.concurrent.atomic.AtomicReference) UaClient(com.prosysopc.ua.client.UaClient) ArrayList(java.util.ArrayList) AASValueTypeDataType(opc.i4aas.AASValueTypeDataType) QualifiedName(com.prosysopc.ua.stack.builtintypes.QualifiedName) RelativePathElement(com.prosysopc.ua.stack.core.RelativePathElement) KeyType(io.adminshell.aas.v3.model.KeyType) LangString(io.adminshell.aas.v3.model.LangString) VariableIds(opc.i4aas.VariableIds) BrowsePathResult(com.prosysopc.ua.stack.core.BrowsePathResult) AASEntityType(opc.i4aas.AASEntityType) Identifiers(com.prosysopc.ua.stack.core.Identifiers) DefaultSubmodel(io.adminshell.aas.v3.model.impl.DefaultSubmodel) Logger(org.slf4j.Logger) AASKeyDataType(opc.i4aas.AASKeyDataType) Datatype(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype) IOException(java.io.IOException) Test(org.junit.Test) SecurityMode(com.prosysopc.ua.stack.transport.security.SecurityMode) AASIdentifierTypeDataType(opc.i4aas.AASIdentifierTypeDataType) TimeUnit(java.util.concurrent.TimeUnit) DefaultAdministrativeInformation(io.adminshell.aas.v3.model.impl.DefaultAdministrativeInformation) ConfigurationException(de.fraunhofer.iosb.ilt.faaast.service.exception.ConfigurationException) AddressSpaceException(com.prosysopc.ua.client.AddressSpaceException) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) Assert(org.junit.Assert) DataValue(com.prosysopc.ua.stack.builtintypes.DataValue) ArrayList(java.util.ArrayList) RelativePathElement(com.prosysopc.ua.stack.core.RelativePathElement) ValueChangeEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ValueChangeEventMessage) BrowsePathTarget(com.prosysopc.ua.stack.core.BrowsePathTarget) RelativePath(com.prosysopc.ua.stack.core.RelativePath) BrowsePathResult(com.prosysopc.ua.stack.core.BrowsePathResult) Reference(io.adminshell.aas.v3.model.Reference) AtomicReference(java.util.concurrent.atomic.AtomicReference) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) QualifiedName(com.prosysopc.ua.stack.builtintypes.QualifiedName) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) UaClient(com.prosysopc.ua.client.UaClient) ValueChangeEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ValueChangeEventMessage) ElementCreateEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ElementCreateEventMessage) EventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.EventMessage) DefaultKey(io.adminshell.aas.v3.model.impl.DefaultKey) DefaultKey(io.adminshell.aas.v3.model.impl.DefaultKey) Key(io.adminshell.aas.v3.model.Key) Test(org.junit.Test)

Example 4 with EventMessage

use of de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.EventMessage in project FAAAST-Service by FraunhoferIOSB.

the class MessageBusInternalTest method testSuperTypeSubscription.

@Test
public void testSuperTypeSubscription() throws InterruptedException {
    MessageBusInternal messageBus = new MessageBusInternal();
    messageBus.start();
    Set<EventMessage> messages = Set.of(valueChangeMessage, errorMessage);
    Set<EventMessage> responses = Collections.synchronizedSet(new HashSet<>());
    CountDownLatch condition = new CountDownLatch(messages.size());
    messageBus.subscribe(SubscriptionInfo.create(EventMessage.class, x -> {
        responses.add(x);
        condition.countDown();
    }));
    messages.forEach(x -> {
        try {
            messageBus.publish(x);
        } catch (Exception e) {
            Assert.fail();
        }
    });
    condition.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
    Assert.assertEquals(messages, responses);
    messageBus.stop();
}
Also used : ErrorEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.error.ErrorEventMessage) KeyElements(io.adminshell.aas.v3.model.KeyElements) ChangeEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ChangeEventMessage) ValueChangeEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ValueChangeEventMessage) BeforeClass(org.junit.BeforeClass) Reference(io.adminshell.aas.v3.model.Reference) Set(java.util.Set) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) TimeUnit(java.util.concurrent.TimeUnit) EventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.EventMessage) CountDownLatch(java.util.concurrent.CountDownLatch) SubscriptionInfo(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.SubscriptionInfo) Map(java.util.Map) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) ErrorLevel(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.error.ErrorLevel) KeyType(io.adminshell.aas.v3.model.KeyType) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) IntValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.IntValue) Assert(org.junit.Assert) DefaultKey(io.adminshell.aas.v3.model.impl.DefaultKey) Collections(java.util.Collections) ErrorEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.error.ErrorEventMessage) ChangeEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ChangeEventMessage) ValueChangeEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ValueChangeEventMessage) EventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.EventMessage) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with EventMessage

use of de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.EventMessage in project FAAAST-Service by FraunhoferIOSB.

the class MessageBusInternalTest method testDistinctTypesSubscription.

@Test
public void testDistinctTypesSubscription() throws InterruptedException {
    MessageBusInternal messageBus = new MessageBusInternal();
    messageBus.start();
    Map<Class<? extends EventMessage>, Set<EventMessage>> messages = Map.of(ChangeEventMessage.class, Set.of(valueChangeMessage), ErrorEventMessage.class, Set.of(errorMessage));
    Map<Class<? extends EventMessage>, Set<EventMessage>> responses = Collections.synchronizedMap(Map.of(ChangeEventMessage.class, new HashSet<>(), ErrorEventMessage.class, new HashSet<>()));
    CountDownLatch condition = new CountDownLatch(messages.values().stream().mapToInt(x -> x.size()).sum());
    responses.entrySet().forEach(entry -> messageBus.subscribe(SubscriptionInfo.create(entry.getKey(), x -> {
        entry.getValue().add(x);
        condition.countDown();
    })));
    messages.values().stream().flatMap(x -> x.stream()).forEach(x -> {
        try {
            messageBus.publish(x);
        } catch (Exception e) {
            Assert.fail();
        }
    });
    condition.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
    Assert.assertEquals(messages, responses);
    messageBus.stop();
}
Also used : ErrorEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.error.ErrorEventMessage) KeyElements(io.adminshell.aas.v3.model.KeyElements) ChangeEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ChangeEventMessage) ValueChangeEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ValueChangeEventMessage) BeforeClass(org.junit.BeforeClass) Reference(io.adminshell.aas.v3.model.Reference) Set(java.util.Set) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) TimeUnit(java.util.concurrent.TimeUnit) EventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.EventMessage) CountDownLatch(java.util.concurrent.CountDownLatch) SubscriptionInfo(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.SubscriptionInfo) Map(java.util.Map) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) ErrorLevel(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.error.ErrorLevel) KeyType(io.adminshell.aas.v3.model.KeyType) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) IntValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.IntValue) Assert(org.junit.Assert) DefaultKey(io.adminshell.aas.v3.model.impl.DefaultKey) Collections(java.util.Collections) Set(java.util.Set) HashSet(java.util.HashSet) ErrorEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.error.ErrorEventMessage) ChangeEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ChangeEventMessage) ValueChangeEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ValueChangeEventMessage) EventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.EventMessage) BeforeClass(org.junit.BeforeClass) ErrorEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.error.ErrorEventMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ChangeEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ChangeEventMessage) ValueChangeEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ValueChangeEventMessage) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

EventMessage (de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.EventMessage)7 SubscriptionInfo (de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.SubscriptionInfo)6 ValueChangeEventMessage (de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ValueChangeEventMessage)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Test (org.junit.Test)5 ChangeEventMessage (de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ChangeEventMessage)4 ErrorEventMessage (de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.error.ErrorEventMessage)4 PropertyValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue)4 Assert (org.junit.Assert)4 ErrorLevel (de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.error.ErrorLevel)3 IntValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.IntValue)3 KeyElements (io.adminshell.aas.v3.model.KeyElements)3 KeyType (io.adminshell.aas.v3.model.KeyType)3 Reference (io.adminshell.aas.v3.model.Reference)3 DefaultKey (io.adminshell.aas.v3.model.impl.DefaultKey)3 DefaultReference (io.adminshell.aas.v3.model.impl.DefaultReference)3 Collections (java.util.Collections)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3