485c3d96b854b3156c052cc12dbe5dad788fc496
[dcaegen2/services.git] /
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  slice-analysis-ms
4  *  ================================================================================
5  *  Copyright (C) 2022 Huawei Canada Limited.
6  *  ==============================================================================
7  *     Licensed under the Apache License, Version 2.0 (the "License");
8  *     you may not use this file except in compliance with the License.
9  *     You may obtain a copy of the License at
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *     Unless required by applicable law or agreed to in writing, software
14  *     distributed under the License is distributed on an "AS IS" BASIS,
15  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *     See the License for the specific language governing permissions and
17  *     limitations under the License.
18  *     ============LICENSE_END=========================================================
19  *
20  *******************************************************************************/
21
22 package org.onap.slice.analysis.ms.dmaap;
23
24 import com.fasterxml.jackson.core.type.TypeReference;
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import org.onap.slice.analysis.ms.models.Configuration;
28 import org.onap.slice.analysis.ms.models.vesnotification.NotificationFields;
29
30 import org.onap.slice.analysis.ms.service.ccvpn.CCVPNPmDatastore;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
35
36 import javax.annotation.PostConstruct;
37 import java.io.IOException;
38
39 /**
40  * Handles Notification on dmaap for ves notification events
41  */
42 @Component
43 public class VesNotificationCallback implements NotificationCallback {
44
45     private Configuration configuration;
46     private String NOTIFICATIONFIELDS = "notificationFields";
47     private String EVENT = "event";
48     private String vesNotifChangeIdentifier;
49     private String vesNotfiChangeType;
50
51     @Autowired
52     CCVPNPmDatastore ccvpnPmDatastore;
53
54     private static Logger log = LoggerFactory.getLogger(VesNotificationCallback.class);
55
56     /**
57      * init ves callback; load configuration.
58      */
59     @PostConstruct
60     public void init(){
61         configuration = Configuration.getInstance();
62         ccvpnPmDatastore = new CCVPNPmDatastore();
63         vesNotifChangeIdentifier = configuration.getVesNotifChangeIdentifier();
64         vesNotfiChangeType = configuration.getVesNotifChangeType();
65     }
66
67     /**
68      * Triggers on handleNofitication method
69      * @param msg incoming message
70      */
71     @Override
72     public void activateCallBack(String msg) {
73         handleNotification(msg);
74     }
75
76     /**
77      * Parse Performance dmaap notification and save to DB
78      * @param msg incoming message
79      */
80     private void handleNotification(String msg) {
81         log.info("Message received from VES : {}" ,msg);
82         ObjectMapper obj = new ObjectMapper();
83         NotificationFields output = null;
84         String notifChangeIdentifier = "";
85         String notifChangeType = "";
86         String cllId = null;
87         String uniId = null;
88         String bw = null;
89         try {
90             JsonNode node = obj.readTree(msg);
91             JsonNode notificationNode = node.get(EVENT).get(NOTIFICATIONFIELDS);
92             output = obj.treeToValue(notificationNode, NotificationFields.class);
93
94             //Filter out target notification changeIdentifier and changeType
95             notifChangeIdentifier = output.getChangeIdentifier();
96             notifChangeType = output.getChangeType();
97             if (notifChangeType.equals(vesNotfiChangeType)
98                 && notifChangeIdentifier.equals(vesNotifChangeIdentifier)) {
99                 cllId = output.getArrayOfNamedHashMap().get(0).getHashMap().getCllId();
100                 uniId = output.getArrayOfNamedHashMap().get(0).getHashMap().getUniId();
101                 bw = output.getArrayOfNamedHashMap().get(0).getHashMap().getBandwidthValue();
102             }
103         }
104         catch (IOException e) {
105             log.error("Error converting VES msg to object, {}", e.getMessage());
106         }
107         if (cllId != null && uniId != null && bw != null){
108             log.info("Saving new CCVPN service usage data into ccvpnPmDatastore");
109             log.debug("new bandwidth data -- serviceId: {}, uniId: {}, bw: {}", cllId, uniId, bw);
110             ccvpnPmDatastore.addUsedBwToEndpoint(cllId, uniId, bw);
111         }
112
113     }
114
115 }