blob: 36d87b5ff31567d92a6e99d41e66aa62d0a175d9 [file] [log] [blame]
Carmelo Cascone326ad2d2017-11-28 18:09:13 -08001/*
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
17package org.onosproject.store.pi.impl;
18
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080019import org.onosproject.net.pi.runtime.PiEntity;
20import org.onosproject.net.pi.runtime.PiHandle;
21import org.onosproject.net.pi.service.PiTranslatable;
22import org.onosproject.net.pi.service.PiTranslatedEntity;
23import org.onosproject.net.pi.service.PiTranslationEvent;
24import org.onosproject.net.pi.service.PiTranslationStore;
25import org.onosproject.net.pi.service.PiTranslationStoreDelegate;
26import org.onosproject.store.AbstractStore;
27import org.onosproject.store.serializers.KryoNamespaces;
28import org.onosproject.store.service.EventuallyConsistentMap;
29import org.onosproject.store.service.EventuallyConsistentMapEvent;
30import org.onosproject.store.service.EventuallyConsistentMapListener;
31import org.onosproject.store.service.StorageService;
32import org.onosproject.store.service.WallClockTimestamp;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070033import org.osgi.service.component.annotations.Activate;
34import org.osgi.service.component.annotations.Deactivate;
35import org.osgi.service.component.annotations.Reference;
36import org.osgi.service.component.annotations.ReferenceCardinality;
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080037import org.slf4j.Logger;
38
39import static com.google.common.base.Preconditions.checkArgument;
40import static com.google.common.base.Preconditions.checkNotNull;
41import static java.lang.String.format;
42import static org.slf4j.LoggerFactory.getLogger;
43
44/**
45 * Distributed implementation of PiTranslationStore.
46 */
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080047public 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 Milkeyd84f89b2018-08-17 14:54:17 -070056 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080057 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}