fix ServiceHandler SpotBugs false positives
[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 import io.fd.honeycomb.transportpce.binding.converter.api.DataObjectConverter;
20 import java.util.Map;
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;
38
39 /**
40  * Converts XML and {@link DataObject} vice versa.
41  *
42  */
43 public abstract class AbstractDataObjectConverter implements DataObjectConverter {
44
45     private static final Logger LOG = LoggerFactory.getLogger(AbstractDataObjectConverter.class);
46
47     private final SchemaContext schemaContext;
48     private final BindingNormalizedNodeSerializer codecRegistry;
49
50     /**
51      * This is the default constructor, which should be used.
52      *
53      * @param schemaContext schema context for converter
54      * @param codecRegistry codec registry used for converting
55      *
56      */
57     protected AbstractDataObjectConverter(SchemaContext schemaContext, BindingNormalizedNodeSerializer codecRegistry) {
58         this.schemaContext = schemaContext;
59         this.codecRegistry = codecRegistry;
60     }
61
62     public SchemaContext getSchemaContext() {
63         return schemaContext;
64     }
65
66     public BindingNormalizedNodeSerializer getCodecRegistry() {
67         return codecRegistry;
68     }
69
70     /**
71      * Transforms the given input {@link NormalizedNode} into the given
72      * {@link DataObject}.
73      *
74      * @param normalizedNode normalized node you want to convert
75      * @param rootNode {@link QName} of converted normalized node root
76      *
77      * <p>
78      * The input object should be {@link ContainerNode}
79      * </p>
80      */
81     @Override
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));
94             }
95             normalizedNode = directChild.get();
96         }
97         YangInstanceIdentifier rootNodeYangInstanceIdentifier = YangInstanceIdentifier.of(rootNode);
98
99         Map.Entry<?, ?> bindingNodeEntry =
100                 codecRegistry.fromNormalizedNode(rootNodeYangInstanceIdentifier, normalizedNode);
101         if (bindingNodeEntry == null) {
102             return Optional.empty();
103         }
104         return Optional.ofNullable((T) bindingNodeEntry.getValue());
105     }
106
107     @Override
108     @SuppressWarnings("unchecked")
109     public <T extends DataObject> Optional<T> getDataObjectFromRpc(
110             @Nonnull NormalizedNode<?, ?> normalizedNode,
111             @Nonnull SchemaPath rpcSchemaPath) {
112
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();
117         }
118         T rpcDataObject = (T) codecRegistry.fromNormalizedNodeRpcData(rpcSchemaPath, (ContainerNode) normalizedNode);
119         return Optional.ofNullable(rpcDataObject);
120     }
121
122     @Override
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());
128     }
129
130     @Override
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);
136         };
137     }
138
139     @Override
140     public <T extends DataContainer> ConvertType<T> rpcData() {
141         return (object, objectClass) -> {
142             ContainerNode normalizedNodeRpcData = getCodecRegistry().toNormalizedNodeRpcData(object);
143             return Optional.ofNullable(normalizedNodeRpcData);
144         };
145     }
146
147     @Override
148     public <T extends Notification> ConvertType<T> notification() {
149         return (object, objectClass) -> {
150             ContainerNode normalizedNodeNotification = getCodecRegistry().toNormalizedNodeNotification(object);
151             return Optional.ofNullable(normalizedNodeNotification);
152         };
153     }
154 }