Changed vpnmanager directory structure
[vpnservice.git] / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / vpnservice / VpnManager.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.vpnservice;
9
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
14 import org.opendaylight.yangtools.concepts.ListenerRegistration;
15 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
16 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance;
17 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInstances;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class VpnManager extends AbstractDataChangeListener<VpnInstance> implements AutoCloseable {
22     private static final Logger LOG = LoggerFactory.getLogger(VpnManager.class);
23     private ListenerRegistration<DataChangeListener> listenerRegistration;
24     private final DataBroker broker;
25
26     /**
27      * Listens for data change related to VPN Instance
28      * Informs the BGP about VRF information
29      * 
30      * @param db - dataBroker reference
31      */
32     public VpnManager(final DataBroker db) {
33         super(VpnInstance.class);
34         broker = db;
35         registerListener(db);
36     }
37
38     private void registerListener(final DataBroker db) {
39         try {
40             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
41                     getWildCardPath(), VpnManager.this, DataChangeScope.SUBTREE);
42         } catch (final Exception e) {
43             LOG.error("VPN Service DataChange listener registration fail!", e);
44             throw new IllegalStateException("VPN Service registration Listener failed.", e);
45         }
46     }
47
48     @Override
49     protected void remove(InstanceIdentifier<VpnInstance> identifier,
50             VpnInstance del) {
51         // TODO Auto-generated method stub
52     }
53
54     @Override
55     protected void update(InstanceIdentifier<VpnInstance> identifier,
56             VpnInstance original, VpnInstance update) {
57         // TODO Auto-generated method stub
58     }
59
60     @Override
61     protected void add(InstanceIdentifier<VpnInstance> identifier,
62             VpnInstance value) {
63         LOG.info("key: " + identifier + ", value=" + value);
64         //TODO: Generate VPN ID for this instance, where to store in model ... ?
65
66         //TODO: Add VRF to BGP
67     }
68
69     private InstanceIdentifier<?> getWildCardPath() {
70         return InstanceIdentifier.create(VpnInstances.class).child(VpnInstance.class);
71     }
72
73     @Override
74     public void close() throws Exception {
75         if (listenerRegistration != null) {
76             try {
77                 listenerRegistration.close();
78             } catch (final Exception e) {
79                 LOG.error("Error when cleaning up DataChangeListener.", e);
80             }
81             listenerRegistration = null;
82         }
83         LOG.info("VPN Manager Closed");
84     }
85 }