BUG-139: Implement PCEP state synchronization avoidance
[bgpcep.git] / pcep / ietf-stateful07 / src / test / java / org / opendaylight / protocol / pcep / ietf / Stateful07SessionProposalFactoryModuleTest.java
1 /*
2  * Copyright (c) 2014 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.protocol.pcep.ietf;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13 import javax.management.InstanceAlreadyExistsException;
14 import javax.management.ObjectName;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.controller.config.api.ValidationException;
18 import org.opendaylight.controller.config.api.jmx.CommitStatus;
19 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
20 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
21 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
22 import org.opendaylight.controller.config.yang.pcep.stateful07.cfg.Stateful07PCEPSessionProposalFactoryModuleFactory;
23 import org.opendaylight.controller.config.yang.pcep.stateful07.cfg.Stateful07PCEPSessionProposalFactoryModuleMXBean;
24
25 public class Stateful07SessionProposalFactoryModuleTest extends AbstractConfigTest {
26
27     private static final String INSTANCE_NAME = "pcep-stateful07-proposal";
28     private static final String FACTORY_NAME = Stateful07PCEPSessionProposalFactoryModuleFactory.NAME;
29
30     @Before
31     public void setUp() throws Exception {
32         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(this.mockedContext, new Stateful07PCEPSessionProposalFactoryModuleFactory()));
33     }
34
35     @Test
36     public void testValidationExceptionDeadTimerValueNotSet() throws Exception {
37         try {
38             createInstance(null, (short) 100, true, true, true, true, true, true, true);
39             fail();
40         } catch (final ValidationException e) {
41             assertTrue(e.getMessage().contains("DeadTimerValue value is not set"));
42         }
43     }
44
45     @Test
46     public void testValidationExceptionKeepAliveTimerNotSet() throws Exception {
47         try {
48             createInstance((short) 200, null, true, true, true, true, true, true, true);
49             fail();
50         } catch (final ValidationException e) {
51             assertTrue(e.getMessage().contains("KeepAliveTimerValue value is not set"));
52         }
53     }
54
55     @Test
56     public void testValidationExceptionStatefulNotSet() throws Exception {
57         try {
58             createInstance((short) 200, (short) 100, null, false, false, false, false, false, false);
59             fail();
60         } catch (final ValidationException e) {
61             assertTrue(e.getMessage().contains("Stateful value is not set"));
62         }
63     }
64
65     @Test
66     public void testValidationExceptionActiveNotSet() throws Exception {
67         try {
68             createInstance((short) 200, (short) 100, true, null, true, true, true, true, true);
69             fail();
70         } catch (final ValidationException e) {
71             assertTrue(e.getMessage().contains("Active value is not set"));
72         }
73     }
74
75     @Test
76     public void testValidationExceptionInstantiatedNotSet() throws Exception {
77         try {
78             createInstance((short) 200, (short) 100, true, true, null, true, true, true, true);
79             fail();
80         } catch (final ValidationException e) {
81             assertTrue(e.getMessage().contains("Initiated value is not set"));
82         }
83     }
84
85     @Test
86     public void testValidationExceptionTriggeredSyncNotSet() throws Exception {
87         try {
88             createInstance((short) 200, (short) 100, true, true, true, null, true, true, true);
89             fail();
90         } catch (final ValidationException e) {
91             assertTrue(e.getMessage().contains("TriggeredInitialSync value is not set"));
92         }
93     }
94
95     @Test
96     public void testValidationExceptionTriggeredResyncNotSet() throws Exception {
97         try {
98             createInstance((short) 200, (short) 100, true, true, true, true, null, true, true);
99             fail();
100         } catch (final ValidationException e) {
101             assertTrue(e.getMessage().contains("TriggeredResync value is not set"));
102         }
103     }
104
105     @Test
106     public void testValidationExceptionDeltaLspSyncNotSet() throws Exception {
107         try {
108             createInstance((short) 200, (short) 100, true, true, true, true, true, null, true);
109             fail();
110         } catch (final ValidationException e) {
111             assertTrue(e.getMessage().contains("DeltaLspSyncCapability value is not set"));
112         }
113     }
114
115     @Test
116     public void testValidationExceptionIncludeDBVersionNotSet() throws Exception {
117         try {
118             createInstance((short) 200, (short) 100, true, true, true, true, true, true, null);
119             fail();
120         } catch (final ValidationException e) {
121             assertTrue(e.getMessage().contains("IncludeDbVersion value is not set"));
122         }
123     }
124
125     @Test
126     public void testValidationExceptionKeepAliveTimerMinValue() throws Exception {
127         try {
128             createInstance((short) 200, (short) -10, true, true, true, true, true, true, true);
129             fail();
130         } catch (final ValidationException e) {
131             assertTrue(e.getMessage().contains("minimum value is 1."));
132         }
133     }
134
135     @Test
136     public void testStatefulAfterCommitted() throws Exception {
137         createInstance((short) 200, (short) 100, false, true, true, true, false, true, true);
138         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
139         final Stateful07PCEPSessionProposalFactoryModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(
140                 FACTORY_NAME, INSTANCE_NAME), Stateful07PCEPSessionProposalFactoryModuleMXBean.class);
141         assertTrue(mxBean.getStateful());
142     }
143
144     @Test
145     public void testNotStatefulAfterCommitted() throws Exception {
146         createInstance((short) 200, (short) 100, false, false, false, false, false, false, false);
147         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
148         final Stateful07PCEPSessionProposalFactoryModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(
149                 FACTORY_NAME, INSTANCE_NAME), Stateful07PCEPSessionProposalFactoryModuleMXBean.class);
150         assertFalse(mxBean.getStateful());
151     }
152
153     @Test
154     public void testIncludeDbVersionAfterCommitted() throws Exception {
155         createInstance((short) 200, (short) 100, false, false, false, false, false, true, false);
156         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
157         final Stateful07PCEPSessionProposalFactoryModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(
158                 FACTORY_NAME, INSTANCE_NAME), Stateful07PCEPSessionProposalFactoryModuleMXBean.class);
159         assertTrue(mxBean.getIncludeDbVersion());
160     }
161
162     @Test
163     public void testNotIncludeDbVersionAfterCommitted() throws Exception {
164         createInstance((short) 200, (short) 100, false, false, false, false, false, false, false);
165         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
166         final Stateful07PCEPSessionProposalFactoryModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(
167                 FACTORY_NAME, INSTANCE_NAME), Stateful07PCEPSessionProposalFactoryModuleMXBean.class);
168         assertFalse(mxBean.getIncludeDbVersion());
169     }
170
171     @Test
172     public void testCreateBean() throws Exception {
173         final CommitStatus status = createInstance();
174         assertBeanCount(1, FACTORY_NAME);
175         assertStatus(status, 1, 0, 0);
176     }
177
178     @Test
179     public void testReusingOldInstance() throws Exception {
180         createInstance();
181         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
182         assertBeanCount(1, FACTORY_NAME);
183         final CommitStatus status = transaction.commit();
184         assertBeanCount(1, FACTORY_NAME);
185         assertStatus(status, 0, 0, 1);
186     }
187
188     @Test
189     public void testReconfigure() throws Exception {
190         createInstance();
191         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
192         assertBeanCount(1, FACTORY_NAME);
193         transaction.newMXBeanProxy(transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
194                 Stateful07PCEPSessionProposalFactoryModuleMXBean.class);
195         final CommitStatus status = transaction.commit();
196         assertBeanCount(1, FACTORY_NAME);
197         assertStatus(status, 0, 0, 1);
198     }
199
200     private CommitStatus createInstance(final Short deadTimer, final Short keepAlive, final Boolean stateful, final Boolean active,
201             final Boolean instant, final Boolean triggeredInitialSync, final Boolean triggeredResync, final Boolean deltaLspSyncCapability, final Boolean includeDbVersion) throws Exception {
202         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
203         createInstance(transaction, deadTimer, keepAlive, stateful, active, instant, triggeredInitialSync, triggeredResync, deltaLspSyncCapability, includeDbVersion);
204         return transaction.commit();
205     }
206
207     private CommitStatus createInstance() throws Exception {
208         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
209         createStateful07SessionProposalInstance(transaction);
210         return transaction.commit();
211     }
212
213     public static ObjectName createStateful07SessionProposalInstance(final ConfigTransactionJMXClient transaction) throws Exception {
214         return createInstance(transaction, (short) 200, (short) 100, true, true, true, true, true, true, true);
215     }
216
217     private static ObjectName createInstance(final ConfigTransactionJMXClient transaction, final Short deadTimer, final Short keepAlive,
218             final Boolean stateful, final Boolean active, final Boolean instant,
219             final Boolean triggeredInitialSync, final Boolean triggeredResync, final Boolean deltaLspSyncCapability, final Boolean includeDbVersion) throws InstanceAlreadyExistsException {
220         final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
221         final Stateful07PCEPSessionProposalFactoryModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated,
222                 Stateful07PCEPSessionProposalFactoryModuleMXBean.class);
223         mxBean.setActive(active);
224         mxBean.setDeadTimerValue(deadTimer);
225         mxBean.setInitiated(instant);
226         mxBean.setKeepAliveTimerValue(keepAlive);
227         mxBean.setStateful(stateful);
228         mxBean.setTriggeredInitialSync(triggeredInitialSync);
229         mxBean.setTriggeredResync(triggeredResync);
230         mxBean.setDeltaLspSyncCapability(deltaLspSyncCapability);
231         mxBean.setIncludeDbVersion(includeDbVersion);
232         return nameCreated;
233     }
234
235 }