Refactor NormalizedNodePayload
[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 output = requireNonNull(entityStream);
40         final var stack = context.inference().toSchemaInferenceStack();
41         // FIXME: this dispatch is here to handle codec transition to 'output', but that should be completely okay with
42         //        the instantiation path we are using (based in Inference).
43         if (!stack.isEmpty()) {
44             final var stmt = stack.currentStatement();
45             if (stmt instanceof RpcEffectiveStatement rpc) {
46                 stack.enterSchemaTree(rpc.output().argument());
47                 writeOperationOutput(stack, context.writerParameters(), (ContainerNode) context.data(), output);
48             } else if (stmt instanceof ActionEffectiveStatement action) {
49                 stack.enterSchemaTree(action.output().argument());
50                 writeOperationOutput(stack, context.writerParameters(), (ContainerNode) context.data(), output);
51             }
52         }
53         writeData(stack, context.writerParameters(), context.data(), output);
54     }
55
56     abstract void writeOperationOutput(@NonNull SchemaInferenceStack stack, @NonNull QueryParameters writerParameters,
57         @NonNull ContainerNode output, @NonNull OutputStream entityStream) throws IOException;
58
59     abstract void writeData(@NonNull SchemaInferenceStack stack, @NonNull QueryParameters writerParameters,
60         @NonNull NormalizedNode data, @NonNull OutputStream entityStream) throws IOException;
61 }