Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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.store.pi.impl; |
| 18 | |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 19 | import org.onosproject.net.pi.runtime.PiEntity; |
| 20 | import org.onosproject.net.pi.runtime.PiHandle; |
| 21 | import org.onosproject.net.pi.service.PiTranslatable; |
| 22 | import org.onosproject.net.pi.service.PiTranslatedEntity; |
| 23 | import org.onosproject.net.pi.service.PiTranslationEvent; |
| 24 | import org.onosproject.net.pi.service.PiTranslationStore; |
| 25 | import org.onosproject.net.pi.service.PiTranslationStoreDelegate; |
| 26 | import org.onosproject.store.AbstractStore; |
| 27 | import org.onosproject.store.serializers.KryoNamespaces; |
| 28 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 29 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 30 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 31 | import org.onosproject.store.service.StorageService; |
| 32 | import org.onosproject.store.service.WallClockTimestamp; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 33 | import org.osgi.service.component.annotations.Activate; |
| 34 | import org.osgi.service.component.annotations.Deactivate; |
| 35 | import org.osgi.service.component.annotations.Reference; |
| 36 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | |
| 39 | import static com.google.common.base.Preconditions.checkArgument; |
| 40 | import static com.google.common.base.Preconditions.checkNotNull; |
| 41 | import static java.lang.String.format; |
| 42 | import static org.slf4j.LoggerFactory.getLogger; |
| 43 | |
| 44 | /** |
| 45 | * Distributed implementation of PiTranslationStore. |
| 46 | */ |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 47 | public abstract class AbstractDistributedPiTranslationStore |
| 48 | <T extends PiTranslatable, E extends PiEntity> |
| 49 | extends AbstractStore<PiTranslationEvent<T, E>, PiTranslationStoreDelegate<T, E>> |
| 50 | implements PiTranslationStore<T, E> { |
| 51 | |
| 52 | private static final String MAP_NAME_TEMPLATE = "onos-pi-translated-%s-map"; |
| 53 | |
| 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 57 | protected StorageService storageService; |
| 58 | |
| 59 | private EventuallyConsistentMap<PiHandle<E>, PiTranslatedEntity<T, E>> |
| 60 | translatedEntities; |
| 61 | |
| 62 | private final EventuallyConsistentMapListener |
| 63 | <PiHandle<E>, PiTranslatedEntity<T, E>> entityMapListener = |
| 64 | new InternalEntityMapListener(); |
| 65 | |
| 66 | /** |
| 67 | * Returns a string that identifies the map maintained by this store among |
| 68 | * others that uses this abstract class. |
| 69 | * |
| 70 | * @return string |
| 71 | */ |
| 72 | protected abstract String mapSimpleName(); |
| 73 | |
| 74 | @Activate |
| 75 | public void activate() { |
| 76 | final String fullMapName = format(MAP_NAME_TEMPLATE, mapSimpleName()); |
| 77 | translatedEntities = storageService |
| 78 | .<PiHandle<E>, PiTranslatedEntity<T, E>>eventuallyConsistentMapBuilder() |
| 79 | .withName(fullMapName) |
| 80 | .withSerializer(KryoNamespaces.API) |
| 81 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 82 | .build(); |
| 83 | translatedEntities.addListener(entityMapListener); |
| 84 | log.info("Started"); |
| 85 | } |
| 86 | |
| 87 | @Deactivate |
| 88 | public void deactivate() { |
| 89 | translatedEntities.removeListener(entityMapListener); |
| 90 | translatedEntities = null; |
| 91 | log.info("Stopped"); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void addOrUpdate(PiHandle<E> handle, PiTranslatedEntity<T, E> entity) { |
| 96 | checkNotNull(handle); |
| 97 | checkNotNull(entity); |
| 98 | checkArgument(handle.entityType().equals(entity.entityType()), |
| 99 | "Entity type must be the same for handle and translated entity"); |
| 100 | translatedEntities.put(handle, entity); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void remove(PiHandle<E> handle) { |
| 105 | checkNotNull(handle); |
| 106 | translatedEntities.remove(handle); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public PiTranslatedEntity<T, E> get(PiHandle<E> handle) { |
| 111 | checkNotNull(handle); |
| 112 | return translatedEntities.get(handle); |
| 113 | } |
| 114 | |
| 115 | public Iterable<PiTranslatedEntity<T, E>> getAll() { |
| 116 | return translatedEntities.values(); |
| 117 | } |
| 118 | |
| 119 | private class InternalEntityMapListener |
| 120 | implements EventuallyConsistentMapListener |
| 121 | <PiHandle<E>, PiTranslatedEntity<T, E>> { |
| 122 | |
| 123 | @Override |
| 124 | public void event(EventuallyConsistentMapEvent<PiHandle<E>, |
| 125 | PiTranslatedEntity<T, E>> event) { |
| 126 | final PiTranslationEvent.Type type; |
| 127 | switch (event.type()) { |
| 128 | case PUT: |
| 129 | type = PiTranslationEvent.Type.LEARNED; |
| 130 | break; |
| 131 | case REMOVE: |
| 132 | type = PiTranslationEvent.Type.FORGOT; |
| 133 | break; |
| 134 | default: |
| 135 | throw new IllegalArgumentException( |
| 136 | "Unknown event type " + event.type().name()); |
| 137 | } |
| 138 | notifyDelegate(new PiTranslationEvent<>(type, event.value())); |
| 139 | } |
| 140 | } |
| 141 | } |