Open Kilda Java Documentation
ResponseSplitterBolt.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.wfm.topology.nbworker.bolts;
17 
24 
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import org.apache.commons.collections4.CollectionUtils;
27 import org.apache.storm.topology.OutputFieldsDeclarer;
28 import org.apache.storm.tuple.Tuple;
29 import org.apache.storm.tuple.Values;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 
33 import java.util.ArrayList;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.UUID;
37 
38 public class ResponseSplitterBolt extends AbstractBolt {
39 
40  private static final Logger LOGGER = LoggerFactory.getLogger(ResponseSplitterBolt.class);
41 
42  @Override
43  protected void handleInput(Tuple input) {
44  List<InfoData> responses = (List<InfoData>) input.getValueByField("response");
45  String correlationId = input.getStringByField("correlationId");
46  LOGGER.debug("Received response correlationId {}", correlationId);
47 
48  sendChunkedResponse(responses, input, correlationId);
49  }
50 
51  private void sendChunkedResponse(List<InfoData> responses, Tuple input, String requestId) {
52  List<Message> messages = new ArrayList<>();
53  if (CollectionUtils.isEmpty(responses)) {
54  LOGGER.debug("No records found in the database");
55  Message message = new ChunkedInfoMessage(null, System.currentTimeMillis(), requestId, null);
56  messages.add(message);
57  } else {
58  String currentRequestId = requestId;
59  String nextRequestId;
60  Iterator<InfoData> iterator = responses.iterator();
61  while (iterator.hasNext()) {
62  InfoData infoData = iterator.next();
63  // generate new request id for the next request if the list contains more elements.
64  if (iterator.hasNext()) {
65  nextRequestId = UUID.randomUUID().toString();
66  } else {
67  nextRequestId = null;
68  }
69 
70  Message message = new ChunkedInfoMessage(infoData, System.currentTimeMillis(), currentRequestId,
71  nextRequestId);
72  messages.add(message);
73  currentRequestId = nextRequestId;
74  }
75 
76  LOGGER.debug("Response is divided into {} messages", messages.size());
77  }
78 
79  // emit all found messages
80  for (Message message : messages) {
81  try {
82  getOutput().emit(input, new Values(Utils.MAPPER.writeValueAsString(message)));
83  } catch (JsonProcessingException e) {
84  LOGGER.error("Error during writing response as json", e);
85  }
86  }
87  }
88 
89  @Override
90  public void declareOutputFields(OutputFieldsDeclarer declarer) {
91  declarer.declare(AbstractTopology.fieldMessage);
92  }
93 }