Bug 5386: Delete QoS and Queues entries from ovsdb on UNI deletion
[unimgr.git] / impl / src / test / java / org / opendaylight / unimgr / command / EvcCreateCommandTest.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.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import java.util.Set;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.unimgr.impl.UnimgrConstants;
28 import org.opendaylight.unimgr.impl.UnimgrMapper;
29 import org.opendaylight.unimgr.utils.EvcUtils;
30 import org.opendaylight.unimgr.utils.MdsalUtils;
31 import org.opendaylight.unimgr.utils.OvsdbUtils;
32 import org.opendaylight.unimgr.utils.UniUtils;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
46 import org.opendaylight.yangtools.yang.binding.DataObject;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.powermock.api.mockito.PowerMockito;
49 import org.powermock.core.classloader.annotations.PrepareForTest;
50 import org.powermock.modules.junit4.PowerMockRunner;
51
52 import com.google.common.base.Optional;
53
54 @RunWith(PowerMockRunner.class)
55 @PrepareForTest({EvcUtils.class, MdsalUtils.class, OvsdbUtils.class, UnimgrMapper.class})
56 public class EvcCreateCommandTest {
57
58     private static final NodeId OVSDB_NODE_ID = new NodeId("ovsdb://7011db35-f44b-4aab-90f6-d89088caf9d8");
59
60     private EvcCreateCommand evcCreateCommand;
61     private Map<InstanceIdentifier<?>, DataObject> changes;
62     private DataBroker dataBroker;
63
64     @SuppressWarnings("unchecked")
65     @Before
66     public void setUp(){
67         PowerMockito.mockStatic(MdsalUtils.class);
68         PowerMockito.mockStatic(EvcUtils.class);
69         PowerMockito.mockStatic(OvsdbUtils.class);
70         PowerMockito.mockStatic(UnimgrMapper.class);
71         changes = mock(Map.class);
72         dataBroker = mock(DataBroker.class);
73         evcCreateCommand = new EvcCreateCommand(dataBroker, changes);
74     }
75
76     /**
77      * Test method for {@link org.opendaylight.unimgr.command.EvcCreateCommand#execute()}.
78      * @throws Exception
79      */
80     @SuppressWarnings({ "rawtypes", "unchecked" })
81     @Test
82     public void testExecute() throws Exception {
83         final UniAugmentation uniAugmentation = mock(UniAugmentation.class);
84         final EvcAugmentation evcAugmentation = mock(EvcAugmentation.class);
85         final List<UniSource> unisSource = new ArrayList<UniSource>();
86         final UniSource uniSource = mock(UniSource.class);
87         final List<UniDest> unisDest = new ArrayList<UniDest>();
88         final UniDest uniDest = mock(UniDest.class);
89         final IpAddress ipAddress = mock(IpAddress.class);
90         final Ipv4Address ipv4Address = mock(Ipv4Address.class);
91         final Optional<Node> optionalOvsdbNode = mock(Optional.class);
92         final Node node = mock(Node.class);
93         final OvsdbNodeRef ovsNodedRef = mock(OvsdbNodeRef.class);
94         final InstanceIdentifier evcKey = InstanceIdentifier
95                 .create(NetworkTopology.class)
96                 .child(Topology.class, new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID))
97                 .child(Node.class, new NodeKey(OVSDB_NODE_ID));
98         final Set<Entry<InstanceIdentifier<?>, DataObject>> setCreated = new HashSet<Map.Entry<InstanceIdentifier<?>,DataObject>>();
99         final Entry<InstanceIdentifier<?>, DataObject> created = new Entry<InstanceIdentifier<?>, DataObject>() {
100             @Override
101             public DataObject setValue(DataObject value) { return null; }
102             @Override
103             public DataObject getValue() { return evcAugmentation; }
104             @Override
105             public InstanceIdentifier getKey() { return evcKey; }
106         };
107
108         setCreated.add(created);
109         unisSource.add(uniSource);
110         unisDest.add(uniDest);
111
112         when(changes.entrySet()).thenReturn(setCreated);
113         when(uniAugmentation.getOvsdbNodeRef()).thenReturn(ovsNodedRef);
114         when(evcAugmentation.getUniSource()).thenReturn(unisSource);
115         when(evcAugmentation.getUniDest()).thenReturn(unisDest);
116         when(uniSource.getIpAddress()).thenReturn(ipAddress);
117         when(uniDest.getIpAddress()).thenReturn(ipAddress);
118         when(uniSource.getUni()).thenReturn(evcKey);
119         when(uniDest.getUni()).thenReturn(evcKey);
120         when(ipAddress.getIpv4Address()).thenReturn(ipv4Address);
121         when(ipv4Address.toString()).thenReturn("ipv4Address_test");
122         when(ipv4Address.getValue()).thenReturn("ipv4AddressValue_test");
123         when(optionalOvsdbNode.isPresent()).thenReturn(true);
124         when(optionalOvsdbNode.get()).thenReturn(node);
125         when(node.getAugmentation(any(Class.class))).thenReturn(uniAugmentation);
126         when(ovsNodedRef.getValue()).thenReturn(evcKey);
127
128         when(MdsalUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class),
129                 any(InstanceIdentifier.class))).thenReturn(optionalOvsdbNode);
130         PowerMockito.doNothing().when(OvsdbUtils.class, "updateMaxRate", dataBroker, uniAugmentation,
131                 uniAugmentation, evcAugmentation);
132         PowerMockito.doNothing().when(OvsdbUtils.class, "createTerminationPointNode", dataBroker,
133                 uniAugmentation, node, UnimgrConstants.DEFAULT_BRIDGE_NAME, UnimgrConstants.DEFAULT_TUNNEL_IFACE);
134         PowerMockito.doNothing().when(OvsdbUtils.class, "createGreTunnel", dataBroker, uniAugmentation,
135                 uniAugmentation, node, UnimgrConstants.DEFAULT_BRIDGE_NAME, UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
136         when(OvsdbUtils.createOvsdbNode(any(DataBroker.class), any(UniAugmentation.class)))
137         .thenReturn(node);
138         when(UnimgrMapper.getOvsdbNodeIid(any(NodeId.class))).thenReturn(evcKey);
139         when(UnimgrMapper.getUniIid(any(DataBroker.class), any(IpAddress.class),
140                 any(LogicalDatastoreType.class))).thenReturn(evcKey);
141
142         evcCreateCommand.execute();
143         PowerMockito.verifyStatic(times(2));
144         OvsdbUtils.createTerminationPointNode(any(DataBroker.class), any(UniAugmentation.class),
145                 any(Node.class), any(String.class), any(String.class));
146         PowerMockito.verifyStatic(times(2));
147         OvsdbUtils.createGreTunnel(any(DataBroker.class), any(UniAugmentation.class),
148                 any(UniAugmentation.class), any(Node.class), any(String.class), any(String.class));
149         PowerMockito.verifyStatic(times(2));
150         EvcUtils.updateEvcNode(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
151                 any(EvcAugmentation.class), any(InstanceIdentifier.class),
152                 any(InstanceIdentifier.class), any(DataBroker.class));
153
154     }
155
156 }