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.notification;
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.NodeIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
39 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public class ORanDOMNotificationToXPath {
50 private static final Logger LOG = LoggerFactory.getLogger(ORanDOMNotificationToXPath.class);
52 public HashMap<String, String> convertDomNotifToXPath(@NonNull DOMNotification domNotification) {
54 ContainerNode notifContainer = domNotification.getBody();
55 HashMap<String, String> xPathData = new HashMap<String, String>();
57 Collection<DataContainerChild> data = notifContainer.body();
58 for (DataContainerChild data1 : data) {
60 recurseDOMData(notifContainer, data1, notifContainer, xPathData, namePath);
62 LOG.debug("XPath Data = {}", xPathData);
67 private void recurseDOMData(@NonNull ContainerNode notifContainer, DataContainerChild domData, DataContainerNode cn,
68 HashMap<String, String> result, String namePath) {
69 NodeIdentifier id = domData.name();
70 namePath += "/" + id.getNodeType().getLocalName();
71 if (domData.getClass().getSimpleName().equals("ImmutableContainerNode")) {
73 ContainerNode cn1 = (ContainerNode) cn.getChildByArg(id);
74 for (DataContainerChild data1 : cn1.body()) {
75 recurseDOMData(notifContainer, data1, cn1, result, namePath);
77 } catch (VerifyException ve) {
78 LOG.debug("{} does not exist", id);
82 if (domData.getClass().getSimpleName().equals("ImmutableChoiceNode")) {
84 ChoiceNode cn1 = (ChoiceNode) cn.getChildByArg(id);
85 for (DataContainerChild data1 : cn1.body()) {
86 // recurseChoiceData(data1, cn1, namePath);
87 recurseDOMData(notifContainer, data1, cn1, result, namePath);
89 } catch (VerifyException ve) {
90 LOG.debug("{} does not exist", id);
94 if (domData.getClass().getSimpleName().equals("ImmutableUnkeyedListNode")) {
96 UnkeyedListNode cn1 = (UnkeyedListNode) cn.getChildByArg(id);
97 for (UnkeyedListEntryNode data1 : cn1.body()) {
98 recurseUnkeyedListEntryNodeData(data1, cn1, result, namePath);
100 } catch (VerifyException ve) {
101 LOG.debug("{} does not exist", id);
105 if (domData.getClass().getSimpleName().equals("ImmutableMapNode")) {
107 MapNode cn1 = (MapNode) cn.getChildByArg(id);
108 for (MapEntryNode data1 : cn1.body()) {
109 recurseMapEntryNodeData(notifContainer, data1, cn1, result, namePath);
111 } catch (VerifyException ve) {
112 LOG.debug("{} does not exist", id);
116 if (domData.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
118 LeafSetNode<?> cn1 = (LeafSetNode<?>) cn.getChildByArg(id);
119 for (LeafSetEntryNode<?> data1 : cn1.body()) {
120 recurseLeafSetEntryNodeData(data1, cn1, result, namePath);
122 } catch (VerifyException ve) {
123 LOG.debug("{} does not exist", id);
127 if (domData.getClass().getSimpleName().equals("ImmutableLeafNode")) {
128 recurseLeafNode(domData, result, namePath);
132 private void recurseLeafSetEntryNodeData(LeafSetEntryNode<?> data, LeafSetNode<?> cn1,
133 HashMap<String, String> result, String namePath) {
134 NodeWithValue<?> pa1 = data.name();
135 namePath += "/" + pa1.getNodeType().getLocalName();
137 if (data.getClass().getSimpleName().equals("ImmutableLeafSetEntryNode")) {
138 LOG.debug("{}={}", namePath, data.body());
139 result.put(namePath, data.body().toString());
143 private void recurseMapEntryNodeData(@NonNull ContainerNode notifContainer, MapEntryNode data, MapNode cn1,
144 HashMap<String, String> result, String namePath) {
145 PathArgument pa1 = data.name();
146 NodeIdentifierWithPredicates ni = data.name();
148 for (QName qn : ni.keySet()) {
149 namePath += "/" + ni.getValue(qn);
152 if (data.getClass().getSimpleName().equals("ImmutableMapEntryNode")) {
153 for (DataContainerChild data1 : data.body()) {
154 if (data1.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
156 LeafSetNode<?> cn2 = (LeafSetNode<?>) data.getChildByArg(data1.name());
157 for (LeafSetEntryNode<?> data2 : cn2.body()) {
158 recurseLeafSetEntryNodeData(data2, cn2, result, namePath);
160 } catch (VerifyException ve) {
161 LOG.debug("{} does not exist", data1.name());
164 recurseLeafNode(data1, result, namePath);
169 if (data.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
171 LeafSetNode<?> cn2 = (LeafSetNode<?>) notifContainer.getChildByArg((NodeIdentifier) pa1);
172 for (LeafSetEntryNode<?> data1 : cn2.body()) {
173 recurseLeafSetEntryNodeData(data1, cn2, result, namePath);
175 } catch (VerifyException ve) {
176 LOG.debug("{} does not exist", pa1);
180 if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
181 LOG.debug("{}={}", namePath, data.body());
182 result.put(namePath, data.body().toString());
186 private void recurseUnkeyedListEntryNodeData(UnkeyedListEntryNode data, UnkeyedListNode cn1,
187 HashMap<String, String> result, String namePath) {
188 PathArgument pa1 = data.name();
189 namePath += "/" + pa1.getNodeType().getLocalName();
191 if (data.getClass().getSimpleName().equals("ImmutableUnkeyedListEntryNode")) {
192 for (DataContainerChild data1 : data.body()) {
193 recurseLeafNode(data1, result, namePath);
197 if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
198 LOG.debug("{}={}", namePath, data.body());
199 result.put(namePath, data.body().toString());
203 public void recurseLeafNode(DataContainerChild data, HashMap<String, String> result, String namePath) {
204 PathArgument pa1 = data.name();
205 if (!(data.getClass().getSimpleName().equals("ImmutableAugmentationNode")))
206 namePath += "/" + pa1.getNodeType().getLocalName();
207 if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
208 LOG.debug("{}={}", namePath, data.body());
209 result.put(namePath, data.body().toString());
213 public void recurseChoiceData(HashMap<String, String> result, DataContainerChild data, ChoiceNode cn,
215 PathArgument pa1 = data.name();
216 namePath += "/" + pa1.getNodeType().getLocalName();
217 // NodeIdentifier nodeId = new NodeIdentifier(pa1.getNodeType());
218 if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
219 LOG.debug("{}={}", namePath, data.body());
220 result.put(namePath, data.body().toString());
224 public Instant getTime(@NonNull DOMNotification domNotification) {
227 if (domNotification instanceof DOMEvent) {
228 eventTime = ((DOMEvent) domNotification).getEventInstant();
229 LOG.debug("Event time {}", eventTime);
231 eventTime = Instant.now();
232 LOG.debug("Defaulting to actual time of processing the notification - {}", eventTime);