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