Revert "Clustering - common infrastructure."
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / lisp / AuthenticationKeyDataListener.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.lispflowmapping.southbound.lisp;
9
10 import java.util.Collection;
11
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;
30
31 /**
32  * DataListener for all AuthenticationKey modification events.
33  *
34  */
35 public class AuthenticationKeyDataListener implements DataTreeChangeListener<AuthenticationKey> {
36     private static final Logger LOG = LoggerFactory.getLogger(AuthenticationKeyDataListener.class);
37
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;
44
45
46     public AuthenticationKeyDataListener(final DataBroker broker, final SimpleMapCache smc) {
47         this.broker = broker;
48         this.smc = 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);
55     }
56
57     public void closeDataChangeListener() {
58         registration.close();
59     }
60
61     public boolean isAuthKeyRefreshing() {
62         return authKeyRefreshing;
63     }
64
65     public void setAuthKeyRefreshing(boolean authKeyRefreshing) {
66         this.authKeyRefreshing = authKeyRefreshing;
67     }
68
69     public long getAuthKeyRefreshingDate() {
70         return authKeyRefreshingDate;
71     }
72
73     @Override
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();
79
80             if (ModificationType.DELETE == mod.getModificationType()) {
81                 final AuthenticationKey authKey = mod.getDataBefore();
82
83                 LOG.trace("Received deleted data");
84                 LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
85                 LOG.trace("Value: {}", authKey);
86
87                 final AuthenticationKey convertedAuthKey = convertToBinaryIfNecessary(authKey);
88
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");
94                 } else {
95                     LOG.trace("Received updated data");
96                 }
97                 // Process newly created or updated authentication keys
98                 final AuthenticationKey authKey = mod.getDataAfter();
99
100                 LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
101                 LOG.trace("Value: {}", authKey);
102
103                 final AuthenticationKey convertedAuthKey = convertToBinaryIfNecessary(authKey);
104
105                 smc.addAuthenticationKey(convertedAuthKey.getEid(), convertedAuthKey.getMappingAuthkey());
106             } else {
107                 LOG.warn("Ignoring unhandled modification type {}", mod.getModificationType());
108             }
109         }
110     }
111
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));
117             return akb.build();
118         }
119         return authKey;
120     }
121
122 }