Eliminate NormalizedNodePayload.headers
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / AbstractNormalizedNodeBodyWriter.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers;
9
10 import static java.util.Objects.requireNonNull;
11
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;
27
28 abstract class AbstractNormalizedNodeBodyWriter implements MessageBodyWriter<NormalizedNodePayload> {
29     @Override
30     public final boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
31             final MediaType mediaType) {
32         return type.equals(NormalizedNodePayload.class);
33     }
34
35     @Override
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();
40         if (data == null) {
41             return;
42         }
43
44         final var output = requireNonNull(entityStream);
45         final var stack = context.inference().toSchemaInferenceStack();
46         // FIXME: this dispatch is here to handle codec transition to 'output', but that should be completely okay with
47         //        the instantiation path we are using (based in Inference).
48         if (!stack.isEmpty()) {
49             final var stmt = stack.currentStatement();
50             if (stmt instanceof RpcEffectiveStatement rpc) {
51                 stack.enterSchemaTree(rpc.output().argument());
52                 writeOperationOutput(stack, context.getWriterParameters(), (ContainerNode) data, output);
53             } else if (stmt instanceof ActionEffectiveStatement action) {
54                 stack.enterSchemaTree(action.output().argument());
55                 writeOperationOutput(stack, context.getWriterParameters(), (ContainerNode) data, output);
56             }
57         }
58         writeData(stack, context.getWriterParameters(), data, output);
59     }
60
61     abstract void writeOperationOutput(@NonNull SchemaInferenceStack stack, @NonNull QueryParameters writerParameters,
62         @NonNull ContainerNode output, @NonNull OutputStream entityStream) throws IOException;
63
64     abstract void writeData(@NonNull SchemaInferenceStack stack, @NonNull QueryParameters writerParameters,
65         @NonNull NormalizedNode data, @NonNull OutputStream entityStream) throws IOException;
66 }