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