blob: f472a90d71cbf03392fdc92041c12d4e4b0fdb71 [file] [log] [blame]
Charles Chanfc5c7802016-05-17 13:13:55 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.segmentrouting.config;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.VlanId;
24import org.onosproject.TestApplicationId;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.PortNumber;
28import org.onosproject.net.config.Config;
29import org.onosproject.net.config.ConfigApplyDelegate;
30import org.onosproject.segmentrouting.SegmentRoutingManager;
31import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
32import java.io.InputStream;
33import java.util.Set;
34import static org.junit.Assert.assertFalse;
35import static org.junit.Assert.assertTrue;
36import static org.junit.Assert.assertThat;
37import static org.hamcrest.Matchers.is;
38
39/**
40 * Tests for class {@link XConnectConfig}.
41 */
42public class XConnectConfigTest {
43 private static final DeviceId DEV1 = DeviceId.deviceId("of:0000000000000001");
44 private static final DeviceId DEV2 = DeviceId.deviceId("of:0000000000000002");
45 private static final VlanId VLAN10 = VlanId.vlanId((short) 10);
46 private static final VlanId VLAN20 = VlanId.vlanId((short) 20);
47 private static final PortNumber PORT3 = PortNumber.portNumber(3);
48 private static final PortNumber PORT4 = PortNumber.portNumber(4);
49 private static final PortNumber PORT5 = PortNumber.portNumber(5);
50 private static final XConnectStoreKey KEY1 = new XConnectStoreKey(DEV1, VLAN10);
51 private static final XConnectStoreKey KEY2 = new XConnectStoreKey(DEV2, VLAN10);
52 private static final XConnectStoreKey KEY3 = new XConnectStoreKey(DEV2, VLAN20);
53 private static final XConnectStoreKey KEY4 = new XConnectStoreKey(DEV2, VlanId.NONE);
54
55 private XConnectConfig config;
56 private XConnectConfig invalidConfig;
57
58 @Before
59 public void setUp() throws Exception {
60 InputStream jsonStream = SegmentRoutingAppConfigTest.class
61 .getResourceAsStream("/xconnect.json");
62 InputStream invalidJsonStream = SegmentRoutingAppConfigTest.class
63 .getResourceAsStream("/xconnect-invalid.json");
64
65 String key = SegmentRoutingManager.SR_APP_ID;
66 ApplicationId subject = new TestApplicationId(key);
67 ObjectMapper mapper = new ObjectMapper();
68 JsonNode jsonNode = mapper.readTree(jsonStream);
69 JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream);
70 ConfigApplyDelegate delegate = new XConnectConfigTest.MockDelegate();
71
72 config = new XConnectConfig();
73 config.init(subject, key, jsonNode, mapper, delegate);
74 invalidConfig = new XConnectConfig();
75 invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate);
76 }
77
78 /**
79 * Tests config validity.
80 */
81 @Test
82 public void testIsValid() {
83 assertTrue(config.isValid());
84 assertFalse(invalidConfig.isValid());
85 }
86
87 /**
88 * Tests getXconnects.
89 */
90 @Test
91 public void testGetXconnects() {
92 Set<XConnectStoreKey> xconnects = config.getXconnects();
93 assertThat(xconnects.size(), is(3));
94 assertTrue(xconnects.contains(KEY1));
95 assertTrue(xconnects.contains(KEY2));
96 assertTrue(xconnects.contains(KEY3));
97 assertFalse(xconnects.contains(KEY4));
98 }
99
100 /**
101 * Tests getPorts.
102 */
103 @Test
104 public void testGetPorts() {
105 Set<PortNumber> ports;
106
107 ports = config.getPorts(KEY1);
108 assertThat(ports.size(), is(2));
109 assertTrue(ports.contains(PORT3));
110 assertTrue(ports.contains(PORT4));
111
112 ports = config.getPorts(KEY2);
113 assertThat(ports.size(), is(2));
114 assertTrue(ports.contains(PORT3));
115 assertTrue(ports.contains(PORT4));
116
117 ports = config.getPorts(KEY3);
118 assertThat(ports.size(), is(2));
119 assertTrue(ports.contains(PORT4));
120 assertTrue(ports.contains(PORT5));
121 }
122
123 private class MockDelegate implements ConfigApplyDelegate {
124 @Override
125 public void onApply(Config config) {
126 }
127 }
128}