|
| 1 | +/* |
| 2 | + * Copyright 2021-2024 Aklivity Inc |
| 3 | + * |
| 4 | + * Licensed under the Aklivity Community License (the "License"); you may not use |
| 5 | + * this file except in compliance with the License. You may obtain a copy of the |
| 6 | + * License at |
| 7 | + * |
| 8 | + * https://www.aklivity.io/aklivity-community-license/ |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OF ANY KIND, either express or implied. See the License for the |
| 13 | + * specific language governing permissions and limitations under the License. |
| 14 | + */ |
| 15 | +package io.aklivity.zilla.runtime.binding.openapi.internal.event; |
| 16 | + |
| 17 | +import static io.aklivity.zilla.runtime.binding.openapi.internal.types.event.OpenapiEventType.UNRESOLVED_REF; |
| 18 | + |
| 19 | +import java.nio.ByteBuffer; |
| 20 | +import java.time.Clock; |
| 21 | + |
| 22 | +import org.agrona.concurrent.AtomicBuffer; |
| 23 | +import org.agrona.concurrent.UnsafeBuffer; |
| 24 | + |
| 25 | +import io.aklivity.zilla.runtime.binding.openapi.internal.OpenapiBinding; |
| 26 | +import io.aklivity.zilla.runtime.binding.openapi.internal.types.event.EventFW; |
| 27 | +import io.aklivity.zilla.runtime.binding.openapi.internal.types.event.OpenapiEventExFW; |
| 28 | +import io.aklivity.zilla.runtime.engine.EngineContext; |
| 29 | +import io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer; |
| 30 | + |
| 31 | +public class OpenapiEventContext |
| 32 | +{ |
| 33 | + private static final int EVENT_BUFFER_CAPACITY = 1024; |
| 34 | + |
| 35 | + private final AtomicBuffer eventBuffer = new UnsafeBuffer(ByteBuffer.allocate(EVENT_BUFFER_CAPACITY)); |
| 36 | + private final AtomicBuffer extensionBuffer = new UnsafeBuffer(ByteBuffer.allocate(EVENT_BUFFER_CAPACITY)); |
| 37 | + private final EventFW.Builder eventRW = new EventFW.Builder(); |
| 38 | + private final OpenapiEventExFW.Builder openapiEventExRW = new OpenapiEventExFW.Builder(); |
| 39 | + private final int openapiTypeId; |
| 40 | + private final int unresolvedRef; |
| 41 | + private final MessageConsumer eventWriter; |
| 42 | + private final Clock clock; |
| 43 | + |
| 44 | + public OpenapiEventContext( |
| 45 | + EngineContext context) |
| 46 | + { |
| 47 | + this.openapiTypeId = context.supplyTypeId(OpenapiBinding.NAME); |
| 48 | + this.unresolvedRef = context.supplyEventId("binding.openapi.unresolved.ref"); |
| 49 | + this.eventWriter = context.supplyEventWriter(); |
| 50 | + this.clock = context.clock(); |
| 51 | + } |
| 52 | + |
| 53 | + public void unresolvedRef( |
| 54 | + long bindingId, |
| 55 | + String ref) |
| 56 | + { |
| 57 | + OpenapiEventExFW extension = openapiEventExRW |
| 58 | + .wrap(extensionBuffer, 0, extensionBuffer.capacity()) |
| 59 | + .unresolvedRef(e -> e |
| 60 | + .typeId(UNRESOLVED_REF.value()) |
| 61 | + .ref(ref) |
| 62 | + ) |
| 63 | + .build(); |
| 64 | + EventFW event = eventRW |
| 65 | + .wrap(eventBuffer, 0, eventBuffer.capacity()) |
| 66 | + .id(unresolvedRef) |
| 67 | + .timestamp(clock.millis()) |
| 68 | + .traceId(0L) |
| 69 | + .namespacedId(bindingId) |
| 70 | + .extension(extension.buffer(), extension.offset(), extension.limit()) |
| 71 | + .build(); |
| 72 | + eventWriter.accept(openapiTypeId, event.buffer(), event.offset(), event.limit()); |
| 73 | + } |
| 74 | +} |
0 commit comments