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