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