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);
}
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;
}
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();
}
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));
}
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));
}
Aggregations