64fb9b8579588812f3a2608c4fb300237c4b5506
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.impl.dom;
23
24 import java.time.Instant;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.opendaylight.mdsal.dom.api.DOMEvent;
29 import org.opendaylight.mdsal.dom.api.DOMNotification;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
36 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import com.google.common.base.VerifyException;
47
48 public class DOMNotificationToXPath {
49         private static final Logger LOG = LoggerFactory.getLogger(DOMNotificationToXPath.class);
50
51         public HashMap<String, String> convertDomNotifToXPath(@NonNull DOMNotification domNotification) {
52                 @NonNull
53                 ContainerNode notifContainer = domNotification.getBody();
54                 HashMap<String, String> xPathData = new HashMap<String, String>();
55
56                 Collection<DataContainerChild> data = notifContainer.body();
57                 for (DataContainerChild data1 : data) {
58                         String namePath = "";
59                         recurseDOMData(notifContainer, data1, notifContainer, xPathData, namePath);
60                 }
61                 LOG.debug("XPath Data = {}", xPathData);
62                 return xPathData;
63
64         }
65
66         private void recurseDOMData(@NonNull ContainerNode notifContainer, DataContainerChild domData, DataContainerNode cn,
67                         HashMap<String, String> result, String namePath) {
68                 PathArgument pa1 = domData.getIdentifier();
69                 namePath += "/" + pa1.getNodeType().getLocalName();
70                 if (domData.getClass().getSimpleName().equals("ImmutableContainerNode")) {
71                         try {
72                                 ContainerNode cn1 = (ContainerNode) cn.getChildByArg(pa1);
73                                 for (DataContainerChild data1 : cn1.body()) {
74                                         recurseDOMData(notifContainer, data1, cn1, result, namePath);
75                                 }
76                         } catch (VerifyException ve) {
77                                 LOG.debug("{} does not exist", pa1);
78                         }
79                 }
80
81                 if (domData.getClass().getSimpleName().equals("ImmutableChoiceNode")) {
82                         try {
83                                 ChoiceNode cn1 = (ChoiceNode) cn.getChildByArg(pa1);
84                                 for (DataContainerChild data1 : cn1.body()) {
85                                         // recurseChoiceData(data1, cn1, namePath);
86                                         recurseDOMData(notifContainer, data1, cn1, result, namePath);
87                                 }
88                         } catch (VerifyException ve) {
89                                 LOG.debug("{} does not exist", pa1);
90                         }
91                 }
92
93                 if (domData.getClass().getSimpleName().equals("ImmutableUnkeyedListNode")) {
94                         try {
95                                 UnkeyedListNode cn1 = (UnkeyedListNode) cn.getChildByArg(pa1);
96                                 for (UnkeyedListEntryNode data1 : cn1.body()) {
97                                         recurseUnkeyedListEntryNodeData(data1, cn1, result, namePath);
98                                 }
99                         } catch (VerifyException ve) {
100                                 LOG.debug("{} does not exist", pa1);
101                         }
102                 }
103
104                 if (domData.getClass().getSimpleName().equals("ImmutableMapNode")) {
105                         try {
106                                 MapNode cn1 = (MapNode) cn.getChildByArg(pa1);
107                                 for (MapEntryNode data1 : cn1.body()) {
108                                         recurseMapEntryNodeData(notifContainer, data1, cn1, result, namePath);
109                                 }
110                         } catch (VerifyException ve) {
111                                 LOG.debug("{} does not exist", pa1);
112                         }
113                 }
114
115                 if (domData.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
116                         try {
117                                 LeafSetNode<?> cn1 = (LeafSetNode<?>) cn.getChildByArg(pa1);
118                                 for (LeafSetEntryNode<?> data1 : cn1.body()) {
119                                         recurseLeafSetEntryNodeData(data1, cn1, result, namePath);
120                                 }
121                         } catch (VerifyException ve) {
122                                 LOG.debug("{} does not exist", pa1);
123                         }
124                 }
125
126                 if (domData.getClass().getSimpleName().equals("ImmutableLeafNode")) {
127                         recurseLeafNode(domData, result, namePath);
128                 }
129         }
130
131         private void recurseLeafSetEntryNodeData(LeafSetEntryNode<?> data, LeafSetNode<?> cn1,
132                         HashMap<String, String> result, String namePath) {
133                 PathArgument pa1 = data.getIdentifier();
134                 namePath += "/" + pa1.getNodeType().getLocalName();
135
136                 if (data.getClass().getSimpleName().equals("ImmutableLeafSetEntryNode")) {
137                         LOG.debug("{}={}", namePath, data.body());
138                         result.put(namePath, data.body().toString());
139                 }
140         }
141
142         private void recurseMapEntryNodeData(@NonNull ContainerNode notifContainer, MapEntryNode data, MapNode cn1,
143                         HashMap<String, String> result, String namePath) {
144                 PathArgument pa1 = data.getIdentifier();
145                 NodeIdentifierWithPredicates ni = data.getIdentifier();
146
147                 for (QName qn : ni.keySet()) {
148                         namePath += "/" + ni.getValue(qn);
149                 }
150
151                 if (data.getClass().getSimpleName().equals("ImmutableMapEntryNode")) {
152                         for (DataContainerChild data1 : data.body()) {
153                                 if (data1.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
154                                         try {
155                                                 LeafSetNode<?> cn2 = (LeafSetNode<?>) data.getChildByArg(data1.getIdentifier());
156                                                 for (LeafSetEntryNode<?> data2 : cn2.body()) {
157                                                         recurseLeafSetEntryNodeData(data2, cn2, result, namePath);
158                                                 }
159                                         } catch (VerifyException ve) {
160                                                 LOG.debug("{} does not exist", data1.getIdentifier());
161                                         }
162                                 } else {
163                                         recurseLeafNode(data1, result, namePath);
164                                 }
165                         }
166                 }
167
168                 if (data.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
169                         try {
170                                 LeafSetNode<?> cn2 = (LeafSetNode<?>) notifContainer.getChildByArg(pa1);
171                                 for (LeafSetEntryNode<?> data1 : cn2.body()) {
172                                         recurseLeafSetEntryNodeData(data1, cn2, result, namePath);
173                                 }
174                         } catch (VerifyException ve) {
175                                 LOG.debug("{} does not exist", pa1);
176                         }
177                 }
178
179                 if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
180                         LOG.debug("{}={}", namePath, data.body());
181                         result.put(namePath, data.body().toString());
182                 }
183         }
184
185         private void recurseUnkeyedListEntryNodeData(UnkeyedListEntryNode data, UnkeyedListNode cn1,
186                         HashMap<String, String> result, String namePath) {
187                 PathArgument pa1 = data.getIdentifier();
188                 namePath += "/" + pa1.getNodeType().getLocalName();
189
190                 if (data.getClass().getSimpleName().equals("ImmutableUnkeyedListEntryNode")) {
191                         for (DataContainerChild data1 : data.body()) {
192                                 recurseLeafNode(data1, result, namePath);
193                         }
194                 }
195
196                 if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
197                         LOG.debug("{}={}", namePath, data.body());
198                         result.put(namePath, data.body().toString());
199                 }
200         }
201
202         public void recurseLeafNode(DataContainerChild data, HashMap<String, String> result, String namePath) {
203                 PathArgument pa1 = data.getIdentifier();
204                 if (!(data.getClass().getSimpleName().equals("ImmutableAugmentationNode")))
205                         namePath += "/" + pa1.getNodeType().getLocalName();
206                 if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
207                         LOG.debug("{}={}", namePath, data.body());
208                         result.put(namePath, data.body().toString());
209                 }
210         }
211
212         public void recurseChoiceData(HashMap<String, String> result, DataContainerChild data, ChoiceNode cn,
213                         String namePath) {
214                 PathArgument pa1 = data.getIdentifier();
215                 namePath += "/" + pa1.getNodeType().getLocalName();
216                 // NodeIdentifier nodeId = new NodeIdentifier(pa1.getNodeType());
217                 if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
218                         LOG.debug("{}={}", namePath, data.body());
219                         result.put(namePath, data.body().toString());
220                 }
221         }
222
223         public Instant getTime(@NonNull DOMNotification domNotification) {
224                 @NonNull
225                 Instant eventTime;
226                 if (domNotification instanceof DOMEvent) {
227                         eventTime = ((DOMEvent) domNotification).getEventInstant();
228                         LOG.info("Event time {}", eventTime);
229                 } else {
230                         eventTime = Instant.now();
231                         LOG.info("Defaulting to actual time of processing the notification - {}", eventTime);
232                 }
233                 return eventTime;
234         }
235 }