[ios-xe-renderer] Increases coverage for PolicyWriterUtil
[groupbasedpolicy.git] / renderers / ios-xe / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ios_xe_provider / impl / writer / PolicyWriterUtilTest.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
9 package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.writer;
10
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Mockito.when;
15
16 import java.util.Collections;
17
18 import com.google.common.base.Optional;
19 import com.google.common.util.concurrent.Futures;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Matchers;
24 import org.mockito.Mock;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
30 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.manager.PolicyManagerImpl;
31 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.util.ServiceChainingUtil;
32 import org.opendaylight.yang.gen.v1.urn.ios.rev160308.ClassNameType;
33 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.ClassMap;
34 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.ClassMapBuilder;
35 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.PolicyMap;
36 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.PolicyMapBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.ServiceChain;
38 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.ServiceChainBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.policy.map.Class;
40 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.policy.map.ClassBuilder;
41 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.ServicePathBuilder;
42 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.function.forwarder.ServiceFfName;
43 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.function.forwarder.ServiceFfNameBuilder;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
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 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 /**
53  * Test for {@link PolicyWriterUtil}.
54  */
55 @RunWith(PowerMockRunner.class)
56 @PrepareForTest({PolicyWriterUtil.class, NetconfTransactionCreator.class, ServiceChainingUtil.class})
57 public class PolicyWriterUtilTest {
58
59     private static final Logger LOG = LoggerFactory.getLogger(PolicyWriterUtilTest.class);
60
61     private static final String UNIT_CLASS_MAP_ENTRY_NAME = "unit-classMapEntry-name";
62     private static final String CLASS_MAP_NAME = "asd";
63     private static final String UNIT_POLICY_MAP_NAME = "unit-policyMap-name";
64     private static final String UNIT_CLASS_NAME = "unit-class-name";
65     private static final String UNIT_SERVICE_FORWARDER_NAME = "unit-service-forwarder-name";
66     // methods
67     private static final String NETCONF_WRITE_ONLY_TRANSACTION = "netconfWriteOnlyTransaction";
68     private static final String NETCONF_READ_ONLY_TRANSACTION = "netconfReadOnlyTransaction";
69     private static final String NETCONF_READ = "netconfRead";
70     @Mock
71     private DataBroker dataBroker;
72     @Mock
73     private WriteTransaction wTx;
74     private java.util.Optional<WriteTransaction> wTxOptional;
75     @Mock
76     private ReadOnlyTransaction rTx;
77     private java.util.Optional<ReadOnlyTransaction> rTxOptional;
78
79     @Before
80     public void setUp() throws Exception {
81         wTxOptional = java.util.Optional.of(wTx);
82         rTxOptional = java.util.Optional.of(rTx);
83     }
84
85     // TODO remove reflection-based stubs everywhere
86
87     @Test
88     public void testWriteClassMap() throws Exception {
89         LOG.debug("scenario: fail with one entry, no writeOnlyTransaction");
90         final ClassMap classMap = new ClassMapBuilder().setName(UNIT_CLASS_MAP_ENTRY_NAME).build();
91         assertFalse(PolicyWriterUtil.writeClassMap(classMap, getLocation()));
92
93         LOG.debug("scenario: fail with one entry, available writeOnlyTransaction, no readOnlyTransaction");
94         PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, NETCONF_WRITE_ONLY_TRANSACTION)).toReturn(wTxOptional);
95         when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
96         assertFalse(PolicyWriterUtil.writeClassMap(classMap, getLocation()));
97
98         LOG.debug("scenario: succeed with one entry, available writeOnlyTransaction, available readOnlyTransaction");
99         PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, NETCONF_READ_ONLY_TRANSACTION)).toReturn(rTxOptional);
100         when(rTx.read(eq(LogicalDatastoreType.CONFIGURATION), Matchers.<InstanceIdentifier<ClassMap>>any()))
101                 .thenReturn(Futures.immediateCheckedFuture(Optional.of(
102                         new ClassMapBuilder().setName(CLASS_MAP_NAME).build())));
103         assertTrue(PolicyWriterUtil.writeClassMap(classMap, getLocation()));
104
105         LOG.debug("scenario: fail with one entry, available writeOnlyTransaction, available readOnlyTransaction, check->null");
106         PowerMockito.stub(PowerMockito.method(PolicyWriterUtil.class, NETCONF_READ)).toReturn(null);
107         assertFalse(PolicyWriterUtil.writeClassMap(classMap, getLocation()));
108     }
109
110     @Test
111     public void testRemoveClassMap() throws Exception {
112         LOG.debug("scenario: pass through with null classMapEntries collection");
113         assertTrue(PolicyWriterUtil.removeClassMap(null, getLocation()));
114
115         LOG.debug("scenario: pass through with empty classMapEntries collection");
116         final ClassMap classMap = new ClassMapBuilder().setName(UNIT_CLASS_MAP_ENTRY_NAME).build();
117         assertTrue(PolicyWriterUtil.removeClassMap(classMap, getLocation()));
118
119         LOG.debug("scenario: fail with one entry, no writeOnlyTransaction");
120         PowerMockito.stub(PowerMockito.method(PolicyWriterUtil.class, NETCONF_READ)).toReturn(new ClassBuilder().build());
121         PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, NETCONF_WRITE_ONLY_TRANSACTION))
122                 .toReturn(java.util.Optional.empty());
123         assertFalse(PolicyWriterUtil.removeClassMap(classMap, getLocation()));
124
125         LOG.debug("scenario: fail with one entry, available writeOnlyTransaction, no readOnlyTransaction");
126         PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, NETCONF_WRITE_ONLY_TRANSACTION)).toReturn(wTxOptional);
127         when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
128         assertFalse(PolicyWriterUtil.removeClassMap(classMap, getLocation()));
129
130         LOG.debug("scenario: succeed with one entry, available writeOnlyTransaction, available readOnlyTransaction");
131         PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, NETCONF_READ_ONLY_TRANSACTION)).toReturn(rTxOptional);
132         PowerMockito.stub(PowerMockito.method(PolicyWriterUtil.class, NETCONF_READ)).toReturn(null);
133         when(rTx.read(eq(LogicalDatastoreType.CONFIGURATION), Matchers.<InstanceIdentifier<ClassMap>>any()))
134                 .thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
135         assertTrue(PolicyWriterUtil.removeClassMap(classMap, getLocation()));
136
137         LOG.debug("scenario: fail with one entry, available writeOnlyTransaction, available readOnlyTransaction, check->false");
138         PowerMockito.stub(PowerMockito.method(PolicyWriterUtil.class, NETCONF_READ)).toReturn(new ClassBuilder().build());
139         assertFalse(PolicyWriterUtil.removeClassMap(classMap, getLocation()));
140     }
141
142     @Test
143     public void testWritePolicyMap() throws Exception {
144         LOG.debug("scenario: fail with one entry, no writeOnlyTransaction");
145         final PolicyMap policyMap = new PolicyMapBuilder().setName(UNIT_POLICY_MAP_NAME).build();
146         assertFalse(PolicyWriterUtil.writePolicyMap(policyMap, getLocation()));
147
148         LOG.debug("scenario: fail with one entry, available writeOnlyTransaction, no readOnlyTransaction");
149         PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, NETCONF_WRITE_ONLY_TRANSACTION)).toReturn(wTxOptional);
150         when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
151         assertFalse(PolicyWriterUtil.writePolicyMap(policyMap, getLocation()));
152
153         LOG.debug("scenario: fail with empty classEntries collection");
154         assertFalse(PolicyWriterUtil.writePolicyMap(policyMap, getLocation()));
155
156         LOG.debug("scenario: succeed with one entry, available writeOnlyTransaction, available readOnlyTransaction");
157         PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, NETCONF_READ_ONLY_TRANSACTION)).toReturn(rTxOptional);
158         when(rTx.read(eq(LogicalDatastoreType.CONFIGURATION), Matchers.<InstanceIdentifier<ClassMap>>any()))
159                 .thenReturn(Futures.immediateCheckedFuture(Optional.of(
160                         new ClassMapBuilder().setName(CLASS_MAP_NAME).build())));
161         assertTrue(PolicyWriterUtil.writePolicyMap(policyMap, getLocation()));
162
163         LOG.debug("scenario: fail with one entry, available writeOnlyTransaction, available readOnlyTransaction, check->null");
164         PowerMockito.stub(PowerMockito.method(PolicyWriterUtil.class, NETCONF_READ)).toReturn(null);
165         assertFalse(PolicyWriterUtil.writePolicyMap(policyMap, getLocation()));
166     }
167
168     @Test
169     public void testRemovePolicyMap() throws Exception {
170         final PolicyMap policyMap = new PolicyMapBuilder().setName(UNIT_POLICY_MAP_NAME).build();
171         PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, NETCONF_WRITE_ONLY_TRANSACTION)).toReturn(wTxOptional);
172         when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
173
174         PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, NETCONF_READ_ONLY_TRANSACTION)).toReturn(rTxOptional);
175         when(rTx.read(eq(LogicalDatastoreType.CONFIGURATION), Matchers.<InstanceIdentifier<ClassMap>>any()))
176                 .thenReturn(Futures.immediateCheckedFuture(Optional.of(
177                         new ClassMapBuilder().setName(CLASS_MAP_NAME).build())));
178         PolicyWriterUtil.writePolicyMap(policyMap, getLocation());
179
180         LOG.debug("scenario: fail to remove");
181         PowerMockito.stub(PowerMockito.method(PolicyWriterUtil.class, NETCONF_READ)).toReturn(new ClassBuilder().build());
182
183         assertFalse(PolicyWriterUtil.removePolicyMap(getLocation()));
184
185         LOG.debug("scenario: remove succeed");
186         PowerMockito.stub(PowerMockito.method(PolicyWriterUtil.class, NETCONF_READ)).toReturn(null);
187
188         assertTrue(PolicyWriterUtil.removePolicyMap(getLocation()));
189     }
190
191     @Test
192     public void testRemovePolicyMapEntry() throws Exception {
193         LOG.debug("scenario: pass through with empty classEntries collection");
194         final Class entry = new ClassBuilder().setName(new ClassNameType(UNIT_CLASS_NAME)).build();
195         assertTrue(PolicyWriterUtil.removePolicyMapEntry(entry, getLocation()));
196
197         LOG.debug("scenario: fail with one entry, no writeOnlyTransaction");
198         PowerMockito.stub(PowerMockito.method(PolicyWriterUtil.class, NETCONF_READ)).toReturn(new ClassBuilder().build());
199         assertFalse(PolicyWriterUtil.removePolicyMapEntry(entry, getLocation()));
200
201         LOG.debug("scenario: fail with one entry, available writeOnlyTransaction, no readOnlyTransaction");
202         PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, NETCONF_WRITE_ONLY_TRANSACTION)).toReturn(wTxOptional);
203         when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
204         assertTrue(PolicyWriterUtil.removePolicyMapEntry(entry, getLocation()));
205     }
206
207     @Test
208     public void testWriteInterface() throws Exception {
209         LOG.debug("scenario: fail with no writeOnlyTransaction");
210         assertFalse(PolicyWriterUtil.writeInterface(getLocation()));
211
212         LOG.debug("scenario: fail - available writeOnlyTransaction, no submit future");
213         PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, NETCONF_WRITE_ONLY_TRANSACTION)).toReturn(wTxOptional);
214         assertFalse(PolicyWriterUtil.writeInterface(getLocation()));
215
216         LOG.debug("scenario: succeed - available writeOnlyTransaction, available future");
217         when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
218         assertTrue(PolicyWriterUtil.writeInterface(getLocation()));
219     }
220
221     @Test
222     public void testRemoveInterface() throws Exception {
223         PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, NETCONF_WRITE_ONLY_TRANSACTION)).toReturn(wTxOptional);
224         when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
225         PolicyWriterUtil.writeInterface(getLocation());
226
227         PowerMockito.stub(PowerMockito.method(PolicyWriterUtil.class, NETCONF_READ)).toReturn(new ClassBuilder().build());
228
229         assertTrue(PolicyWriterUtil.removeInterface(getLocation()));
230
231         PowerMockito.stub(PowerMockito.method(PolicyWriterUtil.class, NETCONF_READ)).toReturn(null);
232
233         assertTrue(PolicyWriterUtil.removeInterface(getLocation()));
234
235         PowerMockito.stub(PowerMockito.method(PolicyWriterUtil.class, NETCONF_READ)).toReturn(new ClassBuilder().build());
236         when(wTx.submit()).thenThrow(TransactionCommitFailedException.class);
237
238         assertFalse(PolicyWriterUtil.removeInterface(getLocation()));
239     }
240
241     @Test
242     public void testWriteRemote() throws Exception {
243         LOG.debug("scenario: succeed with empty List<ServiceFfName>");
244         final ServiceFfName forwarder = new ServiceFfNameBuilder().setName(UNIT_SERVICE_FORWARDER_NAME).build();
245         assertFalse(PolicyWriterUtil.writeRemote(forwarder, getLocation()));
246
247         LOG.debug("scenario: fail - available writeOnlyTransaction, no submit future");
248         PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, NETCONF_WRITE_ONLY_TRANSACTION)).toReturn(wTxOptional);
249         assertFalse(PolicyWriterUtil.writeRemote(forwarder, getLocation()));
250
251         LOG.debug("scenario: succeed - available writeOnlyTransaction, available future");
252         when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
253         assertTrue(PolicyWriterUtil.writeRemote(forwarder, getLocation()));
254     }
255
256     @Test
257     public void testRemoveRemote() throws Exception {
258         final ServiceFfName forwarder = new ServiceFfNameBuilder().setName(UNIT_SERVICE_FORWARDER_NAME).build();
259         PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, NETCONF_WRITE_ONLY_TRANSACTION)).toReturn(wTxOptional);
260         when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
261         PolicyWriterUtil.writeRemote(forwarder, getLocation());
262
263         PowerMockito.stub(PowerMockito.method(PolicyWriterUtil.class, NETCONF_READ)).toReturn(new ClassBuilder().build());
264
265         assertTrue(PolicyWriterUtil.removeRemote(forwarder, getLocation()));
266
267         PowerMockito.stub(PowerMockito.method(PolicyWriterUtil.class, NETCONF_READ)).toReturn(null);
268
269         assertTrue(PolicyWriterUtil.removeRemote(forwarder, getLocation()));
270
271         PowerMockito.stub(PowerMockito.method(PolicyWriterUtil.class, NETCONF_READ)).toReturn(new ClassBuilder().build());
272         when(wTx.submit()).thenThrow(TransactionCommitFailedException.class);
273
274         assertFalse(PolicyWriterUtil.removeRemote(forwarder, getLocation()));
275     }
276
277     @Test
278     public void testWriteServicePath() throws Exception {
279         LOG.debug("scenario: fail with no writeOnlyTransaction");
280         final ServiceChain serviceChain = new ServiceChainBuilder()
281                 .setServicePath(Collections.singletonList(new ServicePathBuilder()
282                         .setServicePathId(42L)
283                         .build()))
284                 .build();
285         assertFalse(PolicyWriterUtil.writeServicePath(serviceChain, getLocation()));
286
287         LOG.debug("scenario: fail - available writeOnlyTransaction, no submit future");
288         PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, NETCONF_WRITE_ONLY_TRANSACTION)).toReturn(wTxOptional);
289         assertFalse(PolicyWriterUtil.writeServicePath(serviceChain, getLocation()));
290
291         LOG.debug("scenario: succeed - available writeOnlyTransaction, available future");
292         when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
293         assertTrue(PolicyWriterUtil.writeServicePath(serviceChain, getLocation()));
294     }
295
296     @Test
297     public void testRemoveServicePath() throws Exception {
298         LOG.debug("scenario: fail with service path present, no writeOnlyTransaction");
299         PowerMockito.stub(PowerMockito.method(PolicyWriterUtil.class, NETCONF_READ)).toReturn(new ClassBuilder().build());
300         final ServiceChain serviceChain = new ServiceChainBuilder()
301                 .setServicePath(Collections.singletonList(new ServicePathBuilder()
302                         .setServicePathId(42L)
303                         .build()))
304                 .build();
305         assertFalse(PolicyWriterUtil.removeServicePath(serviceChain, getLocation()));
306
307         LOG.debug("scenario: fail with service path present, available writeOnlyTransaction, no submit future");
308         PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, NETCONF_WRITE_ONLY_TRANSACTION)).toReturn(wTxOptional);
309         assertFalse(PolicyWriterUtil.removeServicePath(serviceChain, getLocation()));
310
311         LOG.debug("scenario: fail with service path present, available writeOnlyTransaction, available future");
312         when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
313         assertTrue(PolicyWriterUtil.removeServicePath(serviceChain, getLocation()));
314     }
315
316     private PolicyManagerImpl.PolicyMapLocation getLocation() {
317         final String POLICY_MAP = "policy-map";
318         final String INTERFACE = "interface";
319         final NodeId nodeId = new NodeId("node-id");
320         final String IP_ADDRESS = "ip-address";
321         return new PolicyManagerImpl.PolicyMapLocation(POLICY_MAP, INTERFACE, nodeId, IP_ADDRESS, dataBroker);
322     }
323 }