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