Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [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.MplsLabel; |
| 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onosproject.TestApplicationId; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 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.pwaas.DefaultL2Tunnel; |
| 32 | import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription; |
| 33 | import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy; |
| 34 | import org.onosproject.segmentrouting.pwaas.L2Mode; |
| 35 | |
| 36 | import java.io.InputStream; |
| 37 | import java.util.Set; |
| 38 | |
| 39 | import static org.hamcrest.Matchers.is; |
| 40 | import static org.junit.Assert.*; |
| 41 | |
| 42 | /** |
| 43 | * Unit tests for class {@link PwaasConfig}. |
| 44 | */ |
| 45 | public class PwaasConfigTest { |
| 46 | |
| 47 | private static final String TUNNEL_ID_1 = "1"; |
| 48 | private static final String TUNNEL_ID_2 = "20"; |
| 49 | private static final String NOT_PRESENT_TUNNEL_ID = "2"; |
| 50 | private static final ConnectPoint INGRESS_1 = ConnectPoint.deviceConnectPoint("of:0000000000000001/1"); |
| 51 | private static final ConnectPoint INGRESS_2 = ConnectPoint.deviceConnectPoint("of:0000000000000002/1"); |
| 52 | private static final ConnectPoint EGRESS_1 = ConnectPoint.deviceConnectPoint("of:0000000000000011/1"); |
| 53 | private static final ConnectPoint EGRESS_2 = ConnectPoint.deviceConnectPoint("of:0000000000000012/1"); |
| 54 | private static final VlanId INGRESS_INNER_TAG_1 = VlanId.vlanId("10"); |
| 55 | private static final VlanId INGRESS_INNER_TAG_2 = VlanId.vlanId("100"); |
| 56 | private static final VlanId INGRESS_OUTER_TAG_1 = VlanId.vlanId("20"); |
| 57 | private static final VlanId INGRESS_OUTER_TAG_2 = VlanId.vlanId("200"); |
| 58 | private static final VlanId EGRESS_INNER_TAG_1 = VlanId.vlanId("11"); |
| 59 | private static final VlanId EGRESS_INNER_TAG_2 = VlanId.vlanId("110"); |
| 60 | private static final VlanId EGRESS_OUTER_TAG_1 = VlanId.vlanId("21"); |
| 61 | private static final VlanId EGRESS_OUTER_TAG_2 = VlanId.vlanId("210"); |
| 62 | private static final String MODE_1 = "RAW"; |
| 63 | private static final String MODE_2 = "TAGGED"; |
| 64 | private static final boolean ALL_VLAN_1 = true; |
| 65 | private static final boolean ALL_VLAN_2 = false; |
| 66 | private static final VlanId SD_TAG_1 = VlanId.vlanId("40"); |
| 67 | private static final VlanId SD_TAG_2 = VlanId.NONE; |
| 68 | private static final MplsLabel PW_LABEL_1 = MplsLabel.mplsLabel("255"); |
| 69 | private static final MplsLabel PW_LABEL_2 = MplsLabel.mplsLabel("4095"); |
| 70 | |
| 71 | private PwaasConfig config; |
| 72 | private PwaasConfig invalidConfig; |
| 73 | |
| 74 | @Before |
| 75 | public void setUp() throws Exception { |
| 76 | InputStream jsonStream = PwaasConfig.class |
| 77 | .getResourceAsStream("/pwaas.json"); |
| 78 | InputStream invalidJsonStream = PwaasConfig.class |
| 79 | .getResourceAsStream("/pwaas-invalid.json"); |
| 80 | |
| 81 | String key = SegmentRoutingManager.APP_NAME; |
| 82 | ApplicationId subject = new TestApplicationId(key); |
| 83 | ObjectMapper mapper = new ObjectMapper(); |
| 84 | JsonNode jsonNode = mapper.readTree(jsonStream); |
| 85 | JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream); |
| 86 | ConfigApplyDelegate delegate = new MockDelegate(); |
| 87 | |
| 88 | config = new PwaasConfig(); |
| 89 | config.init(subject, key, jsonNode, mapper, delegate); |
| 90 | invalidConfig = new PwaasConfig(); |
| 91 | invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Tests config validity. |
| 96 | */ |
| 97 | @Test |
| 98 | public void testIsValid() { |
| 99 | assertTrue(config.isValid()); |
| 100 | assertFalse(invalidConfig.isValid()); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Tests getPwIds. |
| 105 | */ |
| 106 | @Test |
| 107 | public void testGetPwIds() { |
| 108 | Set<Long> pwIds = config.getPwIds(); |
| 109 | assertThat(pwIds.size(), is(2)); |
| 110 | assertTrue(pwIds.contains(Long.parseLong(TUNNEL_ID_1))); |
| 111 | assertTrue(pwIds.contains(Long.parseLong(TUNNEL_ID_2))); |
| 112 | assertFalse(pwIds.contains(Long.parseLong(NOT_PRESENT_TUNNEL_ID))); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Tests getPwDescription. |
| 117 | */ |
| 118 | @Test |
| 119 | public void testGetPwDescription() { |
| 120 | DefaultL2TunnelDescription l2TunnelDescription = null; |
| 121 | |
| 122 | DefaultL2Tunnel l2Tunnel = new DefaultL2Tunnel( |
| 123 | L2Mode.valueOf(MODE_1), |
| 124 | SD_TAG_1, |
| 125 | Long.parseLong(TUNNEL_ID_1), |
| 126 | PW_LABEL_1 |
| 127 | ); |
| 128 | DefaultL2TunnelPolicy l2TunnelPolicy = new DefaultL2TunnelPolicy( |
| 129 | Long.parseLong(TUNNEL_ID_1), |
| 130 | INGRESS_1, |
| 131 | INGRESS_INNER_TAG_1, |
| 132 | INGRESS_OUTER_TAG_1, |
| 133 | EGRESS_1, |
| 134 | EGRESS_INNER_TAG_1, |
| 135 | EGRESS_OUTER_TAG_1, |
| 136 | ALL_VLAN_1 |
| 137 | ); |
| 138 | l2TunnelDescription = config.getPwDescription(Long.parseLong(TUNNEL_ID_1)); |
| 139 | assertThat(l2TunnelDescription.l2Tunnel().pwMode(), is(l2Tunnel.pwMode())); |
| 140 | assertThat(l2TunnelDescription.l2Tunnel().sdTag(), is(l2Tunnel.sdTag())); |
| 141 | assertThat(l2TunnelDescription.l2Tunnel().tunnelId(), is(l2Tunnel.tunnelId())); |
| 142 | assertThat(l2TunnelDescription.l2Tunnel().pwLabel(), is(l2Tunnel.pwLabel())); |
| 143 | assertThat(l2TunnelDescription.l2TunnelPolicy().tunnelId(), is(l2TunnelPolicy.tunnelId())); |
| 144 | assertThat(l2TunnelDescription.l2TunnelPolicy().cP1InnerTag(), is(l2TunnelPolicy.cP1InnerTag())); |
| 145 | assertThat(l2TunnelDescription.l2TunnelPolicy().cP1OuterTag(), is(l2TunnelPolicy.cP1OuterTag())); |
| 146 | assertThat(l2TunnelDescription.l2TunnelPolicy().cP2InnerTag(), is(l2TunnelPolicy.cP2InnerTag())); |
| 147 | assertThat(l2TunnelDescription.l2TunnelPolicy().cP2OuterTag(), is(l2TunnelPolicy.cP2OuterTag())); |
| 148 | assertThat(l2TunnelDescription.l2TunnelPolicy().cP1(), is(l2TunnelPolicy.cP1())); |
| 149 | assertThat(l2TunnelDescription.l2TunnelPolicy().cP2(), is(l2TunnelPolicy.cP2())); |
| 150 | assertThat(l2TunnelDescription.l2TunnelPolicy().isAllVlan(), is(l2TunnelPolicy.isAllVlan())); |
| 151 | |
| 152 | l2Tunnel = new DefaultL2Tunnel( |
| 153 | L2Mode.valueOf(MODE_2), |
| 154 | SD_TAG_2, |
| 155 | Long.parseLong(TUNNEL_ID_2), |
| 156 | PW_LABEL_2 |
| 157 | ); |
| 158 | l2TunnelPolicy = new DefaultL2TunnelPolicy( |
| 159 | Long.parseLong(TUNNEL_ID_2), |
| 160 | INGRESS_2, |
| 161 | INGRESS_INNER_TAG_2, |
| 162 | INGRESS_OUTER_TAG_2, |
| 163 | EGRESS_2, |
| 164 | EGRESS_INNER_TAG_2, |
| 165 | EGRESS_OUTER_TAG_2, |
| 166 | ALL_VLAN_2 |
| 167 | ); |
| 168 | l2TunnelDescription = config.getPwDescription(Long.parseLong(TUNNEL_ID_2)); |
| 169 | assertThat(l2TunnelDescription.l2Tunnel().pwMode(), is(l2Tunnel.pwMode())); |
| 170 | assertThat(l2TunnelDescription.l2Tunnel().sdTag(), is(l2Tunnel.sdTag())); |
| 171 | assertThat(l2TunnelDescription.l2Tunnel().tunnelId(), is(l2Tunnel.tunnelId())); |
| 172 | assertThat(l2TunnelDescription.l2Tunnel().pwLabel(), is(l2Tunnel.pwLabel())); |
| 173 | assertThat(l2TunnelDescription.l2TunnelPolicy().tunnelId(), is(l2TunnelPolicy.tunnelId())); |
| 174 | assertThat(l2TunnelDescription.l2TunnelPolicy().cP1InnerTag(), is(l2TunnelPolicy.cP1InnerTag())); |
| 175 | assertThat(l2TunnelDescription.l2TunnelPolicy().cP1OuterTag(), is(l2TunnelPolicy.cP1OuterTag())); |
| 176 | assertThat(l2TunnelDescription.l2TunnelPolicy().cP2OuterTag(), is(l2TunnelPolicy.cP2OuterTag())); |
| 177 | assertThat(l2TunnelDescription.l2TunnelPolicy().cP2OuterTag(), is(l2TunnelPolicy.cP2OuterTag())); |
| 178 | assertThat(l2TunnelDescription.l2TunnelPolicy().cP1(), is(l2TunnelPolicy.cP1())); |
| 179 | assertThat(l2TunnelDescription.l2TunnelPolicy().cP2(), is(l2TunnelPolicy.cP2())); |
| 180 | assertThat(l2TunnelDescription.l2TunnelPolicy().isAllVlan(), is(l2TunnelPolicy.isAllVlan())); |
| 181 | |
| 182 | } |
| 183 | |
| 184 | private class MockDelegate implements ConfigApplyDelegate { |
| 185 | @Override |
| 186 | public void onApply(Config config) { |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | } |