5a7c70e3a5bf63ad9f38ea557fb369c126670a6f
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / BindingStructuralType.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.dom.adapter;
9
10 import java.util.Optional;
11 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
26
27 /**
28  * Defines structural mapping of Normalized Node to Binding data
29  * addressable by Instance Identifier.
30  *
31  * <p>
32  * Not all binding data are addressable by instance identifier
33  * and there are some differences.
34  *
35  * <p>
36  * See {@link #NOT_ADDRESSABLE},{@link #INVISIBLE_CONTAINER},{@link #VISIBLE_CONTAINER}
37  * for more details.
38  */
39 enum BindingStructuralType {
40
41     /**
42      * DOM Item is not addressable in Binding Instance Identifier,
43      * data is not lost, but are available only via parent object.
44      *
45      * <p>
46      * Such types of data are leaf-lists, leafs, list without keys
47      * or anyxml.
48      *
49      */
50     NOT_ADDRESSABLE,
51     /**
52      * Data container is addressable in NormalizedNode format,
53      * but in Binding it is not represented in Instance Identifier.
54      *
55      * <p>
56      * This are choice / case nodes.
57      *
58      * <p>
59      * This data is still accessible using parent object and their
60      * children are addressable.
61      *
62      */
63     INVISIBLE_CONTAINER,
64     /**
65      * Data container is addressable in NormalizedNode format,
66      * but in Binding it is not represented in Instance Identifier.
67      *
68      * <p>
69      * This are list nodes.
70      *
71      * <p>
72      * This data is still accessible using parent object and their
73      * children are addressable.
74      *
75      */
76     INVISIBLE_LIST,
77     /**
78      * Data container is addressable in Binding Instance Identifier format
79      * and also YangInstanceIdentifier format.
80      *
81      */
82     VISIBLE_CONTAINER,
83     /**
84      * Mapping algorithm was unable to detect type or was not updated after introduction
85      * of new NormalizedNode type.
86      */
87     UNKNOWN;
88
89     static BindingStructuralType from(final DataTreeCandidateNode domChildNode) {
90         Optional<NormalizedNode<?, ?>> dataBased = domChildNode.getDataAfter();
91         if (!dataBased.isPresent()) {
92             dataBased = domChildNode.getDataBefore();
93         }
94         if (dataBased.isPresent()) {
95             return from(dataBased.get());
96         }
97         return from(domChildNode.getIdentifier());
98     }
99
100     private static BindingStructuralType from(final PathArgument identifier) {
101         if (identifier instanceof NodeIdentifierWithPredicates || identifier instanceof AugmentationIdentifier) {
102             return VISIBLE_CONTAINER;
103         }
104         if (identifier instanceof NodeWithValue) {
105             return NOT_ADDRESSABLE;
106         }
107         return UNKNOWN;
108     }
109
110     static BindingStructuralType from(final NormalizedNode<?, ?> data) {
111         if (isNotAddressable(data)) {
112             return NOT_ADDRESSABLE;
113         }
114         if (data instanceof MapNode) {
115             return INVISIBLE_LIST;
116         }
117         if (data instanceof ChoiceNode) {
118             return INVISIBLE_CONTAINER;
119         }
120         if (isVisibleContainer(data)) {
121             return VISIBLE_CONTAINER;
122         }
123         return UNKNOWN;
124     }
125
126     private static boolean isVisibleContainer(final NormalizedNode<?, ?> data) {
127         return data instanceof MapEntryNode || data instanceof ContainerNode || data instanceof AugmentationNode;
128     }
129
130     private static boolean isNotAddressable(final NormalizedNode<?, ?> normalizedNode) {
131         return normalizedNode instanceof LeafNode
132                 || normalizedNode instanceof AnyXmlNode
133                 || normalizedNode instanceof LeafSetNode
134                 || normalizedNode instanceof LeafSetEntryNode;
135     }
136
137 }