summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriber.java
blob: a0a8eaf8f0653b93ed3f7de7f8970b2055f3dab6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.dcaegen2.services.pmmapper.datarouter;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import io.undertow.util.HeaderValues;
import lombok.Data;
import lombok.NonNull;

import org.onap.dcaegen2.services.pmmapper.config.Configurable;
import org.onap.dcaegen2.services.pmmapper.exceptions.NoMetadataException;
import org.onap.dcaegen2.services.pmmapper.exceptions.ReconfigurationException;
import org.onap.dcaegen2.services.pmmapper.exceptions.TooManyTriesException;
import org.onap.dcaegen2.services.pmmapper.model.EventMetadata;
import org.onap.dcaegen2.services.pmmapper.model.MapperConfig;
import org.onap.dcaegen2.services.pmmapper.model.Event;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.StatusCodes;

import org.onap.dcaegen2.services.pmmapper.utils.HttpServerExchangeAdapter;
import org.onap.dcaegen2.services.pmmapper.utils.RequiredFieldDeserializer;
import org.onap.logging.ref.slf4j.ONAPLogAdapter;
import org.onap.logging.ref.slf4j.ONAPLogConstants;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * Subscriber for events sent from data router
 * Provides an undertow HttpHandler to be used as an endpoint for data router to send events to.
 */
@Data
public class DataRouterSubscriber implements HttpHandler, Configurable {
    public static final String METADATA_HEADER = "X-DMAAP-DR-META";
    public static final String PUB_ID_HEADER = "X-DMAAP-DR-PUBLISH-ID";

    private static final ONAPLogAdapter logger = new ONAPLogAdapter(LoggerFactory.getLogger(DataRouterSubscriber.class));
    private static final int NUMBER_OF_ATTEMPTS = 5;
    private static final int DEFAULT_TIMEOUT = 2000;
    private static final int MAX_JITTER = 50;

    private static final String BAD_METADATA_MESSAGE = "Malformed Metadata.";
    private static final String NO_METADATA_MESSAGE = "Missing Metadata.";

    private boolean limited = false;
    private Random jitterGenerator;
    private Gson metadataBuilder;
    private MapperConfig config;
    public static String subscriberId;
    @NonNull
    private EventReceiver eventReceiver;

    /**
     * @param eventReceiver receiver for any inbound events.
     */
    public DataRouterSubscriber(EventReceiver eventReceiver, MapperConfig config) {
        this.eventReceiver = eventReceiver;
        this.jitterGenerator = new Random();
        this.metadataBuilder = new GsonBuilder().registerTypeAdapter(EventMetadata.class, new RequiredFieldDeserializer<EventMetadata>())
                .create();
        this.config = config;
        this.subscriberId="";
    }

    /**
     * Starts data flow by subscribing to data router through bus controller.
     *
     * @throws TooManyTriesException in the event that timeout has occurred several times.
     */
    public void start() throws TooManyTriesException, InterruptedException {
        try {
            logger.unwrap().info("Starting subscription to DataRouter {}", ONAPLogConstants.Markers.ENTRY);
            subscribe();
            logger.unwrap().info("Successfully started DR Subscriber");
        } finally {
            logger.unwrap().info("{}", ONAPLogConstants.Markers.EXIT);
        }
    }

    private HttpURLConnection getBusControllerConnection(String method, URL resource, int timeout) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) resource.openConnection();
        connection.setRequestMethod(method);
        connection.setConnectTimeout(timeout);
        connection.setReadTimeout(timeout);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);

        final UUID invocationID = logger.invoke(ONAPLogConstants.InvocationMode.SYNCHRONOUS);
        final UUID requestID = UUID.randomUUID();
        connection.setRequestProperty(ONAPLogConstants.Headers.REQUEST_ID, requestID.toString());
        connection.setRequestProperty(ONAPLogConstants.Headers.INVOCATION_ID, invocationID.toString());
        connection.setRequestProperty(ONAPLogConstants.Headers.PARTNER_NAME, MapperConfig.CLIENT_NAME);

        return connection;
    }

    private JsonObject getBusControllerSubscribeBody(MapperConfig config) {
        JsonObject subscriberObj = new JsonObject();
        subscriberObj.addProperty("dcaeLocationName", config.getSubscriberDcaeLocation());
        subscriberObj.addProperty("deliveryURL", config.getBusControllerDeliveryUrl());
        subscriberObj.addProperty("feedId", config.getDmaapDRFeedId());
        subscriberObj.addProperty("lastMod", Instant.now().toString());
        subscriberObj.addProperty("username", config.getBusControllerUserName());
        subscriberObj.addProperty("userpwd", config.getBusControllerPassword());
        subscriberObj.addProperty("privilegedSubscriber", true);
        return subscriberObj;
    }

    private void processResponse(HttpURLConnection connection) throws IOException {
        try (BufferedReader responseBody = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
            String body = responseBody.lines().collect(Collectors.joining(""));
            updateSubscriberId(body);
        } catch (IOException | JsonSyntaxException | IllegalStateException e) {
            throw new IOException("Failed to process response", e);
        }
    }

    private void updateSubscriberId(String responseBody) {
            JsonParser parser = new JsonParser();
            JsonObject responseObject = parser.parse(responseBody).getAsJsonObject();
            this.subscriberId = responseObject.get("subId").getAsString();
    }

    private void subscribe() throws TooManyTriesException, InterruptedException {
        try {
            URL subscribeResource = this.config.getBusControllerSubscriptionUrl();
            JsonObject subscribeBody = this.getBusControllerSubscribeBody(this.config);
            request(NUMBER_OF_ATTEMPTS, DEFAULT_TIMEOUT, "POST", subscribeResource, subscribeBody);
        } catch (MalformedURLException e) {
            throw new IllegalStateException("Subscription URL is malformed", e);
        }

    }
    private void updateSubscriber() throws TooManyTriesException, InterruptedException {
        try {
            URL subscribeResource = this.config.getBusControllerSubscriptionUrl();
            URL updateResource = new URL(String.format("%s/%s", subscribeResource, subscriberId));
            JsonObject subscribeBody = this.getBusControllerSubscribeBody(this.config);
            request(NUMBER_OF_ATTEMPTS, DEFAULT_TIMEOUT, "PUT", updateResource, subscribeBody);
        } catch (MalformedURLException e) {
            throw new IllegalStateException("Subscription URL is malformed", e);
        }
    }

    private void request(int attempts, int timeout, String method, URL resource, JsonObject subscribeBody) throws TooManyTriesException, InterruptedException {
        int subResponse = 504;
        String subMessage = "";
        boolean processFailure = false;
        try {
            HttpURLConnection connection = getBusControllerConnection(method, resource, timeout);
            try (OutputStream bodyStream = connection.getOutputStream();
                 OutputStreamWriter bodyWriter = new OutputStreamWriter(bodyStream, StandardCharsets.UTF_8)) {
                bodyWriter.write(subscribeBody.toString());
            }
            subResponse = connection.getResponseCode();
            subMessage = connection.getResponseMessage();
            if (subResponse < 300) {
                processResponse(connection);
            }
        } catch (IOException e) {
            logger.unwrap().error("Failure to process response", e);
            processFailure = true;
        }
        logger.unwrap().info("Request to bus controller executed with Response Code: '{}' and Response Event: '{}'.", subResponse, subMessage);
        if ((subResponse >= 300 || processFailure) && attempts > 1 ) {
            Thread.sleep(timeout);
            request(--attempts, (timeout * 2) + jitterGenerator.nextInt(MAX_JITTER), method, resource, subscribeBody);
        } else if (subResponse >= 300 || processFailure) {
            throw new TooManyTriesException("Failed to subscribe within appropriate amount of attempts");
        }
    }

    private EventMetadata getMetadata(HttpServerExchange httpServerExchange) throws NoMetadataException {
        String metadata = Optional.ofNullable(httpServerExchange.getRequestHeaders()
                .get(METADATA_HEADER))
                .map((HeaderValues headerValues) -> headerValues.get(0))
                .orElseThrow(() -> new NoMetadataException("Metadata Not found"));
        return metadataBuilder.fromJson(metadata, EventMetadata.class);
    }

    /**
     * Receives inbound requests, verifies that required headers are valid
     * and passes an Event onto the eventReceiver.
     * The forwarded httpServerExchange response is the responsibility of the eventReceiver.
     *
     * @param httpServerExchange inbound http server exchange.
     */
    @Override
    public void handleRequest(HttpServerExchange httpServerExchange) {
        try{
            logger.entering(new HttpServerExchangeAdapter(httpServerExchange));
            if (limited) {
                httpServerExchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE)
                        .getResponseSender()
                        .send(StatusCodes.SERVICE_UNAVAILABLE_STRING);
            } else {
                try {

                    Map<String,String> mdc = MDC.getCopyOfContextMap();
                    EventMetadata metadata = getMetadata(httpServerExchange);
                    String publishIdentity = httpServerExchange.getRequestHeaders().get(PUB_ID_HEADER).getFirst();
                    httpServerExchange.getRequestReceiver()
                            .receiveFullString((callbackExchange, body) ->
                                httpServerExchange.dispatch(() ->
                                        eventReceiver.receive(new Event(callbackExchange, body, metadata, mdc, publishIdentity)))
                            );
                } catch (NoMetadataException exception) {
                    logger.unwrap().info("Bad Request: no metadata found under '{}' header.", METADATA_HEADER, exception);
                    httpServerExchange.setStatusCode(StatusCodes.BAD_REQUEST)
                            .getResponseSender()
                            .send(NO_METADATA_MESSAGE);
                } catch (JsonParseException exception) {
                    logger.unwrap().info("Bad Request: Failure to parse metadata", exception);
                    httpServerExchange.setStatusCode(StatusCodes.BAD_REQUEST)
                            .getResponseSender()
                            .send(BAD_METADATA_MESSAGE);
                }
            }
        } finally {
            logger.exiting();
        }
    }

    @Override
    public void reconfigure(MapperConfig config) throws ReconfigurationException {
        logger.unwrap().info("Checking new Configuration against existing.");
        if(!this.config.dmaapInfoEquals(config) || !this.config.getDmaapDRFeedId().equals(config.getDmaapDRFeedId())){
            logger.unwrap().info("DMaaP Info changes found, reconfiguring.");
            try {
                this.config = config;
                this.updateSubscriber();
            } catch (TooManyTriesException | InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new ReconfigurationException("Failed to reconfigure DataRouter subscriber.", e);
            }
        }

    }
}