Merge "Updating operational datastore"
[vpnservice.git] / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / bgpmanager / BgpConfigurationManager.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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.bgpmanager;
9
10 import java.util.Collections;
11 import java.util.Map;
12 import java.util.Set;
13
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.bgp.rev130715.BgpRouter;
22 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.bgp.rev130715.BgpNeighbors;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.common.base.Optional;
27 import com.google.common.base.Preconditions;
28
29 public class BgpConfigurationManager {
30     private static final Logger LOG = LoggerFactory.getLogger(BgpConfigurationManager.class);
31     private ListenerRegistration<DataChangeListener> listenerRegistration;
32     private final DataBroker broker;
33
34     public BgpConfigurationManager(final DataBroker db) {
35         broker = db;
36         BgpRtrCfgManager rtrCfgManager = new BgpRtrCfgManager(db);
37         BgpNghbrCfgManager nghbrCfgManager = new BgpNghbrCfgManager(db);
38     }
39
40     public class BgpRtrCfgManager extends AbstractDataChangeListener<BgpRouter> implements AutoCloseable {
41
42         public BgpRtrCfgManager(final DataBroker db) {
43             super(BgpRouter.class);
44             registerListener(db);
45         }
46
47         private void registerListener(final DataBroker db) {
48             try {
49                 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
50                     getWildCardPath(), BgpRtrCfgManager.this, DataChangeScope.SUBTREE);
51             } catch (final Exception e) {
52                 LOG.error("BGP Configuration DataChange listener registration fail!", e);
53                 throw new IllegalStateException("BGP Configuration registration Listener failed.", e);
54             }
55         }
56
57         @Override
58         protected void remove(InstanceIdentifier<BgpRouter> identifier,
59                               BgpRouter del) {
60             // TODO Auto-generated method stub
61         }
62
63         @Override
64         protected void update(InstanceIdentifier<BgpRouter> identifier,
65                               BgpRouter original, BgpRouter update) {
66             // TODO Auto-generated method stub
67         }
68
69         @Override
70         protected void add(InstanceIdentifier<BgpRouter> identifier,
71                            BgpRouter value) {
72             LOG.info("key: " + identifier + ", value=" + value);
73
74         }
75
76         private InstanceIdentifier<BgpRouter> getWildCardPath() {
77             return InstanceIdentifier.create(BgpRouter.class);
78         }
79
80         @Override
81         public void close() throws Exception {
82             if (listenerRegistration != null) {
83                 try {
84                     listenerRegistration.close();
85                 } catch (final Exception e) {
86                     LOG.error("Error when cleaning up DataChangeListener.", e);
87                 }
88                 listenerRegistration = null;
89             }
90             LOG.info("Bgp Router Manager Closed");
91         }
92
93     }
94     public class BgpNghbrCfgManager extends AbstractDataChangeListener<BgpNeighbors> implements AutoCloseable {
95
96         public BgpNghbrCfgManager(final DataBroker db) {
97             super(BgpNeighbors.class);
98             registerListener(db);
99         }
100
101         private void registerListener(final DataBroker db) {
102             try {
103                 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
104                     getWildCardPath(), BgpNghbrCfgManager.this, DataChangeScope.SUBTREE);
105             } catch (final Exception e) {
106                 LOG.error("BGP Neighbor DataChange listener registration fail!", e);
107                 throw new IllegalStateException("BGP Neighbor registration Listener failed.", e);
108             }
109         }
110
111         @Override
112         protected void remove(InstanceIdentifier<BgpNeighbors> identifier,
113                               BgpNeighbors del) {
114             // TODO Auto-generated method stub
115         }
116
117         @Override
118         protected void update(InstanceIdentifier<BgpNeighbors> identifier,
119                               BgpNeighbors original, BgpNeighbors update) {
120             // TODO Auto-generated method stub
121         }
122
123         @Override
124         protected void add(InstanceIdentifier<BgpNeighbors> identifier,
125                            BgpNeighbors value) {
126             LOG.info("key: " + identifier + ", value=" + value);
127
128         }
129
130         private InstanceIdentifier<?> getWildCardPath() {
131             return InstanceIdentifier.create(BgpNeighbors.class);
132         }
133
134         @Override
135         public void close() throws Exception {
136             if (listenerRegistration != null) {
137                 try {
138                     listenerRegistration.close();
139                 } catch (final Exception e) {
140                     LOG.error("Error when cleaning up DataChangeListener.", e);
141                 }
142                 listenerRegistration = null;
143             }
144             LOG.info("Bgp Neighbor Manager Closed");
145         }
146
147     }
148 }