Improved code readability / indentation
[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
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.ReadWriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
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.rev151012.Evc;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Uni;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.framework.FrameworkUtil;
32 import org.osgi.framework.ServiceRegistration;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import com.google.common.base.Optional;
37 import com.google.common.util.concurrent.CheckedFuture;
38
39 public class UnimgrProvider implements BindingAwareProvider, AutoCloseable, IUnimgrConsoleProvider {
40
41     private static final Logger LOG = LoggerFactory.getLogger(UnimgrProvider.class);
42
43     private UnimgrDataChangeListener listener;
44     private TransactionInvoker invoker;
45
46     private DataBroker dataBroker;
47     private ServiceRegistration<IUnimgrConsoleProvider> unimgrConsoleRegistration;
48
49     @Override
50     public void onSessionInitiated(ProviderContext session) {
51         LOG.info("UnimgrProvider Session Initiated");
52
53         // Retrieve the data broker to create transactions
54         dataBroker =  session.getSALService(DataBroker.class);
55         invoker = new  TransactionInvoker();
56
57         // Register the unimgr OSGi CLI
58         BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
59         unimgrConsoleRegistration = context.registerService(IUnimgrConsoleProvider.class,
60                                                             this,
61                                                             null);
62
63         // Register the uni data change listener
64         listener = new UnimgrDataChangeListener(dataBroker, invoker);
65
66         // Initialize operational and default config data in MD-SAL data store
67         initDatastore(LogicalDatastoreType.CONFIGURATION,
68                       UnimgrConstants.UNI_TOPOLOGY_ID);
69         initDatastore(LogicalDatastoreType.OPERATIONAL,
70                       UnimgrConstants.UNI_TOPOLOGY_ID);
71         initDatastore(LogicalDatastoreType.CONFIGURATION,
72                       UnimgrConstants.EVC_TOPOLOGY_ID);
73         initDatastore(LogicalDatastoreType.OPERATIONAL,
74                       UnimgrConstants.EVC_TOPOLOGY_ID);
75     }
76
77     @Override
78     public void close() throws Exception {
79         LOG.info("UnimgrProvider Closed");
80         unimgrConsoleRegistration.unregister();
81         listener.close();
82     }
83
84     protected void initDatastore(final LogicalDatastoreType type,
85                                  TopologyId topoId) {
86         InstanceIdentifier<Topology> path = InstanceIdentifier
87                                                 .create(NetworkTopology.class)
88                                                 .child(Topology.class,
89                                                         new TopologyKey(topoId));
90         initializeTopology(type);
91         ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
92         CheckedFuture<Optional<Topology>, ReadFailedException> unimgrTp = transaction.read(type,
93                                                                                            path);
94         try {
95             if (!unimgrTp.get().isPresent()) {
96                 TopologyBuilder tpb = new TopologyBuilder();
97                 tpb.setTopologyId(topoId);
98                 transaction.put(type, path, tpb.build());
99                 transaction.submit();
100             } else {
101                 transaction.cancel();
102             }
103         } catch (Exception e) {
104             LOG.error("Error initializing unimgr topology", e);
105         }
106     }
107
108     private void initializeTopology(LogicalDatastoreType type) {
109         ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
110         InstanceIdentifier<NetworkTopology> path = InstanceIdentifier.create(NetworkTopology.class);
111         CheckedFuture<Optional<NetworkTopology>, ReadFailedException> topology = transaction.read(type,path);
112         try {
113             if (!topology.get().isPresent()) {
114                 NetworkTopologyBuilder ntb = new NetworkTopologyBuilder();
115                 transaction.put(type,path,ntb.build());
116                 transaction.submit();
117             } else {
118                 transaction.cancel();
119             }
120         } catch (Exception e) {
121             LOG.error("Error initializing unimgr topology {}",e);
122         }
123     }
124
125     @Override
126     public boolean addUni(Uni uni) {
127         //TODO This code was left commented as an example
128         if (uni.getIpAddress() == null || uni.getMacAddress() == null) {
129             return false;
130         }
131 //        UniAugmentation uniAugmentation = new UniAugmentationBuilder()
132 //                                                .setIpAddress(uni.getIpAddress())
133 //                                                .setMacAddress(uni.getMacAddress())
134 //                                                .build();
135 //        ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
136 //        InstanceIdentifier<Node> path = UnimgrMapper.getUniAugmentationIidByMac(uni.getMacAddress());
137         return true;
138     }
139
140     @Override
141     public boolean removeUni(String uuid) {
142         // TODO Auto-generated method stub
143         return false;
144     }
145
146     @Override
147     public List<Uni> listUnis(boolean isConfigurationDatastore) {
148         // TODO Auto-generated method stub
149         return null;
150     }
151
152     @Override
153     public Uni getUni(String uuid) {
154         // TODO Auto-generated method stub
155         return null;
156     }
157
158     @Override
159     public boolean removeEvc(String uuid) {
160         // TODO Auto-generated method stub
161         return false;
162     }
163
164     @Override
165     public boolean addEvc(Evc evc) {
166         // TODO Auto-generated method stub
167         return false;
168     }
169
170     @Override
171     public Evc getEvc(String uuid) {
172         // TODO Auto-generated method stub
173         return null;
174     }
175
176 }