03ed1e339bf96f018249b488843be57d8fb78362
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / impl / UnimgrProvider.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.List;
12
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
19 import org.opendaylight.unimgr.api.IUnimgrConsoleProvider;
20 import org.opendaylight.unimgr.command.TransactionInvoker;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev150622.Evcs;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev150622.EvcsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev150622.Unis;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev150622.UnisBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev150622.evcs.Evc;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev150622.unis.Uni;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.FrameworkUtil;
31 import org.osgi.framework.ServiceRegistration;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.google.common.util.concurrent.FutureCallback;
36 import com.google.common.util.concurrent.Futures;
37
38 public class UnimgrProvider implements BindingAwareProvider, AutoCloseable, IUnimgrConsoleProvider {
39
40     private static final Logger LOG = LoggerFactory.getLogger(UnimgrProvider.class);
41
42     private UniDataChangeListener unimgrDataChangeListener;
43     private EvcDataChangeListener evcDataChangeListener;
44     private UnimgrDataChangeListener listener;
45     private TransactionInvoker invoker;
46
47     private DataBroker dataBroker;
48     private ServiceRegistration<IUnimgrConsoleProvider> unimgrConsoleRegistration;
49
50     @Override
51     public void onSessionInitiated(ProviderContext session) {
52         LOG.info("VcpeProvider Session Initiated");
53
54         dataBroker =  session.getSALService(DataBroker.class);
55         invoker = new  TransactionInvoker();
56         // Initialize operational and default config data in MD-SAL data store
57         BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
58         unimgrConsoleRegistration = context.registerService(IUnimgrConsoleProvider.class, this, null);
59
60         unimgrDataChangeListener = new UniDataChangeListener(dataBroker);
61         evcDataChangeListener = new EvcDataChangeListener(dataBroker);
62         listener = new UnimgrDataChangeListener(dataBroker, invoker);
63
64         // Init UNI Config & Operational stores
65         Unis unis = new UnisBuilder().build();
66         initDatastore(LogicalDatastoreType.CONFIGURATION, UnimgrMapper.getUnisIid(), unis);
67         initDatastore(LogicalDatastoreType.OPERATIONAL, UnimgrMapper.getUnisIid(), unis);
68         // Init EVC Config & Operational stores
69         Evcs evcs = new EvcsBuilder().build();
70         initDatastore(LogicalDatastoreType.CONFIGURATION, UnimgrMapper.getEvcsIid(), evcs);
71         initDatastore(LogicalDatastoreType.OPERATIONAL, UnimgrMapper.getEvcsIid(), evcs);
72     }
73
74     @Override
75     public void close() throws Exception {
76         LOG.info("VcpeProvider Closed");
77         unimgrConsoleRegistration.unregister();
78         unimgrDataChangeListener.close();
79         evcDataChangeListener.close();
80         listener.close();
81     }
82
83     @SuppressWarnings({ "unchecked", "rawtypes" })
84     protected void initDatastore(final LogicalDatastoreType store, InstanceIdentifier iid, final DataObject object) {
85         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
86         tx.put(store, iid, object);
87
88         // Perform the tx.submit asynchronously
89         Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
90             @Override
91             public void onSuccess(final Void result) {
92                 LOG.info("initStore {} with object {} succeeded", store, object);
93             }
94             @Override
95             public void onFailure(final Throwable throwable)  {
96                 LOG.error("initStore {} with object {} failed", store, object);
97             }
98         });
99     }
100
101     @Override
102     public boolean addUni(Uni uni) {
103         Unis unis;
104         List<Uni> listOfUnis = listUnis(true);
105
106         try {
107             listOfUnis.add(uni);
108             unis = new UnisBuilder().setUni(listOfUnis).build();
109
110             // Place default config data in data store tree
111             WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
112             tx.put(LogicalDatastoreType.CONFIGURATION, UnimgrMapper.getUnisIid(), unis);
113
114             // Perform the tx.submit synchronously
115             tx.submit();
116         } catch (Exception e) {
117             LOG.error("addUni: failed: {}", e);
118             return false;
119         }
120         return true;
121     }
122
123     @Override
124     public boolean removeUni(String id) {
125         try {
126             // Removes default config data in data store tree
127             WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
128             tx.delete(LogicalDatastoreType.CONFIGURATION, UnimgrMapper.getUniIid(id));
129             System.out.println(UnimgrMapper.getUniIid(id));
130             // Perform the tx.submit synchronously
131             tx.submit();
132         } catch (Exception e) {
133             LOG.info("RemoveUni: failed: {}", e);
134             return false;
135         }
136         return true;
137     }
138
139     @Override
140     public List<Uni> listUnis(boolean isConfigurationDatastore) {
141         List<Uni> listOfUnis = null;
142
143         try {
144             ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction();
145             listOfUnis = tx.read((isConfigurationDatastore) ? LogicalDatastoreType.CONFIGURATION
146                     : LogicalDatastoreType.OPERATIONAL, UnimgrMapper.getUnisIid()).checkedGet().get().getUni();
147         } catch (Exception e) {
148             LOG.error("ListIntents: failed: {}", e);
149         }
150
151         if (listOfUnis == null) {
152             listOfUnis = new ArrayList<Uni>();
153         }
154         LOG.info("ListUnisConfiguration: list of unis retrieved sucessfully");
155         return listOfUnis;
156     }
157
158     @Override
159     public Uni getUni(String id) {
160         Uni uni = null;
161
162         try {
163             InstanceIdentifier<Uni> iid = UnimgrMapper.getUniIid(id);
164             System.out.println(UnimgrMapper.getUniIid(id));
165
166             ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction();
167             uni = tx.read(LogicalDatastoreType.CONFIGURATION, iid).checkedGet().get();
168
169             if (uni == null) {
170                 uni = tx.read(LogicalDatastoreType.OPERATIONAL, iid).checkedGet().get();
171             }
172         } catch (Exception e) {
173             LOG.error("getUni: failed: {}", e);
174             return null;
175         }
176         LOG.info("getUni: Uni retrieved sucessfully");
177         return uni;
178     }
179
180     @Override
181     public boolean removeEvc(String uuid) {
182         try {
183             WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
184             tx.delete(LogicalDatastoreType.CONFIGURATION, UnimgrMapper.getEvcIid(uuid));
185             System.out.println(UnimgrMapper.getEvcIid(uuid));
186             tx.submit();
187         } catch (Exception e) {
188             LOG.info("Remove Evc: failed: {}", e);
189             return false;
190         }
191         return true;
192     }
193
194     @Override
195     public boolean addEvc(Evc evc) {
196         // TODO Auto-generated method stub
197         return false;
198     }
199
200     @Override
201     public Evc getEvc(String uuid) {
202         // TODO Auto-generated method stub
203         return null;
204     }
205 }