Integrate the new yang model with Network Topology augmentation
[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         dataBroker =  session.getSALService(DataBroker.class);
54         invoker = new  TransactionInvoker();
55
56         BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
57         unimgrConsoleRegistration = context.registerService(IUnimgrConsoleProvider.class, this, null);
58
59         listener = new UnimgrDataChangeListener(dataBroker, invoker);
60
61         // Initialize operational and default config data in MD-SAL data store
62         initDatastore(LogicalDatastoreType.CONFIGURATION, UnimgrConstants.UNI_TOPOLOGY_ID);
63         initDatastore(LogicalDatastoreType.OPERATIONAL, UnimgrConstants.UNI_TOPOLOGY_ID);
64         initDatastore(LogicalDatastoreType.CONFIGURATION, UnimgrConstants.EVC_TOPOLOGY_ID);
65         initDatastore(LogicalDatastoreType.OPERATIONAL, UnimgrConstants.EVC_TOPOLOGY_ID);
66     }
67
68     @Override
69     public void close() throws Exception {
70         LOG.info("UnimgrProvider Closed");
71         unimgrConsoleRegistration.unregister();
72         listener.close();
73     }
74
75     protected void initDatastore(final LogicalDatastoreType type, TopologyId topoId) {
76         InstanceIdentifier<Topology> path = InstanceIdentifier
77                 .create(NetworkTopology.class)
78                 .child(Topology.class, new TopologyKey(topoId));
79         initializeTopology(type);
80         ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
81         CheckedFuture<Optional<Topology>, ReadFailedException> unimgrTp = transaction.read(type, path);
82         try {
83             if (!unimgrTp.get().isPresent()) {
84                 TopologyBuilder tpb = new TopologyBuilder();
85                 tpb.setTopologyId(topoId);
86                 transaction.put(type, path, tpb.build());
87                 transaction.submit();
88             } else {
89                 transaction.cancel();
90             }
91         } catch (Exception e) {
92             LOG.error("Error initializing unimgr topology", e);
93         }
94     }
95
96     private void initializeTopology(LogicalDatastoreType type) {
97         ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
98         InstanceIdentifier<NetworkTopology> path = InstanceIdentifier.create(NetworkTopology.class);
99         CheckedFuture<Optional<NetworkTopology>, ReadFailedException> topology = transaction.read(type,path);
100         try {
101             if (!topology.get().isPresent()) {
102                 NetworkTopologyBuilder ntb = new NetworkTopologyBuilder();
103                 transaction.put(type,path,ntb.build());
104                 transaction.submit();
105             } else {
106                 transaction.cancel();
107             }
108         } catch (Exception e) {
109             LOG.error("Error initializing unimgr topology {}",e);
110         }
111     }
112
113     @Override
114     public boolean addUni(Uni uni) {
115         //TODO Uncomment
116         if (uni.getIpAddress() == null || uni.getMacAddress() == null) {
117             return false;
118         }
119 //        UniAugmentation uniAugmentation = new UniAugmentationBuilder()
120 //                                                .setIpAddress(uni.getIpAddress())
121 //                                                .setMacAddress(uni.getMacAddress())
122 //                                                .build();
123 //        ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
124 //        InstanceIdentifier<Node> path = UnimgrMapper.getUniAugmentationIidByMac(uni.getMacAddress());
125         return true;
126     }
127
128     @Override
129     public boolean removeUni(String uuid) {
130         // TODO Auto-generated method stub
131         return false;
132     }
133
134     @Override
135     public List<Uni> listUnis(boolean isConfigurationDatastore) {
136         // TODO Auto-generated method stub
137         return null;
138     }
139
140     @Override
141     public Uni getUni(String uuid) {
142         // TODO Auto-generated method stub
143         return null;
144     }
145
146     @Override
147     public boolean removeEvc(String uuid) {
148         // TODO Auto-generated method stub
149         return false;
150     }
151
152     @Override
153     public boolean addEvc(Evc evc) {
154         // TODO Auto-generated method stub
155         return false;
156     }
157
158     @Override
159     public Evc getEvc(String uuid) {
160         // TODO Auto-generated method stub
161         return null;
162     }
163
164 }