Committed by
Gerrit Code Review
[GEANT] Property netconfReplyTimeout added in Settings.
Change-Id: I60399c5cc9a00857f275fa8016520b2d909e9912
Showing
4 changed files
with
139 additions
and
2 deletions
... | @@ -45,6 +45,19 @@ | ... | @@ -45,6 +45,19 @@ |
45 | <artifactId>ganymed-ssh2</artifactId> | 45 | <artifactId>ganymed-ssh2</artifactId> |
46 | <version>262</version> | 46 | <version>262</version> |
47 | </dependency> | 47 | </dependency> |
48 | + <dependency> | ||
49 | + <groupId>org.onosproject</groupId> | ||
50 | + <artifactId>onos-api</artifactId> | ||
51 | + <classifier>tests</classifier> | ||
52 | + <scope>test</scope> | ||
53 | + </dependency> | ||
54 | + <dependency> | ||
55 | + <groupId>org.onosproject</groupId> | ||
56 | + <artifactId>onlab-osgi</artifactId> | ||
57 | + <version>${project.version}</version> | ||
58 | + <classifier>tests</classifier> | ||
59 | + <scope>test</scope> | ||
60 | + </dependency> | ||
48 | </dependencies> | 61 | </dependencies> |
49 | 62 | ||
50 | <build> | 63 | <build> | ... | ... |
... | @@ -19,10 +19,13 @@ package org.onosproject.netconf.ctl; | ... | @@ -19,10 +19,13 @@ package org.onosproject.netconf.ctl; |
19 | import org.apache.felix.scr.annotations.Activate; | 19 | import org.apache.felix.scr.annotations.Activate; |
20 | import org.apache.felix.scr.annotations.Component; | 20 | import org.apache.felix.scr.annotations.Component; |
21 | import org.apache.felix.scr.annotations.Deactivate; | 21 | import org.apache.felix.scr.annotations.Deactivate; |
22 | +import org.apache.felix.scr.annotations.Modified; | ||
23 | +import org.apache.felix.scr.annotations.Property; | ||
22 | import org.apache.felix.scr.annotations.Reference; | 24 | import org.apache.felix.scr.annotations.Reference; |
23 | import org.apache.felix.scr.annotations.ReferenceCardinality; | 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
24 | import org.apache.felix.scr.annotations.Service; | 26 | import org.apache.felix.scr.annotations.Service; |
25 | import org.onlab.packet.IpAddress; | 27 | import org.onlab.packet.IpAddress; |
28 | +import org.onosproject.cfg.ComponentConfigService; | ||
26 | import org.onosproject.net.Device; | 29 | import org.onosproject.net.Device; |
27 | import org.onosproject.net.DeviceId; | 30 | import org.onosproject.net.DeviceId; |
28 | import org.onosproject.net.device.DeviceService; | 31 | import org.onosproject.net.device.DeviceService; |
... | @@ -42,17 +45,30 @@ import org.slf4j.Logger; | ... | @@ -42,17 +45,30 @@ import org.slf4j.Logger; |
42 | import org.slf4j.LoggerFactory; | 45 | import org.slf4j.LoggerFactory; |
43 | 46 | ||
44 | import java.util.Arrays; | 47 | import java.util.Arrays; |
48 | +import java.util.Dictionary; | ||
45 | import java.util.Map; | 49 | import java.util.Map; |
46 | import java.util.Set; | 50 | import java.util.Set; |
47 | import java.util.concurrent.ConcurrentHashMap; | 51 | import java.util.concurrent.ConcurrentHashMap; |
48 | import java.util.concurrent.CopyOnWriteArraySet; | 52 | import java.util.concurrent.CopyOnWriteArraySet; |
49 | 53 | ||
54 | +import static com.google.common.base.Strings.isNullOrEmpty; | ||
55 | +import static org.onlab.util.Tools.get; | ||
56 | + | ||
50 | /** | 57 | /** |
51 | * The implementation of NetconfController. | 58 | * The implementation of NetconfController. |
52 | */ | 59 | */ |
53 | @Component(immediate = true) | 60 | @Component(immediate = true) |
54 | @Service | 61 | @Service |
55 | public class NetconfControllerImpl implements NetconfController { | 62 | public class NetconfControllerImpl implements NetconfController { |
63 | + private static final String PROP_NETCONF_REPLY_TIMEOUT = "netconfReplyTimeout"; | ||
64 | + private static final int DEFAULT_REPLY_TIMEOUT_SECONDS = 5; | ||
65 | + @Property(name = PROP_NETCONF_REPLY_TIMEOUT, intValue = DEFAULT_REPLY_TIMEOUT_SECONDS, | ||
66 | + label = "Time (in seconds) waiting for a NetConf reply") | ||
67 | + protected static int netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS; | ||
68 | + | ||
69 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
70 | + protected ComponentConfigService cfgService; | ||
71 | + | ||
56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
57 | protected DeviceService deviceService; | 73 | protected DeviceService deviceService; |
58 | 74 | ||
... | @@ -71,15 +87,42 @@ public class NetconfControllerImpl implements NetconfController { | ... | @@ -71,15 +87,42 @@ public class NetconfControllerImpl implements NetconfController { |
71 | 87 | ||
72 | @Activate | 88 | @Activate |
73 | public void activate(ComponentContext context) { | 89 | public void activate(ComponentContext context) { |
90 | + cfgService.registerProperties(getClass()); | ||
91 | + modified(context); | ||
74 | log.info("Started"); | 92 | log.info("Started"); |
75 | } | 93 | } |
76 | 94 | ||
77 | @Deactivate | 95 | @Deactivate |
78 | public void deactivate() { | 96 | public void deactivate() { |
97 | + cfgService.unregisterProperties(getClass(), false); | ||
79 | netconfDeviceMap.clear(); | 98 | netconfDeviceMap.clear(); |
80 | log.info("Stopped"); | 99 | log.info("Stopped"); |
81 | } | 100 | } |
82 | 101 | ||
102 | + @Modified | ||
103 | + public void modified(ComponentContext context) { | ||
104 | + if (context == null) { | ||
105 | + netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS; | ||
106 | + log.info("No component configuration"); | ||
107 | + return; | ||
108 | + } | ||
109 | + | ||
110 | + Dictionary<?, ?> properties = context.getProperties(); | ||
111 | + | ||
112 | + int newNetconfReplyTimeout; | ||
113 | + try { | ||
114 | + String s = get(properties, PROP_NETCONF_REPLY_TIMEOUT); | ||
115 | + newNetconfReplyTimeout = isNullOrEmpty(s) ? | ||
116 | + netconfReplyTimeout : Integer.parseInt(s.trim()); | ||
117 | + } catch (NumberFormatException e) { | ||
118 | + log.warn("Component configuration had invalid value", e); | ||
119 | + return; | ||
120 | + } | ||
121 | + | ||
122 | + netconfReplyTimeout = newNetconfReplyTimeout; | ||
123 | + log.info("Settings: {} = {}", PROP_NETCONF_REPLY_TIMEOUT, netconfReplyTimeout); | ||
124 | + } | ||
125 | + | ||
83 | @Override | 126 | @Override |
84 | public void addDeviceListener(NetconfDeviceListener listener) { | 127 | public void addDeviceListener(NetconfDeviceListener listener) { |
85 | if (!netconfDeviceListeners.contains(listener)) { | 128 | if (!netconfDeviceListeners.contains(listener)) { | ... | ... |
... | @@ -55,7 +55,6 @@ public class NetconfSessionImpl implements NetconfSession { | ... | @@ -55,7 +55,6 @@ public class NetconfSessionImpl implements NetconfSession { |
55 | private static final String MESSAGE_ID_STRING = "message-id"; | 55 | private static final String MESSAGE_ID_STRING = "message-id"; |
56 | private static final String HELLO = "<hello"; | 56 | private static final String HELLO = "<hello"; |
57 | private static final String NEW_LINE = "\n"; | 57 | private static final String NEW_LINE = "\n"; |
58 | - private static final int FUTURE_REPLY_TIMEOUT = 5000; | ||
59 | private static final String ERROR = "ERROR "; | 58 | private static final String ERROR = "ERROR "; |
60 | private static final String END_OF_RPC_OPEN_TAG = "\">"; | 59 | private static final String END_OF_RPC_OPEN_TAG = "\">"; |
61 | private static final String EQUAL = "="; | 60 | private static final String EQUAL = "="; |
... | @@ -196,9 +195,10 @@ public class NetconfSessionImpl implements NetconfSession { | ... | @@ -196,9 +195,10 @@ public class NetconfSessionImpl implements NetconfSession { |
196 | request = formatXmlHeader(request); | 195 | request = formatXmlHeader(request); |
197 | CompletableFuture<String> futureReply = request(request); | 196 | CompletableFuture<String> futureReply = request(request); |
198 | messageIdInteger.incrementAndGet(); | 197 | messageIdInteger.incrementAndGet(); |
198 | + int replyTimeout = NetconfControllerImpl.netconfReplyTimeout; | ||
199 | String rp; | 199 | String rp; |
200 | try { | 200 | try { |
201 | - rp = futureReply.get(FUTURE_REPLY_TIMEOUT, TimeUnit.MILLISECONDS); | 201 | + rp = futureReply.get(replyTimeout, TimeUnit.SECONDS); |
202 | } catch (InterruptedException | ExecutionException | TimeoutException e) { | 202 | } catch (InterruptedException | ExecutionException | TimeoutException e) { |
203 | throw new NetconfException("No matching reply for request " + request, e); | 203 | throw new NetconfException("No matching reply for request " + request, e); |
204 | } | 204 | } | ... | ... |
... | @@ -20,7 +20,10 @@ import org.easymock.EasyMock; | ... | @@ -20,7 +20,10 @@ import org.easymock.EasyMock; |
20 | import org.junit.After; | 20 | import org.junit.After; |
21 | import org.junit.Before; | 21 | import org.junit.Before; |
22 | import org.junit.Test; | 22 | import org.junit.Test; |
23 | +import org.onlab.osgi.ComponentContextAdapter; | ||
23 | import org.onlab.packet.IpAddress; | 24 | import org.onlab.packet.IpAddress; |
25 | +import org.onosproject.cfg.ComponentConfigAdapter; | ||
26 | +import org.onosproject.cfg.ComponentConfigService; | ||
24 | import org.onosproject.net.DeviceId; | 27 | import org.onosproject.net.DeviceId; |
25 | import org.onosproject.net.device.DeviceService; | 28 | import org.onosproject.net.device.DeviceService; |
26 | import org.onosproject.net.key.DeviceKeyService; | 29 | import org.onosproject.net.key.DeviceKeyService; |
... | @@ -32,8 +35,11 @@ import org.onosproject.netconf.NetconfDeviceOutputEvent; | ... | @@ -32,8 +35,11 @@ import org.onosproject.netconf.NetconfDeviceOutputEvent; |
32 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; | 35 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
33 | import org.onosproject.netconf.NetconfException; | 36 | import org.onosproject.netconf.NetconfException; |
34 | import org.onosproject.netconf.NetconfSession; | 37 | import org.onosproject.netconf.NetconfSession; |
38 | +import org.osgi.service.component.ComponentContext; | ||
35 | 39 | ||
36 | import java.lang.reflect.Field; | 40 | import java.lang.reflect.Field; |
41 | +import java.util.Dictionary; | ||
42 | +import java.util.Enumeration; | ||
37 | import java.util.HashSet; | 43 | import java.util.HashSet; |
38 | import java.util.Map; | 44 | import java.util.Map; |
39 | import java.util.Optional; | 45 | import java.util.Optional; |
... | @@ -79,14 +85,17 @@ public class NetconfControllerImplTest { | ... | @@ -79,14 +85,17 @@ public class NetconfControllerImplTest { |
79 | private static final int BAD_DEVICE_PORT = 13; | 85 | private static final int BAD_DEVICE_PORT = 13; |
80 | private static final int IPV6_DEVICE_PORT = 14; | 86 | private static final int IPV6_DEVICE_PORT = 14; |
81 | 87 | ||
88 | + private static ComponentConfigService cfgService = new ComponentConfigAdapter(); | ||
82 | private static DeviceService deviceService = new NetconfDeviceServiceMock(); | 89 | private static DeviceService deviceService = new NetconfDeviceServiceMock(); |
83 | private static DeviceKeyService deviceKeyService = new NetconfDeviceKeyServiceMock(); | 90 | private static DeviceKeyService deviceKeyService = new NetconfDeviceKeyServiceMock(); |
84 | 91 | ||
92 | + private final ComponentContext context = new MockComponentContext(); | ||
85 | 93 | ||
86 | @Before | 94 | @Before |
87 | public void setUp() throws Exception { | 95 | public void setUp() throws Exception { |
88 | ctrl = new NetconfControllerImpl(); | 96 | ctrl = new NetconfControllerImpl(); |
89 | ctrl.deviceFactory = new TestNetconfDeviceFactory(); | 97 | ctrl.deviceFactory = new TestNetconfDeviceFactory(); |
98 | + ctrl.cfgService = cfgService; | ||
90 | ctrl.deviceService = deviceService; | 99 | ctrl.deviceService = deviceService; |
91 | ctrl.deviceKeyService = deviceKeyService; | 100 | ctrl.deviceKeyService = deviceKeyService; |
92 | 101 | ||
... | @@ -125,6 +134,30 @@ public class NetconfControllerImplTest { | ... | @@ -125,6 +134,30 @@ public class NetconfControllerImplTest { |
125 | } | 134 | } |
126 | 135 | ||
127 | /** | 136 | /** |
137 | + * Test initialization of component configuration. | ||
138 | + */ | ||
139 | + @Test | ||
140 | + public void testActivate() { | ||
141 | + assertEquals("Incorrect NetConf session timeout, should be default", | ||
142 | + 5, ctrl.netconfReplyTimeout); | ||
143 | + ctrl.activate(null); | ||
144 | + assertEquals("Incorrect NetConf session timeout, should be default", | ||
145 | + 5, ctrl.netconfReplyTimeout); | ||
146 | + } | ||
147 | + | ||
148 | + /** | ||
149 | + * Test modification of component configuration. | ||
150 | + */ | ||
151 | + @Test | ||
152 | + public void testModified() { | ||
153 | + assertEquals("Incorrect NetConf session timeout, should be default", | ||
154 | + 5, ctrl.netconfReplyTimeout); | ||
155 | + ctrl.modified(context); | ||
156 | + assertEquals("Incorrect NetConf session timeout", | ||
157 | + 1, ctrl.netconfReplyTimeout); | ||
158 | + } | ||
159 | + | ||
160 | + /** | ||
128 | * Test to add DeviceListeners, | 161 | * Test to add DeviceListeners, |
129 | * and also to check whether the netconfDeviceListeners set is | 162 | * and also to check whether the netconfDeviceListeners set is |
130 | * updating or not which was present in NetconfControllerImpl class. | 163 | * updating or not which was present in NetconfControllerImpl class. |
... | @@ -323,4 +356,52 @@ public class NetconfControllerImplTest { | ... | @@ -323,4 +356,52 @@ public class NetconfControllerImplTest { |
323 | } | 356 | } |
324 | 357 | ||
325 | } | 358 | } |
359 | + | ||
360 | + private class MockComponentContext extends ComponentContextAdapter { | ||
361 | + @Override | ||
362 | + public Dictionary getProperties() { | ||
363 | + return new MockDictionary(); | ||
364 | + } | ||
365 | + } | ||
366 | + | ||
367 | + private class MockDictionary extends Dictionary { | ||
368 | + | ||
369 | + @Override | ||
370 | + public int size() { | ||
371 | + return 0; | ||
372 | + } | ||
373 | + | ||
374 | + @Override | ||
375 | + public boolean isEmpty() { | ||
376 | + return false; | ||
377 | + } | ||
378 | + | ||
379 | + @Override | ||
380 | + public Enumeration keys() { | ||
381 | + return null; | ||
382 | + } | ||
383 | + | ||
384 | + @Override | ||
385 | + public Enumeration elements() { | ||
386 | + return null; | ||
387 | + } | ||
388 | + | ||
389 | + @Override | ||
390 | + public Object get(Object key) { | ||
391 | + if (key.equals("netconfReplyTimeout")) { | ||
392 | + return "1"; | ||
393 | + } | ||
394 | + return null; | ||
395 | + } | ||
396 | + | ||
397 | + @Override | ||
398 | + public Object put(Object key, Object value) { | ||
399 | + return null; | ||
400 | + } | ||
401 | + | ||
402 | + @Override | ||
403 | + public Object remove(Object key) { | ||
404 | + return null; | ||
405 | + } | ||
406 | + } | ||
326 | } | 407 | } | ... | ... |
-
Please register or login to post a comment