aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/devicemanager-o-ran-sc/o-ran/ru-fh/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/oran/notification/ORanDOMNotificationToXPath.java
blob: 697265f01a8003f854fadedafd07ef6dc7e8e323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk features
 * ================================================================================
 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 */
package org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.notification;

import com.google.common.base.VerifyException;
import java.time.Instant;
import java.util.Collection;
import java.util.HashMap;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.mdsal.dom.api.DOMEvent;
import org.opendaylight.mdsal.dom.api.DOMNotification;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ORanDOMNotificationToXPath {
    private static final Logger LOG = LoggerFactory.getLogger(ORanDOMNotificationToXPath.class);

    public HashMap<String, String> convertDomNotifToXPath(@NonNull DOMNotification domNotification) {
        @NonNull
        ContainerNode notifContainer = domNotification.getBody();
        HashMap<String, String> xPathData = new HashMap<String, String>();

        Collection<DataContainerChild> data = notifContainer.body();
        for (DataContainerChild data1 : data) {
            String namePath = "";
            recurseDOMData(notifContainer, data1, notifContainer, xPathData, namePath);
        }
        LOG.debug("XPath Data = {}", xPathData);
        return xPathData;

    }

    private void recurseDOMData(@NonNull ContainerNode notifContainer, DataContainerChild domData, DataContainerNode cn,
            HashMap<String, String> result, String namePath) {
        PathArgument pa1 = domData.getIdentifier();
        namePath += "/" + pa1.getNodeType().getLocalName();
        if (domData.getClass().getSimpleName().equals("ImmutableContainerNode")) {
            try {
                ContainerNode cn1 = (ContainerNode) cn.getChildByArg(pa1);
                for (DataContainerChild data1 : cn1.body()) {
                    recurseDOMData(notifContainer, data1, cn1, result, namePath);
                }
            } catch (VerifyException ve) {
                LOG.debug("{} does not exist", pa1);
            }
        }

        if (domData.getClass().getSimpleName().equals("ImmutableChoiceNode")) {
            try {
                ChoiceNode cn1 = (ChoiceNode) cn.getChildByArg(pa1);
                for (DataContainerChild data1 : cn1.body()) {
                    // recurseChoiceData(data1, cn1, namePath);
                    recurseDOMData(notifContainer, data1, cn1, result, namePath);
                }
            } catch (VerifyException ve) {
                LOG.debug("{} does not exist", pa1);
            }
        }

        if (domData.getClass().getSimpleName().equals("ImmutableUnkeyedListNode")) {
            try {
                UnkeyedListNode cn1 = (UnkeyedListNode) cn.getChildByArg(pa1);
                for (UnkeyedListEntryNode data1 : cn1.body()) {
                    recurseUnkeyedListEntryNodeData(data1, cn1, result, namePath);
                }
            } catch (VerifyException ve) {
                LOG.debug("{} does not exist", pa1);
            }
        }

        if (domData.getClass().getSimpleName().equals("ImmutableMapNode")) {
            try {
                MapNode cn1 = (MapNode) cn.getChildByArg(pa1);
                for (MapEntryNode data1 : cn1.body()) {
                    recurseMapEntryNodeData(notifContainer, data1, cn1, result, namePath);
                }
            } catch (VerifyException ve) {
                LOG.debug("{} does not exist", pa1);
            }
        }

        if (domData.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
            try {
                LeafSetNode<?> cn1 = (LeafSetNode<?>) cn.getChildByArg(pa1);
                for (LeafSetEntryNode<?> data1 : cn1.body()) {
                    recurseLeafSetEntryNodeData(data1, cn1, result, namePath);
                }
            } catch (VerifyException ve) {
                LOG.debug("{} does not exist", pa1);
            }
        }

        if (domData.getClass().getSimpleName().equals("ImmutableLeafNode")) {
            recurseLeafNode(domData, result, namePath);
        }
    }

    private void recurseLeafSetEntryNodeData(LeafSetEntryNode<?> data, LeafSetNode<?> cn1,
            HashMap<String, String> result, String namePath) {
        PathArgument pa1 = data.getIdentifier();
        namePath += "/" + pa1.getNodeType().getLocalName();

        if (data.getClass().getSimpleName().equals("ImmutableLeafSetEntryNode")) {
            LOG.debug("{}={}", namePath, data.body());
            result.put(namePath, data.body().toString());
        }
    }

    private void recurseMapEntryNodeData(@NonNull ContainerNode notifContainer, MapEntryNode data, MapNode cn1,
            HashMap<String, String> result, String namePath) {
        PathArgument pa1 = data.getIdentifier();
        NodeIdentifierWithPredicates ni = data.getIdentifier();

        for (QName qn : ni.keySet()) {
            namePath += "/" + ni.getValue(qn);
        }

        if (data.getClass().getSimpleName().equals("ImmutableMapEntryNode")) {
            for (DataContainerChild data1 : data.body()) {
                if (data1.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
                    try {
                        LeafSetNode<?> cn2 = (LeafSetNode<?>) data.getChildByArg(data1.getIdentifier());
                        for (LeafSetEntryNode<?> data2 : cn2.body()) {
                            recurseLeafSetEntryNodeData(data2, cn2, result, namePath);
                        }
                    } catch (VerifyException ve) {
                        LOG.debug("{} does not exist", data1.getIdentifier());
                    }
                } else {
                    recurseLeafNode(data1, result, namePath);
                }
            }
        }

        if (data.getClass().getSimpleName().equals("ImmutableLeafSetNode")) {
            try {
                LeafSetNode<?> cn2 = (LeafSetNode<?>) notifContainer.getChildByArg(pa1);
                for (LeafSetEntryNode<?> data1 : cn2.body()) {
                    recurseLeafSetEntryNodeData(data1, cn2, result, namePath);
                }
            } catch (VerifyException ve) {
                LOG.debug("{} does not exist", pa1);
            }
        }

        if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
            LOG.debug("{}={}", namePath, data.body());
            result.put(namePath, data.body().toString());
        }
    }

    private void recurseUnkeyedListEntryNodeData(UnkeyedListEntryNode data, UnkeyedListNode cn1,
            HashMap<String, String> result, String namePath) {
        PathArgument pa1 = data.getIdentifier();
        namePath += "/" + pa1.getNodeType().getLocalName();

        if (data.getClass().getSimpleName().equals("ImmutableUnkeyedListEntryNode")) {
            for (DataContainerChild data1 : data.body()) {
                recurseLeafNode(data1, result, namePath);
            }
        }

        if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
            LOG.debug("{}={}", namePath, data.body());
            result.put(namePath, data.body().toString());
        }
    }

    public void recurseLeafNode(DataContainerChild data, HashMap<String, String> result, String namePath) {
        PathArgument pa1 = data.getIdentifier();
        if (!(data.getClass().getSimpleName().equals("ImmutableAugmentationNode")))
            namePath += "/" + pa1.getNodeType().getLocalName();
        if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
            LOG.debug("{}={}", namePath, data.body());
            result.put(namePath, data.body().toString());
        }
    }

    public void recurseChoiceData(HashMap<String, String> result, DataContainerChild data, ChoiceNode cn,
            String namePath) {
        PathArgument pa1 = data.getIdentifier();
        namePath += "/" + pa1.getNodeType().getLocalName();
        // NodeIdentifier nodeId = new NodeIdentifier(pa1.getNodeType());
        if (data.getClass().getSimpleName().equals("ImmutableLeafNode")) {
            LOG.debug("{}={}", namePath, data.body());
            result.put(namePath, data.body().toString());
        }
    }

    public Instant getTime(@NonNull DOMNotification domNotification) {
        @NonNull
        Instant eventTime;
        if (domNotification instanceof DOMEvent) {
            eventTime = ((DOMEvent) domNotification).getEventInstant();
            LOG.debug("Event time {}", eventTime);
        } else {
            eventTime = Instant.now();
            LOG.debug("Defaulting to actual time of processing the notification - {}", eventTime);
        }
        return eventTime;
    }
}