3cff4e65b8d711f72289a3b234d1cd8e608b84d0
[dcaegen2/services.git] /
1 /*
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
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 package org.onap.bbs.event.processor.utilities;
22
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;
35
36 import com.google.gson.Gson;
37 import com.google.gson.JsonElement;
38 import com.google.gson.JsonObject;
39 import com.google.gson.JsonParser;
40
41 import java.util.Optional;
42 import java.util.stream.StreamSupport;
43
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;
50
51 import reactor.core.publisher.Flux;
52 import reactor.core.publisher.Mono;
53
54 public class CpeAuthenticationDmaapConsumerJsonParser {
55
56     private static final Logger LOGGER = LoggerFactory.getLogger(CpeAuthenticationDmaapConsumerJsonParser.class);
57     private static final Gson gson = new Gson();
58
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
66             + "}";
67
68     private String pnfSourceName;
69
70     private String oldAuthenticationStatus;
71     private String newAuthenticationStatus;
72     private String stateInterface;
73     private String rgwMacAddress;
74     private String swVersion;
75
76     /**
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
80      */
81     public Flux<CpeAuthenticationConsumerDmaapModel> extractModelFromDmaap(Mono<JsonElement> dmaapResponse) {
82         return dmaapResponse
83                 .flatMapMany(this::createTargetFlux);
84     }
85
86     private Flux<CpeAuthenticationConsumerDmaapModel> createTargetFlux(JsonElement jsonElement) {
87         if (jsonElement.isJsonObject()) {
88             return doCreateTargetFlux(Flux.defer(() -> Flux.just(jsonElement.getAsJsonObject())));
89         }
90         return doCreateTargetFlux(
91                 Flux.defer(() -> Flux.fromStream(StreamSupport.stream(jsonElement.getAsJsonArray().spliterator(), false)
92                         .map(jsonElementFromArray -> getJsonObjectFromAnArray(jsonElementFromArray)
93                                 .orElseGet(JsonObject::new)))));
94     }
95
96     private Flux<CpeAuthenticationConsumerDmaapModel> doCreateTargetFlux(Flux<JsonObject> jsonObject) {
97         return jsonObject
98                 .flatMap(this::transform)
99                 .onErrorResume(exception -> exception instanceof DmaapException, e -> Mono.empty());
100     }
101
102     private Mono<CpeAuthenticationConsumerDmaapModel> transform(JsonObject dmaapResponseJsonObject) {
103
104         LOGGER.trace("Event from DMaaP to be parsed: \n{}", gson.toJson(dmaapResponseJsonObject));
105
106         if (!containsProperHeaders(dmaapResponseJsonObject)) {
107             LOGGER.warn("Incorrect CPE Authentication JSON event - missing headers");
108             return Mono.empty();
109         }
110
111         JsonObject commonEventHeader = dmaapResponseJsonObject.getAsJsonObject(EVENT)
112                 .getAsJsonObject(COMMON_EVENT_HEADER);
113         JsonObject stateChangeFields = dmaapResponseJsonObject.getAsJsonObject(EVENT)
114                 .getAsJsonObject(STATE_CHANGE_FIELDS);
115
116         pnfSourceName = getValueFromJson(commonEventHeader, SOURCE_NAME);
117
118         oldAuthenticationStatus = getValueFromJson(stateChangeFields, OLD_AUTHENTICATION_STATE);
119         newAuthenticationStatus = getValueFromJson(stateChangeFields, NEW_AUTHENTICATION_STATE);
120         stateInterface = getValueFromJson(stateChangeFields, STATE_INTERFACE);
121
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);
126         }
127
128         if (StringUtils.isEmpty(pnfSourceName)
129                 || authenticationStatusMissing(oldAuthenticationStatus)
130                 || authenticationStatusMissing(newAuthenticationStatus)) {
131             String incorrectEvent = dumpJsonData();
132             LOGGER.warn("Incorrect CPE Authentication JSON event: {}", incorrectEvent);
133             return Mono.empty();
134         }
135
136         return Mono.just(ImmutableCpeAuthenticationConsumerDmaapModel.builder()
137                 .correlationId(pnfSourceName)
138                 .oldAuthenticationState(oldAuthenticationStatus)
139                 .newAuthenticationState(newAuthenticationStatus)
140                 .stateInterface(stateInterface)
141                 .rgwMacAddress(rgwMacAddress)
142                 .swVersion(swVersion)
143                 .build());
144     }
145
146     private boolean authenticationStatusMissing(String authenticationStatus) {
147         return StringUtils.isEmpty(authenticationStatus);
148     }
149
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);
153     }
154
155     private String dumpJsonData() {
156         return String.format(CPE_AUTHENTICATION_DUMPING_TEMPLATE,
157                 pnfSourceName,
158                 oldAuthenticationStatus,
159                 newAuthenticationStatus,
160                 stateInterface,
161                 rgwMacAddress,
162                 swVersion
163         );
164     }
165
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());
170     }
171
172     private String getValueFromJson(JsonObject jsonObject, String jsonKey) {
173         return jsonObject.has(jsonKey) ? jsonObject.get(jsonKey).getAsString() : "";
174     }
175 }