HONEYCOMB-8: Move data layer from impl module into submodules
[honeycomb.git] / v3po / data-impl / src / main / java / io / fd / honeycomb / v3po / data / impl / DataTreeUtils.java
1 /*
2  * Copyright (c) 2016 Cisco 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
17 package io.fd.honeycomb.v3po.data.impl;
18
19 import com.google.common.base.Preconditions;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.Map;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
29 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Utility class for various operations on DataTree.
35  */
36 final class DataTreeUtils {
37     private static final Logger LOG = LoggerFactory.getLogger(DataTreeUtils.class);
38
39     private DataTreeUtils() {
40         throw new UnsupportedOperationException("Can't instantiate util class");
41     }
42
43     /**
44      * Translates children of supplied YANG ContainerNode into Binding data.
45      *
46      * @param parent     ContainerNode representing data
47      * @param serializer service for serialization between Java Binding Data representation and NormalizedNode
48      *                   representation.
49      * @return NormalizedNode representation of parent's node children
50      */
51     static Map<InstanceIdentifier<?>, DataObject> childrenFromNormalized(@Nonnull final DataContainerNode parent,
52                                                                          @Nonnull final BindingNormalizedNodeSerializer serializer) {
53
54         Preconditions.checkNotNull(parent, "parent node should not be null");
55         Preconditions.checkNotNull(serializer, "serializer should not be null");
56
57         final Map<InstanceIdentifier<?>, DataObject> map = new HashMap<>();
58
59         final Collection<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> children =
60                 parent.getValue();
61
62         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> child : children) {
63             final YangInstanceIdentifier.PathArgument pathArgument = child.getIdentifier();
64             final YangInstanceIdentifier identifier = YangInstanceIdentifier.create(pathArgument);
65             LOG.debug("VppConfigDataProxy.extractDataObject() child={}, pathArgument={}, identifier={}", child,
66                     pathArgument, identifier);
67
68             final Map.Entry<InstanceIdentifier<?>, DataObject> entry = serializer.fromNormalizedNode(identifier, child);
69             if (entry != null) {
70                 map.put(entry.getKey(), entry.getValue());
71             }
72         }
73
74         return map;
75     }
76 }