blob: 59596fd277c03c0c29f1e1774774653e4f0ddc1d [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.osgi.service.component.annotations.Activate;
20import org.osgi.service.component.annotations.Component;
21import org.osgi.service.component.annotations.Deactivate;
22import org.osgi.service.component.annotations.Reference;
23import org.osgi.service.component.annotations.ReferenceCardinality;
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080024import org.onosproject.net.pi.runtime.PiEntity;
25import org.onosproject.net.pi.runtime.PiHandle;
26import org.onosproject.net.pi.service.PiTranslatable;
27import org.onosproject.net.pi.service.PiTranslatedEntity;
28import org.onosproject.net.pi.service.PiTranslationEvent;
29import org.onosproject.net.pi.service.PiTranslationStore;
30import org.onosproject.net.pi.service.PiTranslationStoreDelegate;
31import org.onosproject.store.AbstractStore;
32import org.onosproject.store.serializers.KryoNamespaces;
33import org.onosproject.store.service.EventuallyConsistentMap;
34import org.onosproject.store.service.EventuallyConsistentMapEvent;
35import org.onosproject.store.service.EventuallyConsistentMapListener;
36import org.onosproject.store.service.StorageService;
37import org.onosproject.store.service.WallClockTimestamp;
38import org.slf4j.Logger;
39
40import static com.google.common.base.Preconditions.checkArgument;
41import static com.google.common.base.Preconditions.checkNotNull;
42import static java.lang.String.format;
43import static org.slf4j.LoggerFactory.getLogger;
44
45/**
46 * Distributed implementation of PiTranslationStore.
47 */
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080048public 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 Milkeyd84f89b2018-08-17 14:54:17 -070057 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080058 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}