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