Activation status handling mechanism for ForwardingConstruct provisioning
[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.unimgr.mef.nrp.common.MountPointHelper;
22 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.ifmgr.cfg.rev150730.InterfaceConfigurations;
23 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.ifmgr.cfg.rev150730._interface.configurations.InterfaceConfiguration;
24 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.L2vpn;
25 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.l2vpn.database.xconnect.groups.XconnectGroup;
26 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;
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.p2p.xconnect.attachment.circuits.AttachmentCircuit;
28 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.g_forwardingconstruct.FcPort;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.powermock.core.classloader.annotations.PrepareForTest;
31 import org.powermock.modules.junit4.PowerMockRunner;
32
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.concurrent.ExecutionException;
36
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertNotNull;
39 import static org.junit.Assert.fail;
40
41 /**
42  * @author marek.ryznar@amartus.com
43  */
44 @RunWith(PowerMockRunner.class)
45 @PrepareForTest(MountPointHelper.class)
46 public class L2vpnBridgeActivatorTest extends AbstractDataBrokerTest{
47
48     private L2vpnBridgeActivator l2vpnBridgeActivator;
49     private MountPointService mountService;
50     private Optional<DataBroker> optBroker;
51     private String nodeName;
52     private String outerName;
53     private String innerName;
54     private String portNo1;
55     private String portNo2;
56     private FcPort port;
57     private FcPort neighbor;
58     private Long mtu ;
59
60     @Before
61     public void setUp(){
62         //given
63         DataBroker broker = getDataBroker();
64         optBroker = Optional.of(broker);
65
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 = Long.valueOf(1500);
77     }
78
79     @Test
80     public void testActivate(){
81         //when
82         l2vpnBridgeActivator.activate(nodeName, outerName, innerName, port, neighbor, mtu);
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
101     @Test
102     public void testDeactivate(){
103         //when
104         l2vpnBridgeActivator.deactivate(nodeName,outerName,innerName,port,neighbor,mtu);
105
106         //then
107         L2vpnActivatorTestUtils.checkDeactivation(optBroker);
108     }
109
110     private void checkL2vpnTree(CheckedFuture<Optional<L2vpn>, ReadFailedException> driverL2vpn) throws InterruptedException, ExecutionException {
111        if (driverL2vpn.get().isPresent()){
112            L2vpn l2vpn = driverL2vpn.get().get();
113            L2vpnActivatorTestUtils.checkL2vpn(l2vpn);
114
115            XconnectGroup xconnectGroup = l2vpn.getDatabase().getXconnectGroups().getXconnectGroup().get(0);
116            L2vpnActivatorTestUtils.checkXConnectGroup(xconnectGroup,outerName);
117
118            P2pXconnect p2pXconnect = xconnectGroup.getP2pXconnects().getP2pXconnect().get(0);
119            L2vpnActivatorTestUtils.checkP2pXconnect(p2pXconnect,innerName);
120
121            List<AttachmentCircuit> attachmentCircuits = p2pXconnect.getAttachmentCircuits().getAttachmentCircuit();
122            assertNotNull(attachmentCircuits);
123            assertEquals(2, attachmentCircuits.size());
124
125            attachmentCircuits.sort(
126                    (AttachmentCircuit ac1, AttachmentCircuit ac2)
127                            -> ac1.getName().getValue().compareTo(ac2.getName().getValue()));
128
129            L2vpnActivatorTestUtils.checkAttachmentCircuit(attachmentCircuits.get(0), portNo1);
130            L2vpnActivatorTestUtils.checkAttachmentCircuit(attachmentCircuits.get(1), portNo2);
131        } else {
132            fail("L2vpn was not found.");
133        }
134     }
135
136     private void checkInterfaceConfigurationTree(CheckedFuture<Optional<InterfaceConfigurations>, ReadFailedException> driverInterfaceConfigurations) throws InterruptedException, ExecutionException{
137         if (driverInterfaceConfigurations.get().isPresent()){
138             InterfaceConfigurations interfaceConfigurations = driverInterfaceConfigurations.get().get();
139             L2vpnActivatorTestUtils.checkInterfaceConfigurations(interfaceConfigurations);
140
141             List<InterfaceConfiguration> interfaceConfigurationList = interfaceConfigurations.getInterfaceConfiguration();
142             interfaceConfigurationList.sort(
143                     (InterfaceConfiguration ic1, InterfaceConfiguration ic2)
144                             -> ic1.getInterfaceName().getValue().compareTo(ic2.getInterfaceName().getValue()));
145
146             L2vpnActivatorTestUtils.checkInterfaceConfiguration(interfaceConfigurationList.get(0),portNo1,false);
147             L2vpnActivatorTestUtils.checkInterfaceConfiguration(interfaceConfigurationList.get(1),portNo2,false);
148         } else {
149             fail("InterfaceConfigurations was not found.");
150         }
151     }
152 }