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.common.converter;
10 //import com.google.common.base.Preconditions;
12 import java.util.Map.Entry;
13 import java.util.Optional;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
16 import org.opendaylight.transportpce.common.converter.api.DataObjectConverter;
17 import org.opendaylight.yangtools.yang.binding.DataContainer;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.opendaylight.yangtools.yang.binding.Notification;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Converts XML and {@link DataObject} vice versa.
35 public abstract class AbstractDataObjectConverter implements DataObjectConverter {
37 private static final Logger LOG = LoggerFactory.getLogger(AbstractDataObjectConverter.class);
39 private final EffectiveModelContext schemaContext;
40 private final BindingNormalizedNodeSerializer codecRegistry;
43 * This is the default constructor, which should be used.
45 * @param schemaContext schema context for converter
46 * @param codecRegistry codec registry used for converting
49 protected AbstractDataObjectConverter(EffectiveModelContext schemaContext,
50 BindingNormalizedNodeSerializer codecRegistry) {
51 this.schemaContext = schemaContext;
52 this.codecRegistry = codecRegistry;
55 public EffectiveModelContext getSchemaContext() {
59 public BindingNormalizedNodeSerializer getCodecRegistry() {
64 * Transforms the given input {@link NormalizedNode} into the given
67 * @param normalizedNode normalized node you want to convert
68 * @param rootNode {@link QName} of converted normalized node root
71 * The input object should be {@link ContainerNode}
75 @SuppressWarnings("unchecked")
76 public <T extends DataObject> Optional<T> getDataObject(
77 @Nonnull NormalizedNode<?, ?> normalizedNode,
78 @Nonnull QName rootNode) {
79 //Preconditions.checkNotNull(normalizedNode);
80 if (normalizedNode instanceof ContainerNode) {
81 YangInstanceIdentifier.PathArgument directChildIdentifier =
82 YangInstanceIdentifier.of(rootNode).getLastPathArgument();
83 Optional<NormalizedNode<?, ?>> directChild =
84 NormalizedNodes.getDirectChild(normalizedNode, directChildIdentifier);
85 if (!directChild.isPresent()) {
86 throw new IllegalStateException(String.format("Could not get the direct child of %s", rootNode));
88 normalizedNode = directChild.get();
90 YangInstanceIdentifier rootNodeYangInstanceIdentifier = YangInstanceIdentifier.of(rootNode);
92 Map.Entry<?, ?> bindingNodeEntry =
93 codecRegistry.fromNormalizedNode(rootNodeYangInstanceIdentifier, normalizedNode);
94 if (bindingNodeEntry == null) {
95 return Optional.empty();
97 return Optional.ofNullable((T) bindingNodeEntry.getValue());
101 @SuppressWarnings("unchecked")
102 public <T extends DataObject> Optional<T> getDataObjectFromRpc(
103 @Nonnull NormalizedNode<?, ?> normalizedNode,
104 @Nonnull SchemaPath rpcSchemaPath) {
106 if (! (normalizedNode instanceof ContainerNode)) {
107 LOG.error("converting normalized node is not ContainerNode. It's actual type is {}",
108 normalizedNode.getClass().getSimpleName());
109 return Optional.empty();
111 T rpcDataObject = (T) codecRegistry.fromNormalizedNodeRpcData(rpcSchemaPath, (ContainerNode) normalizedNode);
112 return Optional.ofNullable(rpcDataObject);
116 public <T extends DataObject> Optional<NormalizedNode<?, ?>> toNormalizedNodes(@Nonnull T object,
117 Class<T> dataObjectClass) {
118 Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalizedNode =
119 codecRegistry.toNormalizedNode(InstanceIdentifier.create(dataObjectClass), object);
120 return Optional.ofNullable(normalizedNode.getValue());
124 public <T extends DataObject> ConvertType<T> dataContainer() {
125 return (object, objectClass) -> {
126 NormalizedNode<?, ?> value =
127 getCodecRegistry().toNormalizedNode(InstanceIdentifier.create(objectClass), object).getValue();
128 return Optional.ofNullable(value);
133 public <T extends DataContainer> ConvertType<T> rpcData() {
134 return (object, objectClass) -> {
135 ContainerNode normalizedNodeRpcData = getCodecRegistry().toNormalizedNodeRpcData(object);
136 return Optional.ofNullable(normalizedNodeRpcData);
141 public <T extends Notification> ConvertType<T> notification() {
142 return (object, objectClass) -> {
143 ContainerNode normalizedNodeNotification = getCodecRegistry().toNormalizedNodeNotification(object);
144 return Optional.ofNullable(normalizedNodeNotification);