2  * ============LICENSE_START=======================================================
 
   3  * BBS-RELOCATION-CPE-AUTHENTICATION-HANDLER
 
   4  * ================================================================================
 
   5  * Copyright (C) 2019 NOKIA Intellectual Property. All rights reserved.
 
   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=========================================================
 
  21 package org.onap.bbs.event.processor.utilities;
 
  23 import static org.onap.bbs.event.processor.utilities.CommonEventFields.COMMON_EVENT_HEADER;
 
  24 import static org.onap.bbs.event.processor.utilities.CommonEventFields.COMMON_FORMAT;
 
  25 import static org.onap.bbs.event.processor.utilities.CommonEventFields.CORRELATION_ID;
 
  26 import static org.onap.bbs.event.processor.utilities.CommonEventFields.EVENT;
 
  27 import static org.onap.bbs.event.processor.utilities.CommonEventFields.SOURCE_NAME;
 
  28 import static org.onap.bbs.event.processor.utilities.CpeAuthenticationEventFields.ADDITIONAL_FIELDS;
 
  29 import static org.onap.bbs.event.processor.utilities.CpeAuthenticationEventFields.NEW_AUTHENTICATION_STATE;
 
  30 import static org.onap.bbs.event.processor.utilities.CpeAuthenticationEventFields.OLD_AUTHENTICATION_STATE;
 
  31 import static org.onap.bbs.event.processor.utilities.CpeAuthenticationEventFields.RGW_MAC_ADDRESS;
 
  32 import static org.onap.bbs.event.processor.utilities.CpeAuthenticationEventFields.STATE_CHANGE_FIELDS;
 
  33 import static org.onap.bbs.event.processor.utilities.CpeAuthenticationEventFields.STATE_INTERFACE;
 
  34 import static org.onap.bbs.event.processor.utilities.CpeAuthenticationEventFields.SW_VERSION;
 
  36 import com.google.gson.Gson;
 
  37 import com.google.gson.JsonElement;
 
  38 import com.google.gson.JsonObject;
 
  39 import com.google.gson.JsonParser;
 
  41 import java.util.Optional;
 
  42 import java.util.stream.StreamSupport;
 
  44 import org.onap.bbs.event.processor.exceptions.DmaapException;
 
  45 import org.onap.bbs.event.processor.model.CpeAuthenticationConsumerDmaapModel;
 
  46 import org.onap.bbs.event.processor.model.ImmutableCpeAuthenticationConsumerDmaapModel;
 
  47 import org.slf4j.Logger;
 
  48 import org.slf4j.LoggerFactory;
 
  49 import org.springframework.util.StringUtils;
 
  51 import reactor.core.publisher.Flux;
 
  52 import reactor.core.publisher.Mono;
 
  54 public class CpeAuthenticationDmaapConsumerJsonParser {
 
  56     private static final Logger LOGGER = LoggerFactory.getLogger(CpeAuthenticationDmaapConsumerJsonParser.class);
 
  57     private static final Gson gson = new Gson();
 
  59     private static final String CPE_AUTHENTICATION_DUMPING_TEMPLATE = "%n{"
 
  60             + "\"" + CORRELATION_ID + COMMON_FORMAT + ","
 
  61             + "\"" + OLD_AUTHENTICATION_STATE + COMMON_FORMAT + ","
 
  62             + "\"" + NEW_AUTHENTICATION_STATE + COMMON_FORMAT + ","
 
  63             + "\"" + STATE_INTERFACE + COMMON_FORMAT + ","
 
  64             + "\"" + RGW_MAC_ADDRESS + COMMON_FORMAT + ","
 
  65             + "\"" + SW_VERSION + COMMON_FORMAT
 
  68     private String pnfSourceName;
 
  70     private String oldAuthenticationStatus;
 
  71     private String newAuthenticationStatus;
 
  72     private String stateInterface;
 
  73     private String rgwMacAddress;
 
  74     private String swVersion;
 
  77      * Translates a response from DMaaP to a reactive {@link CpeAuthenticationConsumerDmaapModel} model.
 
  78      * @param dmaapResponse Response from DMaaP
 
  79      * @return CPE Authentication Consumer DMaaP reactive model
 
  81     public Flux<CpeAuthenticationConsumerDmaapModel> extractModelFromDmaap(Mono<JsonElement> dmaapResponse) {
 
  83                 .flatMapMany(this::createTargetFlux);
 
  86     private Flux<CpeAuthenticationConsumerDmaapModel> createTargetFlux(JsonElement jsonElement) {
 
  87         if (jsonElement.isJsonObject()) {
 
  88             return doCreateTargetFlux(Flux.defer(() -> Flux.just(jsonElement.getAsJsonObject())));
 
  90         return doCreateTargetFlux(
 
  91                 Flux.defer(() -> Flux.fromStream(StreamSupport.stream(jsonElement.getAsJsonArray().spliterator(), false)
 
  92                         .map(jsonElementFromArray -> getJsonObjectFromAnArray(jsonElementFromArray)
 
  93                                 .orElseGet(JsonObject::new)))));
 
  96     private Flux<CpeAuthenticationConsumerDmaapModel> doCreateTargetFlux(Flux<JsonObject> jsonObject) {
 
  98                 .flatMap(this::transform)
 
  99                 .onErrorResume(exception -> exception instanceof DmaapException, e -> Mono.empty());
 
 102     private Mono<CpeAuthenticationConsumerDmaapModel> transform(JsonObject dmaapResponseJsonObject) {
 
 104         LOGGER.trace("Event from DMaaP to be parsed: \n{}", gson.toJson(dmaapResponseJsonObject));
 
 106         if (!containsProperHeaders(dmaapResponseJsonObject)) {
 
 107             LOGGER.warn("Incorrect CPE Authentication JSON event - missing headers");
 
 111         JsonObject commonEventHeader = dmaapResponseJsonObject.getAsJsonObject(EVENT)
 
 112                 .getAsJsonObject(COMMON_EVENT_HEADER);
 
 113         JsonObject stateChangeFields = dmaapResponseJsonObject.getAsJsonObject(EVENT)
 
 114                 .getAsJsonObject(STATE_CHANGE_FIELDS);
 
 116         pnfSourceName = getValueFromJson(commonEventHeader, SOURCE_NAME);
 
 118         oldAuthenticationStatus = getValueFromJson(stateChangeFields, OLD_AUTHENTICATION_STATE);
 
 119         newAuthenticationStatus = getValueFromJson(stateChangeFields, NEW_AUTHENTICATION_STATE);
 
 120         stateInterface = getValueFromJson(stateChangeFields, STATE_INTERFACE);
 
 122         if (stateChangeFields.has(ADDITIONAL_FIELDS)) {
 
 123             JsonObject additionalFields = stateChangeFields.getAsJsonObject(ADDITIONAL_FIELDS);
 
 124             rgwMacAddress = getValueFromJson(additionalFields, RGW_MAC_ADDRESS);
 
 125             swVersion = getValueFromJson(additionalFields, SW_VERSION);
 
 128         if (StringUtils.isEmpty(pnfSourceName)
 
 129                 || authenticationStatusMissing(oldAuthenticationStatus)
 
 130                 || authenticationStatusMissing(newAuthenticationStatus)) {
 
 131             String incorrectEvent = dumpJsonData();
 
 132             LOGGER.warn("Incorrect CPE Authentication JSON event: {}", incorrectEvent);
 
 136         return Mono.just(ImmutableCpeAuthenticationConsumerDmaapModel.builder()
 
 137                 .correlationId(pnfSourceName)
 
 138                 .oldAuthenticationState(oldAuthenticationStatus)
 
 139                 .newAuthenticationState(newAuthenticationStatus)
 
 140                 .stateInterface(stateInterface)
 
 141                 .rgwMacAddress(rgwMacAddress)
 
 142                 .swVersion(swVersion)
 
 146     private boolean authenticationStatusMissing(String authenticationStatus) {
 
 147         return StringUtils.isEmpty(authenticationStatus);
 
 150     private boolean containsProperHeaders(JsonObject jsonObject) {
 
 151         return jsonObject.has(EVENT) && jsonObject.getAsJsonObject(EVENT).has(COMMON_EVENT_HEADER)
 
 152                 && jsonObject.getAsJsonObject(EVENT).has(STATE_CHANGE_FIELDS);
 
 155     private String dumpJsonData() {
 
 156         return String.format(CPE_AUTHENTICATION_DUMPING_TEMPLATE,
 
 158                 oldAuthenticationStatus,
 
 159                 newAuthenticationStatus,
 
 166     Optional<JsonObject> getJsonObjectFromAnArray(JsonElement element) {
 
 167         JsonParser jsonParser = new JsonParser();
 
 168         return element.isJsonPrimitive() ? Optional.of(jsonParser.parse(element.getAsString()).getAsJsonObject())
 
 169                 : Optional.of(jsonParser.parse(element.toString()).getAsJsonObject());
 
 172     private String getValueFromJson(JsonObject jsonObject, String jsonKey) {
 
 173         return jsonObject.has(jsonKey) ? jsonObject.get(jsonKey).getAsString() : "";