Open Kilda Java Documentation
RouterBolt.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 
28 
29 import org.apache.storm.topology.OutputFieldsDeclarer;
30 import org.apache.storm.tuple.Fields;
31 import org.apache.storm.tuple.Tuple;
32 import org.apache.storm.tuple.Values;
33 
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 
37 import java.io.IOException;
38 
39 public class RouterBolt extends AbstractBolt {
40 
41  private static final Logger LOGGER = LoggerFactory.getLogger(RouterBolt.class);
42 
43  @Override
44  protected void handleInput(Tuple input) {
45  String request = input.getString(0);
46 
47  Message message;
48  try {
49  message = Utils.MAPPER.readValue(request, Message.class);
50  } catch (IOException e) {
51  LOGGER.error("Error during parsing request for NBWorker topology", e);
52  return;
53  }
54 
55  if (message instanceof CommandMessage) {
56  LOGGER.debug("Received command message {}", message);
58  CommandData data = command.getData();
59 
60  if (data instanceof BaseRequest) {
61  BaseRequest baseRequest = (BaseRequest) data;
62  processRequest(input, baseRequest, message.getCorrelationId());
63  }
64  } else {
65  unhandledInput(input);
66  }
67  }
68 
69  private void processRequest(Tuple input, BaseRequest request, String correlationId) {
70  if (request instanceof SwitchesBaseRequest) {
71  getOutput().emit(StreamType.SWITCH.toString(), input, new Values(request, correlationId));
72  } else if (request instanceof LinksBaseRequest) {
73  getOutput().emit(StreamType.ISL.toString(), input, new Values(request, correlationId));
74  } else if (request instanceof FlowsBaseRequest) {
75  getOutput().emit(StreamType.FLOW.toString(), input, new Values(request, correlationId));
76  } else {
77  unhandledInput(input);
78  }
79  }
80 
81  @Override
82  public void declareOutputFields(OutputFieldsDeclarer declarer) {
83  declarer.declareStream(StreamType.SWITCH.toString(), new Fields("request", "correlationId"));
84  declarer.declareStream(StreamType.ISL.toString(), new Fields("request", "correlationId"));
85  declarer.declareStream(StreamType.FLOW.toString(), new Fields("request", "correlationId"));
86  }
87 }
static final ObjectMapper MAPPER
Definition: Utils.java:31
void declareOutputFields(OutputFieldsDeclarer declarer)
Definition: RouterBolt.java:82
def command(payload, fields)
Definition: share.py:102
void unhandledInput(Tuple input)