Update project version for Chlorine release train
[transportpce.git] / test-common / src / main / java / org / opendaylight / transportpce / test / converter / AbstractDataObjectConverter.java
1 /*
2  * Copyright © 2016 AT&T 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.transportpce.test.converter;
9
10 import java.util.Map;
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;
28
29 /**
30  * Converts XML and {@link DataObject} vice versa.
31  *
32  */
33 public abstract class AbstractDataObjectConverter implements DataObjectConverter {
34
35     private static final Logger LOG = LoggerFactory.getLogger(AbstractDataObjectConverter.class);
36
37     private final EffectiveModelContext schemaContext;
38     private final BindingNormalizedNodeSerializer codecRegistry;
39
40     /**
41      * This is the default constructor, which should be used.
42      *
43      * @param schemaContext schema context for converter
44      * @param codecRegistry codec registry used for converting
45      *
46      */
47     protected AbstractDataObjectConverter(EffectiveModelContext schemaContext,
48             BindingNormalizedNodeSerializer codecRegistry) {
49         this.schemaContext = schemaContext;
50         this.codecRegistry = codecRegistry;
51     }
52
53     public EffectiveModelContext getSchemaContext() {
54         return schemaContext;
55     }
56
57     public BindingNormalizedNodeSerializer getCodecRegistry() {
58         return codecRegistry;
59     }
60
61     /**
62      * Transforms the given input {@link NormalizedNode} into the given
63      * {@link DataObject}.
64      *
65      * @param normalizedNode normalized node you want to convert
66      * @param rootNode {@link QName} of converted normalized node root
67      *
68      *     <p>
69      *     The input object should be {@link ContainerNode}
70      *     </p>
71      */
72     @Override
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));
85             }
86             normalizedNode = directChild.get();
87         }
88         YangInstanceIdentifier rootNodeYangInstanceIdentifier = YangInstanceIdentifier.of(rootNode);
89
90         Map.Entry<?, ?> bindingNodeEntry =
91                 codecRegistry.fromNormalizedNode(rootNodeYangInstanceIdentifier, normalizedNode);
92         if (bindingNodeEntry == null) {
93             return Optional.empty();
94         }
95         return Optional.ofNullable((T) bindingNodeEntry.getValue());
96     }
97
98     @Override
99     @SuppressWarnings("unchecked")
100     public <T extends DataObject> Optional<T> getDataObjectFromRpc(@Nonnull NormalizedNode normalizedNode) {
101
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();
106         }
107         T rpcDataObject = (T) codecRegistry.fromNormalizedNodeRpcData(
108                 Absolute.of(QName.create(schemaContext.getClass().getSimpleName())),
109                 (ContainerNode) normalizedNode);
110         return Optional.ofNullable(rpcDataObject);
111     }
112
113     @Override
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());
119     }
120
121     @Override
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);
127 //        };
128         return null;
129     }
130
131     @Override
132     public <T extends DataContainer> ConvertType<T> rpcData() {
133         return (object, objectClass) -> {
134             ContainerNode normalizedNodeRpcData = getCodecRegistry().toNormalizedNodeRpcData(object);
135             return Optional.ofNullable(normalizedNodeRpcData);
136         };
137     }
138
139     @Override
140     public <T extends Notification> ConvertType<T> notification() {
141         return (object, objectClass) -> {
142             ContainerNode normalizedNodeNotification = getCodecRegistry().toNormalizedNodeNotification(object);
143             return Optional.ofNullable(normalizedNodeNotification);
144         };
145     }
146 }