Jian Li | 7564231 | 2017-01-19 14:23:05 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jian Li | 7564231 | 2017-01-19 14:23:05 -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 | package org.onosproject.mapping.impl; |
| 17 | |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Iterables; |
| 20 | import com.google.common.collect.Maps; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Service; |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 27 | import org.onosproject.mapping.DefaultMapping; |
| 28 | import org.onosproject.mapping.DefaultMappingEntry; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 29 | import org.onosproject.mapping.Mapping; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 30 | import org.onosproject.mapping.MappingEntry; |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 31 | import org.onosproject.mapping.MappingEvent; |
| 32 | import org.onosproject.mapping.MappingId; |
| 33 | import org.onosproject.mapping.MappingKey; |
Jian Li | 7564231 | 2017-01-19 14:23:05 -0800 | [diff] [blame] | 34 | import org.onosproject.mapping.MappingStore; |
Jian Li | 95edb59 | 2017-01-29 08:42:07 +0900 | [diff] [blame] | 35 | import org.onosproject.mapping.MappingStoreDelegate; |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 36 | import org.onosproject.mapping.MappingTreatment; |
| 37 | import org.onosproject.mapping.MappingValue; |
| 38 | import org.onosproject.mapping.actions.MappingAction; |
| 39 | import org.onosproject.mapping.addresses.MappingAddress; |
| 40 | import org.onosproject.mapping.instructions.MappingInstruction; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 41 | import org.onosproject.net.DeviceId; |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 42 | import org.onosproject.net.device.DeviceService; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 43 | import org.onosproject.store.AbstractStore; |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 44 | import org.onosproject.store.serializers.KryoNamespaces; |
| 45 | import org.onosproject.store.service.ConsistentMap; |
| 46 | import org.onosproject.store.service.MapEvent; |
| 47 | import org.onosproject.store.service.MapEventListener; |
| 48 | import org.onosproject.store.service.Serializer; |
| 49 | import org.onosproject.store.service.StorageService; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 50 | import org.slf4j.Logger; |
| 51 | |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 52 | import java.util.Map; |
| 53 | import java.util.concurrent.atomic.AtomicInteger; |
| 54 | import java.util.stream.Collectors; |
| 55 | |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 56 | import static org.slf4j.LoggerFactory.getLogger; |
Jian Li | 7564231 | 2017-01-19 14:23:05 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Implementation of a distributed store for managing mapping information. |
| 60 | */ |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 61 | @Component(immediate = true) |
| 62 | @Service |
| 63 | public class DistributedMappingStore |
| 64 | extends AbstractStore<MappingEvent, MappingStoreDelegate> |
| 65 | implements MappingStore { |
| 66 | |
| 67 | private final Logger log = getLogger(getClass()); |
| 68 | |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 69 | private ConsistentMap<MappingId, Mapping> database; |
| 70 | private ConsistentMap<MappingId, Mapping> cache; |
| 71 | |
| 72 | private Map<MappingId, Mapping> databaseMap; |
| 73 | private Map<MappingId, Mapping> cacheMap; |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected StorageService storageService; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected DeviceService deviceService; |
| 80 | |
| 81 | private final MapEventListener<MappingId, Mapping> listener = new InternalListener(); |
| 82 | |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 83 | @Activate |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 84 | public void activate() { |
| 85 | |
| 86 | Serializer serializer = Serializer.using(KryoNamespaces.API, |
| 87 | Mapping.class, |
| 88 | DefaultMapping.class, |
| 89 | MappingId.class, |
| 90 | MappingEvent.Type.class, |
| 91 | MappingKey.class, |
| 92 | MappingValue.class, |
| 93 | MappingAddress.class, |
| 94 | MappingAddress.Type.class, |
| 95 | MappingAction.class, |
| 96 | MappingAction.Type.class, |
| 97 | MappingTreatment.class, |
| 98 | MappingInstruction.class, |
| 99 | MappingInstruction.Type.class); |
| 100 | |
| 101 | database = storageService.<MappingId, Mapping>consistentMapBuilder() |
| 102 | .withName("onos-mapping-database") |
| 103 | .withSerializer(serializer) |
| 104 | .build(); |
| 105 | |
| 106 | cache = storageService.<MappingId, Mapping>consistentMapBuilder() |
| 107 | .withName("onos-mapping-cache") |
| 108 | .withSerializer(serializer) |
| 109 | .build(); |
| 110 | |
| 111 | database.addListener(listener); |
| 112 | cache.addListener(listener); |
| 113 | databaseMap = database.asJavaMap(); |
| 114 | cacheMap = cache.asJavaMap(); |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 115 | log.info("Started"); |
| 116 | } |
| 117 | |
| 118 | @Deactivate |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 119 | public void deactivate() { |
| 120 | database.removeListener(listener); |
| 121 | cache.removeListener(listener); |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 122 | log.info("Stopped"); |
| 123 | } |
| 124 | |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 125 | /** |
| 126 | * Obtains map representation of mapping store. |
| 127 | * |
| 128 | * @param type mapping store type |
| 129 | * @return map representation of mapping store |
| 130 | */ |
| 131 | private Map<MappingId, Mapping> getStoreMap(Type type) { |
| 132 | switch (type) { |
| 133 | case MAP_DATABASE: |
| 134 | return databaseMap; |
| 135 | case MAP_CACHE: |
| 136 | return cacheMap; |
| 137 | default: |
| 138 | log.warn("Unrecognized map store type {}", type); |
| 139 | return Maps.newConcurrentMap(); |
| 140 | } |
Jian Li | 95edb59 | 2017-01-29 08:42:07 +0900 | [diff] [blame] | 141 | } |
| 142 | |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 143 | /** |
| 144 | * Obtains mapping store. |
| 145 | * |
| 146 | * @param type mapping store type |
| 147 | * @return mapping store |
| 148 | */ |
| 149 | private ConsistentMap<MappingId, Mapping> getStore(Type type) { |
| 150 | switch (type) { |
| 151 | case MAP_DATABASE: |
| 152 | return database; |
| 153 | case MAP_CACHE: |
| 154 | return cache; |
| 155 | default: |
| 156 | throw new IllegalArgumentException("Wrong mapping store " + type); |
| 157 | } |
Jian Li | 95edb59 | 2017-01-29 08:42:07 +0900 | [diff] [blame] | 158 | } |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 159 | |
| 160 | @Override |
| 161 | public int getMappingCount(Type type) { |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 162 | AtomicInteger sum = new AtomicInteger(0); |
| 163 | deviceService.getDevices().forEach(device -> |
| 164 | sum.addAndGet(Iterables.size(getMappingEntries(type, device.id())))); |
| 165 | return sum.get(); |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
Jian Li | 791dd32 | 2017-04-20 00:15:56 +0900 | [diff] [blame] | 169 | public Iterable<MappingEntry> getAllMappingEntries(Type type) { |
| 170 | |
| 171 | Map<MappingId, Mapping> storeMap = getStoreMap(type); |
| 172 | return ImmutableList.copyOf(storeMap.values().stream() |
| 173 | .map(DefaultMappingEntry::new) |
| 174 | .collect(Collectors.toList())); |
| 175 | } |
| 176 | |
| 177 | @Override |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 178 | public MappingEntry getMappingEntry(Type type, Mapping mapping) { |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 179 | |
| 180 | return new DefaultMappingEntry(getStoreMap(type).get(mapping.id())); |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public Iterable<MappingEntry> getMappingEntries(Type type, DeviceId deviceId) { |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 185 | |
| 186 | Map<MappingId, Mapping> storeMap = getStoreMap(type); |
| 187 | return ImmutableList.copyOf(storeMap.values().stream() |
| 188 | .filter(m -> m.deviceId() == deviceId) |
| 189 | .map(DefaultMappingEntry::new) |
| 190 | .collect(Collectors.toList())); |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
Jian Li | a1f960b | 2017-04-25 19:12:53 +0900 | [diff] [blame] | 194 | public void storeMapping(Type type, MappingEntry mapping) { |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 195 | |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 196 | getStore(type).put(mapping.id(), mapping); |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 200 | public MappingEvent removeMapping(Type type, Mapping mapping) { |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 201 | |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 202 | getStore(type).remove(mapping.id()); |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 203 | return null; |
| 204 | } |
| 205 | |
| 206 | @Override |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 207 | public void pendingDeleteMapping(Type type, Mapping mapping) { |
| 208 | // TODO: this will be implemented when management plane is ready |
| 209 | log.error("This method will be available when management plane is ready"); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public MappingEvent addOrUpdateMappingEntry(Type type, MappingEntry entry) { |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 214 | // TODO: this will be implemented when management plane is ready |
| 215 | log.error("This method will be available when management plane is ready"); |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 216 | return null; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public MappingEvent pendingMappingEntry(Type type, MappingEntry entry) { |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 221 | // TODO: this will be implemented when management plane is ready |
| 222 | log.error("This method will be available when management plane is ready"); |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 223 | return null; |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void purgeMappingEntries(Type type) { |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 228 | getStore(type).clear(); |
| 229 | } |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 230 | |
Jian Li | 2c52e56 | 2017-03-02 00:15:45 +0900 | [diff] [blame] | 231 | /** |
| 232 | * Event listener to notify delegates about mapping events. |
| 233 | */ |
| 234 | private class InternalListener implements MapEventListener<MappingId, Mapping> { |
| 235 | |
| 236 | @Override |
| 237 | public void event(MapEvent<MappingId, Mapping> event) { |
| 238 | final MappingEvent.Type type; |
| 239 | final Mapping mapping; |
| 240 | |
| 241 | switch (event.type()) { |
| 242 | case INSERT: |
| 243 | type = MappingEvent.Type.MAPPING_ADDED; |
| 244 | mapping = event.newValue().value(); |
| 245 | break; |
| 246 | case UPDATE: |
| 247 | type = MappingEvent.Type.MAPPING_UPDATED; |
| 248 | mapping = event.newValue().value(); |
| 249 | break; |
| 250 | case REMOVE: |
| 251 | type = MappingEvent.Type.MAPPING_REMOVED; |
| 252 | mapping = event.oldValue().value(); |
| 253 | break; |
| 254 | default: |
| 255 | throw new IllegalArgumentException("Wrong event type " + event.type()); |
| 256 | } |
| 257 | notifyDelegate(new MappingEvent(type, mapping)); |
| 258 | } |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 259 | } |
Jian Li | 7564231 | 2017-01-19 14:23:05 -0800 | [diff] [blame] | 260 | } |