2 * Copyright (c) 2014 Cisco Systems, Inc. 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.mdsal.binding.dom.codec.gen.impl;
10 import org.opendaylight.mdsal.binding.dom.codec.util.AugmentableDispatchSerializer;
11 import org.opendaylight.mdsal.binding.dom.codec.util.ChoiceDispatchSerializer;
12 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
13 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
14 import org.opendaylight.yangtools.yang.binding.BindingStreamEventWriter;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
16 import org.opendaylight.yangtools.yang.binding.DataObjectSerializerImplementation;
17 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
24 * Concrete implementation of {@link AbstractStreamWriterGenerator} which in runtime generates classes implementing
25 * {@link DataObjectSerializerImplementation} interface and are used to serialize Binding {@link DataObject}.
28 * Actual implementation of codecs is done via static methods, which allows for static wiring of codecs. Choice codec
29 * and Augmentable codecs are static properties of parent codec and stateless implementations are used
30 * ({@link ChoiceDispatchSerializer}, {@link AugmentableDispatchSerializer}), which uses registry to dispatch to
31 * concrete item codec.
33 public final class StreamWriterGenerator extends AbstractStreamWriterGenerator {
34 private static final String UNKNOWN_SIZE = BindingStreamEventWriter.class.getName() + ".UNKNOWN_SIZE";
36 private StreamWriterGenerator(final JavassistUtils utils) {
41 * Create a new instance backed by a specific {@link JavassistUtils} instance.
43 * @param utils JavassistUtils instance to use
44 * @return A new generator
46 public static DataObjectSerializerGenerator create(final JavassistUtils utils) {
47 return new StreamWriterGenerator(utils);
51 protected DataObjectSerializerSource generateContainerSerializer(final GeneratedType type,
52 final ContainerSchemaNode node) {
53 return new AugmentableDataNodeContainerEmitterSource(this, type, node) {
55 public CharSequence emitStartEvent() {
56 return startContainerNode(classReference(type), UNKNOWN_SIZE);
62 protected DataObjectSerializerSource generateNotificationSerializer(final GeneratedType type,
63 final NotificationDefinition node) {
64 return new AugmentableDataNodeContainerEmitterSource(this, type, node) {
66 public CharSequence emitStartEvent() {
67 return startContainerNode(classReference(type), UNKNOWN_SIZE);
73 protected DataObjectSerializerSource generateCaseSerializer(final GeneratedType type, final CaseSchemaNode node) {
74 return new AugmentableDataNodeContainerEmitterSource(this, type, node) {
76 public CharSequence emitStartEvent() {
77 return startCaseNode(classReference(type),UNKNOWN_SIZE);
83 protected DataObjectSerializerSource generateUnkeyedListEntrySerializer(final GeneratedType type,
84 final ListSchemaNode node) {
85 return new AugmentableDataNodeContainerEmitterSource(this, type, node) {
87 public CharSequence emitStartEvent() {
88 return startUnkeyedListItem(UNKNOWN_SIZE);
94 protected DataObjectSerializerSource generateSerializer(final GeneratedType type,
95 final AugmentationSchemaNode schema) {
96 return new DataNodeContainerSerializerSource(this, type, schema) {
98 public CharSequence emitStartEvent() {
99 return startAugmentationNode(classReference(type));
105 protected DataObjectSerializerSource generateMapEntrySerializer(final GeneratedType type,
106 final ListSchemaNode node) {
107 return new AugmentableDataNodeContainerEmitterSource(this, type, node) {
109 public CharSequence emitStartEvent() {
110 return startMapEntryNode(invoke(INPUT, "getKey"), UNKNOWN_SIZE);