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 com.google.common.base.VerifyException;
25 import java.time.Instant;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import org.eclipse.jdt.annotation.NonNull;
29 import org.opendaylight.mdsal.dom.api.DOMEvent;
30 import org.opendaylight.mdsal.dom.api.DOMNotification;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
37 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class DOMNotificationToXPath {
48 private static final Logger LOG = LoggerFactory.getLogger(DOMNotificationToXPath.class);
50 public HashMap<String, String> convertDomNotifToXPath(@NonNull DOMNotification domNotification) {
52 ContainerNode notifContainer = domNotification.getBody();
53 HashMap<String, String> xPathData = new HashMap<String, String>();
55 Collection<DataContainerChild> data = notifContainer.body();
56 for (DataContainerChild data1 : data) {
58 recurseDOMData(notifContainer, data1, notifContainer, xPathData, namePath);
60 LOG.debug("XPath Data = {}", xPathData);
65 private void recurseDOMData(@NonNull ContainerNode notifContainer, DataContainerChild domData, DataContainerNode cn,
66 HashMap<String, String> result, String namePath) {
67 PathArgument pa1 = domData.getIdentifier();
68 namePath += "/" + pa1.getNodeType().getLocalName();
69 if (domData.getClass().getSimpleName().equals("ImmutableContainerNode")) {
71 ContainerNode cn1 = (ContainerNode) cn.getChildByArg(pa1);
72 for (DataContainerChild data1 : cn1.body()) {
73 recurseDOMData(notifContainer, data1, cn1, result, namePath);
75 } catch (VerifyException ve) {
76 LOG.debug("{} does not exist", pa1);
80 if (domData.getClass().getSimpleName().equals("ImmutableChoiceNode")) {
82 ChoiceNode cn1 = (ChoiceNode) cn.getChildByArg(pa1);
83 for (DataContainerChild data1 : cn1.body()) {
84 // recurseChoiceData(data1, cn1, namePath);
85 recurseDOMData(notifContainer, data1, cn1, result, namePath);
87 } catch (VerifyException ve) {
88 LOG.debug("{} does not exist", pa1);
92 if (domData.getClass().getSimpleName().equals("ImmutableUnkeyedListNode")) {
94 UnkeyedListNode cn1 = (UnkeyedListNode) cn.getChildByArg(pa1);
95 for (UnkeyedListEntryNode data1 : cn1.body()) {
96 recurseUnkeyedListEntryNodeData(data1, cn1, result, namePath);
98 } catch (VerifyException ve) {
99 LOG.debug("{} does not exist", pa1);
103 if (domData.getClass().getSimpleName().equals("ImmutableMapNode")) {
105 MapNode cn1 = (MapNode) cn.getChildByArg(pa1);
106 for (MapEntryNode data1 : cn1.body()) {
107 recurseMapEntryNodeData(notifContainer, data1, cn1, result, namePath);
109 } catch (VerifyException ve) {
110 LOG.debug("{} does not exist", pa1);
114 if (domData.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
116 LeafSetNode<?> cn1 = (LeafSetNode<?>) cn.getChildByArg(pa1);
117 for (LeafSetEntryNode<?> data1 : cn1.body()) {
118 recurseLeafSetEntryNodeData(data1, cn1, result, namePath);
120 } catch (VerifyException ve) {
121 LOG.debug("{} does not exist", pa1);
125 if (domData.getClass().getSimpleName().equals("ImmutableLeafNode")) {
126 recurseLeafNode(domData, result, namePath);
130 private void recurseLeafSetEntryNodeData(LeafSetEntryNode<?> data, LeafSetNode<?> cn1,
131 HashMap<String, String> result, String namePath) {
132 PathArgument pa1 = data.getIdentifier();
133 namePath += "/" + pa1.getNodeType().getLocalName();
135 if (data.getClass().getSimpleName().equals("ImmutableLeafSetEntryNode")) {
136 LOG.debug("{}={}", namePath, data.body());
137 result.put(namePath, data.body().toString());
141 private void recurseMapEntryNodeData(@NonNull ContainerNode notifContainer, MapEntryNode data, MapNode cn1,
142 HashMap<String, String> result, String namePath) {
143 PathArgument pa1 = data.getIdentifier();
144 NodeIdentifierWithPredicates ni = data.getIdentifier();
146 for (QName qn : ni.keySet()) {
147 namePath += "/" + ni.getValue(qn);
150 if (data.getClass().getSimpleName().equals("ImmutableMapEntryNode")) {
151 for (DataContainerChild data1 : data.body()) {
152 if (data1.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
154 LeafSetNode<?> cn2 = (LeafSetNode<?>) data.getChildByArg(data1.getIdentifier());
155 for (LeafSetEntryNode<?> data2 : cn2.body()) {
156 recurseLeafSetEntryNodeData(data2, cn2, result, namePath);
158 } catch (VerifyException ve) {
159 LOG.debug("{} does not exist", data1.getIdentifier());
162 recurseLeafNode(data1, result, namePath);
167 if (data.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
169 LeafSetNode<?> cn2 = (LeafSetNode<?>) notifContainer.getChildByArg(pa1);
170 for (LeafSetEntryNode<?> data1 : cn2.body()) {
171 recurseLeafSetEntryNodeData(data1, cn2, result, namePath);
173 } catch (VerifyException ve) {
174 LOG.debug("{} does not exist", pa1);
178 if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
179 LOG.debug("{}={}", namePath, data.body());
180 result.put(namePath, data.body().toString());
184 private void recurseUnkeyedListEntryNodeData(UnkeyedListEntryNode data, UnkeyedListNode cn1,
185 HashMap<String, String> result, String namePath) {
186 PathArgument pa1 = data.getIdentifier();
187 namePath += "/" + pa1.getNodeType().getLocalName();
189 if (data.getClass().getSimpleName().equals("ImmutableUnkeyedListEntryNode")) {
190 for (DataContainerChild data1 : data.body()) {
191 recurseLeafNode(data1, result, namePath);
195 if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
196 LOG.debug("{}={}", namePath, data.body());
197 result.put(namePath, data.body().toString());
201 public void recurseLeafNode(DataContainerChild data, HashMap<String, String> result, String namePath) {
202 PathArgument pa1 = data.getIdentifier();
203 if (!(data.getClass().getSimpleName().equals("ImmutableAugmentationNode")))
204 namePath += "/" + pa1.getNodeType().getLocalName();
205 if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
206 LOG.debug("{}={}", namePath, data.body());
207 result.put(namePath, data.body().toString());
211 public void recurseChoiceData(HashMap<String, String> result, DataContainerChild data, ChoiceNode cn,
213 PathArgument pa1 = data.getIdentifier();
214 namePath += "/" + pa1.getNodeType().getLocalName();
215 // NodeIdentifier nodeId = new NodeIdentifier(pa1.getNodeType());
216 if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
217 LOG.debug("{}={}", namePath, data.body());
218 result.put(namePath, data.body().toString());
222 public Instant getTime(@NonNull DOMNotification domNotification) {
225 if (domNotification instanceof DOMEvent) {
226 eventTime = ((DOMEvent) domNotification).getEventInstant();
227 LOG.info("Event time {}", eventTime);
229 eventTime = Instant.now();
230 LOG.info("Defaulting to actual time of processing the notification - {}", eventTime);