Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.segmentrouting.config; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.VlanId; |
| 24 | import org.onosproject.TestApplicationId; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.config.Config; |
| 29 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 30 | import org.onosproject.segmentrouting.SegmentRoutingManager; |
| 31 | import org.onosproject.segmentrouting.storekey.XConnectStoreKey; |
| 32 | import java.io.InputStream; |
| 33 | import java.util.Set; |
| 34 | import static org.junit.Assert.assertFalse; |
| 35 | import static org.junit.Assert.assertTrue; |
| 36 | import static org.junit.Assert.assertThat; |
| 37 | import static org.hamcrest.Matchers.is; |
| 38 | |
| 39 | /** |
| 40 | * Tests for class {@link XConnectConfig}. |
| 41 | */ |
| 42 | public 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 | |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 65 | String key = SegmentRoutingManager.APP_NAME; |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 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 | } |