Carmelo Cascone | 1a7e4f9 | 2017-11-20 23:04:02 -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 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onlab.util.KryoNamespace; |
| 26 | import org.onosproject.net.pi.model.PiPipeconfId; |
| 27 | import org.onosproject.net.pi.runtime.PiEntity; |
| 28 | import org.onosproject.net.pi.service.PiTranslatable; |
| 29 | import org.onosproject.net.pi.service.PiTranslatedEntity; |
| 30 | import org.onosproject.net.pi.service.PiTranslationEvent; |
| 31 | import org.onosproject.net.pi.service.PiTranslationStore; |
| 32 | import org.onosproject.net.pi.service.PiTranslationStoreDelegate; |
| 33 | import org.onosproject.store.AbstractStore; |
| 34 | import org.onosproject.store.serializers.KryoNamespaces; |
| 35 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 36 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 37 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 38 | import org.onosproject.store.service.StorageService; |
| 39 | import org.onosproject.store.service.WallClockTimestamp; |
| 40 | import org.slf4j.Logger; |
| 41 | |
| 42 | import java.util.Objects; |
| 43 | import java.util.Set; |
| 44 | import java.util.stream.Collectors; |
| 45 | |
| 46 | import static org.slf4j.LoggerFactory.getLogger; |
| 47 | |
| 48 | /** |
| 49 | * Distributed implementation of PiTranslationStore. |
| 50 | */ |
| 51 | @Component(immediate = true) |
| 52 | @Service |
| 53 | public class DistributedPiTranslationStore |
| 54 | extends AbstractStore<PiTranslationEvent, PiTranslationStoreDelegate> |
| 55 | implements PiTranslationStore { |
| 56 | |
| 57 | private static final String DIST_MAP_NAME = "onos-pi-translated-entities-map"; |
| 58 | private final Logger log = getLogger(getClass()); |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected StorageService storageService; |
| 62 | |
| 63 | private EventuallyConsistentMap<PiTranslatedEntityKey, PiTranslatedEntity> |
| 64 | translatedEntities; |
| 65 | |
| 66 | private final EventuallyConsistentMapListener<PiTranslatedEntityKey, |
| 67 | PiTranslatedEntity> entityMapListener = new InternalEntityMapListener(); |
| 68 | |
| 69 | @Activate |
| 70 | public void activate() { |
| 71 | translatedEntities = storageService |
| 72 | .<PiTranslatedEntityKey, PiTranslatedEntity>eventuallyConsistentMapBuilder() |
| 73 | .withName(DIST_MAP_NAME) |
| 74 | .withSerializer(KryoNamespace.newBuilder() |
| 75 | .register(KryoNamespaces.API) |
| 76 | .register(PiTranslatedEntityKey.class) |
| 77 | .build()) |
| 78 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 79 | .build(); |
| 80 | translatedEntities.addListener(entityMapListener); |
| 81 | log.info("Started"); |
| 82 | } |
| 83 | |
| 84 | @Deactivate |
| 85 | public void deactivate() { |
| 86 | translatedEntities.removeListener(entityMapListener); |
| 87 | translatedEntities = null; |
| 88 | log.info("Stopped"); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void addOrUpdate(PiTranslatable original, PiEntity translated, |
| 93 | PiPipeconfId pipeconfId) { |
| 94 | translatedEntities.put(PiTranslatedEntityKey.of(pipeconfId, translated), |
| 95 | new PiTranslatedEntity(original, translated, pipeconfId)); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void remove(PiEntity piEntity, PiPipeconfId pipeconfId) { |
| 100 | translatedEntities.remove( |
| 101 | PiTranslatedEntityKey.of(pipeconfId, piEntity)); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void removeAll(PiPipeconfId pipeconfId) { |
| 106 | // FIXME: this can be heavy, but we assume it won't be called that often |
| 107 | // How often we expect a pipeconf to be removed from the device/system? |
| 108 | final Set<PiTranslatedEntityKey> keysToRemove = translatedEntities |
| 109 | .keySet().parallelStream() |
| 110 | .filter(k -> k.pipeconfId.equals(pipeconfId)) |
| 111 | .collect(Collectors.toSet()); |
| 112 | keysToRemove.forEach(translatedEntities::remove); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public PiTranslatable lookup(PiEntity piEntity, PiPipeconfId pipeconfId) { |
| 117 | PiTranslatedEntity translatedEntity = translatedEntities |
| 118 | .get(PiTranslatedEntityKey.of(pipeconfId, piEntity)); |
| 119 | return translatedEntity == null ? null : translatedEntity.original(); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | private class InternalEntityMapListener |
| 124 | implements EventuallyConsistentMapListener |
| 125 | <PiTranslatedEntityKey, PiTranslatedEntity> { |
| 126 | |
| 127 | @Override |
| 128 | public void event(EventuallyConsistentMapEvent<PiTranslatedEntityKey, |
| 129 | PiTranslatedEntity> event) { |
| 130 | final PiTranslationEvent.Type type; |
| 131 | switch (event.type()) { |
| 132 | case PUT: |
| 133 | type = PiTranslationEvent.Type.LEARNED; |
| 134 | break; |
| 135 | case REMOVE: |
| 136 | type = PiTranslationEvent.Type.FORGOT; |
| 137 | break; |
| 138 | default: |
| 139 | throw new IllegalArgumentException( |
| 140 | "Unknown event type " + event.type().name()); |
| 141 | } |
| 142 | notifyDelegate(new PiTranslationEvent(type, event.value())); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Internal representation of a key that uniquely identifies a translated |
| 148 | * entity. |
| 149 | */ |
| 150 | private static final class PiTranslatedEntityKey { |
| 151 | |
| 152 | private final PiPipeconfId pipeconfId; |
| 153 | private final PiEntity piEntity; |
| 154 | |
| 155 | private PiTranslatedEntityKey(PiPipeconfId pipeconfId, |
| 156 | PiEntity piEntity) { |
| 157 | this.pipeconfId = pipeconfId; |
| 158 | this.piEntity = piEntity; |
| 159 | } |
| 160 | |
| 161 | public static PiTranslatedEntityKey of(PiPipeconfId pipeconfId, |
| 162 | PiEntity piEntity) { |
| 163 | return new PiTranslatedEntityKey(pipeconfId, piEntity); |
| 164 | } |
| 165 | |
| 166 | public static PiTranslatedEntityKey of(PiTranslatedEntity entity) { |
| 167 | return new PiTranslatedEntityKey(entity.pipeconfId(), |
| 168 | entity.translated()); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public int hashCode() { |
| 173 | return Objects.hash(pipeconfId, piEntity); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public boolean equals(Object obj) { |
| 178 | if (this == obj) { |
| 179 | return true; |
| 180 | } |
| 181 | if (obj == null || getClass() != obj.getClass()) { |
| 182 | return false; |
| 183 | } |
| 184 | final PiTranslatedEntityKey other = (PiTranslatedEntityKey) obj; |
| 185 | return Objects.equals(this.pipeconfId, other.pipeconfId) |
| 186 | && Objects.equals(this.piEntity, other.piEntity); |
| 187 | } |
| 188 | } |
| 189 | } |