manually cherry pick this patch https://git.opendaylight.org/gerrit/#/c/34579/
[unimgr.git] / impl / src / test / java / org / opendaylight / unimgr / command / EvcUpdateCommandTest.java
1 /*
2  * Copyright (c) 2016 CableLabs 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.command;
9
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.when;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
23 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.unimgr.impl.UnimgrConstants;
26 import org.opendaylight.unimgr.impl.UnimgrMapper;
27 import org.opendaylight.unimgr.utils.EvcUtils;
28 import org.opendaylight.unimgr.utils.MdsalUtils;
29 import org.opendaylight.unimgr.utils.OvsdbUtils;
30 import org.opendaylight.unimgr.utils.UniUtils;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.powermock.api.mockito.PowerMockito;
47 import org.powermock.core.classloader.annotations.PrepareForTest;
48 import org.powermock.modules.junit4.PowerMockRunner;
49
50 import com.google.common.base.Optional;
51 import com.google.common.util.concurrent.CheckedFuture;
52
53 @RunWith(PowerMockRunner.class)
54 @PrepareForTest({UniUtils.class, EvcUtils.class, MdsalUtils.class, OvsdbUtils.class, UnimgrMapper.class})
55 public class EvcUpdateCommandTest {
56     private static final NodeId OVSDB_NODE_ID = new NodeId("ovsdb://7011db35-f44b-4aab-90f6-d89088caf9d8");
57
58     private EvcUpdateCommand evcUpdateCommand;
59     private Link link;
60     private DataTreeModification<Link> evcLink;
61     private DataBroker dataBroker;
62
63     @SuppressWarnings("unchecked")
64     @Before
65     public void setUp(){
66         PowerMockito.mockStatic(UniUtils.class);
67         PowerMockito.mockStatic(MdsalUtils.class);
68         PowerMockito.mockStatic(EvcUtils.class);
69         PowerMockito.mockStatic(OvsdbUtils.class);
70         PowerMockito.mockStatic(UnimgrMapper.class);
71         dataBroker = mock(DataBroker.class);
72         link = mock(Link.class);
73         evcLink = DataTreeModificationHelper.getEvcLink(link);
74         evcUpdateCommand = new EvcUpdateCommand(dataBroker, evcLink);
75     }
76     /**
77      * Test method for {@link org.opendaylight.unimgr.command.EvcUpdateCommand#execute()}.
78      * @throws Exception
79      */
80     @SuppressWarnings({ "unchecked", "rawtypes" })
81     @Test
82     public void testExecute() throws Exception {
83         final ReadTransaction readTransac = mock(ReadOnlyTransaction.class);
84         final CheckedFuture retFormerEvc = mock(CheckedFuture.class);
85         final UniAugmentation uniAugmentation = mock(UniAugmentation.class);
86         final EvcAugmentation evcAugmentation = mock(EvcAugmentation.class);
87         final EvcAugmentation formerEvc = mock(EvcAugmentation.class);
88         final List<UniSource> unisSource = new ArrayList<UniSource>();
89         final List<UniDest> unisDest = new ArrayList<UniDest>();
90         final UniSource uniSource = mock(UniSource.class);
91         final UniDest uniDest = mock(UniDest.class);
92         final List<UniSource> formerUnisSource = new ArrayList<UniSource>();
93         final List<UniDest> formerUnisDest = new ArrayList<UniDest>();
94         final UniSource formerUniSource = mock(UniSource.class);
95         final UniDest formerUniDest = mock(UniDest.class);
96         final IpAddress ipAddressSource = mock(IpAddress.class);
97         final IpAddress ipAddressDest = mock(IpAddress.class);
98         final IpAddress formerIpAddressSource = mock(IpAddress.class);
99         final IpAddress formerIpAddressDest = mock(IpAddress.class);
100         final Ipv4Address ipv4AddressSource = mock(Ipv4Address.class);
101         final Ipv4Address ipv4AddressDest = mock(Ipv4Address.class);
102         final Ipv4Address formerIpv4AddressSource = mock(Ipv4Address.class);
103         final Ipv4Address formerIpv4AddressDest = mock(Ipv4Address.class);
104         final Optional<Node> optionalOvsdbNode = mock(Optional.class);
105         final Optional<EvcAugmentation> optionalEvcAugm = mock(Optional.class);
106         final Node node = mock(Node.class);
107         final OvsdbNodeRef ovsNodedRef = mock(OvsdbNodeRef.class);
108         final InstanceIdentifier evcKey = InstanceIdentifier
109                 .create(NetworkTopology.class)
110                 .child(Topology.class, new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID))
111                 .child(Node.class, new NodeKey(OVSDB_NODE_ID));
112         unisSource.add(uniSource);
113         unisDest.add(uniDest);
114         formerUnisSource.add(formerUniSource);
115         formerUnisDest.add(formerUniDest);
116
117         // Case EVC1 : formerUni1ip.equals(laterUni1Ip), formerUni2ip.equals(laterUni2Ip)
118         //              iidSource != null, iidDest != null
119         when(link.getAugmentation(EvcAugmentation.class)).thenReturn(evcAugmentation);
120         when(dataBroker.newReadOnlyTransaction()).thenReturn((ReadOnlyTransaction) readTransac);
121         when(readTransac.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class)))
122                 .thenReturn(retFormerEvc);
123         when(retFormerEvc.checkedGet()).thenReturn(optionalEvcAugm);
124         when(optionalEvcAugm.get()).thenReturn(formerEvc);
125         when(formerEvc.getUniSource()).thenReturn(formerUnisSource);
126         when(formerEvc.getUniDest()).thenReturn(formerUnisDest);
127         when(formerUniSource.getIpAddress()).thenReturn(ipAddressSource);
128         when(formerUniDest.getIpAddress()).thenReturn(ipAddressDest);
129         when(uniAugmentation.getOvsdbNodeRef()).thenReturn(ovsNodedRef);
130         when(evcAugmentation.getUniSource()).thenReturn(unisSource);
131         when(evcAugmentation.getUniDest()).thenReturn(unisDest);
132         when(uniSource.getIpAddress()).thenReturn(ipAddressSource);
133         when(uniDest.getIpAddress()).thenReturn(ipAddressDest);
134         when(uniSource.getUni()).thenReturn(evcKey);
135         when(uniDest.getUni()).thenReturn(evcKey);
136         when(formerIpAddressSource.getIpv4Address()).thenReturn(formerIpv4AddressSource);
137         when(formerIpAddressDest.getIpv4Address()).thenReturn(formerIpv4AddressDest);
138         when(formerIpv4AddressSource.toString()).thenReturn("formerIpv4AddressSource_test");
139         when(formerIpv4AddressSource.getValue()).thenReturn("formerIpv4AddressValueSource_test");
140         when(formerIpv4AddressDest.toString()).thenReturn("formerIpv4AddressDest_test");
141         when(formerIpv4AddressDest.getValue()).thenReturn("formerIpv4AddressValueDest_test");
142         when(ipAddressSource.getIpv4Address()).thenReturn(ipv4AddressSource);
143         when(ipAddressDest.getIpv4Address()).thenReturn(ipv4AddressDest);
144         when(optionalOvsdbNode.isPresent()).thenReturn(true);
145         when(optionalOvsdbNode.get()).thenReturn(node);
146         when(node.getAugmentation(any(Class.class))).thenReturn(uniAugmentation);
147         when(ovsNodedRef.getValue()).thenReturn(evcKey);
148
149         PowerMockito.doNothing().when(EvcUtils.class, "deleteEvcData", dataBroker, optionalOvsdbNode);
150         when(MdsalUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class),
151                 any(InstanceIdentifier.class))).thenReturn(optionalOvsdbNode);
152         PowerMockito.doNothing().when(OvsdbUtils.class, "updateMaxRate", dataBroker, uniAugmentation,
153                 uniAugmentation, evcAugmentation);
154         PowerMockito.doNothing().when(OvsdbUtils.class, "createTerminationPointNode", dataBroker,
155                 uniAugmentation, node, UnimgrConstants.DEFAULT_BRIDGE_NAME, UnimgrConstants.DEFAULT_TUNNEL_IFACE);
156         PowerMockito.doNothing().when(OvsdbUtils.class, "createGreTunnel", dataBroker, uniAugmentation,
157                 uniAugmentation, node, UnimgrConstants.DEFAULT_BRIDGE_NAME, UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
158         when(OvsdbUtils.createOvsdbNode(any(DataBroker.class), any(UniAugmentation.class)))
159         .thenReturn(node);
160         when(UnimgrMapper.getOvsdbBridgeNodeIid(any(Node.class))).thenReturn(evcKey);
161         when(UnimgrMapper.getUniIid(any(DataBroker.class), any(IpAddress.class),
162                 any(LogicalDatastoreType.class))).thenReturn(evcKey);
163         verifyExecute(0, 0, 6, 2);
164
165         // iidSource != null, iidDest != null
166         when(formerUniSource.getIpAddress()).thenReturn(formerIpAddressSource);
167         when(formerUniDest.getIpAddress()).thenReturn(formerIpAddressDest);
168         when(uniSource.getUni()).thenReturn(null);
169         when(uniDest.getUni()).thenReturn(null);
170         verifyExecute(2, 0, 12, 4);
171     }
172
173     @SuppressWarnings("unchecked")
174     private void verifyExecute(int getUniTimes, int deleteTimes, int readNodeTimes, int updateEvcTime){
175         evcUpdateCommand.execute();
176         PowerMockito.verifyStatic(times(getUniTimes));
177         UnimgrMapper.getUniIid(any(DataBroker.class), any(IpAddress.class),
178                 any(LogicalDatastoreType.class));
179         PowerMockito.verifyStatic(times(deleteTimes));
180         EvcUtils.deleteEvcData(any(DataBroker.class), any(Optional.class));
181         PowerMockito.verifyStatic(times(readNodeTimes));
182         MdsalUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class),
183                 any(InstanceIdentifier.class));
184         PowerMockito.verifyStatic(times(updateEvcTime));
185         UnimgrMapper.getOvsdbBridgeNodeIid(any(Node.class));
186         PowerMockito.verifyStatic(times(updateEvcTime));
187         OvsdbUtils.createTerminationPointNode(any(DataBroker.class), any(UniAugmentation.class),
188                 any(Node.class), any(String.class), any(String.class));
189         PowerMockito.verifyStatic(times(updateEvcTime));
190         OvsdbUtils.createGreTunnel(any(DataBroker.class), any(UniAugmentation.class),
191                 any(UniAugmentation.class), any(Node.class), any(String.class), any(String.class));
192         PowerMockito.verifyStatic(times(updateEvcTime));
193         EvcUtils.updateEvcNode(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
194                 any(EvcAugmentation.class), any(InstanceIdentifier.class),
195                 any(InstanceIdentifier.class), any(DataBroker.class));
196     }
197
198 }