Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.dhcprelay.store; |
| 18 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 19 | import org.onlab.packet.IpPrefix; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 20 | import org.onlab.util.KryoNamespace; |
| 21 | import org.onosproject.routing.fpm.api.FpmPrefixStoreEvent; |
| 22 | import org.onosproject.routing.fpm.api.FpmRecord; |
| 23 | import org.onosproject.store.StoreDelegate; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 24 | import org.onosproject.store.serializers.KryoNamespaces; |
| 25 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 26 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 27 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 28 | import org.onosproject.store.service.StorageService; |
| 29 | import org.onosproject.store.service.WallClockTimestamp; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 30 | import org.osgi.service.component.annotations.Activate; |
| 31 | import org.osgi.service.component.annotations.Component; |
| 32 | import org.osgi.service.component.annotations.Deactivate; |
| 33 | import org.osgi.service.component.annotations.Reference; |
| 34 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | |
| 37 | import java.util.Collection; |
| 38 | import java.util.Optional; |
| 39 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 40 | import static com.google.common.base.Preconditions.checkNotNull; |
| 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | |
| 43 | /** |
| 44 | * Persistent Fpm Prefix Store with Listener. |
| 45 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 46 | @Component(immediate = true, service = DhcpFpmPrefixStore.class) |
| 47 | //@Property(name = "fpm_type", value = "DHCP") |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 48 | public 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 Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 62 | 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 HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 84 | checkNotNull(delegate, "Delegate can't be null"); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 85 | 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 HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 115 | @Override |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 116 | 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 HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 128 | @Override |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 129 | 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 | } |