2 * Copyright © 2016 Inocybe Technologies 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.it;
10 import static org.ops4j.pax.exam.CoreOptions.composite;
11 import static org.ops4j.pax.exam.CoreOptions.maven;
12 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
14 import java.math.BigInteger;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
23 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
27 import org.opendaylight.controller.mdsal.it.base.AbstractMdsalTestBase;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentationBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDestBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDestKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSourceBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSourceKey;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.Destination;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.Source;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 import org.ops4j.pax.exam.Option;
59 import org.ops4j.pax.exam.junit.PaxExam;
60 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
61 import org.ops4j.pax.exam.options.MavenUrlReference;
62 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
63 import org.ops4j.pax.exam.spi.reactors.PerClass;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
67 import com.google.common.base.Optional;
68 import com.google.common.util.concurrent.CheckedFuture;
70 @RunWith(PaxExam.class)
71 @ExamReactorStrategy(PerClass.class)
72 public class UnimgrIT extends AbstractMdsalTestBase {
73 private static final Logger LOG = LoggerFactory.getLogger(UnimgrIT.class);
74 private DataBroker dataBroker;
76 private static final String MAC_ADDRESS_1 = "68:5b:35:bc:0f:7d";
77 private static final String MAC_ADDRESS_2 = "68:5b:35:bc:0f:7e";
78 private static final String MAC_LAYER = "IEEE 802.3-2005";
79 private static final String MODE = "Full Duplex";
80 private static final String MTU_SIZE = "0";
81 private static final String PHY_MEDIUM = "UNI TypeFull Duplex 2 Physical Interface";
82 private static final String TYPE = "";
83 private static final String IP_1 = "10.0.0.1";
84 private static final String IP_2 = "10.0.0.2";
85 private static final String EVC_ID_1 = "1";
88 public void setup() throws Exception {
91 dataBroker = getSession().getSALService(DataBroker.class);
92 Assert.assertNotNull("db should not be null", dataBroker);
96 public String getModuleName() {
101 public String getInstanceName() {
102 return "unimgr-default";
106 public MavenUrlReference getFeatureRepo() {
108 .groupId("org.opendaylight.unimgr")
109 .artifactId("unimgr-features")
110 .classifier("features")
112 .versionAsInProject();
116 public String getFeatureName() {
121 public Option getLoggingOption() {
122 Option option = editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
123 logConfiguration(UnimgrIT.class),
124 LogLevel.INFO.name());
125 option = composite(option, super.getLoggingOption());
130 public void testUnimgrFeatureLoad() {
131 Assert.assertTrue(true);
135 public void testUnimgr() {
136 InstanceIdentifier<Topology> uniTopoPath = InstanceIdentifier
137 .create(NetworkTopology.class)
138 .child(Topology.class,
139 new TopologyKey(new TopologyId(new Uri("unimgr:uni"))));
140 InstanceIdentifier<Topology> evcTopoPath = InstanceIdentifier
141 .create(NetworkTopology.class)
142 .child(Topology.class,
143 new TopologyKey(new TopologyId(new Uri("unimgr:evc"))));
144 InstanceIdentifier<Topology> ovdbTopoPath = InstanceIdentifier
145 .create(NetworkTopology.class)
146 .child(Topology.class, new TopologyKey(new TopologyId(new Uri("ovsdb:1"))));
148 // Read from md-sal and check if it is initialized with Uni and Evc augmentations
149 Topology topology = read(LogicalDatastoreType.CONFIGURATION, uniTopoPath);
150 Assert.assertNotNull("UNI Topology could not be found in " + LogicalDatastoreType.CONFIGURATION,
152 topology = read(LogicalDatastoreType.OPERATIONAL, uniTopoPath);
153 Assert.assertNotNull("UNI Topology could not be found in " + LogicalDatastoreType.OPERATIONAL,
155 topology = read(LogicalDatastoreType.CONFIGURATION, evcTopoPath);
156 Assert.assertNotNull("EVC Topology could not be found in " + LogicalDatastoreType.CONFIGURATION,
158 topology = read(LogicalDatastoreType.OPERATIONAL, evcTopoPath);
159 Assert.assertNotNull("EVC Topology could not be found in " + LogicalDatastoreType.OPERATIONAL,
161 topology = read(LogicalDatastoreType.CONFIGURATION, ovdbTopoPath);
162 Assert.assertNotNull("OVSDB Topology could not be found in " + LogicalDatastoreType.CONFIGURATION,
164 topology = read(LogicalDatastoreType.CONFIGURATION, ovdbTopoPath);
165 Assert.assertNotNull("OVSDB Topology could not be found in " + LogicalDatastoreType.OPERATIONAL,
169 public void createAndDeleteUNITest() {
170 LOG.info("Test for create and delete UNI");
172 InstanceIdentifier<Node> nodePath = createUniNode(MAC_ADDRESS_1, IP_1);
173 Assert.assertNotNull(nodePath);
174 Assert.assertTrue(validateUni(true, nodePath));
176 InstanceIdentifier<Node> deletedNodePath = deleteNode(MAC_ADDRESS_1, IP_1);
177 Assert.assertNotNull(deletedNodePath);
178 Assert.assertTrue(validateUni(false, deletedNodePath));
182 public void testCreateAndDeleteEvc() {
183 LOG.info("Test for create Evc");
184 // Create an evc between the two Uni nodes
185 InstanceIdentifier<Link> evcIid = createEvcLink(IP_1, MAC_ADDRESS_1, IP_2, MAC_ADDRESS_2, EVC_ID_1);
186 Assert.assertNotNull(evcIid);
188 // Validate Evc create operation
189 boolean status = validateEvc(true, EVC_ID_1);
190 Assert.assertTrue(status);
193 evcIid = deleteEvc(EVC_ID_1);
194 Assert.assertNotNull(evcIid);
196 // Validate Evc delete operation
197 status = validateEvc(false, EVC_ID_1);
198 Assert.assertTrue(status);
201 private boolean validateEvc(boolean forCreate, String evcId) {
202 InstanceIdentifier<Link> iid = getEvcLinkIid(evcId);
203 Link evc = read(LogicalDatastoreType.CONFIGURATION, iid);
204 if (forCreate && evc != null) {
206 } else if (!forCreate && evc == null) {
212 private InstanceIdentifier<Node> createUniNode(String macAddress, String ipAddress) {
213 UniAugmentation uni = new UniAugmentationBuilder()
214 .setMacAddress(new MacAddress(macAddress))
215 .setMacLayer(MAC_LAYER)
217 .setMtuSize(BigInteger.valueOf(Long.valueOf(MTU_SIZE)))
218 .setPhysicalMedium(PHY_MEDIUM)
220 .setIpAddress(new IpAddress(ipAddress.toCharArray()))
223 NodeId uniNodeId = new NodeId(new NodeId("uni://" + uni.getIpAddress().getIpv4Address().getValue().toString()));
224 InstanceIdentifier<Node> uniNodeIid = null;
225 uniNodeIid = getUniIid("uni://" + uni.getIpAddress().getIpv4Address().getValue().toString());
226 NodeKey uniNodeKey = new NodeKey(uniNodeId);
227 Node nodeData = new NodeBuilder()
228 .setNodeId(uniNodeId)
230 .addAugmentation(UniAugmentation.class, uni)
232 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
234 writeUNI(uniNodeIid, nodeData, transaction);
235 LOG.info("Created and submitted a new Uni node {}", nodeData.getNodeId());
237 } catch (Exception e) {
238 transaction.cancel();
239 LOG.error("Could not create Uni Node - Id: {}, {}", uniNodeId, e);
244 private void writeUNI(InstanceIdentifier<Node> uniNodeIid, Node nodeData, WriteTransaction transaction) throws TransactionCommitFailedException {
245 transaction.put(LogicalDatastoreType.CONFIGURATION, uniNodeIid, nodeData);
246 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
250 private InstanceIdentifier<Link> deleteEvc(String evcId) {
251 LinkId evcLinkId = new LinkId(new LinkId("evc://" + evcId));
252 InstanceIdentifier<Link> evcLinkIid = null;
253 evcLinkIid = getEvcLinkIid(evcId);
254 if (evcLinkIid != null) {
255 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
256 transaction.delete(LogicalDatastoreType.CONFIGURATION, evcLinkIid);
257 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
260 } catch (TransactionCommitFailedException e) {
261 LOG.error("Error while deleting Evc {}", evcLinkIid);
263 LOG.info("Deleted an Evc link {}", evcLinkId);
268 private InstanceIdentifier<Link> createEvcLink(String srcUniIp, String srcMac,
269 String dstUniIp, String dstMac, String evcId) {
270 // Create two Uni nodes before creating an Evc
271 InstanceIdentifier<Node> uniIid = createUniNode(srcMac, srcUniIp);
272 Assert.assertNotNull(uniIid);
274 uniIid = createUniNode(dstMac, dstUniIp);
275 Assert.assertNotNull(uniIid);
277 // Create Evc link between the two Uni Nodes
278 List<UniSource> src = new ArrayList<>();
279 InstanceIdentifier<?> srcUniIid = getUniIid("uni://" + srcUniIp);
280 UniSource uniSrc = new UniSourceBuilder()
281 .setIpAddress(new IpAddress(srcUniIp.toCharArray()))
283 .setKey(new UniSourceKey((short) 1))
288 List<UniDest> dst = new ArrayList<>();
289 InstanceIdentifier<?> dstUniIid = getUniIid("uni://" + dstUniIp);;
290 UniDest uniDst = new UniDestBuilder()
291 .setIpAddress(new IpAddress(dstUniIp.toCharArray()))
293 .setKey(new UniDestKey((short) 2))
298 EvcAugmentation evc = new EvcAugmentationBuilder()
303 LinkId evcLinkId = new LinkId(new LinkId("evc://" + evcId));
304 InstanceIdentifier<Link> evcLinkIid = null;
306 evcLinkIid = getEvcLinkIid(evcId);
307 LinkKey evcLinkKey = new LinkKey(evcLinkId);
308 Source mandatorySrcNode = new SourceBuilder().setSourceNode(new NodeId("uni://" + srcUniIp)).build();
309 Destination mandatoryDstNode = new DestinationBuilder().setDestNode(new NodeId("uni://" + dstUniIp)).build();
310 Link linkData = new LinkBuilder()
312 .setSource(mandatorySrcNode)
313 .setDestination(mandatoryDstNode)
314 .setLinkId(evcLinkId)
315 .addAugmentation(EvcAugmentation.class, evc)
317 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
318 transaction.put(LogicalDatastoreType.CONFIGURATION, evcLinkIid, linkData);
319 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
321 LOG.info("Created and submitted a new Evc link {}", evcLinkId);
322 } catch (Exception e) {
323 LOG.error("Exception while creating Evc " + "Evc link Id: {}, {}", evcLinkId, e);
329 private InstanceIdentifier<Node> getUniIid(String nodeId) {
330 NodeId uniNodeId = new NodeId(new NodeId(nodeId));
331 InstanceIdentifier<Node> uniNodeIid = InstanceIdentifier
332 .create(NetworkTopology.class)
333 .child(Topology.class, new TopologyKey(new TopologyId(new Uri("unimgr:uni"))))
334 .child(Node.class, new NodeKey(uniNodeId));
338 private InstanceIdentifier<Link> getEvcLinkIid(String linkId) {
339 LinkId evcLinkId = new LinkId(new LinkId("evc://" + linkId));
340 InstanceIdentifier<Link> linkPath = InstanceIdentifier
341 .create(NetworkTopology.class)
342 .child(Topology.class,new TopologyKey(new TopologyId(new Uri("unimgr:evc"))))
343 .child(Link.class, new LinkKey(evcLinkId));
347 private <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
348 final LogicalDatastoreType store, final InstanceIdentifier<D> path) {
350 final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
351 Optional<D> optionalDataObject;
352 CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
354 optionalDataObject = future.checkedGet();
355 if (optionalDataObject.isPresent()) {
356 result = optionalDataObject.get();
358 LOG.error("{}: Failed to read {}",
359 Thread.currentThread().getStackTrace()[1], path);
361 } catch (ReadFailedException e) {
362 LOG.error("Failed to read {} ", path, e);
368 private boolean validateUni(boolean forCreate, InstanceIdentifier<Node> iid) {
369 Node uni = read(LogicalDatastoreType.CONFIGURATION, iid);
370 if (forCreate && uni != null) {
372 } else if (!forCreate && uni == null) {
378 private InstanceIdentifier<Node> deleteNode(String macAddress, String ipAddress) {
379 UniAugmentation uni = new UniAugmentationBuilder().setMacAddress(new MacAddress(macAddress))
380 .setMacLayer(MAC_LAYER).setMode(MODE).setMtuSize(BigInteger.valueOf(Long.valueOf(MTU_SIZE)))
381 .setPhysicalMedium(PHY_MEDIUM).setType(TYPE).setIpAddress(new IpAddress(ipAddress.toCharArray()))
384 NodeId uniNodeId = new NodeId(new NodeId("uni://" + uni.getIpAddress().getIpv4Address().getValue().toString()));
385 InstanceIdentifier<Node> genericNode = InstanceIdentifier
386 .create(NetworkTopology.class)
387 .child(Topology.class,
388 new TopologyKey(new TopologyId(new Uri("unimgr:uni"))))
390 new NodeKey(uniNodeId));
392 LOG.info("Received a request to delete node {}", genericNode);
393 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
394 transaction.delete(LogicalDatastoreType.CONFIGURATION, genericNode);
396 transaction.submit().checkedGet();
397 } catch (TransactionCommitFailedException e) {
398 LOG.error("Unable to remove node with Iid {} from store {}.", genericNode, LogicalDatastoreType.CONFIGURATION);