CONTROLLER-1641: Handle commit cohorts async
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / 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.controller.md.sal.binding.impl;
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  *
29  * Defines structural mapping of Normalized Node to Binding data
30  * addressable by Instance Identifier.
31  *
32  * Not all binding data are addressable by instance identifier
33  * and there are some differences.
34  *
35  * See {@link #NOT_ADDRESSABLE},{@link #INVISIBLE_CONTAINER},{@link #VISIBLE_CONTAINER}
36  * for more details.
37  *
38  *
39  */
40 enum BindingStructuralType {
41
42     /**
43      * DOM Item is not addressable in Binding Instance Identifier,
44      * data is not lost, but are available only via parent object.
45      *
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      * This are choice / case nodes.
56      *
57      * This data is still accessible using parent object and their
58      * children are addressable.
59      *
60      */
61     INVISIBLE_CONTAINER,
62     /**
63      * Data container is addressable in NormalizedNode format,
64      * but in Binding it is not represented in Instance Identifier.
65      *
66      * This are list nodes.
67      *
68      * This data is still accessible using parent object and their
69      * children are addressable.
70      *
71      */
72     INVISIBLE_LIST,
73     /**
74      * Data container is addressable in Binding Instance Identifier format
75      * and also YangInstanceIdentifier format.
76      *
77      */
78     VISIBLE_CONTAINER,
79     /**
80      * Mapping algorithm was unable to detect type or was not updated after introduction
81      * of new NormalizedNode type.
82      */
83     UNKNOWN;
84
85     static BindingStructuralType from(final DataTreeCandidateNode domChildNode) {
86         Optional<NormalizedNode<?, ?>> dataBased = domChildNode.getDataAfter();
87         if (!dataBased.isPresent()) {
88             dataBased = domChildNode.getDataBefore();
89         }
90         if (dataBased.isPresent()) {
91             return from(dataBased.get());
92         }
93         return from(domChildNode.getIdentifier());
94     }
95
96     private static BindingStructuralType from(final PathArgument identifier) {
97         if(identifier instanceof NodeIdentifierWithPredicates || identifier instanceof AugmentationIdentifier) {
98             return VISIBLE_CONTAINER;
99         }
100         if(identifier instanceof NodeWithValue) {
101             return NOT_ADDRESSABLE;
102         }
103         return UNKNOWN;
104     }
105
106     static BindingStructuralType from(final NormalizedNode<?, ?> data) {
107         if(isNotAddressable(data)) {
108             return NOT_ADDRESSABLE;
109         }
110         if(data instanceof MapNode) {
111             return INVISIBLE_LIST;
112         }
113         if(data instanceof ChoiceNode) {
114             return INVISIBLE_CONTAINER;
115         }
116         if(isVisibleContainer(data)) {
117             return VISIBLE_CONTAINER;
118         }
119         return UNKNOWN;
120     }
121
122     private static boolean isVisibleContainer(final NormalizedNode<?, ?> data) {
123         return data instanceof MapEntryNode || data instanceof ContainerNode || data instanceof AugmentationNode;
124     }
125
126     private static boolean isNotAddressable(final NormalizedNode<?, ?> d) {
127         return d instanceof LeafNode
128                 || d instanceof AnyXmlNode
129                 || d instanceof LeafSetNode
130                 || d instanceof LeafSetEntryNode;
131     }
132
133 }