9bff14a0c4e4ad02d506e7198a48184edb5db6c9
[dcaegen2/services.git] /
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  slice-analysis-ms
4  *  ================================================================================
5  *  Copyright (C) 2022 Huawei Canada Limited.
6  *  Copyright (C) 2022 CTC, Inc.
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.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonParser;
29 import org.onap.slice.analysis.ms.models.Configuration;
30 import org.onap.slice.analysis.ms.service.ccvpn.BandwidthEvaluator;
31 import org.onap.slice.analysis.ms.service.ccvpn.Event;
32 import org.onap.slice.analysis.ms.service.ccvpn.SimpleEvent;
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
40 /**
41  * Handles AAI-EVENT from dmaap
42  */
43 @Component
44 public class AaiEventNotificationCallback implements NotificationCallback {
45
46     private static Logger logger = LoggerFactory.getLogger(AaiEventNotificationCallback.class);
47     private static final String EVENT_HEADER = "event-header";
48     private static final String ACTION = "action";
49     private static final String ENTITY_TYPE = "entity-type";
50     private static final String SOURCE_NAME = "source-name";
51     private static final String ENTITY = "entity";
52     private final JsonParser parser = new JsonParser();
53     private Configuration configuration;
54     private String aaiNotifTargetAction;
55     private String aaiNotifTargetSource;
56     private String aaiNotifTargetEntity;
57
58     @Autowired
59     BandwidthEvaluator bandwidthEvaluator;
60
61     @PostConstruct
62     public void init(){
63         configuration = Configuration.getInstance();
64         aaiNotifTargetAction = configuration.getAaiNotifTargetAction();
65         aaiNotifTargetSource = configuration.getAaiNotifTargetSource();
66         aaiNotifTargetEntity = configuration.getAaiNotifTargetEntity();
67     }
68
69     @Override
70     public void activateCallBack(String msg) {
71         handleNotification(msg);
72     }
73
74     private void handleNotification(String msg) {
75         JsonElement jsonElement = parser.parse(msg);
76         if (jsonElement.isJsonObject()){
77             //handle a single AAI_EVENT
78             logger.debug("Handle a single aai-event");
79             handleMsgJsonObject(jsonElement.getAsJsonObject());
80         } else if (jsonElement.isJsonArray()){
81             //handle a series of AAI_EVENT
82             logger.debug("Handle a series of aai-event");
83             JsonArray jsonArray = jsonElement.getAsJsonArray();
84             for (int i=0,e=jsonArray.size(); i<e; i++){
85                 if (jsonArray.get(i).isJsonPrimitive()){
86                     // Deal with a batch of event message
87                     handleNotification(jsonArray.get(i).getAsString());
88                 } else {
89                     handleMsgJsonObject(jsonArray.get(i).getAsJsonObject());
90                 }
91
92             }
93         }
94     }
95
96     private void handleMsgJsonObject(JsonObject jsonObject){
97         JsonObject header = jsonObject.get(EVENT_HEADER).getAsJsonObject();
98         if (!header.has(ACTION) || !header.get(ACTION).getAsString().equals(aaiNotifTargetAction)){
99             return;
100         }
101         if (!header.has(ENTITY_TYPE) || !header.get(ENTITY_TYPE).getAsString().equals(aaiNotifTargetEntity)){
102             return;
103         }
104         if (!header.has(SOURCE_NAME) || !header.get(SOURCE_NAME).getAsString().equals(aaiNotifTargetSource)){
105             return;
106         }
107         JsonObject entity = jsonObject.get(ENTITY).getAsJsonObject();
108         JsonObject body = getNestedJsonObject(entity, aaiNotifTargetEntity);
109         logger.info("AAI-EVENT entity object {}", body);
110         if (body == null){
111             return;
112         }
113         Event event = new SimpleEvent<>(SimpleEvent.Type.ONDEMAND_CHECK, body);
114         bandwidthEvaluator.post(event);
115     }
116
117     private JsonObject getNestedJsonObject(JsonObject obj, String target){
118         for (String k: obj.keySet()){
119             if (k.equals(target)){
120                 //Found it;
121                 return obj.getAsJsonArray(k).get(0).getAsJsonObject();
122             }
123             if (obj.get(k).isJsonObject()) {
124                 return getNestedJsonObject(obj.getAsJsonObject(k), target);
125             } else if (obj.get(k).isJsonArray()){
126                 JsonElement tmp = obj.getAsJsonArray(k).get(0);
127                 if (tmp.isJsonObject()){
128                     return getNestedJsonObject(tmp.getAsJsonObject(), target);
129                 }
130             }
131         }
132         return null;
133     }
134 }