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;
20 import io.fd.honeycomb.transportpce.binding.converter.api.DataObjectConverter;
23 import java.util.Map.Entry;
24 import java.util.Optional;
26 import javax.annotation.Nonnull;
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;
44 * Converts XML and {@link DataObject} vice versa.
47 public abstract class AbstractDataObjectConverter implements DataObjectConverter {
49 private static final Logger LOG = LoggerFactory.getLogger(AbstractDataObjectConverter.class);
51 private final SchemaContext schemaContext;
52 private final BindingNormalizedNodeSerializer codecRegistry;
55 * This is the default constructor, which should be used.
57 * @param schemaContext schema context for converter
58 * @param codecRegistry codec registry used for converting
61 protected AbstractDataObjectConverter(SchemaContext schemaContext, BindingNormalizedNodeSerializer codecRegistry) {
62 this.schemaContext = schemaContext;
63 this.codecRegistry = codecRegistry;
66 public SchemaContext getSchemaContext() {
70 public BindingNormalizedNodeSerializer getCodecRegistry() {
75 * Transforms the given input {@link NormalizedNode} into the given
78 * @param normalizedNode normalized node you want to convert
79 * @param rootNode {@link QName} of converted normalized node root
82 * The input object should be {@link ContainerNode}
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));
99 normalizedNode = directChild.get();
101 YangInstanceIdentifier rootNodeYangInstanceIdentifier = YangInstanceIdentifier.of(rootNode);
103 Map.Entry<?, ?> bindingNodeEntry =
104 codecRegistry.fromNormalizedNode(rootNodeYangInstanceIdentifier, normalizedNode);
105 if (bindingNodeEntry == null) {
106 return Optional.empty();
108 return Optional.ofNullable((T) bindingNodeEntry.getValue());
112 @SuppressWarnings("unchecked")
113 public <T extends DataObject> Optional<T> getDataObjectFromRpc(
114 @Nonnull NormalizedNode<?, ?> normalizedNode,
115 @Nonnull SchemaPath rpcSchemaPath) {
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();
122 T rpcDataObject = (T) codecRegistry.fromNormalizedNodeRpcData(rpcSchemaPath, (ContainerNode) normalizedNode);
123 return Optional.ofNullable(rpcDataObject);
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());
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);
144 public <T extends DataContainer> ConvertType<T> rpcData() {
145 return (object, objectClass) -> {
146 ContainerNode normalizedNodeRpcData = getCodecRegistry().toNormalizedNodeRpcData(object);
147 return Optional.ofNullable(normalizedNodeRpcData);
152 public <T extends Notification> ConvertType<T> notification() {
153 return (object, objectClass) -> {
154 ContainerNode normalizedNodeNotification = getCodecRegistry().toNormalizedNodeNotification(object);
155 return Optional.ofNullable(normalizedNodeNotification);