Search in sources :

Example 16 with FileDescriptor

use of org.infinispan.protostream.descriptors.FileDescriptor in project kogito-runtimes by kiegroup.

the class ProtostreamProtobufAdapterTypeProviderTest method testFileDescriptorSortWithKogitoFirstInOrigList.

@Test
void testFileDescriptorSortWithKogitoFirstInOrigList() {
    ProtostreamProtobufAdapterTypeProvider prov = new ProtostreamProtobufAdapterTypeProvider();
    FileDescriptor fd1 = new FileDescriptor.Builder().withPackageName("kogito").build();
    FileDescriptor fd2 = new FileDescriptor.Builder().withPackageName("org.kie.kogito.app").build();
    List<FileDescriptor> fdCollectionKogitoOrder1 = Arrays.asList(fd1, fd2);
    List<FileDescriptor> fdCollectionSorted = prov.sortFds(fdCollectionKogitoOrder1);
    Assertions.assertThat(fdCollectionSorted).hasSameElementsAs(fdCollectionKogitoOrder1);
    Assertions.assertThat(fdCollectionSorted.get(0)).isEqualTo(fd1);
}
Also used : FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Test(org.junit.jupiter.api.Test)

Example 17 with FileDescriptor

use of org.infinispan.protostream.descriptors.FileDescriptor in project kogito-apps by kiegroup.

the class ProtoIndexParser method createEntityIndexDescriptors.

static Map<String, EntityIndexDescriptor> createEntityIndexDescriptors(FileDescriptor desc, Map<String, EntityIndexDescriptor> entityIndexes) {
    desc.getMessageTypes().forEach(mDesc -> {
        String typeName = mDesc.getFullName();
        EntityIndexDescriptor entityIndex = entityIndexes.get(typeName);
        if (entityIndex != null) {
            // Add the fields without index
            Set<String> fieldNames = entityIndex.getAttributeDescriptors().stream().map(AttributeDescriptor::getName).collect(toSet());
            mDesc.getFields().stream().filter(fDesc -> !fieldNames.contains(fDesc.getName())).forEach(fDesc -> entityIndex.getAttributeDescriptors().add(createAttributeDescriptor(fDesc, null)));
        }
    });
    return entityIndexes;
}
Also used : FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) AnnotationMetadataCreator(org.infinispan.protostream.AnnotationMetadataCreator) Set(java.util.Set) Descriptor(org.infinispan.protostream.descriptors.Descriptor) AttributeDescriptor(org.kie.kogito.persistence.api.schema.AttributeDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) Map(java.util.Map) Optional(java.util.Optional) Configuration(org.infinispan.protostream.config.Configuration) IndexDescriptor(org.kie.kogito.persistence.api.schema.IndexDescriptor) AnnotationElement(org.infinispan.protostream.descriptors.AnnotationElement) Collectors.toSet(java.util.stream.Collectors.toSet) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor)

Example 18 with FileDescriptor

use of org.infinispan.protostream.descriptors.FileDescriptor in project kogito-apps by kiegroup.

the class ProtobufService method onStart.

void onStart(@Observes StartupEvent ev) {
    kogitoDescriptors.getFileDescriptors().forEach((name, bytes) -> {
        LOGGER.info("Registering Kogito ProtoBuffer file: {}", name);
        String content = new String(bytes);
        try {
            SerializationContext ctx = createSerializationContext(FileDescriptorSource.fromString(name, content));
            FileDescriptor desc = ctx.getFileDescriptors().get(name);
            Map<String, EntityIndexDescriptor> entityIndexes = desc.getMessageTypes().stream().map(t -> t.<EntityIndexDescriptor>getProcessedAnnotation(INDEXED_ANNOTATION)).filter(Objects::nonNull).collect(toMap(EntityIndexDescriptor::getName, Function.identity()));
            Map<String, EntityIndexDescriptor> entityIndexDescriptors = createEntityIndexDescriptors(desc, entityIndexes);
            schemaEvent.fire(new SchemaRegisteredEvent(new SchemaDescriptor(name, content, entityIndexDescriptors, null), SCHEMA_TYPE));
        } catch (ProtobufValidationException e) {
            throw new ProtobufFileRegistrationException(e);
        }
    });
    protobufMonitorService.startMonitoring();
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) SchemaDescriptor(org.kie.kogito.persistence.api.schema.SchemaDescriptor) SchemaRegisteredEvent(org.kie.kogito.persistence.api.schema.SchemaRegisteredEvent) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor)

Example 19 with FileDescriptor

use of org.infinispan.protostream.descriptors.FileDescriptor in project kogito-apps by kiegroup.

the class ProtobufService method registerProtoBufferType.

public void registerProtoBufferType(String content) throws ProtobufValidationException {
    LOGGER.debug("Registering new ProtoBuffer file with content: \n{}", content);
    content = content.replaceAll("kogito.Date", "string");
    SerializationContext ctx = createSerializationContext(kogitoDescriptors, FileDescriptorSource.fromString(DOMAIN_MODEL_PROTO_NAME, content));
    FileDescriptor desc = ctx.getFileDescriptors().get(DOMAIN_MODEL_PROTO_NAME);
    Option processIdOption = desc.getOption("kogito_id");
    if (processIdOption == null || processIdOption.getValue() == null) {
        throw new ProtobufValidationException("Missing marker for process id in proto file, please add option kogito_id=\"processid\"");
    }
    String processId = (String) processIdOption.getValue();
    Option model = desc.getOption("kogito_model");
    if (model == null || model.getValue() == null) {
        throw new ProtobufValidationException("Missing marker for main message type in proto file, please add option kogito_model=\"messagename\"");
    }
    String messageName = (String) model.getValue();
    String fullTypeName = desc.getPackage() == null ? messageName : desc.getPackage() + "." + messageName;
    Descriptor descriptor;
    try {
        descriptor = ctx.getMessageDescriptor(fullTypeName);
    } catch (IllegalArgumentException ex) {
        throw new ProtobufValidationException(format("Could not find message with name: %s in proto file, e, please review option kogito_model", fullTypeName));
    }
    validateDescriptorField(messageName, descriptor, Constants.KOGITO_DOMAIN_ATTRIBUTE);
    Map<String, EntityIndexDescriptor> entityIndexes = desc.getMessageTypes().stream().map(t -> t.<EntityIndexDescriptor>getProcessedAnnotation(INDEXED_ANNOTATION)).filter(Objects::nonNull).collect(toMap(EntityIndexDescriptor::getName, Function.identity()));
    Map<String, EntityIndexDescriptor> entityIndexedDescriptors = createEntityIndexDescriptors(desc, entityIndexes);
    try {
        schemaEvent.fire(new SchemaRegisteredEvent(new SchemaDescriptor(processId + ".proto", content, entityIndexedDescriptors, new ProcessDescriptor(processId, fullTypeName)), SCHEMA_TYPE));
    } catch (RuntimeException ex) {
        throw new ProtobufValidationException(ex.getMessage(), ex);
    }
    domainModelEvent.fire(new FileDescriptorRegisteredEvent(desc));
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) SchemaDescriptor(org.kie.kogito.persistence.api.schema.SchemaDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) SchemaRegisteredEvent(org.kie.kogito.persistence.api.schema.SchemaRegisteredEvent) Descriptor(org.infinispan.protostream.descriptors.Descriptor) SchemaDescriptor(org.kie.kogito.persistence.api.schema.SchemaDescriptor) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) ProcessDescriptor(org.kie.kogito.persistence.api.schema.ProcessDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Option(org.infinispan.protostream.descriptors.Option) ProcessDescriptor(org.kie.kogito.persistence.api.schema.ProcessDescriptor) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor)

Example 20 with FileDescriptor

use of org.infinispan.protostream.descriptors.FileDescriptor in project kogito-apps by kiegroup.

the class ProtoDomainModelProducer method onFileDescriptorRegistered.

public void onFileDescriptorRegistered(@Observes FileDescriptorRegisteredEvent event) {
    FileDescriptor descriptor = event.getDescriptor();
    String rootMessage = (String) descriptor.getOption("kogito_model").getValue();
    String processId = (String) descriptor.getOption("kogito_id").getValue();
    Map<String, Descriptor> map = descriptor.getMessageTypes().stream().collect(toMap(AnnotatedDescriptorImpl::getName, desc -> desc));
    Descriptor rootDescriptor = map.remove(rootMessage);
    DomainDescriptor domain = new DomainDescriptorMapper().apply(rootDescriptor);
    List<DomainDescriptor> additionalTypes = map.values().stream().map(desc -> new DomainDescriptorMapper().apply(desc)).collect(toList());
    domainEvent.fire(new DomainModelRegisteredEvent(processId, domain, additionalTypes));
}
Also used : Event(javax.enterprise.event.Event) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Descriptor(org.infinispan.protostream.descriptors.Descriptor) Function(java.util.function.Function) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Inject(javax.inject.Inject) AnnotatedDescriptorImpl(org.infinispan.protostream.impl.AnnotatedDescriptorImpl) AttributeDescriptor(org.kie.kogito.persistence.api.proto.AttributeDescriptor) FileDescriptorRegisteredEvent(org.kie.kogito.persistence.protobuf.FileDescriptorRegisteredEvent) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Collectors.toMap(java.util.stream.Collectors.toMap) DomainDescriptor(org.kie.kogito.persistence.api.proto.DomainDescriptor) Map(java.util.Map) Observes(javax.enterprise.event.Observes) DomainModelRegisteredEvent(org.kie.kogito.persistence.api.proto.DomainModelRegisteredEvent) ApplicationScoped(javax.enterprise.context.ApplicationScoped) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) AttributeDescriptor(org.kie.kogito.persistence.api.proto.AttributeDescriptor) DomainDescriptor(org.kie.kogito.persistence.api.proto.DomainDescriptor) DomainModelRegisteredEvent(org.kie.kogito.persistence.api.proto.DomainModelRegisteredEvent) DomainDescriptor(org.kie.kogito.persistence.api.proto.DomainDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor)

Aggregations

FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)43 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)24 Test (org.junit.Test)23 Descriptor (org.infinispan.protostream.descriptors.Descriptor)12 Configuration (org.infinispan.protostream.config.Configuration)11 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)11 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)11 List (java.util.List)8 SerializationContext (org.infinispan.protostream.SerializationContext)8 AnnotationElement (org.infinispan.protostream.descriptors.AnnotationElement)8 GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 DescriptorParserException (org.infinispan.protostream.DescriptorParserException)5 ExtendDescriptor (org.infinispan.protostream.descriptors.ExtendDescriptor)5 EntityIndexDescriptor (org.kie.kogito.persistence.api.schema.EntityIndexDescriptor)4 HashMap (java.util.HashMap)3 ResolutionContext (org.infinispan.protostream.descriptors.ResolutionContext)3 Ignore (org.junit.Ignore)3 Test (org.junit.jupiter.api.Test)3