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