b28c118303f3952b2a63f2df3e55060f7c1dde37
[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.notification;
23
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;
48
49 public class ORanDOMNotificationToXPath {
50     private static final Logger LOG = LoggerFactory.getLogger(ORanDOMNotificationToXPath.class);
51
52     public HashMap<String, String> convertDomNotifToXPath(@NonNull DOMNotification domNotification) {
53         @NonNull
54         ContainerNode notifContainer = domNotification.getBody();
55         HashMap<String, String> xPathData = new HashMap<String, String>();
56
57         Collection<DataContainerChild> data = notifContainer.body();
58         for (DataContainerChild data1 : data) {
59             String namePath = "";
60             recurseDOMData(notifContainer, data1, notifContainer, xPathData, namePath);
61         }
62         LOG.debug("XPath Data = {}", xPathData);
63         return xPathData;
64
65     }
66
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")) {
72             try {
73                 ContainerNode cn1 = (ContainerNode) cn.getChildByArg(id);
74                 for (DataContainerChild data1 : cn1.body()) {
75                     recurseDOMData(notifContainer, data1, cn1, result, namePath);
76                 }
77             } catch (VerifyException ve) {
78                 LOG.debug("{} does not exist", id);
79             }
80         }
81
82         if (domData.getClass().getSimpleName().equals("ImmutableChoiceNode")) {
83             try {
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);
88                 }
89             } catch (VerifyException ve) {
90                 LOG.debug("{} does not exist", id);
91             }
92         }
93
94         if (domData.getClass().getSimpleName().equals("ImmutableUnkeyedListNode")) {
95             try {
96                 UnkeyedListNode cn1 = (UnkeyedListNode) cn.getChildByArg(id);
97                 for (UnkeyedListEntryNode data1 : cn1.body()) {
98                     recurseUnkeyedListEntryNodeData(data1, cn1, result, namePath);
99                 }
100             } catch (VerifyException ve) {
101                 LOG.debug("{} does not exist", id);
102             }
103         }
104
105         if (domData.getClass().getSimpleName().equals("ImmutableMapNode")) {
106             try {
107                 MapNode cn1 = (MapNode) cn.getChildByArg(id);
108                 for (MapEntryNode data1 : cn1.body()) {
109                     recurseMapEntryNodeData(notifContainer, data1, cn1, result, namePath);
110                 }
111             } catch (VerifyException ve) {
112                 LOG.debug("{} does not exist", id);
113             }
114         }
115
116         if (domData.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
117             try {
118                 LeafSetNode<?> cn1 = (LeafSetNode<?>) cn.getChildByArg(id);
119                 for (LeafSetEntryNode<?> data1 : cn1.body()) {
120                     recurseLeafSetEntryNodeData(data1, cn1, result, namePath);
121                 }
122             } catch (VerifyException ve) {
123                 LOG.debug("{} does not exist", id);
124             }
125         }
126
127         if (domData.getClass().getSimpleName().equals("ImmutableLeafNode")) {
128             recurseLeafNode(domData, result, namePath);
129         }
130     }
131
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();
136
137         if (data.getClass().getSimpleName().equals("ImmutableLeafSetEntryNode")) {
138             LOG.debug("{}={}", namePath, data.body());
139             result.put(namePath, data.body().toString());
140         }
141     }
142
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();
147
148         for (QName qn : ni.keySet()) {
149             namePath += "/" + ni.getValue(qn);
150         }
151
152         if (data.getClass().getSimpleName().equals("ImmutableMapEntryNode")) {
153             for (DataContainerChild data1 : data.body()) {
154                 if (data1.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
155                     try {
156                         LeafSetNode<?> cn2 = (LeafSetNode<?>) data.getChildByArg(data1.name());
157                         for (LeafSetEntryNode<?> data2 : cn2.body()) {
158                             recurseLeafSetEntryNodeData(data2, cn2, result, namePath);
159                         }
160                     } catch (VerifyException ve) {
161                         LOG.debug("{} does not exist", data1.name());
162                     }
163                 } else {
164                     recurseLeafNode(data1, result, namePath);
165                 }
166             }
167         }
168
169         if (data.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
170             try {
171                 LeafSetNode<?> cn2 = (LeafSetNode<?>) notifContainer.getChildByArg((NodeIdentifier) pa1);
172                 for (LeafSetEntryNode<?> data1 : cn2.body()) {
173                     recurseLeafSetEntryNodeData(data1, cn2, result, namePath);
174                 }
175             } catch (VerifyException ve) {
176                 LOG.debug("{} does not exist", pa1);
177             }
178         }
179
180         if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
181             LOG.debug("{}={}", namePath, data.body());
182             result.put(namePath, data.body().toString());
183         }
184     }
185
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();
190
191         if (data.getClass().getSimpleName().equals("ImmutableUnkeyedListEntryNode")) {
192             for (DataContainerChild data1 : data.body()) {
193                 recurseLeafNode(data1, result, namePath);
194             }
195         }
196
197         if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
198             LOG.debug("{}={}", namePath, data.body());
199             result.put(namePath, data.body().toString());
200         }
201     }
202
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());
210         }
211     }
212
213     public void recurseChoiceData(HashMap<String, String> result, DataContainerChild data, ChoiceNode cn,
214             String namePath) {
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());
221         }
222     }
223
224     public Instant getTime(@NonNull DOMNotification domNotification) {
225         @NonNull
226         Instant eventTime;
227         if (domNotification instanceof DOMEvent) {
228             eventTime = ((DOMEvent) domNotification).getEventInstant();
229             LOG.debug("Event time {}", eventTime);
230         } else {
231             eventTime = Instant.now();
232             LOG.debug("Defaulting to actual time of processing the notification - {}", eventTime);
233         }
234         return eventTime;
235     }
236 }