2 * Copyright © 2016 AT&T 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.transportpce.test.converter;
11 import java.util.Map.Entry;
12 import java.util.Optional;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
15 import org.opendaylight.yangtools.yang.binding.DataContainer;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.opendaylight.yangtools.yang.binding.Notification;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Converts XML and {@link DataObject} vice versa.
33 public abstract class AbstractDataObjectConverter implements DataObjectConverter {
35 private static final Logger LOG = LoggerFactory.getLogger(AbstractDataObjectConverter.class);
37 private final EffectiveModelContext schemaContext;
38 private final BindingNormalizedNodeSerializer codecRegistry;
41 * This is the default constructor, which should be used.
43 * @param schemaContext schema context for converter
44 * @param codecRegistry codec registry used for converting
47 protected AbstractDataObjectConverter(EffectiveModelContext schemaContext,
48 BindingNormalizedNodeSerializer codecRegistry) {
49 this.schemaContext = schemaContext;
50 this.codecRegistry = codecRegistry;
53 public EffectiveModelContext getSchemaContext() {
57 public BindingNormalizedNodeSerializer getCodecRegistry() {
62 * Transforms the given input {@link NormalizedNode} into the given
65 * @param normalizedNode normalized node you want to convert
66 * @param rootNode {@link QName} of converted normalized node root
69 * The input object should be {@link ContainerNode}
73 @SuppressWarnings("unchecked")
74 public <T extends DataObject> Optional<T> getDataObject(
75 @Nonnull NormalizedNode normalizedNode,
76 @Nonnull QName rootNode) {
77 //Preconditions.checkNotNull(normalizedNode);
78 if (normalizedNode instanceof ContainerNode) {
79 YangInstanceIdentifier.PathArgument directChildIdentifier =
80 YangInstanceIdentifier.of(rootNode).getLastPathArgument();
81 Optional<NormalizedNode> directChild =
82 NormalizedNodes.getDirectChild(normalizedNode, directChildIdentifier);
83 if (!directChild.isPresent()) {
84 throw new IllegalStateException(String.format("Could not get the direct child of %s", rootNode));
86 normalizedNode = directChild.get();
88 YangInstanceIdentifier rootNodeYangInstanceIdentifier = YangInstanceIdentifier.of(rootNode);
90 Map.Entry<?, ?> bindingNodeEntry =
91 codecRegistry.fromNormalizedNode(rootNodeYangInstanceIdentifier, normalizedNode);
92 if (bindingNodeEntry == null) {
93 return Optional.empty();
95 return Optional.ofNullable((T) bindingNodeEntry.getValue());
99 @SuppressWarnings("unchecked")
100 public <T extends DataObject> Optional<T> getDataObjectFromRpc(@Nonnull NormalizedNode normalizedNode) {
102 if (! (normalizedNode instanceof ContainerNode)) {
103 LOG.error("converting normalized node is not ContainerNode. It's actual type is {}",
104 normalizedNode.getClass().getSimpleName());
105 return Optional.empty();
107 T rpcDataObject = (T) codecRegistry.fromNormalizedNodeRpcData(
108 Absolute.of(QName.create(schemaContext.getClass().getSimpleName())),
109 (ContainerNode) normalizedNode);
110 return Optional.ofNullable(rpcDataObject);
114 public <T extends DataObject> Optional<NormalizedNode> toNormalizedNodes(@Nonnull T object,
115 Class dataObjectClass) {
116 Entry<YangInstanceIdentifier, NormalizedNode> normalizedNode =
117 codecRegistry.toNormalizedNode(InstanceIdentifier.create(dataObjectClass), object);
118 return Optional.ofNullable(normalizedNode.getValue());
122 public <T extends DataObject> ConvertType<T> dataContainer() {
123 // return (object, objectClass) -> {
124 // NormalizedNode value =
125 // getCodecRegistry().toNormalizedNode(InstanceIdentifier.create(objectClass), object).getValue();
126 // return Optional.ofNullable(value);
132 public <T extends DataContainer> ConvertType<T> rpcData() {
133 return (object, objectClass) -> {
134 ContainerNode normalizedNodeRpcData = getCodecRegistry().toNormalizedNodeRpcData(object);
135 return Optional.ofNullable(normalizedNodeRpcData);
140 public <T extends Notification> ConvertType<T> notification() {
141 return (object, objectClass) -> {
142 ContainerNode normalizedNodeNotification = getCodecRegistry().toNormalizedNodeNotification(object);
143 return Optional.ofNullable(normalizedNodeNotification);