dc2cd77536e17c3c871bf1055f3ef1c0a30c8a4f
[dcaegen2/services.git] / components / slice-analysis-ms / src / main / java / org / onap / slice / analysis / ms / dmaap / AaiEventNotificationCallback.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.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonParser;
28 import org.onap.slice.analysis.ms.models.Configuration;
29 import org.onap.slice.analysis.ms.service.ccvpn.BandwidthEvaluator;
30 import org.onap.slice.analysis.ms.service.ccvpn.Event;
31 import org.onap.slice.analysis.ms.service.ccvpn.SimpleEvent;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Component;
34
35 import javax.annotation.PostConstruct;
36
37 /**
38  * Handles AAI-EVENT from dmaap
39  */
40 @Component
41 public class AaiEventNotificationCallback implements NotificationCallback {
42
43     private static final String EVENT_HEADER = "event-header";
44     private static final String ACTION = "action";
45     private static final String ENTITY_TYPE = "entity-type";
46     private static final String SOURCE_NAME = "source-name";
47     private static final String ENTITY = "entity";
48     private final JsonParser parser = new JsonParser();
49     private Configuration configuration;
50     private String aaiNotifTargetAction;
51     private String aaiNotifTargetSource;
52     private String aaiNotifTargetEntity;
53
54     @Autowired
55     BandwidthEvaluator bandwidthEvaluator;
56
57     @PostConstruct
58     public void init(){
59         configuration = Configuration.getInstance();
60         aaiNotifTargetAction = configuration.getAaiNotifTargetAction();
61         aaiNotifTargetSource = configuration.getAaiNotifTargetSource();
62         aaiNotifTargetEntity = configuration.getAaiNotifTargetEntity();
63     }
64
65     @Override
66     public void activateCallBack(String msg) {
67         handleNotification(msg);
68     }
69
70     private void handleNotification(String msg) {
71         JsonElement jsonElement = parser.parse(msg);
72         if (jsonElement.isJsonObject()){
73             //handle a single AAI_EVENT
74             handleMsgJsonObject(jsonElement.getAsJsonObject());
75         } else if (jsonElement.isJsonArray()){
76             //handle a series of AAI_EVENT
77             JsonArray jsonArray = jsonElement.getAsJsonArray();
78             for (int i=0,e=jsonArray.size(); i<e; i++){
79                 handleMsgJsonObject(jsonArray.get(i).getAsJsonObject());
80             }
81         }
82     }
83
84     private void handleMsgJsonObject(JsonObject jsonObject){
85         JsonObject header = jsonObject.get(EVENT_HEADER).getAsJsonObject();
86         if (header.has(ACTION) && header.get(ACTION).getAsString().equals(aaiNotifTargetAction)) {
87             if (header.has(ENTITY_TYPE) && header.get(ENTITY_TYPE).getAsString().equals(aaiNotifTargetEntity)){
88                 if (header.has(SOURCE_NAME) && header.get(SOURCE_NAME).getAsString().equals(aaiNotifTargetSource)) {
89                     JsonObject body = jsonObject.get(ENTITY).getAsJsonObject();
90                     Event event = new SimpleEvent<>(SimpleEvent.Type.ONDEMAND_CHECK, body);
91                     bandwidthEvaluator.post(event);
92                 }
93             }
94         }
95     }
96 }