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;
10 import java.util.List;
12 import org.mef.nrp.impl.ActivationDriverRepoService;
13 import org.mef.nrp.impl.ActivationDriverRepoServiceImpl;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
20 import org.opendaylight.unimgr.api.IUnimgrConsoleProvider;
21 import org.opendaylight.unimgr.utils.EvcUtils;
22 import org.opendaylight.unimgr.utils.MdsalUtils;
23 import org.opendaylight.unimgr.utils.OvsdbUtils;
24 import org.opendaylight.unimgr.utils.UniUtils;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Evc;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.osgi.framework.BundleContext;
42 import org.osgi.framework.FrameworkUtil;
43 import org.osgi.framework.ServiceRegistration;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import com.google.common.base.Optional;
48 import com.google.common.util.concurrent.CheckedFuture;
50 public class UnimgrProvider implements BindingAwareProvider, AutoCloseable, IUnimgrConsoleProvider {
52 private static final Logger LOG = LoggerFactory.getLogger(UnimgrProvider.class);
53 private DataBroker dataBroker;
54 private EvcDataTreeChangeListener evcListener;
55 private OvsNodeDataTreeChangeListener ovsListener;
56 private UniDataTreeChangeListener uniListener;
57 private ServiceRegistration<IUnimgrConsoleProvider> unimgrConsoleRegistration;
58 private FCRouteChangeListener fwConstructListener;
60 public UnimgrProvider() {
61 LOG.info("Unimgr provider initialized");
65 public boolean addEvc(final EvcAugmentation evc) {
66 // TODO Auto-generated method stub
71 public boolean addUni(final UniAugmentation uniAug) {
72 if ((uniAug == null) || (uniAug.getIpAddress() == null) || (uniAug.getMacAddress() == null)) {
75 return UniUtils.createUniNode(dataBroker, uniAug);
79 public void close() throws Exception {
80 LOG.info("UnimgrProvider Closed");
81 unimgrConsoleRegistration.unregister();
85 fwConstructListener.close();
89 public Evc getEvc(final String uuid) {
90 // TODO Auto-generated method stub
95 public UniAugmentation getUni(final IpAddress ipAddress) {
96 return UniUtils.getUni(dataBroker, LogicalDatastoreType.OPERATIONAL, ipAddress);
99 protected void initDatastore(final LogicalDatastoreType type,
100 final TopologyId topoId) {
101 final InstanceIdentifier<Topology> path = InstanceIdentifier
102 .create(NetworkTopology.class)
103 .child(Topology.class,
104 new TopologyKey(topoId));
105 initializeTopology(type);
106 final ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
107 final CheckedFuture<Optional<Topology>, ReadFailedException> unimgrTp = transaction.read(type,
110 if (!unimgrTp.get().isPresent()) {
111 final TopologyBuilder tpb = new TopologyBuilder();
112 tpb.setTopologyId(topoId);
113 transaction.put(type, path, tpb.build());
114 transaction.submit().get();
116 transaction.cancel();
118 } catch (final Exception e) {
119 LOG.error("Error initializing unimgr topology", e);
123 private void initializeTopology(final LogicalDatastoreType type) {
124 final ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
125 final InstanceIdentifier<NetworkTopology> path = InstanceIdentifier.create(NetworkTopology.class);
126 final CheckedFuture<Optional<NetworkTopology>, ReadFailedException> topology = transaction.read(type,path);
128 if (!topology.get().isPresent()) {
129 final NetworkTopologyBuilder ntb = new NetworkTopologyBuilder();
130 transaction.put(type,path,ntb.build());
131 transaction.submit().get();
133 transaction.cancel();
135 } catch (final Exception e) {
136 LOG.error("Error initializing unimgr topology {}", e);
141 public List<UniAugmentation> listUnis(final LogicalDatastoreType dataStoreType) {
142 return UniUtils.getUnis(dataBroker, dataStoreType);
146 public void onSessionInitiated(final ProviderContext session) {
147 LOG.info("UnimgrProvider Session Initiated");
149 // Retrieve the data broker to create transactions
150 dataBroker = session.getSALService(DataBroker.class);
151 // Register the unimgr OSGi CLI
152 final BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
153 unimgrConsoleRegistration = context.registerService(IUnimgrConsoleProvider.class, this, null);
155 // Register the data trees change listener
156 uniListener = new UniDataTreeChangeListener(dataBroker);
157 evcListener = new EvcDataTreeChangeListener(dataBroker);
158 ovsListener = new OvsNodeDataTreeChangeListener(dataBroker);
160 ActivationDriverRepoService activationDriverRepoService = new ActivationDriverRepoServiceImpl();
161 context.registerService(ActivationDriverRepoService.class, activationDriverRepoService, null);
163 fwConstructListener = new FCRouteChangeListener(dataBroker);
164 fwConstructListener.setActivationDriverRepoService(activationDriverRepoService);
166 // Initialize operational and default config data in MD-SAL data store
167 initDatastore(LogicalDatastoreType.CONFIGURATION,
168 UnimgrConstants.UNI_TOPOLOGY_ID);
169 initDatastore(LogicalDatastoreType.OPERATIONAL,
170 UnimgrConstants.UNI_TOPOLOGY_ID);
171 initDatastore(LogicalDatastoreType.CONFIGURATION,
172 UnimgrConstants.EVC_TOPOLOGY_ID);
173 initDatastore(LogicalDatastoreType.OPERATIONAL,
174 UnimgrConstants.EVC_TOPOLOGY_ID);
178 public boolean removeEvc(final String uuid) {
179 // TODO Auto-generated method stub
184 public boolean removeUni(final IpAddress ipAddress) {
185 final InstanceIdentifier<Node> iidUni = UnimgrMapper.getUniIid(dataBroker, ipAddress, LogicalDatastoreType.CONFIGURATION);
186 if (iidUni == null) {
189 return MdsalUtils.deleteNode(dataBroker, iidUni, LogicalDatastoreType.CONFIGURATION);
193 public boolean updateEvc(final InstanceIdentifier<Link> evcKey, final EvcAugmentation evc, final UniSource uniSource,
194 final UniDest uniDest) {
195 final InstanceIdentifier<?> sourceUniIid = uniSource.getUni();
196 final InstanceIdentifier<?> destinationUniIid = uniDest.getUni();
197 return EvcUtils.updateEvcNode(LogicalDatastoreType.CONFIGURATION, evcKey, evc, sourceUniIid,
198 destinationUniIid, dataBroker);
202 public boolean updateUni(final UniAugmentation uni) {
203 // Remove the old UNI with IpAdress and create a new one with updated informations
205 LOG.trace("UNI updated {}.", uni.getIpAddress().getIpv4Address());
206 final InstanceIdentifier<?> uniIID = UnimgrMapper.getUniIid(dataBroker,
207 uni.getIpAddress(), LogicalDatastoreType.OPERATIONAL);
209 if (uni.getOvsdbNodeRef() != null) {
210 final OvsdbNodeRef ovsdbNodeRef = uni.getOvsdbNodeRef();
211 ovsdbNode = MdsalUtils.readNode(dataBroker,
212 LogicalDatastoreType.OPERATIONAL, ovsdbNodeRef.getValue()).get();
214 UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, uniIID, uni, ovsdbNode, dataBroker);
215 LOG.trace("UNI updated {}.", uni.getIpAddress().getIpv4Address());
217 final Optional<Node> optionalOvsdbNode = OvsdbUtils.findOvsdbNode(dataBroker, uni);
218 ovsdbNode = optionalOvsdbNode.get();
220 MdsalUtils.deleteNode(dataBroker, uniIID, LogicalDatastoreType.OPERATIONAL);
221 return (UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION, uniIID,
222 uni, ovsdbNode, dataBroker) && UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, uniIID,
223 uni, ovsdbNode, dataBroker));