2 * Copyright (c) 2015 CableLabs and others. All rights reserved.
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
8 package org.opendaylight.unimgr.impl;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
20 import org.opendaylight.unimgr.api.IUnimgrDataChangeListener;
21 import org.opendaylight.unimgr.command.Command;
22 import org.opendaylight.unimgr.command.EvcCreateCommand;
23 import org.opendaylight.unimgr.command.EvcDeleteCommand;
24 import org.opendaylight.unimgr.command.EvcUpdateCommand;
25 import org.opendaylight.unimgr.command.TransactionInvoker;
26 import org.opendaylight.unimgr.command.UniCreateCommand;
27 import org.opendaylight.unimgr.command.UniDeleteCommand;
28 import org.opendaylight.unimgr.command.UniUpdateCommand;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class UnimgrDataChangeListener implements IUnimgrDataChangeListener {
37 private static final Logger LOG = LoggerFactory.getLogger(UnimgrDataChangeListener.class);
38 private Map<String, ListenerRegistration<DataChangeListener>> listeners;
39 private DataBroker dataBroker;
40 private TransactionInvoker invoker;
42 public UnimgrDataChangeListener(DataBroker dataBroker, TransactionInvoker invoker) {
43 this.dataBroker = dataBroker;
44 this.invoker = invoker;
45 listeners = new HashMap<String, ListenerRegistration<DataChangeListener>>();
46 ListenerRegistration<DataChangeListener> uniListener = dataBroker.registerDataChangeListener(
47 LogicalDatastoreType.CONFIGURATION, UnimgrMapper.getUnisIid()
48 , this, DataChangeScope.SUBTREE);
49 ListenerRegistration<DataChangeListener> evcListener = dataBroker.registerDataChangeListener(
50 LogicalDatastoreType.CONFIGURATION, UnimgrMapper.getEvcIid()
51 , this, DataChangeScope.SUBTREE);
52 ListenerRegistration<DataChangeListener> ovsdbListener = dataBroker.registerDataChangeListener(
53 LogicalDatastoreType.OPERATIONAL, UnimgrMapper.getOvsdbTopologyIdentifier()
54 , this, DataChangeScope.SUBTREE);
55 listeners.put("uni", uniListener);
56 listeners.put("evc", evcListener);
57 listeners.put("ovsdb", ovsdbListener);
61 public void onDataChanged(
62 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
63 create(change.getCreatedData());
64 update(change.getUpdatedData());
69 public void create(Map<InstanceIdentifier<?>, DataObject> changes) {
70 if (changes != null) {
71 List<Command> commands = new ArrayList<Command>();
72 commands.add(new UniCreateCommand(dataBroker, changes));
73 commands.add(new EvcCreateCommand(dataBroker, changes));
74 invoker.setCommands(commands);
80 public void update(Map<InstanceIdentifier<?>, DataObject> changes) {
81 if (changes != null) {
82 List<Command> commands = new ArrayList<Command>();
83 commands.add(new UniUpdateCommand(dataBroker, changes));
84 commands.add(new EvcUpdateCommand(dataBroker, changes));
85 invoker.setCommands(commands);
92 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
93 if (changes != null) {
94 List<Command> commands = new ArrayList<Command>();
95 commands.add(new UniDeleteCommand(dataBroker, changes));
96 commands.add(new EvcDeleteCommand(dataBroker, changes));
97 invoker.setCommands(commands);
103 public void close() throws Exception {
104 LOG.info("VcpeDataChangeListener stopped.");
105 for (Map.Entry<String, ListenerRegistration<DataChangeListener>> entry : listeners.entrySet()) {
106 ListenerRegistration<DataChangeListener> value = entry.getValue();