blob: bc95d0b0d3057a74c3c3996417f4fa36123aa428 [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
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.apache.felix.scr.annotations.Property;
26import org.onlab.util.KryoNamespace;
27import org.onosproject.store.StoreDelegate;
28import org.onlab.packet.IpPrefix;
29import org.onosproject.store.serializers.KryoNamespaces;
30import org.onosproject.store.service.EventuallyConsistentMap;
31import org.onosproject.store.service.EventuallyConsistentMapEvent;
32import org.onosproject.store.service.EventuallyConsistentMapListener;
33import org.onosproject.store.service.StorageService;
34import org.onosproject.store.service.WallClockTimestamp;
35import org.onosproject.routing.fpm.api.FpmRecord;
36import org.onosproject.routing.fpm.api.FpmPrefixStoreEvent;
37
38
39
40
41import org.slf4j.Logger;
42
43import java.util.Collection;
44import java.util.Optional;
45
46
47
48
49import static com.google.common.base.Preconditions.checkNotNull;
50import static org.slf4j.LoggerFactory.getLogger;
51
52/**
53 * Persistent Fpm Prefix Store with Listener.
54 */
55@Component(immediate = true)
56@Property(name = "fpm_type", value = "DHCP")
57@Service
58public class DistributedFpmPrefixStore implements DhcpFpmPrefixStore {
59
60 private static final KryoNamespace APP_KRYO = KryoNamespace.newBuilder()
61 .register(KryoNamespaces.API)
62 .register(FpmRecord.class)
63 .register(FpmRecord.Type.class)
64 .build();
65
66 private Logger log = getLogger(getClass());
67 private StoreDelegate<FpmPrefixStoreEvent> delegate;
68 private EventuallyConsistentMap<IpPrefix, FpmRecord> dhcpFpmRecords;
69 private EventuallyConsistentMapListener<IpPrefix, FpmRecord> listener;
70
71 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected StorageService storageService;
73
74 @Activate
75 protected void activated() {
76 dhcpFpmRecords = storageService.<IpPrefix, FpmRecord>eventuallyConsistentMapBuilder()
77 .withName("DHCP-FPM-Records")
78 .withTimestampProvider((k, v) -> new WallClockTimestamp())
79 .withSerializer(APP_KRYO)
80 .withPersistence()
81 .build();
82 listener = new InternalMapListener();
83 dhcpFpmRecords.addListener(listener);
84 }
85
86 @Deactivate
87 protected void deactivated() {
88 dhcpFpmRecords.removeListener(listener);
89 dhcpFpmRecords.destroy().join();
90 }
91
92 @Override
93 public void setDelegate(StoreDelegate<FpmPrefixStoreEvent> delegate) {
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -080094 checkNotNull(delegate, "Delegate can't be null");
Kalhee Kimba366062017-11-07 16:32:09 +000095 this.delegate = delegate;
96 }
97
98 @Override
99 public void unsetDelegate(StoreDelegate<FpmPrefixStoreEvent> delegate) {
100 this.delegate = null;
101 }
102
103 @Override
104 public boolean hasDelegate() {
105 return delegate != null;
106 }
107
108 @Override
109 public Optional<FpmRecord> getFpmRecord(IpPrefix prefix) {
110 checkNotNull(prefix, "Prefix can't be null");
111 return Optional.ofNullable(dhcpFpmRecords.get(prefix));
112 }
113
114 @Override
115 public Collection<FpmRecord> getFpmRecords() {
116 return dhcpFpmRecords.values();
117 }
118
119 /**
120 * Add a dhcp fpm entry.
121 *
122 * @param prefix the route prefix in the advertisement
123 * @param fpmRecord the route for fpm
124 **/
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -0800125 @Override
Kalhee Kimba366062017-11-07 16:32:09 +0000126 public void addFpmRecord(IpPrefix prefix, FpmRecord fpmRecord) {
127 checkNotNull(prefix, "Prefix can't be null");
128 checkNotNull(fpmRecord, "Fpm record can't be null");
129 dhcpFpmRecords.put(prefix, fpmRecord);
130 }
131
132 /**
133 * Remove a dhcp fpm entry.
134 *
135 * @param prefix the route prefix in the advertisement
136 * @return none
137 **/
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -0800138 @Override
Kalhee Kimba366062017-11-07 16:32:09 +0000139 public Optional<FpmRecord> removeFpmRecord(IpPrefix prefix) {
140 checkNotNull(prefix, "Prefix can't be null");
141 return Optional.ofNullable(dhcpFpmRecords.remove(prefix));
142 }
143
144 /**
145 * Internal map listener for Fpm records map.
146 */
147 private class InternalMapListener implements EventuallyConsistentMapListener<IpPrefix, FpmRecord> {
148 @Override
149 public void event(EventuallyConsistentMapEvent<IpPrefix, FpmRecord> event) {
150 FpmPrefixStoreEvent.Type eventType;
151 switch (event.type()) {
152 case PUT:
153 eventType = FpmPrefixStoreEvent.Type.ADD;
154 break;
155 case REMOVE:
156 eventType = FpmPrefixStoreEvent.Type.REMOVE;
157 break;
158 default:
159 log.warn("Unknown event type {}", event.type());
160 return;
161 }
162 if (delegate != null) {
163 delegate.notify(new FpmPrefixStoreEvent(eventType, event.value()));
164 }
165 }
166 }
167}