aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/vesdomain/stnddefined/StrimziKafkaStndDefinedFaultVESMsgConsumer.java
blob: 625537c9075ba3c304954dd0dc12f4fcf0914518 (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
/*
 * ============LICENSE_START========================================================================
 * ONAP : ccsdk feature sdnr wt mountpoint-registrar
 * =================================================================================================
 * Copyright (C) 2022 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.mountpointregistrar.vesdomain.stnddefined;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import java.time.Instant;
import java.time.ZoneId;
import java.util.Map;
import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.config.GeneralConfig;
import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.InvalidMessageException;
import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.StrimziKafkaVESMsgConsumerImpl;
import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.vesdomain.fault.FaultNotificationClient;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StrimziKafkaStndDefinedFaultVESMsgConsumer extends StrimziKafkaVESMsgConsumerImpl {

    private static final Logger LOG = LoggerFactory.getLogger(StrimziKafkaStndDefinedFaultVESMsgConsumer.class);
    Map<String, String> payloadMapMessage = null;
    String faultNodeId;
    String notificationType;

    public StrimziKafkaStndDefinedFaultVESMsgConsumer(GeneralConfig generalConfig) {
        super(generalConfig);
        LOG.info("StrimziKafkaStndDefinedFaultVESMsgConsumer started successfully");
    }

    /*
     * Supports processing of notifyNewAlarm and notifyClearedAlarm messages ONLY
     */
    @Override
    public void processMsg(String msg) throws InvalidMessageException, JsonProcessingException {
        LOG.debug("Processing StndDefined Fault message {}", msg);
        JsonNode rootNode = convertMessageToJsonNode(msg);
        try {

            faultNodeId = rootNode.at("/event/commonEventHeader/sourceName").textValue();
            notificationType = rootNode.at("/event/stndDefinedFields/data/notificationType").textValue();

            if (notificationType.equalsIgnoreCase("notifyNewAlarm")) {
                LOG.info("Read stndDefined Fault message of type - {} with id {} from Kafka topic", notificationType,
                        faultNodeId);
                processNewAlarm(rootNode);
            } else if (notificationType.equalsIgnoreCase("notifyClearedAlarm")) {
                LOG.info("Read stdnDefined Fault message of type - {} with id {} from Kafka topic", notificationType,
                        faultNodeId);
                processClearedAlarm(rootNode);
            } else {
                LOG.warn(
                        "Read stdnDefined Fault message of type - {} with id {} from Kafka topic. No suitable implementation for processing this message",
                        notificationType, faultNodeId);
                throw new InvalidMessageException();
            }
            // Send Fault Notification
            String baseUrl = getBaseUrl();
            String sdnrUser = getSDNRUser();
            String sdnrPasswd = getSDNRPasswd();

            FaultNotificationClient faultClient = new FaultNotificationClient(baseUrl);
            LOG.debug("Setting RESTConf Authorization values - {} : {}", sdnrUser, sdnrPasswd);
            faultClient.setAuthorization(sdnrUser, sdnrPasswd);
            String message = faultClient.prepareMessageFromPayloadMap(payloadMapMessage);
            faultClient.sendNotification(message);
        } catch (NullPointerException e) {
            LOG.warn("Message is invalid, sending aborted, processing stopped because one of fields is missing");
            throw new InvalidMessageException("Missing field");
        }

    }

    private void processClearedAlarm(JsonNode rootNode) {
        String faultOccurrenceTime =
                Instant.ofEpochMilli(rootNode.at("/event/commonEventHeader/startEpochMicrosec").longValue() / 1000)
                        .atZone(ZoneId.of("Z")).toString();
        int faultSequence = rootNode.at("/event/commonEventHeader/sequence").intValue();
        String faultObjectId = rootNode.at("/event/stndDefinedFields/data/alarmId").textValue();
        String faultReason = rootNode.at("/event/stndDefinedFields/data/probableCause").textValue();
        String faultSeverity = SeverityType.NonAlarmed.toString();

        payloadMapMessage = FaultNotificationClient.createFaultNotificationPayloadMap(faultNodeId,
                Integer.toString(faultSequence), faultOccurrenceTime, faultObjectId, faultReason, faultSeverity);

    }

    private void processNewAlarm(JsonNode rootNode) {
        String faultOccurrenceTime =
                Instant.ofEpochMilli(rootNode.at("/event/commonEventHeader/startEpochMicrosec").longValue() / 1000)
                        .atZone(ZoneId.of("Z")).toString();
        int faultSequence = rootNode.at("/event/commonEventHeader/sequence").intValue();
        String faultObjectId = rootNode.at("/event/stndDefinedFields/data/alarmId").textValue();
        String faultReason = rootNode.at("/event/stndDefinedFields/data/probableCause").textValue();
        String faultSeverity =
                getSDNRSeverityType(rootNode.at("/event/stndDefinedFields/data/perceivedSeverity").textValue());

        payloadMapMessage = FaultNotificationClient.createFaultNotificationPayloadMap(faultNodeId,
                Integer.toString(faultSequence), faultOccurrenceTime, faultObjectId, faultReason, faultSeverity);

    }

    /*
     * 3GPP Definition PerceivedSeverity: type: string enum: - INDETERMINATE -
     * CRITICAL - MAJOR - MINOR - WARNING - CLEARED
     *
     */
    private String getSDNRSeverityType(String faultSeverity) {
        if (faultSeverity.equalsIgnoreCase("critical")) {
            faultSeverity = SeverityType.Critical.toString();
        } else if (faultSeverity.equalsIgnoreCase("major")) {
            faultSeverity = SeverityType.Major.toString();
        } else if (faultSeverity.equalsIgnoreCase("minor")) {
            faultSeverity = SeverityType.Minor.toString();
        } else if (faultSeverity.equalsIgnoreCase("warning") || faultSeverity.equalsIgnoreCase("indeterminate")) {
            faultSeverity = SeverityType.Warning.toString();
        } else if (faultSeverity.equalsIgnoreCase("cleared")) {
            faultSeverity = SeverityType.NonAlarmed.toString();
        } else {
            faultSeverity = SeverityType.NonAlarmed.toString();
        }
        return faultSeverity;
    }

}