2 * Copyright (c) 2016 Cisco Systems, Inc. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.lispflowmapping.southbound.lisp;
10 import java.util.Collection;
12 import java.util.concurrent.ConcurrentHashMap;
13 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
14 import org.opendaylight.lispflowmapping.mapcache.AuthKeyDb;
15 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.DataObjectModification;
18 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
19 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
20 import org.opendaylight.mdsal.binding.api.DataTreeModification;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.map.register.cache.metadata.EidLispAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.map.register.cache.metadata.EidLispAddressKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingDatabase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.database.VirtualNetworkIdentifier;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * DataListener for all AuthenticationKey modification events.
38 public class AuthenticationKeyDataListener implements ClusteredDataTreeChangeListener<AuthenticationKey> {
39 private static final Logger LOG = LoggerFactory.getLogger(AuthenticationKeyDataListener.class);
41 private final AuthKeyDb akdb;
42 private final DataBroker broker;
43 private final InstanceIdentifier<AuthenticationKey> path;
44 private final ListenerRegistration<ClusteredDataTreeChangeListener<AuthenticationKey>> registration;
45 private final ConcurrentHashMap<Eid, Long> updatedEntries;
47 public AuthenticationKeyDataListener(final DataBroker broker, final AuthKeyDb akdb) {
50 this.path = InstanceIdentifier.create(MappingDatabase.class).child(VirtualNetworkIdentifier.class)
51 .child(AuthenticationKey.class);
52 LOG.trace("Registering AuthenticationKey listener.");
53 final DataTreeIdentifier<AuthenticationKey> dataTreeIdentifier = DataTreeIdentifier.create(
54 LogicalDatastoreType.CONFIGURATION, path);
55 registration = broker.registerDataTreeChangeListener(dataTreeIdentifier, this);
56 this.updatedEntries = new ConcurrentHashMap<>();
59 public void closeDataChangeListener() {
64 public synchronized void onDataTreeChanged(Collection<DataTreeModification<AuthenticationKey>> changes) {
65 for (DataTreeModification<AuthenticationKey> change : changes) {
66 final DataObjectModification<AuthenticationKey> mod = change.getRootNode();
68 if (ModificationType.DELETE == mod.getModificationType()) {
69 final AuthenticationKey authKey = mod.getDataBefore();
71 LOG.trace("Received deleted data");
72 LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
73 LOG.trace("Value: {}", authKey);
75 final AuthenticationKey convertedAuthKey = convertToBinaryIfNecessary(authKey);
77 akdb.removeAuthenticationKey(convertedAuthKey.getEid());
78 updatedEntries.put(convertedAuthKey.getEid(), System.currentTimeMillis());
79 } else if (ModificationType.WRITE == mod.getModificationType() || ModificationType.SUBTREE_MODIFIED == mod
80 .getModificationType()) {
81 if (ModificationType.WRITE == mod.getModificationType()) {
82 LOG.trace("Received created data");
84 LOG.trace("Received updated data");
86 // Process newly created or updated authentication keys
87 final AuthenticationKey authKey = mod.getDataAfter();
89 LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
90 LOG.trace("Value: {}", authKey);
92 final AuthenticationKey convertedAuthKey = convertToBinaryIfNecessary(authKey);
94 akdb.addAuthenticationKey(convertedAuthKey.getEid(), convertedAuthKey.getMappingAuthkey());
95 updatedEntries.put(convertedAuthKey.getEid(), System.currentTimeMillis());
97 LOG.warn("Ignoring unhandled modification type {}", mod.getModificationType());
103 * We maintain a HashMap with the update times of AuthenticationKey objects in the updatedEntries field. We keep
104 * entries in the HashMap for the Map-Register cache timeout interval, and lazy remove them afterwards. As a result
105 * the same EID will be considered updated during that interval, even on subsequent queries. This is necessary
106 * because more than one xTR may register the same EID, and to avoid complexity we don't store origin information.
107 * The performance trade-off is not significant, because during a typical cache timeout the same xTR will send only
108 * a few registration packets (2 for the default value of 90s, when UDP Map-Registers are sent at 1 minute
111 * @param eids List of EIDs to check
112 * @param timeout MapRegister cache timeout value
113 * @return false if any of the EIDs in the eids list was updated in the last timout period, true otherwise
115 public synchronized boolean authKeysForEidsUnchanged(Map<EidLispAddressKey, EidLispAddress> eids, long timeout) {
116 boolean result = true;
117 Long currentTime = System.currentTimeMillis();
118 for (EidLispAddress eidLispAddress : eids.values()) {
119 Long updateTime = updatedEntries.get(eidLispAddress.getEid());
120 if (updateTime != null) {
122 if (currentTime - updateTime > timeout) {
123 updatedEntries.remove(eidLispAddress.getEid());
130 private static AuthenticationKey convertToBinaryIfNecessary(AuthenticationKey authKey) {
131 Eid originalEid = authKey.getEid();
132 if (LispAddressUtil.addressNeedsConversionToBinary(originalEid.getAddress())) {
133 AuthenticationKeyBuilder akb = new AuthenticationKeyBuilder(authKey);
134 akb.setEid(LispAddressUtil.convertToBinary(originalEid));