Ray Milkey | 874581a | 2018-03-02 10:20:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.net.config.basics; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.IpPrefix; |
| 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.packet.VlanId; |
| 28 | import org.onosproject.net.ConnectPoint; |
| 29 | import org.onosproject.net.NetTestTools; |
| 30 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 31 | import org.onosproject.net.host.InterfaceIpAddress; |
| 32 | import org.onosproject.net.intf.Interface; |
| 33 | |
| 34 | import java.util.Collection; |
| 35 | import java.util.List; |
| 36 | import java.util.Set; |
| 37 | |
| 38 | import static org.hamcrest.MatcherAssert.assertThat; |
| 39 | import static org.hamcrest.Matchers.allOf; |
| 40 | import static org.hamcrest.Matchers.contains; |
| 41 | import static org.hamcrest.Matchers.hasItems; |
| 42 | import static org.hamcrest.Matchers.hasSize; |
| 43 | import static org.hamcrest.Matchers.is; |
| 44 | import static org.hamcrest.Matchers.notNullValue; |
| 45 | import static org.hamcrest.Matchers.nullValue; |
| 46 | |
| 47 | public class InterfaceConfigTest extends AbstractConfigTest { |
| 48 | |
| 49 | private JsonNode data; |
| 50 | |
| 51 | private ConnectPoint cp1 = NetTestTools.connectPoint("device1", 1); |
| 52 | private ConfigApplyDelegate delegate = configApply -> { }; |
| 53 | private VlanId vl1 = VlanId.vlanId((short) 1); |
| 54 | |
| 55 | private String name1 = getName(1); |
| 56 | private String name2 = getName(2); |
| 57 | private String name3 = getName(3); |
| 58 | private String name4 = getName(4); |
| 59 | |
| 60 | @Before |
| 61 | public void setUp() { |
| 62 | data = getTestJson("interface-config-1.json"); |
| 63 | } |
| 64 | |
| 65 | private String getName(int index) { |
| 66 | String nameTemplate = "interface"; |
| 67 | return nameTemplate + Integer.toString(index); |
| 68 | } |
| 69 | |
| 70 | private MacAddress getMac(int index) { |
| 71 | String macTemplate = "AB:CD:EF:00:00:0"; |
| 72 | return MacAddress.valueOf(macTemplate + Integer.toString(index)); |
| 73 | } |
| 74 | |
| 75 | private InterfaceIpAddress getIp(int index, int position) { |
| 76 | IpPrefix subnet1 = IpPrefix.valueOf("1.2.0.0/16"); |
| 77 | IpAddress ip = IpAddress.valueOf("1.2.3." + Integer.toString(index + ((position - 1) * 10))); |
| 78 | return new InterfaceIpAddress(ip, subnet1); |
| 79 | } |
| 80 | |
| 81 | private Interface findInterface(Collection<Interface> ifs, String name) { |
| 82 | return ifs.stream() |
| 83 | .filter(iface -> iface.name().equals(name)) |
| 84 | .findFirst() |
| 85 | .orElse(null); |
| 86 | } |
| 87 | |
| 88 | private void checkInterface(Interface fetchedInterface, int index) { |
| 89 | String name = getName(index); |
| 90 | assertThat(fetchedInterface, notNullValue()); |
| 91 | assertThat(fetchedInterface.name(), is(name)); |
| 92 | assertThat(fetchedInterface.connectPoint(), is(cp1)); |
| 93 | assertThat(fetchedInterface.mac(), is(getMac(index))); |
| 94 | List<InterfaceIpAddress> fetchedIpAddresses = fetchedInterface.ipAddressesList(); |
| 95 | assertThat(fetchedIpAddresses, hasItems(getIp(index, 1), getIp(index, 2))); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Tests construction, setters and getters of an InterfaceConfig object. |
| 100 | */ |
| 101 | @Test |
| 102 | public void testConstruction() throws Exception { |
| 103 | InterfaceConfig config = new InterfaceConfig(); |
| 104 | config.init(cp1, "KEY", data, mapper, delegate); |
| 105 | |
| 106 | assertThat(config.isValid(), is(true)); |
| 107 | assertThat(config.getInterfaces(), hasSize(4)); |
| 108 | |
| 109 | Interface fetchedInterface1 = findInterface(config.getInterfaces(), name1); |
| 110 | checkInterface(fetchedInterface1, 1); |
| 111 | assertThat(fetchedInterface1.vlan(), is(vl1)); |
| 112 | |
| 113 | Interface fetchedInterface2 = findInterface(config.getInterfaces(), name2); |
| 114 | checkInterface(fetchedInterface2, 2); |
| 115 | assertThat(fetchedInterface2.vlanUntagged().toShort(), is((short) 22)); |
| 116 | assertThat(fetchedInterface2.vlan().toShort(), is((short) 2)); |
| 117 | |
| 118 | Interface fetchedInterface3 = findInterface(config.getInterfaces(), name3); |
| 119 | checkInterface(fetchedInterface3, 3); |
| 120 | assertThat(fetchedInterface3.vlanTagged(), is(allOf(notNullValue(), hasSize(1)))); |
| 121 | assertThat(fetchedInterface3.vlan().toShort(), is((short) 3)); |
| 122 | assertThat(fetchedInterface3.vlanTagged(), contains(VlanId.vlanId((short) 33))); |
| 123 | |
| 124 | Interface fetchedInterface4 = findInterface(config.getInterfaces(), name4); |
| 125 | checkInterface(fetchedInterface4, 4); |
| 126 | assertThat(fetchedInterface4.vlanTagged(), is(allOf(notNullValue(), hasSize(1)))); |
| 127 | assertThat(fetchedInterface4.vlanTagged(), contains(VlanId.vlanId((short) 44))); |
| 128 | assertThat(fetchedInterface4.vlanNative().toShort(), is((short) 4)); |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void testAddRemoveInterface() throws Exception { |
| 133 | InterfaceConfig config = new InterfaceConfig(); |
| 134 | config.init(cp1, "KEY", data, mapper, delegate); |
| 135 | |
| 136 | Set<VlanId> vlanTagged = ImmutableSet.of(VlanId.vlanId((short) 33), VlanId.vlanId((short) 44)); |
| 137 | String newName = "interface5"; |
| 138 | Interface toAdd1 = new Interface( |
| 139 | newName, |
| 140 | cp1, |
| 141 | ImmutableList.of(getIp(5, 1), getIp(5, 2)), |
| 142 | getMac(5), |
| 143 | vl1, |
| 144 | null, |
| 145 | vlanTagged, |
| 146 | vl1); |
| 147 | config.addInterface(toAdd1); |
| 148 | |
| 149 | assertThat(config.isValid(), is(true)); |
| 150 | assertThat(config.getInterfaces(), allOf(notNullValue(), hasSize(5))); |
| 151 | |
| 152 | assertThat(findInterface(config.getInterfaces(), name1), notNullValue()); |
| 153 | assertThat(findInterface(config.getInterfaces(), name2), notNullValue()); |
| 154 | assertThat(findInterface(config.getInterfaces(), name3), notNullValue()); |
| 155 | |
| 156 | Interface addedInterface1 = findInterface(config.getInterfaces(), newName); |
| 157 | checkInterface(addedInterface1, 5); |
| 158 | assertThat(addedInterface1.vlan(), is(vl1)); |
| 159 | assertThat(addedInterface1.vlanTagged(), allOf(notNullValue(), hasSize(2))); |
| 160 | |
| 161 | config.removeInterface(newName); |
| 162 | assertThat(config.getInterfaces(), allOf(notNullValue(), hasSize(4))); |
| 163 | assertThat(findInterface(config.getInterfaces(), newName), nullValue()); |
| 164 | |
| 165 | newName = getName(6); |
| 166 | Interface toAdd2 = new Interface( |
| 167 | newName, |
| 168 | cp1, |
| 169 | ImmutableList.of(getIp(6, 1), getIp(6, 2)), |
| 170 | getMac(6), |
| 171 | vl1, |
| 172 | VlanId.vlanId((short) 77), |
| 173 | null, |
| 174 | vl1); |
| 175 | config.addInterface(toAdd2); |
| 176 | |
| 177 | Interface addedInterface2 = findInterface(config.getInterfaces(), newName); |
| 178 | checkInterface(addedInterface2, 6); |
| 179 | assertThat(addedInterface2.vlan(), is(vl1)); |
| 180 | assertThat(addedInterface2.vlanUntagged().toShort(), is((short) 77)); |
| 181 | } |
| 182 | |
| 183 | } |