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