1 /*******************************************************************************
 
   2  *  ============LICENSE_START=======================================================
 
   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
 
  12  *          http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21  *******************************************************************************/
 
  23 package org.onap.slice.analysis.ms.dmaap;
 
  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;
 
  38 import javax.annotation.PostConstruct;
 
  41  * Handles AAI-EVENT from dmaap
 
  44 public class AaiEventNotificationCallback implements NotificationCallback {
 
  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;
 
  59     BandwidthEvaluator bandwidthEvaluator;
 
  63         configuration = Configuration.getInstance();
 
  64         aaiNotifTargetAction = configuration.getAaiNotifTargetAction();
 
  65         aaiNotifTargetSource = configuration.getAaiNotifTargetSource();
 
  66         aaiNotifTargetEntity = configuration.getAaiNotifTargetEntity();
 
  70     public void activateCallBack(String msg) {
 
  71         handleNotification(msg);
 
  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());
 
  89                     handleMsgJsonObject(jsonArray.get(i).getAsJsonObject());
 
  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)){
 
 101         if (!header.has(ENTITY_TYPE) || !header.get(ENTITY_TYPE).getAsString().equals(aaiNotifTargetEntity)){
 
 104         if (!header.has(SOURCE_NAME) || !header.get(SOURCE_NAME).getAsString().equals(aaiNotifTargetSource)){
 
 107         JsonObject entity = jsonObject.get(ENTITY).getAsJsonObject();
 
 108         JsonObject body = getNestedJsonObject(entity, aaiNotifTargetEntity);
 
 109         logger.info("AAI-EVENT entity object {}", body);
 
 113         Event event = new SimpleEvent<>(SimpleEvent.Type.ONDEMAND_CHECK, body);
 
 114         bandwidthEvaluator.post(event);
 
 117     private JsonObject getNestedJsonObject(JsonObject obj, String target){
 
 118         for (String k: obj.keySet()){
 
 119             if (k.equals(target)){
 
 121                 return obj.getAsJsonArray(k).get(0).getAsJsonObject();
 
 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);