Migrate implementation/neutron/southbound to IETF YANG model
[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.Map;
11 import java.util.Set;
12
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
15 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMappingSystem;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingDatabase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.database.VirtualNetworkIdentifier;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * DataListener for all AuthenticationKey modification events.
26  *
27  * @author Lorand Jakab
28  *
29  */
30 public class AuthenticationKeyDataListener extends AbstractDataListener {
31     private static final Logger LOG = LoggerFactory.getLogger(AuthenticationKeyDataListener.class);
32     private IMappingSystem mapSystem;
33
34     public AuthenticationKeyDataListener(DataBroker broker, IMappingSystem mapSystem) {
35         setBroker(broker);
36         setMappingSystem(mapSystem);
37         setPath(InstanceIdentifier.create(MappingDatabase.class).child(VirtualNetworkIdentifier.class)
38                 .child(AuthenticationKey.class));
39         LOG.trace("Registering AuthenticationKey listener.");
40         registerDataChangeListener();
41     }
42
43     @Override
44     public void onDataChanged(
45             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
46
47         // Process newly created authentication keys
48         Map<InstanceIdentifier<?>, DataObject> createdData = change.getCreatedData();
49         for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : createdData.entrySet()) {
50             if (entry.getValue() instanceof AuthenticationKey) {
51                 AuthenticationKey authkey = (AuthenticationKey)entry.getValue();
52
53                 LOG.trace("Received created data");
54                 LOG.trace("Key: {}", entry.getKey());
55                 LOG.trace("Value: {}", authkey);
56
57                 mapSystem.addAuthenticationKey(authkey.getEid(), authkey.getMappingAuthkey());
58             }
59         }
60
61         // Process updated authentication keys
62         Map<InstanceIdentifier<?>, DataObject> updatedData = change.getUpdatedData();
63         for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : updatedData.entrySet()) {
64             if (entry.getValue() instanceof AuthenticationKey) {
65                 AuthenticationKey authkey = (AuthenticationKey)entry.getValue();
66
67                 LOG.trace("Received changed data");
68                 LOG.trace("Key: {}", entry.getKey());
69                 LOG.trace("Value: {}", authkey);
70
71                 mapSystem.addAuthenticationKey(authkey.getEid(), authkey.getMappingAuthkey());
72             }
73         }
74
75         // Process deleted authentication keys
76         Set<InstanceIdentifier<?>> removedData = change.getRemovedPaths();
77         for (InstanceIdentifier<?> entry : removedData) {
78             DataObject dataObject = change.getOriginalData().get(entry);
79             if (dataObject instanceof AuthenticationKey) {
80                 AuthenticationKey authkey = (AuthenticationKey)dataObject;
81
82                 LOG.trace("Received deleted data");
83                 LOG.trace("Key: {}", entry);
84                 LOG.trace("Value: {}", authkey);
85
86                 mapSystem.removeAuthenticationKey(authkey.getEid());
87             }
88         }
89     }
90
91     void setMappingSystem(IMappingSystem msmr) {
92         this.mapSystem = msmr;
93     }
94 }