1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 *******************************************************************************/
22 package org.onap.slice.analysis.ms.dmaap;
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;
37 import javax.annotation.PostConstruct;
40 * Handles AAI-EVENT from dmaap
43 public class AaiEventNotificationCallback implements NotificationCallback {
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;
58 BandwidthEvaluator bandwidthEvaluator;
62 configuration = Configuration.getInstance();
63 aaiNotifTargetAction = configuration.getAaiNotifTargetAction();
64 aaiNotifTargetSource = configuration.getAaiNotifTargetSource();
65 aaiNotifTargetEntity = configuration.getAaiNotifTargetEntity();
69 public void activateCallBack(String msg) {
70 handleNotification(msg);
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());
88 handleMsgJsonObject(jsonArray.get(i).getAsJsonObject());
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)){
100 if (!header.has(ENTITY_TYPE) || !header.get(ENTITY_TYPE).getAsString().equals(aaiNotifTargetEntity)){
103 if (!header.has(SOURCE_NAME) || !header.get(SOURCE_NAME).getAsString().equals(aaiNotifTargetSource)){
106 JsonObject entity = jsonObject.get(ENTITY).getAsJsonObject();
107 JsonObject body = getNestedJsonObject(entity, aaiNotifTargetEntity);
108 logger.debug("AAI-EVENT entity object {}", body);
112 Event event = new SimpleEvent<>(SimpleEvent.Type.ONDEMAND_CHECK, body);
113 bandwidthEvaluator.post(event);
116 private JsonObject getNestedJsonObject(JsonObject obj, String target){
117 for (String k: obj.keySet()){
118 if (k.equals(target)){
120 return obj.getAsJsonArray(k).get(0).getAsJsonObject();
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);