Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | package org.onosproject.mapping.impl; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.ChassisId; |
| 23 | import org.onlab.packet.IpPrefix; |
| 24 | import org.onosproject.mapping.DefaultMapping; |
| 25 | import org.onosproject.mapping.DefaultMappingKey; |
| 26 | import org.onosproject.mapping.DefaultMappingTreatment; |
| 27 | import org.onosproject.mapping.DefaultMappingValue; |
| 28 | import org.onosproject.mapping.Mapping; |
| 29 | import org.onosproject.mapping.MappingKey; |
| 30 | import org.onosproject.mapping.MappingTreatment; |
| 31 | import org.onosproject.mapping.MappingValue; |
| 32 | import org.onosproject.mapping.actions.MappingAction; |
| 33 | import org.onosproject.mapping.actions.MappingActions; |
| 34 | import org.onosproject.mapping.addresses.MappingAddress; |
| 35 | import org.onosproject.mapping.addresses.MappingAddresses; |
| 36 | import org.onosproject.net.Annotations; |
| 37 | import org.onosproject.net.DefaultAnnotations; |
| 38 | import org.onosproject.net.DefaultDevice; |
| 39 | import org.onosproject.net.Device; |
| 40 | import org.onosproject.net.DeviceId; |
| 41 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 42 | import org.onosproject.net.provider.ProviderId; |
| 43 | import org.onosproject.store.service.TestStorageService; |
| 44 | |
| 45 | import java.util.List; |
| 46 | |
| 47 | import static org.junit.Assert.assertFalse; |
| 48 | import static org.junit.Assert.assertThat; |
| 49 | import static org.junit.Assert.assertTrue; |
| 50 | import static org.hamcrest.Matchers.is; |
| 51 | import static org.onosproject.mapping.MappingStore.Type.MAP_DATABASE; |
| 52 | |
| 53 | /** |
| 54 | * Unit tests for DistributedMappingStore. |
| 55 | */ |
| 56 | public class DistributedMappingStoreTest { |
| 57 | private static final DeviceId DEVICE_ID_1 = DeviceId.deviceId("foo"); |
| 58 | private static final DeviceId DEVICE_ID_2 = DeviceId.deviceId("bar"); |
| 59 | private static final String IP_ADDRESS = "1.2.3.4/24"; |
| 60 | |
| 61 | private DistributedMappingStore mappingStore; |
| 62 | private Mapping mapping1; |
| 63 | private Mapping mapping2; |
| 64 | |
| 65 | private Device device1; |
| 66 | private Device device2; |
| 67 | |
| 68 | /** |
| 69 | * Sets up the storage service test harness. |
| 70 | */ |
| 71 | @Before |
| 72 | public void setUp() { |
| 73 | mappingStore = new DistributedMappingStore(); |
| 74 | mappingStore.storageService = new TestStorageService(); |
| 75 | mappingStore.deviceService = new InternalDeviceServiceAdapter(); |
| 76 | mappingStore.setDelegate(event -> { |
| 77 | }); |
| 78 | |
| 79 | IpPrefix ipPrefix = IpPrefix.valueOf(IP_ADDRESS); |
| 80 | MappingAddress address = MappingAddresses.ipv4MappingAddress(ipPrefix); |
| 81 | |
| 82 | MappingKey key = DefaultMappingKey.builder() |
| 83 | .withAddress(address) |
| 84 | .build(); |
| 85 | |
| 86 | MappingAction action = MappingActions.noAction(); |
| 87 | MappingTreatment treatment = DefaultMappingTreatment.builder() |
| 88 | .withAddress(address) |
| 89 | .setUnicastPriority(10) |
| 90 | .setUnicastWeight(10) |
| 91 | .build(); |
| 92 | |
| 93 | MappingValue value = DefaultMappingValue.builder() |
| 94 | .withAction(action) |
| 95 | .add(treatment) |
| 96 | .build(); |
| 97 | |
| 98 | device1 = new MockDevice(ProviderId.NONE, DEVICE_ID_1, Device.Type.OTHER, |
| 99 | "foo.inc", "0", "0", "0", null, |
| 100 | DefaultAnnotations.builder().build()); |
| 101 | |
| 102 | device2 = new MockDevice(ProviderId.NONE, DEVICE_ID_2, Device.Type.OTHER, |
| 103 | "foo.inc", "0", "0", "0", null, |
| 104 | DefaultAnnotations.builder().build()); |
| 105 | |
| 106 | mapping1 = DefaultMapping.builder() |
| 107 | .forDevice(DEVICE_ID_1) |
| 108 | .withId(1000L) |
| 109 | .withKey(key) |
| 110 | .withValue(value) |
| 111 | .build(); |
| 112 | |
| 113 | mapping2 = DefaultMapping.builder() |
| 114 | .forDevice(DEVICE_ID_2) |
| 115 | .withId(2000L) |
| 116 | .withKey(key) |
| 117 | .withValue(value) |
| 118 | .build(); |
| 119 | |
| 120 | mappingStore.activate(); |
| 121 | } |
| 122 | |
| 123 | private class InternalDeviceServiceAdapter extends DeviceServiceAdapter { |
| 124 | |
| 125 | List<Device> devices = Lists.newArrayList(); |
| 126 | |
| 127 | @Override |
| 128 | public Iterable<Device> getDevices() { |
| 129 | devices.add(device1); |
| 130 | devices.add(device2); |
| 131 | return devices; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | private class MockDevice extends DefaultDevice { |
| 136 | public MockDevice(ProviderId providerId, DeviceId id, Type type, |
| 137 | String manufacturer, String hwVersion, String swVersion, |
| 138 | String serialNumber, ChassisId chassisId, Annotations... annotations) { |
| 139 | super(providerId, id, type, manufacturer, hwVersion, swVersion, serialNumber, |
| 140 | chassisId, annotations); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Tears down the mapping1 store. |
| 146 | */ |
| 147 | @After |
| 148 | public void tearDown() { |
| 149 | mappingStore.deactivate(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Tests adding, removing and getting. |
| 154 | */ |
| 155 | @Test |
| 156 | public void basics() { |
| 157 | mappingStore.storeMapping(MAP_DATABASE, mapping1); |
| 158 | mappingStore.storeMapping(MAP_DATABASE, mapping2); |
| 159 | |
| 160 | assertThat("There should be one mapping1 in the map database.", |
| 161 | mappingStore.getMappingCount(MAP_DATABASE), is(2)); |
| 162 | assertTrue("There should be one mapping1 in the map database.", |
| 163 | mapping1.equals(mappingStore.getMappingEntries(MAP_DATABASE, DEVICE_ID_1) |
| 164 | .iterator().next())); |
| 165 | assertTrue("The mapping1 should be identical.", |
| 166 | mappingStore.getMappingEntry(MAP_DATABASE, mapping1).equals(mapping1)); |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 167 | mappingStore.removeMapping(MAP_DATABASE, mapping1); |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 168 | assertFalse("There should not be any mapping1 in the map database.", |
| 169 | mappingStore.getMappingEntries(MAP_DATABASE, DEVICE_ID_1).iterator().hasNext()); |
| 170 | } |
| 171 | } |