blob: 980ab1144a9a5d7ca154cd86178471c874a8264f [file] [log] [blame]
Carmelo Casconee5b28722018-06-22 17:28:28 +02001/*
Carmelo Cascone4c289b72019-01-22 15:30:45 -08002 * Copyright 2019-present Open Networking Foundation
Carmelo Casconee5b28722018-06-22 17:28:28 +02003 *
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
Carmelo Cascone4c289b72019-01-22 15:30:45 -080017package org.onosproject.p4runtime.ctl.controller;
Carmelo Casconee5b28722018-06-22 17:28:28 +020018
19import org.onlab.util.KryoNamespace;
20import org.onosproject.net.DeviceId;
21import org.onosproject.store.serializers.KryoNamespaces;
22import org.onosproject.store.service.AtomicCounterMap;
23import org.onosproject.store.service.Serializer;
24import org.onosproject.store.service.StorageService;
25import org.slf4j.Logger;
26
27import java.math.BigInteger;
28import java.util.concurrent.ExecutionException;
29import java.util.concurrent.TimeUnit;
30import java.util.concurrent.TimeoutException;
31
32import static org.slf4j.LoggerFactory.getLogger;
33
34/**
35 * Distributed implementation of a generator of P4Runtime election IDs.
36 */
Carmelo Cascone4c289b72019-01-22 15:30:45 -080037final class DistributedElectionIdGenerator {
Carmelo Casconee5b28722018-06-22 17:28:28 +020038
39 private final Logger log = getLogger(this.getClass());
40
Carmelo Cascone03ae0ac2018-10-11 08:31:59 -070041 // FIXME: counter map use long, but P4Runtime accepts 128bit election IDs
Carmelo Casconee5b28722018-06-22 17:28:28 +020042 private AtomicCounterMap<DeviceId> electionIds;
43
44 /**
45 * Creates a new election ID generator using the given storage service.
46 *
47 * @param storageService storage service
48 */
49 DistributedElectionIdGenerator(StorageService storageService) {
50 KryoNamespace serializer = KryoNamespace.newBuilder()
51 .register(KryoNamespaces.API)
52 .build();
53 this.electionIds = storageService.<DeviceId>atomicCounterMapBuilder()
54 .withName("p4runtime-election-ids")
55 .withSerializer(Serializer.using(serializer))
56 .build();
57 }
58
59 /**
60 * Returns an election ID for the given device ID. The first election ID for
61 * a given device ID is always 1.
62 *
63 * @param deviceId device ID
64 * @return new election ID
65 */
66 BigInteger generate(DeviceId deviceId) {
67 if (electionIds == null) {
68 return null;
69 }
70 // Default value is 0 for AtomicCounterMap.
71 return BigInteger.valueOf(electionIds.incrementAndGet(deviceId));
72 }
73
74 /**
75 * Destroy the backing distributed primitive of this generator.
76 */
77 void destroy() {
78 try {
79 electionIds.destroy().get(10, TimeUnit.SECONDS);
80 } catch (InterruptedException | ExecutionException | TimeoutException e) {
81 log.error("Exception while destroying distributed counter map", e);
82 } finally {
83 electionIds = null;
84 }
85 }
86}