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/impl/dom/ORanDOMChangeNotificationListener.java
blob: f8e8b6da2bb2b6d2dc51337f0e29d1132ab0d356 (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
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk features
 * ================================================================================
 * Copyright (C) 2021 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.impl.dom;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.time.Instant;
import org.eclipse.jdt.annotation.NonNull;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl;
import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESCommonEventHeaderPOJO;
import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESNotificationFieldsPOJO;
import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
import org.opendaylight.mdsal.dom.api.DOMEvent;
import org.opendaylight.mdsal.dom.api.DOMNotification;
import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogEntity;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SourceType;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ORanDOMChangeNotificationListener implements DOMNotificationListener {

    private static final Logger log = LoggerFactory.getLogger(ORanDOMChangeNotificationListener.class);

    private final NetconfDomAccessor netconfDomAccessor;
    private final DataProvider databaseService;
    private @NonNull VESCollectorService vesCollectorService;
    private final DOMNotificationToXPath domNotificationXPath;
    private ORanDOMNotifToVESEventAssembly mapper = null;
    private static int sequenceNo = 0;

    public ORanDOMChangeNotificationListener(NetconfDomAccessor netconfDomAccessor,
            @NonNull VESCollectorService vesCollectorService, DataProvider databaseService) {
        this.netconfDomAccessor = netconfDomAccessor;
        this.databaseService = databaseService;
        this.vesCollectorService = vesCollectorService;
        domNotificationXPath = new DOMNotificationToXPath();
    }

    @Override
    public void onNotification(@NonNull DOMNotification domNotification) {
        if (domNotification.getType()
                .equals(Absolute.of(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CONFIG_CHANGE))) {
            handleNetconfConfigChange(domNotification);
        }
    }

    private void handleNetconfConfigChange(@NonNull DOMNotification domNotification) {
        DateAndTime eventTime;
        Instant notificationEventTime = null;
        if (domNotification instanceof DOMEvent) {
            notificationEventTime = ((DOMEvent) domNotification).getEventInstant();
            eventTime = NetconfTimeStampImpl.getConverter().getTimeStamp(notificationEventTime.toString());
        } else {
            eventTime = NetconfTimeStampImpl.getConverter().getTimeStamp();
        }

        ContainerNode cn = domNotification.getBody();

        // Process the changed-by child
        //		ContainerNode changedByContainerNode = (ContainerNode) cn
        //				.childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_CHANGEDBY));
        //		ChoiceNode serverOrUserIDVal = (ChoiceNode) changedByContainerNode
        //				.childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_SERVERORUSER));
        //		@SuppressWarnings("unused")
        //		String userIDValue = serverOrUserIDVal
        //				.childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_USERNAME)).body()
        //				.toString();
        //		@SuppressWarnings("unused")
        //		Integer sessionIDVal = Integer.valueOf(serverOrUserIDVal
        //				.childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_SESSIONID)).body()
        //				.toString());
        //
        //		// Process the datastore child
        //		@SuppressWarnings("unused")
        //		String datastoreValue = cn
        //				.childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_DATASTORE)).body()
        //				.toString();

        // Process the edit child
        UnkeyedListNode editList = (UnkeyedListNode) cn
                .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_EDITNODE));
        if (editList != null) {
            for (int listCnt = 0; listCnt < editList.size(); listCnt++) {
                String operationValue = editList.childAt(listCnt)
                        .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_OPERATION))
                        .body().toString();
                String targetValue = editList.childAt(listCnt)
                        .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_TARGET))
                        .body().toString();

                EventlogEntity eventLogEntity1 = new EventlogBuilder()
                        .setNodeId(netconfDomAccessor.getNodeId().getValue()).setCounter(sequenceNo++)
                        .setTimestamp(eventTime).setObjectId(targetValue).setAttributeName("N.A")
                        .setSourceType(SourceType.Netconf).setNewValue(String.valueOf(operationValue)).build();
                databaseService.writeEventLog(eventLogEntity1);
            }
        }

        if (vesCollectorService.getConfig().isVESCollectorEnabled()) {
            if (mapper == null) {
                this.mapper = new ORanDOMNotifToVESEventAssembly(netconfDomAccessor, vesCollectorService);
            }
            VESCommonEventHeaderPOJO header =
                    mapper.createVESCommonEventHeader(domNotificationXPath.getTime(domNotification),
                            ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CONFIG_CHANGE.getLocalName(),
                            sequenceNo);
            VESNotificationFieldsPOJO body =
                    mapper.createVESNotificationFields(domNotificationXPath.convertDomNotifToXPath(domNotification),
                            ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CONFIG_CHANGE.getLocalName());
            log.debug("domNotification in XPath format = {}",
                    domNotificationXPath.convertDomNotifToXPath(domNotification));
            try {
                vesCollectorService.publishVESMessage(vesCollectorService.generateVESEvent(header, body));
            } catch (JsonProcessingException e) {
                log.warn("Exception while generating JSON object ", e);

            }
        }

    }
}