584da7b7e6a744c09f6fd6fb686be6fe007576c0
[dcaegen2/services.git] / components / slice-analysis-ms / src / main / java / org / onap / slice / analysis / ms / dmaap / VesNotificationCallback.java
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         vesNotifChangeIdentifier = configuration.getVesNotifChangeIdentifier();
63         vesNotfiChangeType = configuration.getVesNotifChangeType();
64     }
65
66     /**
67      * Triggers on handleNofitication method
68      * @param msg incoming message
69      */
70     @Override
71     public void activateCallBack(String msg) {
72         handleNotification(msg);
73     }
74
75     /**
76      * Parse Performance dmaap notification and save to DB 
77      * @param msg incoming message
78      */
79     private void handleNotification(String msg) {
80         log.info("Message received from VES : {}" ,msg);
81         ObjectMapper obj = new ObjectMapper();
82         NotificationFields output = null;
83         String notifChangeIdentifier = "";
84         String notifChangeType = "";
85         String cllId = null;
86         String uniId = null;
87         String bw = null;
88         try {
89             JsonNode node = obj.readTree(msg);
90             JsonNode notificationNode = node.get(EVENT).get(NOTIFICATIONFIELDS);
91             output = obj.treeToValue(notificationNode, NotificationFields.class);
92
93             //Filter out target notification changeIdentifier and changeType
94             notifChangeIdentifier = output.getChangeIdentifier();
95             notifChangeType = output.getChangeType();
96             if (notifChangeType.equals(vesNotfiChangeType)
97             && notifChangeIdentifier.equals(vesNotifChangeIdentifier)) {
98                 cllId = output.getArrayOfNamedHashMap().get(0).getHashMap().getCllId();
99                 uniId = output.getArrayOfNamedHashMap().get(0).getHashMap().getUniId();
100                 bw = output.getArrayOfNamedHashMap().get(0).getHashMap().getBandwidthValue();
101             }
102         }
103         catch (IOException e) {
104             log.error("Error converting VES msg to object, {}", e.getMessage());
105         }
106         if (cllId != null && uniId != null && bw != null){
107             log.info("Saving new CCVPN service usage data into ccvpnPmDatastore");
108             log.debug("new bandwidth data -- serviceId: {}, uniId: {}, bw: {}", cllId, uniId, bw);
109             ccvpnPmDatastore.addUsedBwToEndpoint(cllId, uniId, bw);
110         }
111
112     }
113
114 }