Open Kilda Java Documentation
VerificationBolt.java
Go to the documentation of this file.
1 /* Copyright 2018 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.flow.bolts;
17 
23 
24 import org.apache.storm.topology.OutputFieldsDeclarer;
25 import org.apache.storm.tuple.Fields;
26 import org.apache.storm.tuple.Tuple;
27 import org.apache.storm.tuple.Values;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 
31 public class VerificationBolt extends AbstractBolt {
32  public static final String FIELD_ID_FLOW_ID = Utils.FLOW_ID;
33  public static final String FIELD_ID_OUTPUT = "payload";
34  public static final String FIELD_ID_INPUT = AbstractTopology.MESSAGE_FIELD;
35 
36  public static final String STREAM_ID_PROXY = "proxy";
37  public static final Fields STREAM_FIELDS_PROXY = new Fields(FIELD_ID_FLOW_ID, FIELD_ID_OUTPUT, FIELD_ID_INPUT);
38 
39  private static final Logger logger = LoggerFactory.getLogger(VerificationBolt.class);
40 
41  @Override
42  public void declareOutputFields(OutputFieldsDeclarer outputManager) {
43  outputManager.declareStream(STREAM_ID_PROXY, STREAM_FIELDS_PROXY);
44  }
45 
46  @Override
47  protected void handleInput(Tuple input) {
48  String source = input.getSourceComponent();
49 
50  if (source.equals(ComponentType.CRUD_BOLT.toString())) {
51  proxyRequest(input);
52  } else if (source.equals(ComponentType.SPEAKER_BOLT.toString())) {
53  consumePingReply(input);
54  } else {
55  logger.warn("Unexpected input from {} - is topology changes without code change?", source);
56  }
57  }
58 
59  private void proxyRequest(Tuple input) {
60  Values proxyData = new Values(
61  input.getValueByField(CrudBolt.FIELD_ID_FLOW_ID),
62  input.getValueByField(CrudBolt.FIELD_ID_BIFLOW),
63  input.getValueByField(CrudBolt.FIELD_ID_MESSAGE));
64  getOutput().emit(STREAM_ID_PROXY, input, proxyData);
65  }
66 
67  private void consumePingReply(Tuple input) {
68  UniFlowVerificationResponse response = fetchUniFlowResponse(input);
69  Values payload = new Values(response.getFlowId(), response, null);
70  getOutput().emit(STREAM_ID_PROXY, input, payload);
71  }
72 
73  private UniFlowVerificationResponse fetchUniFlowResponse(Tuple input) {
74  UniFlowVerificationResponse value;
75  try {
76  value = (UniFlowVerificationResponse) input.getValueByField(SpeakerBolt.FIELD_ID_PAYLOAD);
77  } catch (ClassCastException e) {
78  throw new IllegalArgumentException(
79  String.format("Can't deserialize into %s", UniFlowVerificationResponse.class.getName()), e);
80  }
81 
82  return value;
83  }
84 }
void declareOutputFields(OutputFieldsDeclarer outputManager)
value
Definition: nodes.py:62
source
Definition: nodes.py:53
static final String FLOW_ID
Definition: Utils.java:61