2 * Copyright (c) 2019 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.yangtools.yang.data.util;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.annotations.Beta;
15 import java.io.IOException;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadataStreamWriter;
18 import org.opendaylight.yangtools.yang.data.api.schema.stream.AnydataExtension;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
20 import org.opendaylight.yangtools.yang.model.api.AnyDataSchemaNode;
23 public class AnydataNodeDataWithSchema extends SimpleNodeDataWithSchema<AnyDataSchemaNode> {
24 private Class<?> objectModel;
26 public AnydataNodeDataWithSchema(final AnyDataSchemaNode dataSchemaNode) {
27 super(dataSchemaNode);
30 public AnydataNodeDataWithSchema(final AnyDataSchemaNode dataSchemaNode, final Class<?> objectModel) {
31 super(dataSchemaNode);
32 this.objectModel = requireNonNull(objectModel);
36 public Object getValue() {
37 return getObjectModel().cast(super.getValue());
41 public void setValue(final Object value) {
42 final Class<?> clazz = getObjectModel();
43 checkArgument(clazz.isInstance(value), "Value %s is not compatible with %s", clazz);
44 super.setValue(value);
48 protected void write(final NormalizedNodeStreamWriter writer, final NormalizedMetadataStreamWriter metaWriter)
50 final AnydataExtension ext = writer.getExtensions().getInstance(AnydataExtension.class);
52 writer.nextDataSchemaNode(getSchema());
53 if (ext.startAnydataNode(provideNodeIdentifier(), getObjectModel())) {
54 writer.scalarValue(getValue());
60 public final @NonNull Class<?> getObjectModel() {
61 checkState(objectModel != null, "Object model not set");
65 public void setObjectModel(final Class<?> newObjectModel) {
66 checkState(objectModel == null, "Object model already set to %s", objectModel);
67 objectModel = requireNonNull(newObjectModel);