Use Set instead of Map to store listeners
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / impl / UnimgrDataChangeListener.java
1 /*
2  * Copyright (c) 2015 CableLabs and others.  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.unimgr.impl;
9
10 import java.util.ArrayList;
11 import java.util.HashSet;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Set;
15
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.unimgr.api.IUnimgrDataChangeListener;
22 import org.opendaylight.unimgr.command.Command;
23 import org.opendaylight.unimgr.command.EvcCreateCommand;
24 import org.opendaylight.unimgr.command.EvcDeleteCommand;
25 import org.opendaylight.unimgr.command.EvcUpdateCommand;
26 import org.opendaylight.unimgr.command.TransactionInvoker;
27 import org.opendaylight.unimgr.command.UniCreateCommand;
28 import org.opendaylight.unimgr.command.UniDeleteCommand;
29 import org.opendaylight.unimgr.command.UniUpdateCommand;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class UnimgrDataChangeListener  implements IUnimgrDataChangeListener {
37
38     private static final Logger LOG = LoggerFactory.getLogger(UnimgrDataChangeListener.class);
39     private Set<ListenerRegistration<DataChangeListener>> listeners;
40     private DataBroker dataBroker;
41     private TransactionInvoker invoker;
42
43     public UnimgrDataChangeListener(DataBroker dataBroker, TransactionInvoker invoker) {
44         this.dataBroker = dataBroker;
45         this.invoker = invoker;
46         listeners = new HashSet<ListenerRegistration<DataChangeListener>>();
47         listeners.add(dataBroker.registerDataChangeListener(
48                 LogicalDatastoreType.CONFIGURATION, UnimgrMapper.createUniIid()
49                 , this, DataChangeScope.SUBTREE));
50         listeners.add(dataBroker.registerDataChangeListener(
51                 LogicalDatastoreType.CONFIGURATION, UnimgrMapper.createEvcIid()
52                 , this, DataChangeScope.SUBTREE));
53         listeners.add(dataBroker.registerDataChangeListener(
54                 LogicalDatastoreType.OPERATIONAL, UnimgrMapper.getOvsdbTopologyIdentifier()
55                 , this, DataChangeScope.SUBTREE));
56     }
57
58     @Override
59     public void onDataChanged(
60             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
61         create(change.getCreatedData());
62         update(change.getUpdatedData());
63         delete(change);
64     }
65
66     @Override
67     public void create(Map<InstanceIdentifier<?>, DataObject> changes) {
68         if (changes != null) {
69             List<Command> commands = new ArrayList<Command>();
70             commands.add(new UniCreateCommand(dataBroker, changes));
71             commands.add(new EvcCreateCommand(dataBroker, changes));
72             invoker.setCommands(commands);
73             invoker.invoke();
74         }
75     }
76
77     @Override
78     public void update(Map<InstanceIdentifier<?>, DataObject> changes) {
79         if (changes != null) {
80             List<Command> commands = new ArrayList<Command>();
81             commands.add(new UniUpdateCommand(dataBroker, changes));
82             commands.add(new EvcUpdateCommand(dataBroker, changes));
83             invoker.setCommands(commands);
84             invoker.invoke();
85         }
86     }
87
88     @Override
89     public void delete(
90             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
91         if (changes != null) {
92             List<Command> commands = new ArrayList<Command>();
93             commands.add(new UniDeleteCommand(dataBroker, changes));
94             commands.add(new EvcDeleteCommand(dataBroker, changes));
95             invoker.setCommands(commands);
96             invoker.invoke();
97         }
98     }
99
100     @Override
101     public void close() throws Exception {
102         LOG.info("UnimgrDataChangeListener stopped.");
103         for (ListenerRegistration<DataChangeListener> listener : listeners) {
104             if (listener != null) {
105                 listener.close();
106             }
107         }
108     }
109 }