Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 17 | package org.onosproject.segmentrouting.config; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.TestApplicationId; |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 28 | import org.onosproject.net.config.Config; |
| 29 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 30 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 31 | import java.util.Set; |
| 32 | |
| 33 | import static org.hamcrest.Matchers.is; |
| 34 | import static org.junit.Assert.assertThat; |
| 35 | import static org.junit.Assert.assertTrue; |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 36 | import static org.junit.Assert.assertFalse; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Tests for class {@link SegmentRoutingAppConfig}. |
| 40 | */ |
| 41 | public class SegmentRoutingAppConfigTest { |
| 42 | private static final ApplicationId APP_ID = |
| 43 | new TestApplicationId(SegmentRoutingService.SR_APP_ID); |
| 44 | |
| 45 | private SegmentRoutingAppConfig config; |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 46 | private SegmentRoutingAppConfig invalidConfig; |
| 47 | private static final String JSON_STRING = "{" + |
| 48 | "\"vRouterMacs\" : [" + |
| 49 | " \"00:00:00:00:00:01\"," + |
| 50 | " \"00:00:00:00:00:02\"" + |
| 51 | "]," + |
| 52 | "\"vRouterId\" : \"of:1\"," + |
| 53 | "\"excludePorts\" : [" + |
| 54 | " \"port1\"," + |
| 55 | " \"port2\"" + |
| 56 | "]}"; |
| 57 | private static final String INVALID_JSON_STRING = "{" + |
| 58 | "\"vRouterMacs\" : [" + |
| 59 | " \"00:00:00:00:00:01\"," + |
| 60 | " \"00:00:00:00:00:02\"" + |
| 61 | "]," + |
| 62 | "\"excludePorts\" : [" + |
| 63 | " \"port1\"," + |
| 64 | " \"port2\"" + |
| 65 | "]}"; |
| 66 | private static final MacAddress ROUTER_MAC_1 = MacAddress.valueOf("00:00:00:00:00:01"); |
| 67 | private static final MacAddress ROUTER_MAC_2 = MacAddress.valueOf("00:00:00:00:00:02"); |
| 68 | private static final MacAddress ROUTER_MAC_3 = MacAddress.valueOf("00:00:00:00:00:03"); |
| 69 | private static final String PORT_NAME_1 = "port1"; |
| 70 | private static final String PORT_NAME_2 = "port2"; |
| 71 | private static final String PORT_NAME_3 = "port3"; |
| 72 | private static final DeviceId VROUTER_ID_1 = DeviceId.deviceId("of:1"); |
| 73 | private static final DeviceId VROUTER_ID_2 = DeviceId.deviceId("of:2"); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Initialize test related variables. |
| 77 | * |
| 78 | * @throws Exception |
| 79 | */ |
| 80 | @Before |
| 81 | public void setUp() throws Exception { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 82 | ApplicationId subject = APP_ID; |
| 83 | String key = SegmentRoutingService.SR_APP_ID; |
| 84 | ObjectMapper mapper = new ObjectMapper(); |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 85 | JsonNode jsonNode = mapper.readTree(JSON_STRING); |
| 86 | JsonNode invalidJsonNode = mapper.readTree(INVALID_JSON_STRING); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 87 | ConfigApplyDelegate delegate = new MockDelegate(); |
| 88 | |
| 89 | config = new SegmentRoutingAppConfig(); |
| 90 | config.init(subject, key, jsonNode, mapper, delegate); |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 91 | invalidConfig = new SegmentRoutingAppConfig(); |
| 92 | invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 96 | * Tests config validity. |
| 97 | * |
| 98 | * @throws Exception |
| 99 | */ |
| 100 | @Test |
| 101 | public void testIsValid() throws Exception { |
| 102 | assertTrue(config.isValid()); |
| 103 | assertFalse(invalidConfig.isValid()); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Tests vRouterMacs getter. |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 108 | * |
| 109 | * @throws Exception |
| 110 | */ |
| 111 | @Test |
| 112 | public void testVRouters() throws Exception { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 113 | Set<MacAddress> vRouters = config.vRouterMacs(); |
| 114 | assertThat(vRouters.size(), is(2)); |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 115 | assertTrue(vRouters.contains(ROUTER_MAC_1)); |
| 116 | assertTrue(vRouters.contains(ROUTER_MAC_2)); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 120 | * Tests vRouterMacs setter. |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 121 | * |
| 122 | * @throws Exception |
| 123 | */ |
| 124 | @Test |
| 125 | public void testSetVRouters() throws Exception { |
| 126 | ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder(); |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 127 | builder.add(ROUTER_MAC_3); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 128 | config.setVRouterMacs(builder.build()); |
| 129 | |
| 130 | Set<MacAddress> macs = config.vRouterMacs(); |
| 131 | assertThat(macs.size(), is(1)); |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 132 | assertTrue(macs.contains(ROUTER_MAC_3)); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Tests vRouterId getter. |
| 137 | * |
| 138 | * @throws Exception |
| 139 | */ |
| 140 | @Test |
| 141 | public void testVRouterId() throws Exception { |
| 142 | assertThat(config.vRouterId(), is(VROUTER_ID_1)); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Tests vRouterId setter. |
| 147 | * |
| 148 | * @throws Exception |
| 149 | */ |
| 150 | @Test |
| 151 | public void testSetVRouterId() throws Exception { |
| 152 | config.setVRouterId(VROUTER_ID_2); |
| 153 | assertThat(config.vRouterId(), is(VROUTER_ID_2)); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Tests excludePort getter. |
| 158 | * |
| 159 | * @throws Exception |
| 160 | */ |
| 161 | @Test |
| 162 | public void testExcludePorts() throws Exception { |
| 163 | Set<String> excludePorts = config.excludePorts(); |
| 164 | assertThat(excludePorts.size(), is(2)); |
| 165 | assertTrue(excludePorts.contains(PORT_NAME_1)); |
| 166 | assertTrue(excludePorts.contains(PORT_NAME_2)); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Tests excludePort setter. |
| 171 | * |
| 172 | * @throws Exception |
| 173 | */ |
| 174 | @Test |
| 175 | public void testSetExcludePorts() throws Exception { |
| 176 | ImmutableSet.Builder<String> builder = ImmutableSet.builder(); |
| 177 | builder.add(PORT_NAME_3); |
| 178 | config.setExcludePorts(builder.build()); |
| 179 | |
| 180 | Set<String> excludePorts = config.excludePorts(); |
| 181 | assertThat(excludePorts.size(), is(1)); |
| 182 | assertTrue(excludePorts.contains(PORT_NAME_3)); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | private class MockDelegate implements ConfigApplyDelegate { |
| 186 | @Override |
| 187 | public void onApply(Config config) { |
| 188 | } |
| 189 | } |
| 190 | } |