commit is_exclusive flag implementation and e-tree services
[unimgr.git] / cisco-xr-driver / src / test / java / org / opendaylight / unimgr / mef / nrp / cisco / xr / l2vpn / activator / L2vpnP2pConnectionActivatorTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems Inc and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.unimgr.mef.nrp.cisco.xr.l2vpn.activator;
9
10 import static org.junit.Assert.fail;
11
12 import java.util.List;
13 import java.util.concurrent.ExecutionException;
14
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
20 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
24 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
25 import org.opendaylight.unimgr.mef.nrp.api.EndPoint;
26 import org.opendaylight.unimgr.mef.nrp.cisco.xr.common.MountPointHelper;
27 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.ifmgr.cfg.rev150730.InterfaceConfigurations;
28 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.ifmgr.cfg.rev150730._interface.configurations.InterfaceConfiguration;
29 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.ifmgr.cfg.rev150730._interface.configurations._interface.configuration.mtus.Mtu;
30 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.L2vpn;
31 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.l2vpn.database.xconnect.groups.XconnectGroup;
32 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.l2vpn.database.xconnect.groups.xconnect.group.p2p.xconnects.P2pXconnect;
33 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.l2vpn.database.xconnect.groups.xconnect.group.p2p.xconnects.p2p.xconnect.attachment.circuits.AttachmentCircuit;
34 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.l2vpn.database.xconnect.groups.xconnect.group.p2p.xconnects.p2p.xconnect.pseudowires.Pseudowire;
35 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.l2vpn.database.xconnect.groups.xconnect.group.p2p.xconnects.p2p.xconnect.pseudowires.pseudowire.Neighbor;
36 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.l2vpn.database.xconnect.groups.xconnect.group.p2p.xconnects.p2p.xconnect.pseudowires.pseudowire.pseudowire.content.MplsStaticLabels;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
40
41 import com.google.common.base.Optional;
42 import com.google.common.util.concurrent.CheckedFuture;
43
44 /**
45  * @author marek.ryznar@amartus.com
46  */
47 @RunWith(PowerMockRunner.class)
48 @PrepareForTest(MountPointHelper.class)
49 public class L2vpnP2pConnectionActivatorTest extends AbstractDataBrokerTest {
50
51     private L2vpnP2pConnectActivator l2VpnP2PConnectActivator;
52     private MountPointService mountService;
53     private Optional<DataBroker> optBroker;
54     private Long mtu;
55     private String deviceName = "localhost";
56     private String portNo1="80";
57     private String portNo2="8080";
58     private String serviceId = "serviceId";
59     private List<EndPoint> endPoints;
60
61     @Before
62     public void setUp() {
63         //given
64         DataBroker broker = getDataBroker();
65         optBroker = Optional.of(broker);
66
67         mountService = L2vpnTestUtils.getMockedMountPointService(optBroker);
68         l2VpnP2PConnectActivator = new L2vpnP2pConnectActivator(broker,mountService);
69
70         mtu = Long.valueOf(1500);
71         endPoints = L2vpnTestUtils.mockEndpoints(deviceName,deviceName,portNo1,portNo2);
72     }
73
74     @Test
75     public void testActivateAndDeactivate() {
76         //when
77         try {
78             l2VpnP2PConnectActivator.activate(endPoints, serviceId, true, "");
79         } catch (TransactionCommitFailedException e) {
80             fail("Error during activation : " + e.getMessage());
81         }
82
83         //then
84         ReadOnlyTransaction transaction = optBroker.get().newReadOnlyTransaction();
85
86         InstanceIdentifier<L2vpn> l2vpn = InstanceIdentifier.builder(L2vpn.class).build();
87         InstanceIdentifier<InterfaceConfigurations> interfaceConfigurations = InstanceIdentifier.builder(InterfaceConfigurations.class).build();
88
89         CheckedFuture<Optional<L2vpn>, ReadFailedException> driverL2vpn = transaction.read(LogicalDatastoreType.CONFIGURATION, l2vpn);
90         CheckedFuture<Optional<InterfaceConfigurations>, ReadFailedException> driverInterfaceConfigurations = transaction.read(LogicalDatastoreType.CONFIGURATION, interfaceConfigurations);
91
92         try {
93             checkL2vpnTree(driverL2vpn);
94             checkInterfaceConfigurationTree(driverInterfaceConfigurations);
95         } catch (InterruptedException | ExecutionException e) {
96             fail(e.getMessage());
97         }
98
99         //when
100         deactivate();
101
102         //then
103         L2vpnTestUtils.checkDeactivated(optBroker,portNo1);
104     }
105
106     private void deactivate() {
107         //when
108         try {
109             l2VpnP2PConnectActivator.deactivate(endPoints,serviceId);
110         } catch (TransactionCommitFailedException e) {
111             fail("Error during deactivation : " + e.getMessage());
112         }
113     }
114
115     private void checkL2vpnTree(CheckedFuture<Optional<L2vpn>, ReadFailedException> driverL2vpn) throws InterruptedException, ExecutionException{
116         if (driverL2vpn.get().isPresent()) {
117             L2vpn l2vpn = driverL2vpn.get().get();
118             L2vpnTestUtils.checkL2vpn(l2vpn);
119
120             XconnectGroup xconnectGroup = l2vpn.getDatabase().getXconnectGroups().getXconnectGroup().get(0);
121             L2vpnTestUtils.checkXConnectGroup(xconnectGroup,"EUR16-"+serviceId);
122
123             P2pXconnect p2pXconnect = xconnectGroup.getP2pXconnects().getP2pXconnect().get(0);
124             L2vpnTestUtils.checkP2pXconnect(p2pXconnect,"EUR16-p2p-"+serviceId);
125
126             AttachmentCircuit attachmentCircuit = p2pXconnect.getAttachmentCircuits().getAttachmentCircuit().get(0);
127             L2vpnTestUtils.checkAttachmentCircuit(attachmentCircuit,portNo1);
128
129             Pseudowire pseudowire = p2pXconnect.getPseudowires().getPseudowire().get(0);
130             L2vpnTestUtils.checkPseudowire(pseudowire);
131
132             Neighbor neighbor = pseudowire.getNeighbor().get(0);
133             L2vpnTestUtils.checkNeighbor(neighbor);
134
135             MplsStaticLabels mplsStaticLabels = neighbor.getMplsStaticLabels();
136             L2vpnTestUtils.checkMplsStaticLabels(mplsStaticLabels);
137         } else {
138             fail("L2vpn was not found.");
139         }
140     }
141
142     private void checkInterfaceConfigurationTree(CheckedFuture<Optional<InterfaceConfigurations>, ReadFailedException> driverInterfaceConfigurations) throws InterruptedException, ExecutionException{
143         if (driverInterfaceConfigurations.get().isPresent()) {
144             InterfaceConfigurations interfaceConfigurations = driverInterfaceConfigurations.get().get();
145             L2vpnTestUtils.checkInterfaceConfigurations(interfaceConfigurations);
146
147             InterfaceConfiguration interfaceConfiguration = interfaceConfigurations.getInterfaceConfiguration().get(0);
148             L2vpnTestUtils.checkInterfaceConfiguration(interfaceConfiguration,portNo1,true);
149
150             Mtu mtu1 = interfaceConfiguration.getMtus().getMtu().get(0);
151             L2vpnTestUtils.checkMtu(mtu1,mtu);
152         } else {
153             fail("InterfaceConfigurations was not found.");
154         }
155     }
156
157 }