BUG-139: Implement PCEP state synchronization avoidance
[bgpcep.git] / pcep / segment-routing / src / test / java / org / opendaylight / controller / config / yang / pcep / sr / cfg / SrPCEPSessionProposalFactoryModuleTest.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
9 package org.opendaylight.controller.config.yang.pcep.sr.cfg;
10
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import javax.management.InstanceAlreadyExistsException;
15 import javax.management.ObjectName;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.controller.config.api.ValidationException;
19 import org.opendaylight.controller.config.api.jmx.CommitStatus;
20 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
21 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
22 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
23 import org.opendaylight.controller.config.yang.pcep.stateful07.cfg.Stateful07PCEPSessionProposalFactoryModuleMXBean;
24
25 public class SrPCEPSessionProposalFactoryModuleTest extends AbstractConfigTest {
26
27     private static final String INSTANCE_NAME = "sr02-session-proposal";
28     private static final String FACTORY_NAME = SrPCEPSessionProposalFactoryModuleFactory.NAME;
29
30     @Before
31     public void setUp() throws Exception {
32         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(this.mockedContext, new SrPCEPSessionProposalFactoryModuleFactory()));
33     }
34
35     @Test
36     public void testValidationExceptionDeadTimerValueNotSet() throws Exception {
37         try {
38             createInstance(null, (short) 100, true, 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, 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, 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, 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, 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 testValidationExceptionKeepAliveTimerMinValue() throws Exception {
87         try {
88             createInstance((short) 200, (short) -10, true, true, true, true, true, true, true, true);
89             fail();
90         } catch (final ValidationException e) {
91             assertTrue(e.getMessage().contains("minimum value is 1."));
92         }
93     }
94
95     @Test
96     public void testStatefulAfterCommitted() throws Exception {
97         createInstance((short) 200, (short) 100, false, true, true, true, true, true, true, true);
98         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
99         final Stateful07PCEPSessionProposalFactoryModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(
100                 FACTORY_NAME, INSTANCE_NAME), Stateful07PCEPSessionProposalFactoryModuleMXBean.class);
101         assertTrue(mxBean.getStateful());
102     }
103
104     @Test
105     public void testNotStatefulAfterCommitted() throws Exception {
106         createInstance((short) 200, (short) 100, false, false, false, false, false, false, false, false);
107         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
108         final Stateful07PCEPSessionProposalFactoryModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(
109                 FACTORY_NAME, INSTANCE_NAME), Stateful07PCEPSessionProposalFactoryModuleMXBean.class);
110         assertFalse(mxBean.getStateful());
111     }
112
113     @Test
114     public void testIncludeDbVersionAfterCommitted() throws Exception {
115         createInstance((short) 200, (short) 100, false, false, false, false, false, false, true, false);
116         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
117         final Stateful07PCEPSessionProposalFactoryModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(
118                 FACTORY_NAME, INSTANCE_NAME), Stateful07PCEPSessionProposalFactoryModuleMXBean.class);
119         assertTrue(mxBean.getIncludeDbVersion());
120     }
121
122     @Test
123     public void testNotIncludeDbVersionAfterCommitted() throws Exception {
124         createInstance((short) 200, (short) 100, false, false, false, false, false, false, false, false);
125         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
126         final Stateful07PCEPSessionProposalFactoryModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(
127                 FACTORY_NAME, INSTANCE_NAME), Stateful07PCEPSessionProposalFactoryModuleMXBean.class);
128         assertFalse(mxBean.getIncludeDbVersion());
129     }
130
131     @Test
132     public void testValidationExceptionSrCapableValueNotSet() throws Exception {
133         try {
134             createInstance((short) 200, (short) 100, true, true, true, null, false, false, false, false);
135             fail();
136         } catch (final ValidationException e) {
137             assertTrue(e.getMessage().contains("SrCapable value is not set"));
138         }
139     }
140
141     @Test
142     public void testValidationExceptionTriggeredSyncNotSet() throws Exception {
143         try {
144             createInstance((short) 200, (short) 100, true, true, true, true, null, true, true, true);
145             fail();
146         } catch (final ValidationException e) {
147             assertTrue(e.getMessage().contains("TriggeredInitialSync value is not set"));
148         }
149     }
150
151     @Test
152     public void testValidationExceptionTriggeredResyncNotSet() throws Exception {
153         try {
154             createInstance((short) 200, (short) 100, true, true, true, true, true, null, true, true);
155             fail();
156         } catch (final ValidationException e) {
157             assertTrue(e.getMessage().contains("TriggeredResync value is not set"));
158         }
159     }
160
161     @Test
162     public void testValidationExceptionDeltaLspSyncNotSet() throws Exception {
163         try {
164             createInstance((short) 200, (short) 100, true, true, true, true, true, true, null, true);
165             fail();
166         } catch (final ValidationException e) {
167             assertTrue(e.getMessage().contains("DeltaLspSyncCapability value is not set"));
168         }
169     }
170
171     @Test
172     public void testValidationExceptionIncludeDBVersionNotSet() throws Exception {
173         try {
174             createInstance((short) 200, (short) 100, true, true, true, true, true, true, true, null);
175             fail();
176         } catch (final ValidationException e) {
177             assertTrue(e.getMessage().contains("IncludeDbVersion value is not set"));
178         }
179     }
180
181     @Test
182     public void testCreateBean() throws Exception {
183         final CommitStatus status = createInstance();
184         assertBeanCount(1, FACTORY_NAME);
185         assertStatus(status, 1, 0, 0);
186     }
187
188     @Test
189     public void testReusingOldInstance() throws Exception {
190         createInstance();
191         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
192         assertBeanCount(1, FACTORY_NAME);
193         final CommitStatus status = transaction.commit();
194         assertBeanCount(1, FACTORY_NAME);
195         assertStatus(status, 0, 0, 1);
196     }
197
198     @Test
199     public void testReconfigure() throws Exception {
200         createInstance();
201         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
202         assertBeanCount(1, FACTORY_NAME);
203         transaction.newMXBeanProxy(transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
204                 Stateful07PCEPSessionProposalFactoryModuleMXBean.class);
205         final CommitStatus status = transaction.commit();
206         assertBeanCount(1, FACTORY_NAME);
207         assertStatus(status, 0, 0, 1);
208     }
209
210     private CommitStatus createInstance() throws Exception {
211         return createInstance((short) 200, (short) 100, true, true, true, true, true, true, true, true);
212     }
213
214     private CommitStatus createInstance(final Short deadTimer, final Short keepAlive,
215             final Boolean stateful, final Boolean active, final Boolean instant, final Boolean srCapable,
216             final Boolean triggeredInitialSync, final Boolean triggeredResync, final Boolean deltaLspSyncCapability, final Boolean includeDbVersion) throws Exception {
217         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
218         createInstance(transaction, deadTimer, keepAlive, stateful, active, instant, srCapable, triggeredInitialSync, triggeredResync, deltaLspSyncCapability, includeDbVersion);
219         return transaction.commit();
220     }
221
222     private ObjectName createInstance(final ConfigTransactionJMXClient transaction, final Short deadTimer, final Short keepAlive,
223             final Boolean stateful, final Boolean active, final Boolean instant, final Boolean srCapable,
224             final Boolean triggeredInitialSync, final Boolean triggeredResync, final Boolean deltaLspSyncCapability, final Boolean includeDbVersion) throws InstanceAlreadyExistsException {
225         final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
226         final SrPCEPSessionProposalFactoryModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated,
227                 SrPCEPSessionProposalFactoryModuleMXBean.class);
228         mxBean.setActive(active);
229         mxBean.setDeadTimerValue(deadTimer);
230         mxBean.setInitiated(instant);
231         mxBean.setKeepAliveTimerValue(keepAlive);
232         mxBean.setStateful(stateful);
233         mxBean.setSrCapable(srCapable);
234         mxBean.setTriggeredInitialSync(triggeredInitialSync);
235         mxBean.setTriggeredResync(triggeredResync);
236         mxBean.setDeltaLspSyncCapability(deltaLspSyncCapability);
237         mxBean.setIncludeDbVersion(includeDbVersion);
238         return nameCreated;
239     }
240 }