Support adding RESTCONF data to DAO
[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.implementation.LispMappingService;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.MappingDatabase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.db.instance.AuthenticationKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.mapping.database.InstanceId;
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 LispMappingService msmr;
33
34     public AuthenticationKeyDataListener(DataBroker broker, LispMappingService msmr) {
35         setBroker(broker);
36         setMsmr(msmr);
37         setPath(InstanceIdentifier.create(MappingDatabase.class).child(InstanceId.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                 msmr.addAuthenticationKey(authkey.getLispAddressContainer(),
58                         authkey.getMaskLength(), authkey.getAuthkey());
59             }
60         }
61
62         // Process updated authentication keys
63         Map<InstanceIdentifier<?>, DataObject> updatedData = change.getUpdatedData();
64         for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : updatedData.entrySet()) {
65             if (entry.getValue() instanceof AuthenticationKey) {
66                 AuthenticationKey authkey = (AuthenticationKey)entry.getValue();
67
68                 LOG.trace("Received changed data");
69                 LOG.trace("Key: {}", entry.getKey());
70                 LOG.trace("Value: {}", authkey);
71
72                 msmr.addAuthenticationKey(authkey.getLispAddressContainer(),
73                         authkey.getMaskLength(), authkey.getAuthkey());
74             }
75         }
76
77         // Process deleted authentication keys
78         Set<InstanceIdentifier<?>> removedData = change.getRemovedPaths();
79         for (InstanceIdentifier<?> entry : removedData) {
80             DataObject dataObject = change.getOriginalData().get(entry);
81             if (dataObject instanceof AuthenticationKey) {
82                 AuthenticationKey authkey = (AuthenticationKey)dataObject;
83
84                 LOG.trace("Received deleted data");
85                 LOG.trace("Key: {}", entry);
86                 LOG.trace("Value: {}", authkey);
87
88                 msmr.removeAuthenticationKey(authkey.getLispAddressContainer(), authkey.getMaskLength());
89             }
90         }
91     }
92
93     void setMsmr(LispMappingService msmr) {
94         this.msmr = msmr;
95     }
96 }