Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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.p4runtime.ctl.controller; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
pierventre | 381d454 | 2021-01-15 17:27:48 +0100 | [diff] [blame^] | 20 | import org.apache.commons.lang3.tuple.ImmutablePair; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 21 | import org.apache.commons.lang3.tuple.Pair; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 22 | import org.onlab.util.KryoNamespace; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.store.serializers.KryoNamespaces; |
| 25 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 26 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 27 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 28 | import org.onosproject.store.service.StorageService; |
| 29 | import org.onosproject.store.service.WallClockTimestamp; |
| 30 | import org.osgi.service.component.annotations.Activate; |
| 31 | import org.osgi.service.component.annotations.Component; |
| 32 | import org.osgi.service.component.annotations.Deactivate; |
| 33 | import org.osgi.service.component.annotations.Reference; |
| 34 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 35 | import org.slf4j.Logger; |
| 36 | |
| 37 | import java.math.BigInteger; |
| 38 | import java.util.concurrent.ConcurrentMap; |
| 39 | |
| 40 | import static com.google.common.base.Preconditions.checkNotNull; |
| 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | |
| 43 | /** |
| 44 | * Distributed implementation of MasterElectionIdStore. |
| 45 | */ |
| 46 | @Component(immediate = true, service = MasterElectionIdStore.class) |
| 47 | public class DistributedMasterElectionIdStore implements MasterElectionIdStore { |
| 48 | |
| 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 50 | protected StorageService storageService; |
| 51 | |
| 52 | private static final KryoNamespace SERIALIZER = KryoNamespace.newBuilder() |
| 53 | .register(KryoNamespaces.API) |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 54 | .register(Pair.class) |
pierventre | 381d454 | 2021-01-15 17:27:48 +0100 | [diff] [blame^] | 55 | .register(ImmutablePair.class) |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 56 | .register(Long.class) |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 57 | .register(BigInteger.class) |
| 58 | .build(); |
| 59 | |
| 60 | private final Logger log = getLogger(getClass()); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 61 | private final EventuallyConsistentMapListener<Pair<DeviceId, Long>, BigInteger> mapListener = |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 62 | new InternalMapListener(); |
| 63 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 64 | private EventuallyConsistentMap<Pair<DeviceId, Long>, BigInteger> masterElectionIds; |
| 65 | private ConcurrentMap<Pair<DeviceId, Long>, MasterElectionIdListener> listeners = |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 66 | Maps.newConcurrentMap(); |
| 67 | |
| 68 | @Activate |
| 69 | public void activate() { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 70 | listeners = Maps.newConcurrentMap(); |
| 71 | masterElectionIds = storageService.<Pair<DeviceId, Long>, |
| 72 | BigInteger>eventuallyConsistentMapBuilder() |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 73 | .withName("p4runtime-master-election-ids") |
| 74 | .withSerializer(SERIALIZER) |
| 75 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 76 | .build(); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 77 | masterElectionIds.addListener(mapListener); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 78 | log.info("Started"); |
| 79 | } |
| 80 | |
| 81 | @Deactivate |
| 82 | public void deactivate() { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 83 | masterElectionIds.removeListener(mapListener); |
| 84 | masterElectionIds.destroy(); |
| 85 | masterElectionIds = null; |
| 86 | listeners.clear(); |
| 87 | listeners = null; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 88 | log.info("Stopped"); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | @Override |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 93 | public void set(DeviceId deviceId, long p4DeviceId, BigInteger electionId) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 94 | checkNotNull(deviceId); |
| 95 | checkNotNull(electionId); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 96 | masterElectionIds.put(Pair.of(deviceId, p4DeviceId), electionId); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 100 | public BigInteger get(DeviceId deviceId, long p4DeviceId) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 101 | checkNotNull(deviceId); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 102 | return masterElectionIds.get(Pair.of(deviceId, p4DeviceId)); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 106 | public void remove(DeviceId deviceId, long p4DeviceId) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 107 | checkNotNull(deviceId); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 108 | masterElectionIds.remove(Pair.of(deviceId, p4DeviceId)); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 112 | public void removeAll(DeviceId deviceId) { |
| 113 | masterElectionIds.keySet().forEach(k -> { |
| 114 | if (k.getLeft().equals(deviceId)) { |
| 115 | masterElectionIds.remove(k); |
| 116 | } |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void setListener(DeviceId deviceId, long p4DeviceId, |
| 122 | MasterElectionIdListener newListener) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 123 | checkNotNull(deviceId); |
| 124 | checkNotNull(newListener); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 125 | listeners.compute(Pair.of(deviceId, p4DeviceId), (x, existingListener) -> { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 126 | if (existingListener == null || existingListener == newListener) { |
| 127 | return newListener; |
| 128 | } else { |
| 129 | log.error("Cannot add listener as one already exist for {}", deviceId); |
| 130 | return existingListener; |
| 131 | } |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | @Override |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 136 | public void unsetListener(DeviceId deviceId, long p4DeviceId) { |
| 137 | listeners.remove(Pair.of(deviceId, p4DeviceId)); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 140 | private class InternalMapListener implements EventuallyConsistentMapListener<Pair<DeviceId, Long>, BigInteger> { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 141 | @Override |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 142 | public void event(EventuallyConsistentMapEvent<Pair<DeviceId, Long>, BigInteger> event) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 143 | final MasterElectionIdListener listener = listeners.get(event.key()); |
| 144 | if (listener == null) { |
| 145 | return; |
| 146 | } |
| 147 | switch (event.type()) { |
| 148 | case PUT: |
| 149 | listener.updated(event.value()); |
| 150 | break; |
| 151 | case REMOVE: |
| 152 | listener.updated(null); |
| 153 | break; |
| 154 | default: |
| 155 | log.error("Unrecognized map event type {}", event.type()); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |