2 * Copyright (c) 2018 AT&T and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.fd.honeycomb.transportpce.binding.converter;
18 import com.google.common.base.Preconditions;
19 import io.fd.honeycomb.transportpce.binding.converter.api.DataObjectConverter;
21 import java.util.Map.Entry;
22 import java.util.Optional;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
25 import org.opendaylight.yangtools.yang.binding.DataContainer;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.binding.Notification;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Converts XML and {@link DataObject} vice versa.
43 public abstract class AbstractDataObjectConverter implements DataObjectConverter {
45 private static final Logger LOG = LoggerFactory.getLogger(AbstractDataObjectConverter.class);
47 private final SchemaContext schemaContext;
48 private final BindingNormalizedNodeSerializer codecRegistry;
51 * This is the default constructor, which should be used.
53 * @param schemaContext schema context for converter
54 * @param codecRegistry codec registry used for converting
57 protected AbstractDataObjectConverter(SchemaContext schemaContext, BindingNormalizedNodeSerializer codecRegistry) {
58 this.schemaContext = schemaContext;
59 this.codecRegistry = codecRegistry;
62 public SchemaContext getSchemaContext() {
66 public BindingNormalizedNodeSerializer getCodecRegistry() {
71 * Transforms the given input {@link NormalizedNode} into the given
74 * @param normalizedNode normalized node you want to convert
75 * @param rootNode {@link QName} of converted normalized node root
78 * The input object should be {@link ContainerNode}
82 @SuppressWarnings("unchecked")
83 public <T extends DataObject> Optional<T> getDataObject(
84 @Nonnull NormalizedNode<?, ?> normalizedNode,
85 @Nonnull QName rootNode) {
86 Preconditions.checkNotNull(normalizedNode);
87 if (normalizedNode instanceof ContainerNode) {
88 YangInstanceIdentifier.PathArgument directChildIdentifier =
89 YangInstanceIdentifier.of(rootNode).getLastPathArgument();
90 Optional<NormalizedNode<?, ?>> directChild =
91 NormalizedNodes.getDirectChild(normalizedNode, directChildIdentifier);
92 if (!directChild.isPresent()) {
93 throw new IllegalStateException(String.format("Could not get the direct child of %s", rootNode));
95 normalizedNode = directChild.get();
97 YangInstanceIdentifier rootNodeYangInstanceIdentifier = YangInstanceIdentifier.of(rootNode);
99 Map.Entry<?, ?> bindingNodeEntry =
100 codecRegistry.fromNormalizedNode(rootNodeYangInstanceIdentifier, normalizedNode);
101 if (bindingNodeEntry == null) {
102 return Optional.empty();
104 return Optional.ofNullable((T) bindingNodeEntry.getValue());
108 @SuppressWarnings("unchecked")
109 public <T extends DataObject> Optional<T> getDataObjectFromRpc(
110 @Nonnull NormalizedNode<?, ?> normalizedNode,
111 @Nonnull SchemaPath rpcSchemaPath) {
113 if (! (normalizedNode instanceof ContainerNode)) {
114 LOG.error("converting normalized node is not ContainerNode. It's actual type is {}",
115 normalizedNode.getClass().getSimpleName());
116 return Optional.empty();
118 T rpcDataObject = (T) codecRegistry.fromNormalizedNodeRpcData(rpcSchemaPath, (ContainerNode) normalizedNode);
119 return Optional.ofNullable(rpcDataObject);
123 public <T extends DataObject> Optional<NormalizedNode<?, ?>> toNormalizedNodes(@Nonnull T object,
124 Class<T> dataObjectClass) {
125 Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalizedNode =
126 codecRegistry.toNormalizedNode(InstanceIdentifier.create(dataObjectClass), object);
127 return Optional.ofNullable(normalizedNode.getValue());
131 public <T extends DataObject> ConvertType<T> dataContainer() {
132 return (object, objectClass) -> {
133 NormalizedNode<?, ?> value =
134 getCodecRegistry().toNormalizedNode(InstanceIdentifier.create(objectClass), object).getValue();
135 return Optional.ofNullable(value);
140 public <T extends DataContainer> ConvertType<T> rpcData() {
141 return (object, objectClass) -> {
142 ContainerNode normalizedNodeRpcData = getCodecRegistry().toNormalizedNodeRpcData(object);
143 return Optional.ofNullable(normalizedNodeRpcData);
148 public <T extends Notification> ConvertType<T> notification() {
149 return (object, objectClass) -> {
150 ContainerNode normalizedNodeNotification = getCodecRegistry().toNormalizedNodeNotification(object);
151 return Optional.ofNullable(normalizedNodeNotification);