aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/DMaaPCMVESMsgConsumer.java
blob: 8412e3730f6618234887175b1e2c563c90805c5c (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
/*
 * ============LICENSE_START========================================================================
 * ONAP : ccsdk feature sdnr wt mountpoint-registrar
 * =================================================================================================
 * Copyright (C) 2021 Samsung Electronics 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.impl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import java.time.Instant;
import java.time.ZoneId;
import java.util.Iterator;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DMaaPCMVESMsgConsumer extends DMaaPVESMsgConsumerImpl {

    private static final Logger LOG = LoggerFactory.getLogger(DMaaPCMVESMsgConsumer.class);

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

    @Override
    public void processMsg(String msg) throws InvalidMessageException, JsonProcessingException {
        LOG.debug("Processing CM message {}", msg);
        JsonNode rootNode = convertMessageToJsonNode(msg);
        try {

            String cmNodeId = rootNode.at("/event/commonEventHeader/reportingEntityName").textValue();
            String notificationType = rootNode.at("/event/stndDefinedFields/data/notificationType").textValue();

            if (notificationType.equalsIgnoreCase("notifyMOIChanges")) {
                LOG.info("Read CM message from DMaaP topic that is moiChanges type with id {}", cmNodeId);
                processMoiChanges(rootNode);
            } else if (notificationType.equalsIgnoreCase("notifyMOICreation")) {
                LOG.info("Read CM message from DMaaP topic that is moiCreation type with id {}", cmNodeId);
                sendCMNotification(preparePayloadMapFromMoi(rootNode, "/event/stndDefinedFields/data/attributeList"));
            } else if (notificationType.equalsIgnoreCase("notifyMOIDeletion")) {
                LOG.info("Read CM message from DMaaP topic that is moiDeletion type with id {}", cmNodeId);
                sendCMNotification(preparePayloadMapFromMoi(rootNode,"/event/stndDefinedFields/data/attributeList"));
            } else if (notificationType.equalsIgnoreCase("notifyMOIAttributeValueChanges")) {
                LOG.info("Read CM message from DMaaP topic that is moiAttributeValueChanges type with id {}", cmNodeId);
                sendCMNotification(preparePayloadMapFromMoi(rootNode,"/event/stndDefinedFields/data/attributeListValueChanges"));
            } else {
                LOG.warn("Message is invalid, sending aborted, wrong CM notification type {}", notificationType);
                throw new InvalidMessageException();
            }

        } catch (NullPointerException e) {
            LOG.warn("Message is invalid, sending aborted, processing stopped because one of fields is missing");
            throw new InvalidMessageException("Missing field");
        }
    }

    private CMBasicHeaderFieldsNotification prepareCMCommonHeaderFields(JsonNode rootNode) {
        return CMBasicHeaderFieldsNotification.builder()
            .withCMNodeId(rootNode.at("/event/commonEventHeader/reportingEntityName").textValue())
            .withCMSequence(rootNode.at("/event/commonEventHeader/sequence").toString())
            .withCMOccurrenceTime(Instant
                .ofEpochMilli(
                    rootNode.at("/event/commonEventHeader/startEpochMicrosec").longValue() / 1000)
                .atZone(ZoneId.of("Z")).toString())
            .withSourceId(rootNode.at("/event/commonEventHeader/sourceId").textValue())
            .withNotificationType(rootNode.at("/event/stndDefinedFields/data/notificationType").textValue())
            .build();
    }

    private void processMoiChanges(JsonNode rootNode) {
        Iterator<JsonNode> nodes = rootNode
            .at("/event/stndDefinedFields/data/moiChanges")
            .elements();
        while (nodes.hasNext()) {
            sendCMNotification(preparePayloadMapFromMoiChangesArray(rootNode, nodes));
        }
    }

    public Map<String, String> preparePayloadMapFromMoiChangesArray(JsonNode rootNode, Iterator<JsonNode> nodes) {
        JsonNode slaidNode = nodes.next();
        return CMNotificationClient.createCMNotificationPayloadMap(
            CMNotification.builder()
                .withCMBasicHeaderFieldsNotification(
                    prepareCMCommonHeaderFields(rootNode))
                .withCMNotificationId(slaidNode.get("notificationId").toString())
                .withCMSourceIndicator(slaidNode.get("sourceIndicator").textValue())
                .withCMPath(slaidNode.get("path").textValue())
                .withCMOperation(slaidNode.get("operation").textValue())
                .withCMValue(slaidNode.get("value").toString()
                    .replace("\"", ""))
                .build());
    }

    public Map<String, String> preparePayloadMapFromMoi(JsonNode rootNode, String cmValueKey){
        return CMNotificationClient.createCMNotificationPayloadMap(
            CMNotification.builder()
                .withCMBasicHeaderFieldsNotification(
                    prepareCMCommonHeaderFields(rootNode))
                .withCMSourceIndicator(rootNode.at("/event/stndDefinedFields/data/sourceIndicator").textValue())
                .withCMValue(rootNode.at(cmValueKey).toString()
                    .replace("\"", ""))
                .build());
    }

    private void sendCMNotification(Map<String, String> payloadMapMessage) {
        CMNotificationClient cmClient = setRESTConfAuthorization();
        String message = cmClient.prepareMessageFromPayloadMap(payloadMapMessage);
        cmClient.sendNotification(message);
    }


    private CMNotificationClient setRESTConfAuthorization() {
        String sdnrUser = getSDNRUser();
        String sdnrPasswd = getSDNRPasswd();

        CMNotificationClient cmClient = new CMNotificationClient(getBaseUrl());
        LOG.debug("Setting RESTConf Authorization values - {} : {}", sdnrUser, sdnrPasswd);
        cmClient.setAuthorization(sdnrUser, sdnrPasswd);
        return cmClient;
    }
}