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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
14 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
20 import org.opendaylight.lispflowmapping.mapcache.SimpleMapCache;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingDatabase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.database.VirtualNetworkIdentifier;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * DataListener for all AuthenticationKey modification events.
35 public class AuthenticationKeyDataListener implements DataTreeChangeListener<AuthenticationKey> {
36 private static final Logger LOG = LoggerFactory.getLogger(AuthenticationKeyDataListener.class);
38 private final SimpleMapCache smc;
39 private final DataBroker broker;
40 private final InstanceIdentifier<AuthenticationKey> path;
41 private ListenerRegistration<DataTreeChangeListener<AuthenticationKey>> registration;
42 private boolean authKeyRefreshing = false;
43 private long authKeyRefreshingDate;
46 public AuthenticationKeyDataListener(final DataBroker broker, final SimpleMapCache smc) {
49 this.path = InstanceIdentifier.create(MappingDatabase.class).child(VirtualNetworkIdentifier.class)
50 .child(AuthenticationKey.class);
51 LOG.trace("Registering AuthenticationKey listener.");
52 final DataTreeIdentifier<AuthenticationKey> dataTreeIdentifier = new DataTreeIdentifier<>(
53 LogicalDatastoreType.CONFIGURATION, path);
54 registration = broker.registerDataTreeChangeListener(dataTreeIdentifier, this);
57 public void closeDataChangeListener() {
61 public boolean isAuthKeyRefreshing() {
62 return authKeyRefreshing;
65 public void setAuthKeyRefreshing(boolean authKeyRefreshing) {
66 this.authKeyRefreshing = authKeyRefreshing;
69 public long getAuthKeyRefreshingDate() {
70 return authKeyRefreshingDate;
74 public void onDataTreeChanged(Collection<DataTreeModification<AuthenticationKey>> changes) {
75 authKeyRefreshing = true;
76 authKeyRefreshingDate = System.currentTimeMillis();
77 for (DataTreeModification<AuthenticationKey> change : changes) {
78 final DataObjectModification<AuthenticationKey> mod = change.getRootNode();
80 if (ModificationType.DELETE == mod.getModificationType()) {
81 final AuthenticationKey authKey = mod.getDataBefore();
83 LOG.trace("Received deleted data");
84 LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
85 LOG.trace("Value: {}", authKey);
87 final AuthenticationKey convertedAuthKey = convertToBinaryIfNecessary(authKey);
89 smc.removeAuthenticationKey(convertedAuthKey.getEid());
90 } else if (ModificationType.WRITE == mod.getModificationType() || ModificationType.SUBTREE_MODIFIED == mod
91 .getModificationType()) {
92 if (ModificationType.WRITE == mod.getModificationType()) {
93 LOG.trace("Received created data");
95 LOG.trace("Received updated data");
97 // Process newly created or updated authentication keys
98 final AuthenticationKey authKey = mod.getDataAfter();
100 LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
101 LOG.trace("Value: {}", authKey);
103 final AuthenticationKey convertedAuthKey = convertToBinaryIfNecessary(authKey);
105 smc.addAuthenticationKey(convertedAuthKey.getEid(), convertedAuthKey.getMappingAuthkey());
107 LOG.warn("Ignoring unhandled modification type {}", mod.getModificationType());
112 private static AuthenticationKey convertToBinaryIfNecessary(AuthenticationKey authKey) {
113 Eid originalEid = authKey.getEid();
114 if (LispAddressUtil.addressNeedsConversionToBinary(originalEid.getAddress())) {
115 AuthenticationKeyBuilder akb = new AuthenticationKeyBuilder(authKey);
116 akb.setEid(LispAddressUtil.convertToBinary(originalEid));