blob: f199671e8c766aeab6516353245ebef32085c583 [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 Milkeyd84f89b2018-08-17 14:54:17 -070046@Component(immediate = true, service = DhcpFpmPrefixStore.class)
47//@Property(name = "fpm_type", value = "DHCP")
Kalhee Kimba366062017-11-07 16:32:09 +000048public class DistributedFpmPrefixStore implements DhcpFpmPrefixStore {
49
50 private static final KryoNamespace APP_KRYO = KryoNamespace.newBuilder()
51 .register(KryoNamespaces.API)
52 .register(FpmRecord.class)
53 .register(FpmRecord.Type.class)
54 .build();
55
56 private Logger log = getLogger(getClass());
57 private StoreDelegate<FpmPrefixStoreEvent> delegate;
58 private EventuallyConsistentMap<IpPrefix, FpmRecord> dhcpFpmRecords;
59 private EventuallyConsistentMapListener<IpPrefix, FpmRecord> listener;
60
Ray Milkeyd84f89b2018-08-17 14:54:17 -070061 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Kalhee Kimba366062017-11-07 16:32:09 +000062 protected StorageService storageService;
63
64 @Activate
65 protected void activated() {
66 dhcpFpmRecords = storageService.<IpPrefix, FpmRecord>eventuallyConsistentMapBuilder()
67 .withName("DHCP-FPM-Records")
68 .withTimestampProvider((k, v) -> new WallClockTimestamp())
69 .withSerializer(APP_KRYO)
70 .withPersistence()
71 .build();
72 listener = new InternalMapListener();
73 dhcpFpmRecords.addListener(listener);
74 }
75
76 @Deactivate
77 protected void deactivated() {
78 dhcpFpmRecords.removeListener(listener);
79 dhcpFpmRecords.destroy().join();
80 }
81
82 @Override
83 public void setDelegate(StoreDelegate<FpmPrefixStoreEvent> delegate) {
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -080084 checkNotNull(delegate, "Delegate can't be null");
Kalhee Kimba366062017-11-07 16:32:09 +000085 this.delegate = delegate;
86 }
87
88 @Override
89 public void unsetDelegate(StoreDelegate<FpmPrefixStoreEvent> delegate) {
90 this.delegate = null;
91 }
92
93 @Override
94 public boolean hasDelegate() {
95 return delegate != null;
96 }
97
98 @Override
99 public Optional<FpmRecord> getFpmRecord(IpPrefix prefix) {
100 checkNotNull(prefix, "Prefix can't be null");
101 return Optional.ofNullable(dhcpFpmRecords.get(prefix));
102 }
103
104 @Override
105 public Collection<FpmRecord> getFpmRecords() {
106 return dhcpFpmRecords.values();
107 }
108
109 /**
110 * Add a dhcp fpm entry.
111 *
112 * @param prefix the route prefix in the advertisement
113 * @param fpmRecord the route for fpm
114 **/
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -0800115 @Override
Kalhee Kimba366062017-11-07 16:32:09 +0000116 public void addFpmRecord(IpPrefix prefix, FpmRecord fpmRecord) {
117 checkNotNull(prefix, "Prefix can't be null");
118 checkNotNull(fpmRecord, "Fpm record can't be null");
119 dhcpFpmRecords.put(prefix, fpmRecord);
120 }
121
122 /**
123 * Remove a dhcp fpm entry.
124 *
125 * @param prefix the route prefix in the advertisement
126 * @return none
127 **/
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -0800128 @Override
Kalhee Kimba366062017-11-07 16:32:09 +0000129 public Optional<FpmRecord> removeFpmRecord(IpPrefix prefix) {
130 checkNotNull(prefix, "Prefix can't be null");
131 return Optional.ofNullable(dhcpFpmRecords.remove(prefix));
132 }
133
134 /**
135 * Internal map listener for Fpm records map.
136 */
137 private class InternalMapListener implements EventuallyConsistentMapListener<IpPrefix, FpmRecord> {
138 @Override
139 public void event(EventuallyConsistentMapEvent<IpPrefix, FpmRecord> event) {
140 FpmPrefixStoreEvent.Type eventType;
141 switch (event.type()) {
142 case PUT:
143 eventType = FpmPrefixStoreEvent.Type.ADD;
144 break;
145 case REMOVE:
146 eventType = FpmPrefixStoreEvent.Type.REMOVE;
147 break;
148 default:
149 log.warn("Unknown event type {}", event.type());
150 return;
151 }
152 if (delegate != null) {
153 delegate.notify(new FpmPrefixStoreEvent(eventType, event.value()));
154 }
155 }
156 }
157}