2 * Copyright (c) 2015 CableLabs and others. All rights reserved.
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
8 package org.opendaylight.unimgr.impl;
11 import java.util.List;
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;
36 import com.google.common.base.Optional;
37 import com.google.common.util.concurrent.CheckedFuture;
39 public class UnimgrProvider implements BindingAwareProvider, AutoCloseable, IUnimgrConsoleProvider {
41 private static final Logger LOG = LoggerFactory.getLogger(UnimgrProvider.class);
43 private UnimgrDataChangeListener listener;
44 private TransactionInvoker invoker;
46 private DataBroker dataBroker;
47 private ServiceRegistration<IUnimgrConsoleProvider> unimgrConsoleRegistration;
50 public void onSessionInitiated(ProviderContext session) {
51 LOG.info("UnimgrProvider Session Initiated");
53 // Retrieve the data broker to create transactions
54 dataBroker = session.getSALService(DataBroker.class);
55 invoker = new TransactionInvoker();
57 // Register the unimgr OSGi CLI
58 BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
59 unimgrConsoleRegistration = context.registerService(IUnimgrConsoleProvider.class,
63 // Register the uni data change listener
64 listener = new UnimgrDataChangeListener(dataBroker, invoker);
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);
78 public void close() throws Exception {
79 LOG.info("UnimgrProvider Closed");
80 unimgrConsoleRegistration.unregister();
84 protected void initDatastore(final LogicalDatastoreType type,
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,
95 if (!unimgrTp.get().isPresent()) {
96 TopologyBuilder tpb = new TopologyBuilder();
97 tpb.setTopologyId(topoId);
98 transaction.put(type, path, tpb.build());
101 transaction.cancel();
103 } catch (Exception e) {
104 LOG.error("Error initializing unimgr topology", e);
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);
113 if (!topology.get().isPresent()) {
114 NetworkTopologyBuilder ntb = new NetworkTopologyBuilder();
115 transaction.put(type,path,ntb.build());
116 transaction.submit();
118 transaction.cancel();
120 } catch (Exception e) {
121 LOG.error("Error initializing unimgr topology {}",e);
126 public boolean addUni(Uni uni) {
127 //TODO This code was left commented as an example
128 if (uni.getIpAddress() == null || uni.getMacAddress() == null) {
131 // UniAugmentation uniAugmentation = new UniAugmentationBuilder()
132 // .setIpAddress(uni.getIpAddress())
133 // .setMacAddress(uni.getMacAddress())
135 // ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
136 // InstanceIdentifier<Node> path = UnimgrMapper.getUniAugmentationIidByMac(uni.getMacAddress());
141 public boolean removeUni(String uuid) {
142 // TODO Auto-generated method stub
147 public List<Uni> listUnis(boolean isConfigurationDatastore) {
148 // TODO Auto-generated method stub
153 public Uni getUni(String uuid) {
154 // TODO Auto-generated method stub
159 public boolean removeEvc(String uuid) {
160 // TODO Auto-generated method stub
165 public boolean addEvc(Evc evc) {
166 // TODO Auto-generated method stub
171 public Evc getEvc(String uuid) {
172 // TODO Auto-generated method stub