blob: 884d9cc463d6dce679b5cbf47e0b51e25b0fc9b3 [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
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;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070024import org.apache.felix.scr.annotations.Service;
Yi Tsengb8e19f12017-06-07 15:47:23 -070025import org.onlab.packet.DHCP;
26import org.onlab.packet.DHCP6;
27import org.onlab.util.KryoNamespace;
28import org.onosproject.net.HostId;
29import org.onosproject.store.StoreDelegate;
30import org.onosproject.store.serializers.KryoNamespaces;
31import org.onosproject.store.service.EventuallyConsistentMap;
32import org.onosproject.store.service.EventuallyConsistentMapEvent;
33import org.onosproject.store.service.EventuallyConsistentMapListener;
34import org.onosproject.store.service.StorageService;
35import org.onosproject.store.service.WallClockTimestamp;
36import org.slf4j.Logger;
37
38import java.util.Collection;
39import java.util.Optional;
40
41import static com.google.common.base.Preconditions.checkNotNull;
42import static org.slf4j.LoggerFactory.getLogger;
43
44/**
45 * Distributed DHCP relay store.
46 */
Yi Tseng7a38f9a2017-06-09 14:36:40 -070047@Component(immediate = true)
48@Service
Yi Tsengb8e19f12017-06-07 15:47:23 -070049public 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 Kimd94ceea2017-11-29 19:03:02 +000055 .register(DhcpRelayCounters.class)
Yi Tsengb8e19f12017-06-07 15:47:23 -070056 .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 Tseng15813842017-10-02 14:27:24 -070070 .withTimestampProvider((hostId, record) -> {
71 if (record != null) {
72 return new WallClockTimestamp(record.lastSeen());
73 } else {
74 return new WallClockTimestamp();
75 }
76 })
Yi Tsengb8e19f12017-06-07 15:47:23 -070077 .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 Tseng483ac6f2017-08-02 15:03:31 -070091 checkNotNull(delegate, "Delegate can't be null");
Yi Tsengb8e19f12017-06-07 15:47:23 -070092 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}