Add Honeynode emulator for device221
[transportpce.git] / tests / honeynode / 2.2.1 / honeynode-common / src / main / java / io / fd / honeycomb / transportpce / binding / converter / AbstractDataObjectConverter.java
1 /*
2  * Copyright (c) 2018 AT&T and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.fd.honeycomb.transportpce.binding.converter;
17
18 import com.google.common.base.Preconditions;
19
20 import io.fd.honeycomb.transportpce.binding.converter.api.DataObjectConverter;
21
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import java.util.Optional;
25
26 import javax.annotation.Nonnull;
27
28 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
29 import org.opendaylight.yangtools.yang.binding.DataContainer;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.binding.Notification;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Converts XML and {@link DataObject} vice versa.
45  *
46  */
47 public abstract class AbstractDataObjectConverter implements DataObjectConverter {
48
49     private static final Logger LOG = LoggerFactory.getLogger(AbstractDataObjectConverter.class);
50
51     private final SchemaContext schemaContext;
52     private final BindingNormalizedNodeSerializer codecRegistry;
53
54     /**
55      * This is the default constructor, which should be used.
56      *
57      * @param schemaContext schema context for converter
58      * @param codecRegistry codec registry used for converting
59      *
60      */
61     protected AbstractDataObjectConverter(SchemaContext schemaContext, BindingNormalizedNodeSerializer codecRegistry) {
62         this.schemaContext = schemaContext;
63         this.codecRegistry = codecRegistry;
64     }
65
66     public SchemaContext getSchemaContext() {
67         return schemaContext;
68     }
69
70     public BindingNormalizedNodeSerializer getCodecRegistry() {
71         return codecRegistry;
72     }
73
74     /**
75      * Transforms the given input {@link NormalizedNode} into the given
76      * {@link DataObject}.
77      *
78      * @param normalizedNode normalized node you want to convert
79      * @param rootNode {@link QName} of converted normalized node root
80      *
81      * <p>
82      * The input object should be {@link ContainerNode}
83      * </p>
84      */
85     @Override
86     @SuppressWarnings("unchecked")
87     public <T extends DataObject> Optional<T> getDataObject(
88             @Nonnull NormalizedNode<?, ?> normalizedNode,
89             @Nonnull QName rootNode) {
90         Preconditions.checkNotNull(normalizedNode);
91         if (normalizedNode instanceof ContainerNode) {
92             YangInstanceIdentifier.PathArgument directChildIdentifier =
93                     YangInstanceIdentifier.of(rootNode).getLastPathArgument();
94             Optional<NormalizedNode<?, ?>> directChild =
95                     NormalizedNodes.getDirectChild(normalizedNode, directChildIdentifier).toJavaUtil();
96             if (!directChild.isPresent()) {
97                 throw new IllegalStateException(String.format("Could not get the direct child of %s", rootNode));
98             }
99             normalizedNode = directChild.get();
100         }
101         YangInstanceIdentifier rootNodeYangInstanceIdentifier = YangInstanceIdentifier.of(rootNode);
102
103         Map.Entry<?, ?> bindingNodeEntry =
104                 codecRegistry.fromNormalizedNode(rootNodeYangInstanceIdentifier, normalizedNode);
105         if (bindingNodeEntry == null) {
106             return Optional.empty();
107         }
108         return Optional.ofNullable((T) bindingNodeEntry.getValue());
109     }
110
111     @Override
112     @SuppressWarnings("unchecked")
113     public <T extends DataObject> Optional<T> getDataObjectFromRpc(
114             @Nonnull NormalizedNode<?, ?> normalizedNode,
115             @Nonnull SchemaPath rpcSchemaPath) {
116
117         if (! (normalizedNode instanceof ContainerNode)) {
118             LOG.error("converting normalized node is not ContainerNode. It's actual type is {}",
119                     normalizedNode.getClass().getSimpleName());
120             return Optional.empty();
121         }
122         T rpcDataObject = (T) codecRegistry.fromNormalizedNodeRpcData(rpcSchemaPath, (ContainerNode) normalizedNode);
123         return Optional.ofNullable(rpcDataObject);
124     }
125
126     @Override
127     public <T extends DataObject> Optional<NormalizedNode<?, ?>> toNormalizedNodes(@Nonnull T object,
128             Class<T> dataObjectClass) {
129         Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalizedNode =
130                 codecRegistry.toNormalizedNode(InstanceIdentifier.create(dataObjectClass), object);
131         return Optional.ofNullable(normalizedNode.getValue());
132     }
133
134     @Override
135     public <T extends DataObject> ConvertType<T> dataContainer() {
136         return (object, objectClass) -> {
137             NormalizedNode<?, ?> value =
138                     getCodecRegistry().toNormalizedNode(InstanceIdentifier.create(objectClass), object).getValue();
139             return Optional.ofNullable(value);
140         };
141     }
142
143     @Override
144     public <T extends DataContainer> ConvertType<T> rpcData() {
145         return (object, objectClass) -> {
146             ContainerNode normalizedNodeRpcData = getCodecRegistry().toNormalizedNodeRpcData(object);
147             return Optional.ofNullable(normalizedNodeRpcData);
148         };
149     }
150
151     @Override
152     public <T extends Notification> ConvertType<T> notification() {
153         return (object, objectClass) -> {
154             ContainerNode normalizedNodeNotification = getCodecRegistry().toNormalizedNodeNotification(object);
155             return Optional.ofNullable(normalizedNodeNotification);
156         };
157     }
158 }