9fe0c2779ecc37909f60091efb3a8e67c60b208c
[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_FORMAT;
24 import static org.onap.bbs.event.processor.utilities.CommonEventFields.CORRELATION_ID;
25 import static org.onap.bbs.event.processor.utilities.ReRegistrationEventFields.ADDITIONAL_FIELDS;
26 import static org.onap.bbs.event.processor.utilities.ReRegistrationEventFields.ATTACHMENT_POINT;
27 import static org.onap.bbs.event.processor.utilities.ReRegistrationEventFields.CVLAN;
28 import static org.onap.bbs.event.processor.utilities.ReRegistrationEventFields.REMOTE_ID;
29 import static org.onap.bbs.event.processor.utilities.ReRegistrationEventFields.SVLAN;
30
31 import com.google.gson.Gson;
32 import com.google.gson.JsonElement;
33 import com.google.gson.JsonObject;
34 import com.google.gson.JsonParser;
35
36 import java.util.Optional;
37 import java.util.stream.StreamSupport;
38
39 import org.onap.bbs.event.processor.exceptions.DmaapException;
40 import org.onap.bbs.event.processor.model.ImmutableReRegistrationConsumerDmaapModel;
41 import org.onap.bbs.event.processor.model.ReRegistrationConsumerDmaapModel;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.util.StringUtils;
45
46 import reactor.core.publisher.Flux;
47 import reactor.core.publisher.Mono;
48
49 public class ReRegistrationDmaapConsumerJsonParser {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(ReRegistrationDmaapConsumerJsonParser.class);
52     private static final Gson gson = new Gson();
53
54     private static final String RE_REGISTRATION_DUMPING_TEMPLATE = "%n{"
55             + "\"" + CORRELATION_ID + COMMON_FORMAT + ","
56             + "\"" + ATTACHMENT_POINT + COMMON_FORMAT + ","
57             + "\"" + REMOTE_ID + COMMON_FORMAT + ","
58             + "\"" + CVLAN + COMMON_FORMAT + ","
59             + "\"" + SVLAN + COMMON_FORMAT
60             + "}";
61
62     private String pnfCorrelationId;
63
64     private String attachmentPoint;
65     private String remoteId;
66     private String cvlan;
67     private String svlan;
68
69     /**
70      * Translates a response from DMaaP to a reactive {@link ReRegistrationConsumerDmaapModel} model.
71      * @param dmaapResponse Response from DMaaP
72      * @return Re-Registration Consumer DMaaP reactive model
73      */
74     public Flux<ReRegistrationConsumerDmaapModel> extractModelFromDmaap(Mono<JsonElement> dmaapResponse) {
75         return dmaapResponse
76                 .flatMapMany(this::createTargetFlux);
77     }
78
79     private Flux<ReRegistrationConsumerDmaapModel> createTargetFlux(JsonElement jsonElement) {
80         if (jsonElement.isJsonObject()) {
81             return doCreateTargetFlux(Flux.defer(() -> Flux.just(jsonElement.getAsJsonObject())));
82         }
83         return doCreateTargetFlux(
84                 Flux.defer(() -> Flux.fromStream(StreamSupport.stream(jsonElement.getAsJsonArray().spliterator(), false)
85                         .map(jsonElementFromArray -> getJsonObjectFromAnArray(jsonElementFromArray)
86                                 .orElseGet(JsonObject::new)))));
87     }
88
89     private Flux<ReRegistrationConsumerDmaapModel> doCreateTargetFlux(Flux<JsonObject> jsonObject) {
90         return jsonObject
91                 .flatMap(this::transform)
92                 .onErrorResume(exception -> exception instanceof DmaapException, e -> Mono.empty());
93     }
94
95     private Mono<ReRegistrationConsumerDmaapModel> transform(JsonObject dmaapResponseJsonObject) {
96
97         LOGGER.trace("Event from DMaaP to be parsed: \n{}", gson.toJson(dmaapResponseJsonObject));
98
99         if (!containsProperHeaders(dmaapResponseJsonObject)) {
100             LOGGER.warn("Incorrect JsonObject - missing headers");
101             return Mono.empty();
102         }
103
104         JsonObject pnfReRegistrationFields =
105                 dmaapResponseJsonObject.getAsJsonObject(ADDITIONAL_FIELDS);
106
107         pnfCorrelationId = getValueFromJson(dmaapResponseJsonObject, CORRELATION_ID);
108
109         attachmentPoint = getValueFromJson(pnfReRegistrationFields, ATTACHMENT_POINT);
110         remoteId = getValueFromJson(pnfReRegistrationFields, REMOTE_ID);
111         cvlan = getValueFromJson(pnfReRegistrationFields, CVLAN);
112         svlan = getValueFromJson(pnfReRegistrationFields, SVLAN);
113
114         if (StringUtils.isEmpty(pnfCorrelationId) || anyImportantPropertyMissing()) {
115             String incorrectEvent = dumpJsonData();
116             LOGGER.warn("Incorrect Re-Registration JSON event: {}", incorrectEvent);
117             return Mono.empty();
118         }
119
120         return Mono.just(ImmutableReRegistrationConsumerDmaapModel.builder()
121                 .correlationId(pnfCorrelationId)
122                 .attachmentPoint(attachmentPoint)
123                 .remoteId(remoteId)
124                 .cVlan(cvlan)
125                 .sVlan(svlan)
126                 .build());
127     }
128
129     private boolean anyImportantPropertyMissing() {
130         return StringUtils.isEmpty(attachmentPoint)
131                 || StringUtils.isEmpty(remoteId)
132                 || StringUtils.isEmpty(cvlan)
133                 || StringUtils.isEmpty(svlan);
134     }
135
136     private boolean containsProperHeaders(JsonObject jsonObject) {
137         return jsonObject.has(ADDITIONAL_FIELDS);
138     }
139
140     private String dumpJsonData() {
141         return String.format(RE_REGISTRATION_DUMPING_TEMPLATE,
142                 pnfCorrelationId,
143                 attachmentPoint,
144                 remoteId,
145                 cvlan,
146                 svlan
147         );
148     }
149
150     Optional<JsonObject> getJsonObjectFromAnArray(JsonElement element) {
151         JsonParser jsonParser = new JsonParser();
152         return element.isJsonPrimitive() ? Optional.of(jsonParser.parse(element.getAsString()).getAsJsonObject())
153                 : Optional.of(jsonParser.parse(element.toString()).getAsJsonObject());
154     }
155
156     private String getValueFromJson(JsonObject jsonObject, String jsonKey) {
157         return jsonObject.has(jsonKey) ? jsonObject.get(jsonKey).getAsString() : "";
158     }
159 }