blob: 60e04f9905691077df4ebf75eb02deb3c5d84f3d [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)
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}