Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 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 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Service; |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 25 | import org.onlab.packet.DHCP; |
| 26 | import org.onlab.packet.DHCP6; |
| 27 | import org.onlab.util.KryoNamespace; |
| 28 | import org.onosproject.net.HostId; |
| 29 | import org.onosproject.store.StoreDelegate; |
| 30 | import org.onosproject.store.serializers.KryoNamespaces; |
| 31 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 32 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 33 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 34 | import org.onosproject.store.service.StorageService; |
| 35 | import org.onosproject.store.service.WallClockTimestamp; |
| 36 | import org.slf4j.Logger; |
| 37 | |
| 38 | import java.util.Collection; |
| 39 | import java.util.Optional; |
| 40 | |
| 41 | import static com.google.common.base.Preconditions.checkNotNull; |
| 42 | import static org.slf4j.LoggerFactory.getLogger; |
| 43 | |
| 44 | /** |
| 45 | * Distributed DHCP relay store. |
| 46 | */ |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 47 | @Component(immediate = true) |
| 48 | @Service |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 49 | public class DistributedDhcpRelayStore implements DhcpRelayStore { |
| 50 | private static final KryoNamespace APP_KRYO = KryoNamespace.newBuilder() |
| 51 | .register(KryoNamespaces.API) |
| 52 | .register(DhcpRecord.class) |
| 53 | .register(DHCP.MsgType.class) |
| 54 | .register(DHCP6.MsgType.class) |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 55 | .register(DhcpRelayCounters.class) |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 56 | .build(); |
| 57 | |
| 58 | private Logger log = getLogger(getClass()); |
| 59 | private StoreDelegate<DhcpRelayStoreEvent> delegate; |
| 60 | private EventuallyConsistentMap<HostId, DhcpRecord> dhcpRecords; |
| 61 | private EventuallyConsistentMapListener<HostId, DhcpRecord> listener; |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected StorageService storageService; |
| 65 | |
| 66 | @Activate |
| 67 | protected void activated() { |
| 68 | dhcpRecords = storageService.<HostId, DhcpRecord>eventuallyConsistentMapBuilder() |
| 69 | .withName("DHCP-Relay-Records") |
Yi Tseng | 1581384 | 2017-10-02 14:27:24 -0700 | [diff] [blame] | 70 | .withTimestampProvider((hostId, record) -> { |
| 71 | if (record != null) { |
| 72 | return new WallClockTimestamp(record.lastSeen()); |
| 73 | } else { |
| 74 | return new WallClockTimestamp(); |
| 75 | } |
| 76 | }) |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 77 | .withSerializer(APP_KRYO) |
| 78 | .build(); |
| 79 | listener = new InternalMapListener(); |
| 80 | dhcpRecords.addListener(listener); |
| 81 | } |
| 82 | |
| 83 | @Deactivate |
| 84 | protected void deactivated() { |
| 85 | dhcpRecords.removeListener(listener); |
| 86 | dhcpRecords.destroy().join(); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void setDelegate(StoreDelegate<DhcpRelayStoreEvent> delegate) { |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 91 | checkNotNull(delegate, "Delegate can't be null"); |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 92 | this.delegate = delegate; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void unsetDelegate(StoreDelegate<DhcpRelayStoreEvent> delegate) { |
| 97 | this.delegate = null; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public boolean hasDelegate() { |
| 102 | return delegate != null; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void updateDhcpRecord(HostId hostId, DhcpRecord dhcpRecord) { |
| 107 | checkNotNull(hostId, "Host id can't be null"); |
| 108 | checkNotNull(dhcpRecord, "DHCP record can't be null"); |
| 109 | dhcpRecords.put(hostId, dhcpRecord); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public Optional<DhcpRecord> getDhcpRecord(HostId hostId) { |
| 114 | checkNotNull(hostId, "Host id can't be null"); |
| 115 | return Optional.ofNullable(dhcpRecords.get(hostId)); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public Collection<DhcpRecord> getDhcpRecords() { |
| 120 | return dhcpRecords.values(); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public Optional<DhcpRecord> removeDhcpRecord(HostId hostId) { |
| 125 | checkNotNull(hostId, "Host id can't be null"); |
| 126 | return Optional.ofNullable(dhcpRecords.remove(hostId)); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Internal map listener for DHCP records map. |
| 131 | */ |
| 132 | private class InternalMapListener implements EventuallyConsistentMapListener<HostId, DhcpRecord> { |
| 133 | @Override |
| 134 | public void event(EventuallyConsistentMapEvent<HostId, DhcpRecord> event) { |
| 135 | DhcpRelayStoreEvent.Type eventType; |
| 136 | switch (event.type()) { |
| 137 | case PUT: |
| 138 | eventType = DhcpRelayStoreEvent.Type.UPDATED; |
| 139 | break; |
| 140 | case REMOVE: |
| 141 | eventType = DhcpRelayStoreEvent.Type.REMOVED; |
| 142 | break; |
| 143 | default: |
| 144 | log.warn("Unknown event type {}", event.type()); |
| 145 | return; |
| 146 | } |
| 147 | if (delegate != null) { |
| 148 | delegate.notify(new DhcpRelayStoreEvent(eventType, event.value())); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |