BUG-9079 Make PCEP session recoverable from exception
[bgpcep.git] / pcep / topology / topology-provider / src / test / java / org / opendaylight / bgpcep / pcep / topology / provider / SyncOptimizationTest.java
1 /*
2  * Copyright (c) 2015 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.bgpcep.pcep.topology.provider;
10
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13
14 import java.math.BigInteger;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.MockitoAnnotations;
20 import org.opendaylight.protocol.pcep.PCEPSession;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Stateful1;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Stateful1Builder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Tlvs3;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Tlvs3Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.lsp.db.version.tlv.LspDbVersionBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1Builder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.StatefulBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
31
32 public class SyncOptimizationTest {
33
34     @Mock
35     private PCEPSession pcepSession;
36
37     @Before
38     public void setUp() {
39         MockitoAnnotations.initMocks(this);
40     }
41
42     @Test
43     public void testDoesLspDbMatchPositive() {
44         final Tlvs tlvs = createTlvs(1L, false, false);
45         Mockito.doReturn(tlvs).when(this.pcepSession).localSessionCharacteristics();
46         Mockito.doReturn(tlvs).when(this.pcepSession).getRemoteTlvs();
47         final SyncOptimization syncOpt = new SyncOptimization(this.pcepSession);
48         assertTrue(syncOpt.doesLspDbMatch());
49     }
50
51     @Test
52     public void testDoesLspDbMatchNegative() {
53         final Tlvs localTlvs = createTlvs(1L, false, false);
54         final Tlvs remoteTlvs = createTlvs(2L, false, false);
55         Mockito.doReturn(localTlvs).when(this.pcepSession).localSessionCharacteristics();
56         Mockito.doReturn(remoteTlvs).when(this.pcepSession).getRemoteTlvs();
57         final SyncOptimization syncOpt = new SyncOptimization(this.pcepSession);
58         assertFalse(syncOpt.doesLspDbMatch());
59     }
60
61     @Test
62     public void testIsSyncAvoidanceEnabledPositive() {
63         final Tlvs tlvs = createTlvs(1L, true, false);
64         Mockito.doReturn(tlvs).when(this.pcepSession).localSessionCharacteristics();
65         Mockito.doReturn(tlvs).when(this.pcepSession).getRemoteTlvs();
66         final SyncOptimization syncOpt = new SyncOptimization(this.pcepSession);
67         assertTrue(syncOpt.isSyncAvoidanceEnabled());
68     }
69
70     @Test
71     public void testIsSyncAvoidanceEnabledNegative() {
72         final Tlvs localTlvs = createTlvs(1L, true, false);
73         final Tlvs remoteTlvs = createTlvs(2L, false, false);
74         Mockito.doReturn(localTlvs).when(this.pcepSession).localSessionCharacteristics();
75         Mockito.doReturn(remoteTlvs).when(this.pcepSession).getRemoteTlvs();
76         final SyncOptimization syncOpt = new SyncOptimization(this.pcepSession);
77         assertFalse(syncOpt.isSyncAvoidanceEnabled());
78     }
79
80     @Test
81     public void testIsDeltaSyncEnabledPositive() {
82         final Tlvs tlvs = createTlvs(1L, true, true);
83         Mockito.doReturn(tlvs).when(this.pcepSession).localSessionCharacteristics();
84         Mockito.doReturn(tlvs).when(this.pcepSession).getRemoteTlvs();
85         final SyncOptimization syncOpt = new SyncOptimization(this.pcepSession);
86         assertTrue(syncOpt.isDeltaSyncEnabled());
87     }
88
89     @Test
90     public void testIsDeltaSyncEnabledNegative() {
91         final Tlvs localTlvs = createTlvs(1L, true, true);
92         final Tlvs remoteTlvs = createTlvs(2L, false, false);
93         Mockito.doReturn(localTlvs).when(this.pcepSession).localSessionCharacteristics();
94         Mockito.doReturn(remoteTlvs).when(this.pcepSession).getRemoteTlvs();
95         final SyncOptimization syncOpt = new SyncOptimization(this.pcepSession);
96         assertFalse(syncOpt.isDeltaSyncEnabled());
97     }
98
99     @Test
100     public void testIsDbVersionPresentPositive() {
101         final Tlvs localTlvs = createTlvs(null, false, false);
102         final Tlvs remoteTlvs = createTlvs(2L, false, false);
103         Mockito.doReturn(localTlvs).when(this.pcepSession).localSessionCharacteristics();
104         Mockito.doReturn(remoteTlvs).when(this.pcepSession).getRemoteTlvs();
105         final SyncOptimization syncOpt = new SyncOptimization(this.pcepSession);
106         assertTrue(syncOpt.isDbVersionPresent());
107     }
108
109     @Test
110     public void testIsDbVersionPresentNegative() {
111         final Tlvs tlvs = createTlvs(null, true, false);
112         Mockito.doReturn(tlvs).when(this.pcepSession).localSessionCharacteristics();
113         Mockito.doReturn(tlvs).when(this.pcepSession).getRemoteTlvs();
114         final SyncOptimization syncOpt = new SyncOptimization(this.pcepSession);
115         assertFalse(syncOpt.isDbVersionPresent());
116     }
117
118     private static Tlvs createTlvs(final Long lspDbVersion, final boolean includeDbVresion, final boolean includeDeltaSync) {
119         return new TlvsBuilder()
120             .addAugmentation(Tlvs1.class, new Tlvs1Builder().setStateful(
121                 new StatefulBuilder().addAugmentation(Stateful1.class,
122                         new Stateful1Builder()
123                             .setIncludeDbVersion(includeDbVresion).setDeltaLspSyncCapability(includeDeltaSync).build()).build()).build())
124             .addAugmentation(Tlvs3.class, new Tlvs3Builder().setLspDbVersion(
125                 new LspDbVersionBuilder().setLspDbVersionValue(lspDbVersion != null ? BigInteger.valueOf(lspDbVersion) : null).build()).build()).build();
126     }
127 }