2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.impl.dom;
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;
46 import com.google.common.base.VerifyException;
48 public class DOMNotificationToXPath {
49 private static final Logger LOG = LoggerFactory.getLogger(DOMNotificationToXPath.class);
51 public HashMap<String, String> convertDomNotifToXPath(@NonNull DOMNotification domNotification) {
53 ContainerNode notifContainer = domNotification.getBody();
54 HashMap<String, String> xPathData = new HashMap<String, String>();
56 Collection<DataContainerChild> data = notifContainer.body();
57 for (DataContainerChild data1 : data) {
59 recurseDOMData(notifContainer, data1, notifContainer, xPathData, namePath);
61 LOG.debug("XPath Data = {}", xPathData);
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")) {
72 ContainerNode cn1 = (ContainerNode) cn.getChildByArg(pa1);
73 for (DataContainerChild data1 : cn1.body()) {
74 recurseDOMData(notifContainer, data1, cn1, result, namePath);
76 } catch (VerifyException ve) {
77 LOG.debug("{} does not exist", pa1);
81 if (domData.getClass().getSimpleName().equals("ImmutableChoiceNode")) {
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);
88 } catch (VerifyException ve) {
89 LOG.debug("{} does not exist", pa1);
93 if (domData.getClass().getSimpleName().equals("ImmutableUnkeyedListNode")) {
95 UnkeyedListNode cn1 = (UnkeyedListNode) cn.getChildByArg(pa1);
96 for (UnkeyedListEntryNode data1 : cn1.body()) {
97 recurseUnkeyedListEntryNodeData(data1, cn1, result, namePath);
99 } catch (VerifyException ve) {
100 LOG.debug("{} does not exist", pa1);
104 if (domData.getClass().getSimpleName().equals("ImmutableMapNode")) {
106 MapNode cn1 = (MapNode) cn.getChildByArg(pa1);
107 for (MapEntryNode data1 : cn1.body()) {
108 recurseMapEntryNodeData(notifContainer, data1, cn1, result, namePath);
110 } catch (VerifyException ve) {
111 LOG.debug("{} does not exist", pa1);
115 if (domData.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
117 LeafSetNode<?> cn1 = (LeafSetNode<?>) cn.getChildByArg(pa1);
118 for (LeafSetEntryNode<?> data1 : cn1.body()) {
119 recurseLeafSetEntryNodeData(data1, cn1, result, namePath);
121 } catch (VerifyException ve) {
122 LOG.debug("{} does not exist", pa1);
126 if (domData.getClass().getSimpleName().equals("ImmutableLeafNode")) {
127 recurseLeafNode(domData, result, namePath);
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();
136 if (data.getClass().getSimpleName().equals("ImmutableLeafSetEntryNode")) {
137 LOG.debug("{}={}", namePath, data.body());
138 result.put(namePath, data.body().toString());
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();
147 for (QName qn : ni.keySet()) {
148 namePath += "/" + ni.getValue(qn);
151 if (data.getClass().getSimpleName().equals("ImmutableMapEntryNode")) {
152 for (DataContainerChild data1 : data.body()) {
153 if (data1.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
155 LeafSetNode<?> cn2 = (LeafSetNode<?>) data.getChildByArg(data1.getIdentifier());
156 for (LeafSetEntryNode<?> data2 : cn2.body()) {
157 recurseLeafSetEntryNodeData(data2, cn2, result, namePath);
159 } catch (VerifyException ve) {
160 LOG.debug("{} does not exist", data1.getIdentifier());
163 recurseLeafNode(data1, result, namePath);
168 if (data.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
170 LeafSetNode<?> cn2 = (LeafSetNode<?>) notifContainer.getChildByArg(pa1);
171 for (LeafSetEntryNode<?> data1 : cn2.body()) {
172 recurseLeafSetEntryNodeData(data1, cn2, result, namePath);
174 } catch (VerifyException ve) {
175 LOG.debug("{} does not exist", pa1);
179 if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
180 LOG.debug("{}={}", namePath, data.body());
181 result.put(namePath, data.body().toString());
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();
190 if (data.getClass().getSimpleName().equals("ImmutableUnkeyedListEntryNode")) {
191 for (DataContainerChild data1 : data.body()) {
192 recurseLeafNode(data1, result, namePath);
196 if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
197 LOG.debug("{}={}", namePath, data.body());
198 result.put(namePath, data.body().toString());
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());
212 public void recurseChoiceData(HashMap<String, String> result, DataContainerChild data, ChoiceNode cn,
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());
223 public Instant getTime(@NonNull DOMNotification domNotification) {
226 if (domNotification instanceof DOMEvent) {
227 eventTime = ((DOMEvent) domNotification).getEventInstant();
228 LOG.info("Event time {}", eventTime);
230 eventTime = Instant.now();
231 LOG.info("Defaulting to actual time of processing the notification - {}", eventTime);