blob: 070748b655fc2c68b0c80eab366825c46f0c7996 [file] [log] [blame]
Kalhee Kimba366062017-11-07 16:32:09 +00001/*
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.dhcprelay.store;
18
Kalhee Kimba366062017-11-07 16:32:09 +000019import org.onlab.packet.IpPrefix;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.onlab.util.KryoNamespace;
21import org.onosproject.routing.fpm.api.FpmPrefixStoreEvent;
22import org.onosproject.routing.fpm.api.FpmRecord;
23import org.onosproject.store.StoreDelegate;
Kalhee Kimba366062017-11-07 16:32:09 +000024import org.onosproject.store.serializers.KryoNamespaces;
25import org.onosproject.store.service.EventuallyConsistentMap;
26import org.onosproject.store.service.EventuallyConsistentMapEvent;
27import org.onosproject.store.service.EventuallyConsistentMapListener;
28import org.onosproject.store.service.StorageService;
29import org.onosproject.store.service.WallClockTimestamp;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070030import org.osgi.service.component.annotations.Activate;
31import org.osgi.service.component.annotations.Component;
32import org.osgi.service.component.annotations.Deactivate;
33import org.osgi.service.component.annotations.Reference;
34import org.osgi.service.component.annotations.ReferenceCardinality;
Kalhee Kimba366062017-11-07 16:32:09 +000035import org.slf4j.Logger;
36
37import java.util.Collection;
38import java.util.Optional;
39
Kalhee Kimba366062017-11-07 16:32:09 +000040import static com.google.common.base.Preconditions.checkNotNull;
41import static org.slf4j.LoggerFactory.getLogger;
42
43/**
44 * Persistent Fpm Prefix Store with Listener.
45 */
Ray Milkey687c00c2018-10-31 10:18:41 -070046@Component(
47 immediate = true,
Ray Milkey5504bd22019-03-22 16:24:38 -070048 service = DhcpFpmPrefixStore.class,
49 property = {
50 "_fpm_type=DHCP"
51 }
Ray Milkey687c00c2018-10-31 10:18:41 -070052)
Kalhee Kimba366062017-11-07 16:32:09 +000053public class DistributedFpmPrefixStore implements DhcpFpmPrefixStore {
54
55 private static final KryoNamespace APP_KRYO = KryoNamespace.newBuilder()
56 .register(KryoNamespaces.API)
57 .register(FpmRecord.class)
58 .register(FpmRecord.Type.class)
59 .build();
60
61 private Logger log = getLogger(getClass());
62 private StoreDelegate<FpmPrefixStoreEvent> delegate;
63 private EventuallyConsistentMap<IpPrefix, FpmRecord> dhcpFpmRecords;
64 private EventuallyConsistentMapListener<IpPrefix, FpmRecord> listener;
65
Ray Milkeyd84f89b2018-08-17 14:54:17 -070066 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Kalhee Kimba366062017-11-07 16:32:09 +000067 protected StorageService storageService;
68
69 @Activate
70 protected void activated() {
71 dhcpFpmRecords = storageService.<IpPrefix, FpmRecord>eventuallyConsistentMapBuilder()
72 .withName("DHCP-FPM-Records")
73 .withTimestampProvider((k, v) -> new WallClockTimestamp())
74 .withSerializer(APP_KRYO)
75 .withPersistence()
76 .build();
77 listener = new InternalMapListener();
78 dhcpFpmRecords.addListener(listener);
79 }
80
81 @Deactivate
82 protected void deactivated() {
83 dhcpFpmRecords.removeListener(listener);
84 dhcpFpmRecords.destroy().join();
85 }
86
87 @Override
88 public void setDelegate(StoreDelegate<FpmPrefixStoreEvent> delegate) {
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -080089 checkNotNull(delegate, "Delegate can't be null");
Kalhee Kimba366062017-11-07 16:32:09 +000090 this.delegate = delegate;
91 }
92
93 @Override
94 public void unsetDelegate(StoreDelegate<FpmPrefixStoreEvent> delegate) {
95 this.delegate = null;
96 }
97
98 @Override
99 public boolean hasDelegate() {
100 return delegate != null;
101 }
102
103 @Override
104 public Optional<FpmRecord> getFpmRecord(IpPrefix prefix) {
105 checkNotNull(prefix, "Prefix can't be null");
106 return Optional.ofNullable(dhcpFpmRecords.get(prefix));
107 }
108
109 @Override
110 public Collection<FpmRecord> getFpmRecords() {
111 return dhcpFpmRecords.values();
112 }
113
114 /**
115 * Add a dhcp fpm entry.
116 *
117 * @param prefix the route prefix in the advertisement
118 * @param fpmRecord the route for fpm
119 **/
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -0800120 @Override
Kalhee Kimba366062017-11-07 16:32:09 +0000121 public void addFpmRecord(IpPrefix prefix, FpmRecord fpmRecord) {
122 checkNotNull(prefix, "Prefix can't be null");
123 checkNotNull(fpmRecord, "Fpm record can't be null");
124 dhcpFpmRecords.put(prefix, fpmRecord);
125 }
126
127 /**
128 * Remove a dhcp fpm entry.
129 *
130 * @param prefix the route prefix in the advertisement
131 * @return none
132 **/
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -0800133 @Override
Kalhee Kimba366062017-11-07 16:32:09 +0000134 public Optional<FpmRecord> removeFpmRecord(IpPrefix prefix) {
135 checkNotNull(prefix, "Prefix can't be null");
136 return Optional.ofNullable(dhcpFpmRecords.remove(prefix));
137 }
138
139 /**
140 * Internal map listener for Fpm records map.
141 */
142 private class InternalMapListener implements EventuallyConsistentMapListener<IpPrefix, FpmRecord> {
143 @Override
144 public void event(EventuallyConsistentMapEvent<IpPrefix, FpmRecord> event) {
145 FpmPrefixStoreEvent.Type eventType;
146 switch (event.type()) {
147 case PUT:
148 eventType = FpmPrefixStoreEvent.Type.ADD;
149 break;
150 case REMOVE:
151 eventType = FpmPrefixStoreEvent.Type.REMOVE;
152 break;
153 default:
154 log.warn("Unknown event type {}", event.type());
155 return;
156 }
157 if (delegate != null) {
158 delegate.notify(new FpmPrefixStoreEvent(eventType, event.value()));
159 }
160 }
161 }
162}