Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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) |
| 55 | .build(); |
| 56 | |
| 57 | private Logger log = getLogger(getClass()); |
| 58 | private StoreDelegate<DhcpRelayStoreEvent> delegate; |
| 59 | private EventuallyConsistentMap<HostId, DhcpRecord> dhcpRecords; |
| 60 | private EventuallyConsistentMapListener<HostId, DhcpRecord> listener; |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected StorageService storageService; |
| 64 | |
| 65 | @Activate |
| 66 | protected void activated() { |
| 67 | dhcpRecords = storageService.<HostId, DhcpRecord>eventuallyConsistentMapBuilder() |
| 68 | .withName("DHCP-Relay-Records") |
| 69 | .withTimestampProvider((hostId, record) -> new WallClockTimestamp(record.lastSeen())) |
| 70 | .withSerializer(APP_KRYO) |
| 71 | .build(); |
| 72 | listener = new InternalMapListener(); |
| 73 | dhcpRecords.addListener(listener); |
| 74 | } |
| 75 | |
| 76 | @Deactivate |
| 77 | protected void deactivated() { |
| 78 | dhcpRecords.removeListener(listener); |
| 79 | dhcpRecords.destroy().join(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void setDelegate(StoreDelegate<DhcpRelayStoreEvent> delegate) { |
| 84 | checkNotNull("Delegate can't be null", delegate); |
| 85 | this.delegate = delegate; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void unsetDelegate(StoreDelegate<DhcpRelayStoreEvent> delegate) { |
| 90 | this.delegate = null; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean hasDelegate() { |
| 95 | return delegate != null; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void updateDhcpRecord(HostId hostId, DhcpRecord dhcpRecord) { |
| 100 | checkNotNull(hostId, "Host id can't be null"); |
| 101 | checkNotNull(dhcpRecord, "DHCP record can't be null"); |
| 102 | dhcpRecords.put(hostId, dhcpRecord); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public Optional<DhcpRecord> getDhcpRecord(HostId hostId) { |
| 107 | checkNotNull(hostId, "Host id can't be null"); |
| 108 | return Optional.ofNullable(dhcpRecords.get(hostId)); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public Collection<DhcpRecord> getDhcpRecords() { |
| 113 | return dhcpRecords.values(); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public Optional<DhcpRecord> removeDhcpRecord(HostId hostId) { |
| 118 | checkNotNull(hostId, "Host id can't be null"); |
| 119 | return Optional.ofNullable(dhcpRecords.remove(hostId)); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Internal map listener for DHCP records map. |
| 124 | */ |
| 125 | private class InternalMapListener implements EventuallyConsistentMapListener<HostId, DhcpRecord> { |
| 126 | @Override |
| 127 | public void event(EventuallyConsistentMapEvent<HostId, DhcpRecord> event) { |
| 128 | DhcpRelayStoreEvent.Type eventType; |
| 129 | switch (event.type()) { |
| 130 | case PUT: |
| 131 | eventType = DhcpRelayStoreEvent.Type.UPDATED; |
| 132 | break; |
| 133 | case REMOVE: |
| 134 | eventType = DhcpRelayStoreEvent.Type.REMOVED; |
| 135 | break; |
| 136 | default: |
| 137 | log.warn("Unknown event type {}", event.type()); |
| 138 | return; |
| 139 | } |
| 140 | if (delegate != null) { |
| 141 | delegate.notify(new DhcpRelayStoreEvent(eventType, event.value())); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |