2 * Copyright (c) 2016 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.ovsdb.rev150105.OvsdbNodeRef;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Evc;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.FrameworkUtil;
39 import org.osgi.framework.ServiceRegistration;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.google.common.base.Optional;
44 import com.google.common.util.concurrent.CheckedFuture;
46 public class UnimgrProvider implements BindingAwareProvider, AutoCloseable, IUnimgrConsoleProvider {
48 private static final Logger LOG = LoggerFactory.getLogger(UnimgrProvider.class);
50 private UnimgrDataChangeListener listener;
51 private TransactionInvoker invoker;
53 private DataBroker dataBroker;
54 private ServiceRegistration<IUnimgrConsoleProvider> unimgrConsoleRegistration;
56 public UnimgrProvider() {
57 LOG.info("Unimgr provider initialized");
61 public void onSessionInitiated(ProviderContext session) {
62 LOG.info("UnimgrProvider Session Initiated");
64 // Retrieve the data broker to create transactions
65 dataBroker = session.getSALService(DataBroker.class);
66 invoker = new TransactionInvoker();
68 // Register the unimgr OSGi CLI
69 final BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
70 unimgrConsoleRegistration = context.registerService(IUnimgrConsoleProvider.class,
74 // Register the uni data change listener
75 listener = new UnimgrDataChangeListener(dataBroker, invoker);
77 // Initialize operational and default config data in MD-SAL data store
78 initDatastore(LogicalDatastoreType.CONFIGURATION,
79 UnimgrConstants.UNI_TOPOLOGY_ID);
80 initDatastore(LogicalDatastoreType.OPERATIONAL,
81 UnimgrConstants.UNI_TOPOLOGY_ID);
82 initDatastore(LogicalDatastoreType.CONFIGURATION,
83 UnimgrConstants.EVC_TOPOLOGY_ID);
84 initDatastore(LogicalDatastoreType.OPERATIONAL,
85 UnimgrConstants.EVC_TOPOLOGY_ID);
89 public void close() throws Exception {
90 LOG.info("UnimgrProvider Closed");
91 unimgrConsoleRegistration.unregister();
95 protected void initDatastore(final LogicalDatastoreType type,
97 final InstanceIdentifier<Topology> path = InstanceIdentifier
98 .create(NetworkTopology.class)
99 .child(Topology.class,
100 new TopologyKey(topoId));
101 initializeTopology(type);
102 final ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
103 final CheckedFuture<Optional<Topology>, ReadFailedException> unimgrTp = transaction.read(type,
106 if (!unimgrTp.get().isPresent()) {
107 final TopologyBuilder tpb = new TopologyBuilder();
108 tpb.setTopologyId(topoId);
109 transaction.put(type, path, tpb.build());
110 transaction.submit();
112 transaction.cancel();
114 } catch (final Exception e) {
115 LOG.error("Error initializing unimgr topology", e);
119 private void initializeTopology(LogicalDatastoreType type) {
120 final ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
121 final InstanceIdentifier<NetworkTopology> path = InstanceIdentifier.create(NetworkTopology.class);
122 final CheckedFuture<Optional<NetworkTopology>, ReadFailedException> topology = transaction.read(type,path);
124 if (!topology.get().isPresent()) {
125 final NetworkTopologyBuilder ntb = new NetworkTopologyBuilder();
126 transaction.put(type,path,ntb.build());
127 transaction.submit();
129 transaction.cancel();
131 } catch (final Exception e) {
132 LOG.error("Error initializing unimgr topology {}",e);
137 public boolean addUni(UniAugmentation uniAug) {
138 if (uniAug == null || uniAug.getIpAddress() == null || uniAug.getMacAddress() == null) {
141 return UnimgrUtils.createUniNode(dataBroker, uniAug);
145 public boolean removeUni(IpAddress ipAddress) {
146 final InstanceIdentifier<Node> iidUni = UnimgrMapper.getUniIid(dataBroker, ipAddress, LogicalDatastoreType.CONFIGURATION);
150 return UnimgrUtils.deleteNode(dataBroker, iidUni, LogicalDatastoreType.CONFIGURATION);
154 public List<UniAugmentation> listUnis(LogicalDatastoreType dataStoreType) {
155 return UnimgrUtils.getUnis(dataBroker, dataStoreType);
159 public UniAugmentation getUni(IpAddress ipAddress) {
160 return UnimgrUtils.getUni(dataBroker, LogicalDatastoreType.OPERATIONAL, ipAddress);
165 public boolean removeEvc(String uuid) {
166 // TODO Auto-generated method stub
171 public boolean addEvc(EvcAugmentation evc) {
172 if(evc == null || evc.getUniDest().isEmpty() || evc.getUniSource().isEmpty())
175 return UnimgrUtils.createEvc(dataBroker, evc);
179 public Evc getEvc(String uuid) {
180 // TODO Auto-generated method stub
185 public boolean updateEvc(InstanceIdentifier<Link> evcKey, EvcAugmentation evc, UniSource uniSource,
187 final InstanceIdentifier<?> sourceUniIid = uniSource.getUni();
188 final InstanceIdentifier<?> destinationUniIid = uniDest.getUni();
189 return UnimgrUtils.updateEvcNode(LogicalDatastoreType.CONFIGURATION, evcKey, evc, sourceUniIid,
190 destinationUniIid, dataBroker);
194 public boolean updateUni(UniAugmentation uni) {
195 // Remove the old UNI with IpAdress and create a new one with updated informations
197 LOG.trace("UNI updated {}.", uni.getIpAddress().getIpv4Address());
198 final InstanceIdentifier<?> uniIID = UnimgrMapper.getUniIid(dataBroker,
199 uni.getIpAddress(), LogicalDatastoreType.OPERATIONAL);
201 if (uni.getOvsdbNodeRef() != null) {
202 final OvsdbNodeRef ovsdbNodeRef = uni.getOvsdbNodeRef();
203 ovsdbNode= UnimgrUtils.readNode(dataBroker,
204 LogicalDatastoreType.OPERATIONAL, ovsdbNodeRef.getValue()).get();
206 UnimgrUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, uniIID, uni, ovsdbNode, dataBroker);
207 LOG.trace("UNI updated {}.", uni.getIpAddress().getIpv4Address());
209 final Optional<Node> optionalOvsdbNode = UnimgrUtils.findOvsdbNode(dataBroker, uni);
210 ovsdbNode = optionalOvsdbNode.get();
212 UnimgrUtils.deleteNode(dataBroker, uniIID, LogicalDatastoreType.OPERATIONAL);
213 return (UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION, uniIID,
214 uni, ovsdbNode, dataBroker) && UnimgrUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, uniIID,
215 uni, ovsdbNode, dataBroker));