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.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Evc;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.osgi.framework.BundleContext;
32 import org.osgi.framework.FrameworkUtil;
33 import org.osgi.framework.ServiceRegistration;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.common.base.Optional;
38 import com.google.common.util.concurrent.CheckedFuture;
40 public class UnimgrProvider implements BindingAwareProvider, AutoCloseable, IUnimgrConsoleProvider {
42 private static final Logger LOG = LoggerFactory.getLogger(UnimgrProvider.class);
44 private UnimgrDataChangeListener listener;
45 private TransactionInvoker invoker;
47 private DataBroker dataBroker;
48 private ServiceRegistration<IUnimgrConsoleProvider> unimgrConsoleRegistration;
50 public UnimgrProvider() {
51 LOG.info("Unimgr provider initialized");
55 public void onSessionInitiated(ProviderContext session) {
56 LOG.info("UnimgrProvider Session Initiated");
58 // Retrieve the data broker to create transactions
59 dataBroker = session.getSALService(DataBroker.class);
60 invoker = new TransactionInvoker();
62 // Register the unimgr OSGi CLI
63 BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
64 unimgrConsoleRegistration = context.registerService(IUnimgrConsoleProvider.class,
68 // Register the uni data change listener
69 listener = new UnimgrDataChangeListener(dataBroker, invoker);
71 // Initialize operational and default config data in MD-SAL data store
72 initDatastore(LogicalDatastoreType.CONFIGURATION,
73 UnimgrConstants.UNI_TOPOLOGY_ID);
74 initDatastore(LogicalDatastoreType.OPERATIONAL,
75 UnimgrConstants.UNI_TOPOLOGY_ID);
76 initDatastore(LogicalDatastoreType.CONFIGURATION,
77 UnimgrConstants.EVC_TOPOLOGY_ID);
78 initDatastore(LogicalDatastoreType.OPERATIONAL,
79 UnimgrConstants.EVC_TOPOLOGY_ID);
83 public void close() throws Exception {
84 LOG.info("UnimgrProvider Closed");
85 unimgrConsoleRegistration.unregister();
89 protected void initDatastore(final LogicalDatastoreType type,
91 InstanceIdentifier<Topology> path = InstanceIdentifier
92 .create(NetworkTopology.class)
93 .child(Topology.class,
94 new TopologyKey(topoId));
95 initializeTopology(type);
96 ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
97 CheckedFuture<Optional<Topology>, ReadFailedException> unimgrTp = transaction.read(type,
100 if (!unimgrTp.get().isPresent()) {
101 TopologyBuilder tpb = new TopologyBuilder();
102 tpb.setTopologyId(topoId);
103 transaction.put(type, path, tpb.build());
104 transaction.submit();
106 transaction.cancel();
108 } catch (Exception e) {
109 LOG.error("Error initializing unimgr topology", e);
113 private void initializeTopology(LogicalDatastoreType type) {
114 ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
115 InstanceIdentifier<NetworkTopology> path = InstanceIdentifier.create(NetworkTopology.class);
116 CheckedFuture<Optional<NetworkTopology>, ReadFailedException> topology = transaction.read(type,path);
118 if (!topology.get().isPresent()) {
119 NetworkTopologyBuilder ntb = new NetworkTopologyBuilder();
120 transaction.put(type,path,ntb.build());
121 transaction.submit();
123 transaction.cancel();
125 } catch (Exception e) {
126 LOG.error("Error initializing unimgr topology {}",e);
131 public boolean addUni(UniAugmentation uniAug) {
132 if (uniAug == null || uniAug.getIpAddress() == null || uniAug.getMacAddress() == null) {
135 return UnimgrUtils.createUniNode(dataBroker, uniAug);
139 public boolean removeUni(IpAddress ipAddress) {
140 // TODO Auto-generated method stub
145 public List<UniAugmentation> listUnis(LogicalDatastoreType dataStoreType) {
146 return UnimgrUtils.getUnis(dataBroker, dataStoreType);
150 public UniAugmentation getUni(IpAddress ipAddress) {
151 return UnimgrUtils.getUni(dataBroker, LogicalDatastoreType.CONFIGURATION, ipAddress);
156 public boolean removeEvc(String uuid) {
157 // TODO Auto-generated method stub
162 public boolean addEvc(Evc evc) {
163 // TODO Auto-generated method stub
168 public Evc getEvc(String uuid) {
169 // TODO Auto-generated method stub