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