2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers;
10 import static java.util.Objects.requireNonNull;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Type;
16 import javax.ws.rs.core.MediaType;
17 import javax.ws.rs.core.MultivaluedMap;
18 import javax.ws.rs.ext.MessageBodyWriter;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
21 import org.opendaylight.restconf.nb.rfc8040.legacy.QueryParameters;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
28 abstract class AbstractNormalizedNodeBodyWriter implements MessageBodyWriter<NormalizedNodePayload> {
30 public final boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
31 final MediaType mediaType) {
32 return type.equals(NormalizedNodePayload.class);
36 public final void writeTo(final NormalizedNodePayload context, final Class<?> type, final Type genericType,
37 final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders,
38 final OutputStream entityStream) throws IOException {
39 final var data = context.getData();
43 if (httpHeaders != null) {
44 for (var entry : context.getNewHeaders().entrySet()) {
45 httpHeaders.add(entry.getKey(), entry.getValue());
49 final var output = requireNonNull(entityStream);
50 final var iidContext = context.getInstanceIdentifierContext();
51 final var stack = iidContext.inference().toSchemaInferenceStack();
52 // FIXME: this dispatch is here to handle codec transition to 'output', but that should be completely okay with
53 // the instantiation path we are using (based in Inference).
54 if (!stack.isEmpty()) {
55 final var stmt = stack.currentStatement();
56 if (stmt instanceof RpcEffectiveStatement rpc) {
57 stack.enterSchemaTree(rpc.output().argument());
58 writeOperationOutput(stack, context.getWriterParameters(), (ContainerNode) data, output);
59 } else if (stmt instanceof ActionEffectiveStatement action) {
60 stack.enterSchemaTree(action.output().argument());
61 writeOperationOutput(stack, context.getWriterParameters(), (ContainerNode) data, output);
64 writeData(stack, context.getWriterParameters(), data, output);
67 abstract void writeOperationOutput(@NonNull SchemaInferenceStack stack, @NonNull QueryParameters writerParameters,
68 @NonNull ContainerNode output, @NonNull OutputStream entityStream) throws IOException;
70 abstract void writeData(@NonNull SchemaInferenceStack stack, @NonNull QueryParameters writerParameters,
71 @NonNull NormalizedNode data, @NonNull OutputStream entityStream) throws IOException;