Open Kilda Java Documentation
IslStatsBoltTest.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.islstats.bolts;
17 
18 import static org.hamcrest.CoreMatchers.containsString;
19 import static org.hamcrest.CoreMatchers.instanceOf;
20 import static org.hamcrest.CoreMatchers.is;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertThat;
23 
34 
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 
41 import java.util.List;
42 import java.util.Map;
43 
44 public class IslStatsBoltTest {
45  private static final SwitchId SWITCH1_ID = new SwitchId("00:00:b0:d2:f5:b0:09:34");
46  private static final String SWITCH1_ID_OTSD_FORMAT = SWITCH1_ID.toOtsdFormat();
47  private static final int SWITCH1_PORT = 1;
48  private static final int PATH1_SEQID = 1;
49  private static final long PATH1_LATENCY = 10L;
50  private static final PathNode NODE1 = new PathNode(SWITCH1_ID, SWITCH1_PORT, PATH1_SEQID, PATH1_LATENCY);
51 
52  private static final SwitchId SWITCH2_ID = new SwitchId("00:00:b0:d2:f5:00:5e:18");
53  private static final String SWITCH2_ID_OTSD_FORMAT = SWITCH2_ID.toOtsdFormat();
54  private static final int SWITCH2_PORT = 5;
55  private static final int PATH2_SEQID = 2;
56  private static final long PATH2_LATENCY = 15L;
57  private static final PathNode NODE2 = new PathNode(SWITCH2_ID, SWITCH2_PORT, PATH2_SEQID, PATH2_LATENCY);
58 
59  private static final int LATENCY = 1000;
60  private static final List<PathNode> PATH = java.util.Arrays.asList(NODE1, NODE2);
61  private static final long SPEED = 400L;
62  private static final IslChangeType STATE = IslChangeType.DISCOVERED;
63  private static final long AVAILABLE_BANDWIDTH = 500L;
64  private static final IslInfoData ISL_INFO_DATA = new IslInfoData(LATENCY, PATH, SPEED, STATE, AVAILABLE_BANDWIDTH);
65  private static final long TIMESTAMP = 1507433872L;
66 
67  private IslStatsBolt statsBolt = new IslStatsBolt();
68 
69  private static final String CORRELATION_ID = "system";
70  private static final Destination DESTINATION = null;
71  private static final InfoMessage MESSAGE = new InfoMessage(ISL_INFO_DATA, TIMESTAMP, CORRELATION_ID, DESTINATION);
72 
73  private static Logger logger = LoggerFactory.getLogger(IslStatsBolt.class);
74 
75  @Rule
76  public ExpectedException thrown = ExpectedException.none();
77 
78  @Test
79  public void buildTsdbTuple() throws Exception {
80  List<Object> tsdbTuple = statsBolt.buildTsdbTuple(ISL_INFO_DATA, TIMESTAMP);
81  assertThat(tsdbTuple.size(), is(1));
82 
83  Datapoint datapoint = Utils.MAPPER.readValue(tsdbTuple.get(0).toString(), Datapoint.class);
84  assertEquals("pen.isl.latency", datapoint.getMetric());
85  assertEquals((Long) TIMESTAMP, datapoint.getTime());
86  assertEquals(LATENCY, datapoint.getValue());
87 
88  Map<String, String> pathNode = datapoint.getTags();
89  assertEquals(SWITCH1_ID_OTSD_FORMAT, pathNode.get("src_switch"));
90  assertEquals(SWITCH2_ID_OTSD_FORMAT, pathNode.get("dst_switch"));
91  assertEquals(SWITCH1_PORT, Integer.parseInt(pathNode.get("src_port")));
92  assertEquals(SWITCH2_PORT, Integer.parseInt(pathNode.get("dst_port")));
93  }
94 
95  @Test
96  public void getInfoData() throws Exception {
97  Object data = statsBolt.getInfoData(MESSAGE);
98  assertThat(data, instanceOf(InfoData.class));
99  }
100 
101  @Test
102  public void getIslInfoData() throws Exception {
103  Object data = statsBolt.getIslInfoData(statsBolt.getInfoData(MESSAGE));
104  assertThat(data, instanceOf(IslInfoData.class));
105  assertEquals(ISL_INFO_DATA, data);
106 
107  thrown.expect(Exception.class);
108  thrown.expectMessage(containsString("is not an IslInfoData"));
109  PortInfoData portData = new PortInfoData();
110  InfoMessage badMessage = new InfoMessage(portData, TIMESTAMP, CORRELATION_ID, null);
111  statsBolt.getIslInfoData(statsBolt.getIslInfoData(badMessage.getData()));
112  }
113 }
static final ObjectMapper MAPPER
Definition: Utils.java:31
List< Object > buildTsdbTuple(IslInfoData data, long timestamp)