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.command;
11 import java.util.Map.Entry;
12 import java.util.concurrent.ExecutionException;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
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.unimgr.impl.UnimgrConstants;
19 import org.opendaylight.unimgr.impl.UnimgrMapper;
20 import org.opendaylight.unimgr.impl.UnimgrUtils;
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.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.google.common.base.Optional;
32 import com.google.common.util.concurrent.CheckedFuture;
34 public class EvcUpdateCommand extends AbstractUpdateCommand {
36 private static final Logger LOG = LoggerFactory.getLogger(EvcUpdateCommand.class);
38 public EvcUpdateCommand(final DataBroker dataBroker,
39 final Map<InstanceIdentifier<?>, DataObject> changes) {
40 super.dataBroker = dataBroker;
41 super.changes = changes;
45 public void execute() {
46 for (final Entry<InstanceIdentifier<?>, DataObject> updated : changes.entrySet()) {
47 if (updated.getValue() != null && updated.getValue() instanceof EvcAugmentation) {
48 final EvcAugmentation evc = (EvcAugmentation) updated.getValue();
49 final InstanceIdentifier<?> evcKey = updated.getKey();
51 // FIXME: For now, we assume that there is 1 uni per
53 if (evc.getUniDest() == null || evc.getUniDest().isEmpty()) {
54 LOG.error("Destination UNI cannot be null.");
57 if (evc.getUniSource() == null || evc.getUniSource().isEmpty()) {
58 LOG.error("Source UNI cannot be null.");
62 final Ipv4Address laterUni1Ip = evc.getUniSource().iterator().next().getIpAddress().getIpv4Address();
63 final Ipv4Address laterUni2Ip = evc.getUniDest().iterator().next().getIpAddress().getIpv4Address();
64 LOG.trace("New EVC created, source IP: {} destination IP {}.", laterUni1Ip, laterUni2Ip);
67 final ReadTransaction readTransac = dataBroker.newReadOnlyTransaction();
68 final CheckedFuture<?, ReadFailedException> retFormerEvc = readTransac.read(LogicalDatastoreType.OPERATIONAL, evcKey);
69 EvcAugmentation formerEvc;
71 formerEvc = (EvcAugmentation) ((Optional<EvcAugmentation>) retFormerEvc.checkedGet()).get();
72 final Ipv4Address formerUni1ip = formerEvc.getUniDest().iterator().next().getIpAddress().getIpv4Address();
73 final Ipv4Address formerUni2ip = formerEvc.getUniDest().iterator().next().getIpAddress().getIpv4Address();
75 if (formerUni1ip.equals(laterUni1Ip)) {
77 } else if (formerUni1ip.equals(laterUni2Ip)) {
80 LOG.info("{} is not part of the EVC, removing configuration", formerUni1ip);
81 final InstanceIdentifier<?> formerUniIID = UnimgrMapper.getUniIid(dataBroker, new IpAddress(formerUni1ip), LogicalDatastoreType.OPERATIONAL);
82 final Optional<Node> formerUni = UnimgrUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, formerUniIID);
83 UnimgrUtils.deleteEvcData(dataBroker, formerUni);
85 if (formerUni2ip.equals(laterUni1Ip)) {
87 } else if (formerUni2ip.equals(laterUni2Ip)) {
90 LOG.info("{} is not part of the EVC, removing configuration", formerUni1ip);
91 final InstanceIdentifier<?> formerUniIID = UnimgrMapper.getUniIid(dataBroker, new IpAddress(formerUni1ip), LogicalDatastoreType.OPERATIONAL);
92 final Optional<Node> formerUni = UnimgrUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, formerUniIID);
93 UnimgrUtils.deleteEvcData(dataBroker, formerUni);
95 } catch (ReadFailedException e) {
96 LOG.error("Failed to retrieve former EVC {}", evcKey, e);
99 InstanceIdentifier<Node> sourceUniIid;
100 InstanceIdentifier<Node> destinationUniIid;
102 final InstanceIdentifier<?> iidSource = evc.getUniSource().iterator().next().getUni();
103 if (iidSource != null) {
104 sourceUniIid = iidSource.firstIdentifierOf(Node.class);
106 sourceUniIid = UnimgrMapper.getUniIid(dataBroker,
107 evc.getUniSource().iterator().next().getIpAddress(),
108 LogicalDatastoreType.OPERATIONAL);
110 final InstanceIdentifier<?> iidDest = evc.getUniDest().iterator().next().getUni();
111 if (iidDest != null) {
112 destinationUniIid = iidDest.firstIdentifierOf(Node.class);
114 destinationUniIid = UnimgrMapper.getUniIid(dataBroker,
115 evc.getUniDest().iterator().next().getIpAddress(),
116 LogicalDatastoreType.OPERATIONAL);
119 // Retrieve the source and destination UNIs
120 final Optional<Node> optionalUniSource = UnimgrUtils.readNode(dataBroker,
121 LogicalDatastoreType.OPERATIONAL,
123 final Optional<Node> optionalUniDestination = UnimgrUtils.readNode(dataBroker,
124 LogicalDatastoreType.OPERATIONAL,
127 Node uniSource = null;
128 Node uniDestination = null;
130 if (optionalUniSource.isPresent() && optionalUniDestination.isPresent()) {
131 uniSource = optionalUniSource.get();
132 uniDestination = optionalUniDestination.get();
134 // Retrieve the source and/or destination OVSDB node
135 final UniAugmentation sourceUniAugmentation =
136 uniSource.getAugmentation(UniAugmentation.class);
137 final UniAugmentation destinationUniAugmentation =
138 uniDestination.getAugmentation(UniAugmentation.class);
139 final Optional<Node> optionalSourceOvsdbNode =
140 UnimgrUtils.readNode(dataBroker,
141 LogicalDatastoreType.OPERATIONAL,
142 sourceUniAugmentation
145 final Optional<Node> optionalDestinationOvsdbNode =
146 UnimgrUtils.readNode(dataBroker,
147 LogicalDatastoreType.OPERATIONAL,
148 destinationUniAugmentation
151 if (optionalSourceOvsdbNode.isPresent() && optionalDestinationOvsdbNode.isPresent()) {
152 // Retrieve the source and/or destination bridge
153 final InstanceIdentifier<Node> sourceBridgeIid =
154 UnimgrMapper.getOvsdbBridgeNodeIid(optionalSourceOvsdbNode.get());
155 final Optional<Node> optionalSourceBr = UnimgrUtils.readNode(dataBroker,
156 LogicalDatastoreType.OPERATIONAL,
158 final InstanceIdentifier<Node> destinationBridgeIid =
159 UnimgrMapper.getOvsdbBridgeNodeIid(optionalDestinationOvsdbNode.get());
160 final Optional<Node> optionalDestinationBr = UnimgrUtils.readNode(dataBroker,
161 LogicalDatastoreType.OPERATIONAL,
162 destinationBridgeIid);
163 Node sourceBr = null;
164 Node destinationBr = null;
165 if (optionalSourceBr.isPresent() && optionalDestinationBr.isPresent()) {
166 sourceBr = optionalSourceBr.get();
167 destinationBr = optionalDestinationBr.get();
169 // Creating termination points (OVSDB CONFIG
171 UnimgrUtils.createTerminationPointNode(dataBroker,
172 uniSource.getAugmentation(UniAugmentation.class),
174 UnimgrConstants.DEFAULT_BRIDGE_NAME,
175 UnimgrConstants.DEFAULT_TUNNEL_IFACE);
177 // Create GRE tunnel (OVSDB CONFIG datastore)
178 UnimgrUtils.createGreTunnel(dataBroker,
179 uniSource.getAugmentation(UniAugmentation.class),
180 uniDestination.getAugmentation(UniAugmentation.class),
182 UnimgrConstants.DEFAULT_BRIDGE_NAME,
183 UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
185 // Create termination points (CONFIG datastore)
186 UnimgrUtils.createTerminationPointNode(dataBroker,
187 uniSource.getAugmentation(UniAugmentation.class),
189 UnimgrConstants.DEFAULT_BRIDGE_NAME,
190 UnimgrConstants.DEFAULT_TUNNEL_IFACE);
192 // Create GRE tunnel (OVSDB CONFIG datastore)
193 UnimgrUtils.createGreTunnel(dataBroker,
194 uniDestination.getAugmentation(UniAugmentation.class),
195 uniSource.getAugmentation(UniAugmentation.class), destinationBr,
196 UnimgrConstants.DEFAULT_BRIDGE_NAME,
197 UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
200 UnimgrUtils.updateEvcNode(LogicalDatastoreType.CONFIGURATION,
206 UnimgrUtils.updateEvcNode(LogicalDatastoreType.OPERATIONAL,
213 LOG.info("Unable to retrieve the source and/or destination bridge.");
216 LOG.info("Uname to retrieve the source and/or destination ovsdbNode.");
219 LOG.info("Unable to retrieve the source and/or destination Uni.");