Open Kilda Java Documentation
TestMessageMock.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.northbound.controller;
17 
18 import static java.util.Collections.singletonList;
21 
54 
55 import org.springframework.stereotype.Component;
56 
57 import java.util.Collections;
58 import java.util.List;
59 import java.util.Map;
60 import java.util.concurrent.ConcurrentHashMap;
61 
66 @Component
68  static final String FLOW_ID = "ff:00";
69  static final SwitchId SWITCH_ID = new SwitchId(FLOW_ID);
70  static final String ERROR_FLOW_ID = "error-flow";
71  static final String TEST_SWITCH_ID = "ff:01";
72  static final long TEST_SWITCH_RULE_COOKIE = 1L;
73  static final FlowEndpointPayload flowEndpoint = new FlowEndpointPayload(SWITCH_ID, 1, 1);
74  static final FlowPayload flow =
75  new FlowPayload(FLOW_ID, flowEndpoint, flowEndpoint, 10000, false, FLOW_ID, null,
76  FlowState.UP.getState());
77  static final FlowIdStatusPayload flowStatus = new FlowIdStatusPayload(FLOW_ID, FlowState.UP);
78  static final PathInfoData path = new PathInfoData(0L, Collections.emptyList());
79  static final List<PathNodePayload> pathPayloadsList =
80  Collections.singletonList(new PathNodePayload(SWITCH_ID, 1, 1));
81  static final FlowPathPayload flowPath = new FlowPathPayload(FLOW_ID, pathPayloadsList, pathPayloadsList);
82  static final Flow flowModel = new Flow(FLOW_ID, 10000, false, 0L, FLOW_ID, null, SWITCH_ID,
83  SWITCH_ID, 1, 1, 1, 1, 1, 1, path, FlowState.UP);
84 
85  private static final FlowResponse flowResponse = new FlowResponse(flowModel);
86  private static final FlowReadResponse FLOW_RESPONSE =
87  new FlowReadResponse(new BidirectionalFlow(flowModel, flowModel));
88  private static final SwitchRulesResponse switchRulesResponse =
89  new SwitchRulesResponse(singletonList(TEST_SWITCH_RULE_COOKIE));
90  private static final Map<String, CommandData> messages = new ConcurrentHashMap<>();
91 
98  private Message formatResponse(final String correlationId, final CommandData data) {
99  if (data instanceof FlowCreateRequest) {
100  return new InfoMessage(flowResponse, 0, correlationId, Destination.NORTHBOUND);
101  } else if (data instanceof FlowDeleteRequest) {
102  return new InfoMessage(flowResponse, 0, correlationId, Destination.NORTHBOUND);
103  } else if (data instanceof FlowUpdateRequest) {
104  return new InfoMessage(flowResponse, 0, correlationId, Destination.NORTHBOUND);
105  } else if (data instanceof FlowReadRequest) {
106  return getReadFlowResponse(((FlowReadRequest) data).getFlowId(), correlationId);
107  } else if (data instanceof FlowsDumpRequest) {
108  return new ChunkedInfoMessage(FLOW_RESPONSE, 0, correlationId, null);
109  } else if (data instanceof SwitchRulesDeleteRequest) {
110  return new InfoMessage(switchRulesResponse, 0, correlationId, Destination.NORTHBOUND);
111  } else {
112  return null;
113  }
114  }
115 
116  @Override
117  public Object poll(String correlationId) {
119 
120  if (messages.containsKey(correlationId)) {
121  data = messages.remove(correlationId);
122  } else if (messages.containsKey(SYSTEM_CORRELATION_ID)) {
123  data = messages.remove(SYSTEM_CORRELATION_ID);
124  } else {
125  throw new MessageException(correlationId, System.currentTimeMillis(),
126  OPERATION_TIMED_OUT, KafkaMessageConsumer.TIMEOUT_ERROR_MESSAGE, "kilda-test");
127  }
128  return formatResponse(correlationId, data);
129  }
130 
131  @Override
132  public void clear() {
133  messages.clear();
134  }
135 
136  @Override
137  public void send(String topic, Message message) {
138  if (message instanceof CommandMessage) {
139  messages.put(message.getCorrelationId(), ((CommandMessage) message).getData());
140  }
141  }
142 
143  private Message getReadFlowResponse(String flowId, String correlationId) {
144  if (ERROR_FLOW_ID.equals(flowId)) {
145  return new ErrorMessage(new ErrorData(ErrorType.NOT_FOUND, "Flow was not found", ERROR_FLOW_ID),
146  0, correlationId, Destination.NORTHBOUND);
147  } else {
148  return new InfoMessage(FLOW_RESPONSE, 0, correlationId, Destination.NORTHBOUND);
149  }
150  }
151 }
static final String SYSTEM_CORRELATION_ID
Definition: Utils.java:73