Merge "Restoring authentication key from MDSAL."
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / mdsal / AuthenticationKeyDataListener.java
1 /*
2  * Copyright (c) 2015 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.implementation.mdsal;
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.DataTreeModification;
15 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMappingSystem;
16 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingDatabase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.database.VirtualNetworkIdentifier;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
26
27 /**
28  * DataListener for all AuthenticationKey modification events.
29  *
30  * @author Lorand Jakab
31  *
32  */
33 public class AuthenticationKeyDataListener extends AbstractDataListener<AuthenticationKey> {
34     private static final Logger LOG = LoggerFactory.getLogger(AuthenticationKeyDataListener.class);
35     private IMappingSystem mapSystem;
36
37     public AuthenticationKeyDataListener(DataBroker broker, IMappingSystem mapSystem) {
38         setBroker(broker);
39         setMappingSystem(mapSystem);
40         setPath(InstanceIdentifier.create(MappingDatabase.class).child(VirtualNetworkIdentifier.class)
41                 .child(AuthenticationKey.class));
42         LOG.trace("Registering AuthenticationKey listener.");
43         registerDataChangeListener();
44     }
45
46     @Override
47     public void onDataTreeChanged(Collection<DataTreeModification<AuthenticationKey>> changes) {
48         for (DataTreeModification<AuthenticationKey> change : changes) {
49             final DataObjectModification<AuthenticationKey> mod = change.getRootNode();
50
51             if (ModificationType.DELETE == mod.getModificationType()) {
52                 final AuthenticationKey authKey = mod.getDataBefore();
53
54                 LOG.trace("Received deleted data");
55                 LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
56                 LOG.trace("Value: {}", authKey);
57
58                 final AuthenticationKey convertedAuthKey = convertToBinaryIfNecessary(authKey);
59
60                 mapSystem.removeAuthenticationKey(convertedAuthKey.getEid());
61             } else if (ModificationType.WRITE == mod.getModificationType() || ModificationType.SUBTREE_MODIFIED == mod
62                     .getModificationType()) {
63                 if (ModificationType.WRITE == mod.getModificationType()) {
64                     LOG.trace("Received created data");
65                 } else {
66                     LOG.trace("Received updated data");
67                 }
68                 // Process newly created or updated authentication keys
69                 final AuthenticationKey authKey = mod.getDataAfter();
70
71                 LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
72                 LOG.trace("Value: {}", authKey);
73
74                 final AuthenticationKey convertedAuthKey = convertToBinaryIfNecessary(authKey);
75
76                 mapSystem.addAuthenticationKey(convertedAuthKey.getEid(), convertedAuthKey.getMappingAuthkey());
77             } else {
78                 LOG.warn("Ignoring unhandled modification type {}", mod.getModificationType());
79             }
80         }
81     }
82
83     private static AuthenticationKey convertToBinaryIfNecessary(AuthenticationKey authKey) {
84         Eid originalEid = authKey.getEid();
85         if (LispAddressUtil.addressNeedsConversionToBinary(originalEid.getAddress())) {
86             AuthenticationKeyBuilder akb = new AuthenticationKeyBuilder(authKey);
87             akb.setEid(LispAddressUtil.convertToBinary(originalEid));
88             return akb.build();
89         }
90         return authKey;
91     }
92
93     void setMappingSystem(IMappingSystem msmr) {
94         this.mapSystem = msmr;
95     }
96 }