Merge "BUG-2329 Add test for anyxmls inside rpc resonse for netcfon-connector"
[controller.git] / opendaylight / clustering / test / src / main / java / org / opendaylight / controller / clustering / test / internal / SimpleClient.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.clustering.test.internal;
11
12 import java.net.InetAddress;
13 import java.util.ArrayList;
14 import java.util.EnumSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Properties;
18 import java.util.Set;
19 import java.util.concurrent.ConcurrentMap;
20
21 import org.eclipse.osgi.framework.console.CommandInterpreter;
22 import org.eclipse.osgi.framework.console.CommandProvider;
23 import org.opendaylight.controller.clustering.services.CacheConfigException;
24 import org.opendaylight.controller.clustering.services.CacheExistException;
25 import org.opendaylight.controller.clustering.services.CacheListenerAddException;
26 import org.opendaylight.controller.clustering.services.IClusterServices;
27 import org.opendaylight.controller.clustering.services.IGetUpdates;
28 import org.opendaylight.controller.clustering.services.IListenRoleChange;
29 import org.opendaylight.controller.clustering.services.ListenRoleChangeAddException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class SimpleClient implements CommandProvider {
34     protected static Logger logger = LoggerFactory
35             .getLogger(SimpleClient.class);
36     IClusterServices icluster;
37     DoListenRoleChanged doListen;
38
39     public void _tbegin(CommandInterpreter ci) {
40         if (this.icluster == null) {
41             ci.println("\nNo Clustering services available");
42             return;
43         }
44         try {
45             this.icluster.tbegin();
46             ci.println("Transaction Open "
47                     + this.icluster.tgetTransaction().toString());
48         } catch (Exception e) {
49             ci.println("Caught exception during transaction begin: " + e);
50         }
51     }
52
53     public void _tcommit(CommandInterpreter ci) {
54         if (this.icluster == null) {
55             ci.println("\nNo Clustering services available");
56             return;
57         }
58         try {
59             ci.println("Committing transaction ....."
60                     + this.icluster.tgetTransaction().toString());
61             this.icluster.tcommit();
62             ci.println("Transaction Committed");
63         } catch (Exception e) {
64             ci.println("Caught exception during transaction commit: " + e);
65         }
66     }
67
68     public void _trollback(CommandInterpreter ci) {
69         if (this.icluster == null) {
70             ci.println("\nNo Clustering services available");
71             return;
72         }
73         try {
74             ci.println("Rolling back transaction ....."
75                     + this.icluster.tgetTransaction().toString());
76             this.icluster.trollback();
77             ci.println("Transaction Rolled Back");
78         } catch (Exception e) {
79             ci.println("Caught exception during transaction rollback: " + e);
80         }
81     }
82
83     public void _cacheinfo(CommandInterpreter ci) {
84         if (this.icluster == null) {
85             ci.println("\nNo Clustering services available");
86             return;
87         }
88         String containerName = ci.nextArgument();
89         if (containerName == null) {
90             ci.println("containerName not supplied");
91             return;
92         }
93         String cacheName = ci.nextArgument();
94         if (cacheName == null) {
95             ci.println("Cache not supplied");
96             return;
97         }
98         if (!this.icluster.existCache(containerName, cacheName)) {
99             ci.println("\tCache " + cacheName + " doesn't exists");
100             return;
101         }
102         ci.println("\tInfo for cache " + cacheName + " on container "
103                 + containerName);
104         Properties p = this.icluster.getCacheProperties(containerName,
105                 cacheName);
106         if (p != null) {
107             for (String key : p.stringPropertyNames()) {
108                 ci.println("\t\t" + key + " = " + p.getProperty(key));
109             }
110         }
111     }
112
113     public void _setLogLevel(CommandInterpreter ci) {
114         String loggerName = ci.nextArgument();
115         if (loggerName == null) {
116             ci.println("Logger Name not supplied");
117             return;
118         }
119         String loggerLevel = ci.nextArgument();
120         if (loggerLevel == null) {
121             ci.println("Logger Level not supplied");
122             return;
123         }
124
125         ch.qos.logback.classic.Logger l = (ch.qos.logback.classic.Logger) LoggerFactory
126                 .getLogger(loggerName);
127         ch.qos.logback.classic.Level level = ch.qos.logback.classic.Level
128                 .toLevel(loggerLevel);
129         if (level == null) {
130             ci.println("Level not understood");
131             return;
132         }
133         l.setLevel(level);
134     }
135
136     private String retrieveLogLevel(ch.qos.logback.classic.Logger l) {
137         if (l == null) {
138             return ("Logger not supplied");
139         }
140         ch.qos.logback.classic.Level level = l.getLevel();
141         if (level == null) {
142             return ("Logger " + l.getName() + " at unknown level");
143         } else {
144             return ("Logger " + l.getName() + " at level " + l.getLevel()
145                     .toString());
146         }
147     }
148
149     public void _getLogLevel(CommandInterpreter ci) {
150         String loggerName = ci.nextArgument();
151         ch.qos.logback.classic.LoggerContext lc = (ch.qos.logback.classic.LoggerContext) LoggerFactory
152                 .getILoggerFactory();
153         if (lc != null) {
154             for (ch.qos.logback.classic.Logger l : lc.getLoggerList()) {
155                 if ((loggerName == null) || l.getName().startsWith(loggerName)) {
156                     ci.println(retrieveLogLevel(l));
157                 }
158             }
159         }
160     }
161
162     public void _create(CommandInterpreter ci) {
163         if (this.icluster == null) {
164             ci.println("\nNo Clustering services available");
165             return;
166         }
167         String containerName = ci.nextArgument();
168         if (containerName == null) {
169             ci.println("containerName not supplied");
170             return;
171         }
172         String cacheName = ci.nextArgument();
173         if (cacheName == null) {
174             ci.println("Cache not supplied");
175             return;
176         }
177         try {
178             if (cacheName.startsWith("T-")) {
179                 this.icluster.createCache(containerName, cacheName, EnumSet
180                         .of(IClusterServices.cacheMode.TRANSACTIONAL));
181             } else {
182                 this.icluster.createCache(containerName, cacheName, EnumSet
183                         .of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
184             }
185         } catch (CacheExistException ce) {
186             ci
187                     .println("\nCache already exits - destroy and recreate if needed");
188             return;
189         } catch (CacheConfigException cfe) {
190             ci.println("\nCache configured with contrasting parameters");
191             return;
192         }
193
194         if (this.icluster.existCache(containerName, cacheName)) {
195             ci.println(cacheName + " has been created on container "
196                     + containerName);
197         }
198     }
199
200     public void _destroy(CommandInterpreter ci) {
201         if (this.icluster == null) {
202             ci.println("\nNo Clustering services available");
203             return;
204         }
205         String containerName = ci.nextArgument();
206         if (containerName == null) {
207             ci.println("containerName not supplied");
208             return;
209         }
210         String cacheName = ci.nextArgument();
211         if (cacheName == null) {
212             ci.println("Cache not supplied");
213             return;
214         }
215         if (this.icluster.existCache(containerName, cacheName)) {
216             this.icluster.destroyCache(containerName, cacheName);
217             ci.println(cacheName + " has been destroyed");
218         }
219     }
220
221     public void _listen(CommandInterpreter ci) {
222         if (this.icluster == null) {
223             ci.println("\nNo Clustering services available");
224             return;
225         }
226         String containerName = ci.nextArgument();
227         if (containerName == null) {
228             ci.println("containerName not supplied");
229             return;
230         }
231         String cacheName = ci.nextArgument();
232         if (cacheName == null) {
233             ci.println("Cache not supplied");
234             return;
235         }
236         try {
237             this.icluster.addListener(containerName, cacheName,
238                     new LoggingListener());
239             ci.println("cache " + cacheName + " on container " + containerName
240                     + " is begin monitored for updates");
241         } catch (CacheListenerAddException clae) {
242             ci.println("Couldn't attach the listener to cache " + cacheName
243                     + " on container " + containerName);
244         }
245     }
246
247     @SuppressWarnings("deprecation") //IGetUpdates intentionally deprecated
248     public void _unlisten(CommandInterpreter ci) {
249         if (this.icluster == null) {
250             ci.println("\nNo Clustering services available");
251             return;
252         }
253         String containerName = ci.nextArgument();
254         if (containerName == null) {
255             ci.println("containerName not supplied");
256             return;
257         }
258         String cacheName = ci.nextArgument();
259         if (cacheName == null) {
260             ci.println("Cache not supplied");
261             return;
262         }
263
264         Set<IGetUpdates<?, ?>> listeners = this.icluster.getListeners(
265                 containerName, cacheName);
266         for (IGetUpdates<?, ?> l : listeners) {
267             this.icluster.removeListener(containerName, cacheName, l);
268         }
269         ci.println(cacheName + " is no longer being monitored for updates");
270     }
271
272     public void _myController(CommandInterpreter ci) {
273         if (this.icluster == null) {
274             ci.println("\nNo Clustering services available");
275             return;
276         }
277         ci.println("This Controller : " +icluster.getMyAddress().getHostAddress());
278     }
279
280     public void _getClusterNodes(CommandInterpreter ci) {
281         if (this.icluster == null) {
282             ci.println("\nNo Clustering services available");
283             return;
284         }
285         for (InetAddress address : icluster.getClusteredControllers()) {
286             ci.println("\t"+address.getHostAddress());
287         }
288     }
289
290     public void _listcaches(CommandInterpreter ci) {
291         if (this.icluster == null) {
292             ci.println("\nNo Clustering services available");
293             return;
294         }
295         String containerName = ci.nextArgument().toLowerCase();
296         if (containerName == null) {
297             ci.println("containerName not supplied");
298             return;
299         }
300
301         // For user's convenience, let's return the sorted cache list
302         List<String> sortedCacheList = new ArrayList<String>(this.icluster
303                 .getCacheList(containerName));
304         java.util.Collections.sort(sortedCacheList);
305         for (String cacheName : sortedCacheList) {
306             ci.println("\t" + cacheName);
307         }
308     }
309
310     public void _put(CommandInterpreter ci) {
311         ConcurrentMap<Integer, StringContainer> c;
312         if (this.icluster == null) {
313             ci.println("\nNo Clustering services available");
314             return;
315         }
316         String containerName = ci.nextArgument();
317         if (containerName == null) {
318             ci.println("containerName not supplied");
319             return;
320         }
321         String cacheName = ci.nextArgument();
322         if (cacheName == null) {
323             ci.println("Cache not supplied");
324             return;
325         }
326         String sKey = ci.nextArgument();
327         String sValue = ci.nextArgument();
328         if (sKey == null) {
329             ci.println("Key not supplied");
330             return;
331         }
332         if (sValue == null) {
333             ci.println("Value not supplied");
334             return;
335         }
336         Integer key = null;
337         try {
338             key = Integer.valueOf(sKey);
339         } catch (NumberFormatException nfe) {
340             ci.println("Key is not a valid integer: " + sKey);
341         }
342
343         c = (ConcurrentMap<Integer, StringContainer>) this.icluster.getCache(
344                 containerName, cacheName);
345         if (c != null) {
346             ci.println("\nAdd mapping " + key + " = " + sValue);
347             try {
348                 c.put(key, new StringContainer(sValue));
349             } catch (Exception e) {
350                 ci.println("Exception raised:" + e);
351                 ci.println("\tStacktrace:");
352                 e.printStackTrace();
353             }
354         } else {
355             ci.println("Cache " + cacheName + " on container " + containerName
356                     + " not existant!");
357         }
358     }
359
360     public void _remove(CommandInterpreter ci) {
361         ConcurrentMap<Integer, StringContainer> c;
362         if (this.icluster == null) {
363             ci.println("\nNo Clustering services available");
364             return;
365         }
366         String containerName = ci.nextArgument();
367         if (containerName == null) {
368             ci.println("containerName not supplied");
369             return;
370         }
371         String cacheName = ci.nextArgument();
372         if (cacheName == null) {
373             ci.println("Cache not supplied");
374             return;
375         }
376         String sKey = ci.nextArgument();
377         if (sKey == null) {
378             ci.println("Key not supplied");
379             return;
380         }
381         Integer key = null;
382         try {
383             key = Integer.valueOf(sKey);
384         } catch (NumberFormatException nfe) {
385             ci.println("Key is not a valid integer: " + sKey);
386         }
387         c = (ConcurrentMap<Integer, StringContainer>) this.icluster.getCache(
388                 containerName, cacheName);
389         if (c != null) {
390             ci.println("\nDelete key " + key);
391             c.remove(key);
392         } else {
393             ci.println("Cache " + cacheName + " on container " + containerName
394                     + " not existant!");
395         }
396     }
397
398     public void _dumper(CommandInterpreter ci) {
399         ConcurrentMap<Object, Object> c;
400         String containerName = ci.nextArgument().toLowerCase();
401         if (containerName == null) {
402             ci.println("containerName not supplied");
403             return;
404         }
405         String cacheName = ci.nextArgument();
406         if (cacheName == null) {
407             ci.println("Cache not supplied");
408             return;
409         }
410         int count = 0;
411         c = (ConcurrentMap<Object, Object>) this.icluster.getCache(containerName, cacheName);
412         if (c != null) {
413             for (Map.Entry<Object, Object> e : c.entrySet()) {
414                 Map.Entry<Object, Object> entry = e;
415                 Object v = entry.getValue();
416                 String res = "<NOT KNOWN>";
417                 if (v != null) {
418                     res = v.toString();
419                 }
420                 ci.println("Element " + entry.getKey() + "(hashCode="
421                         + entry.getKey().hashCode() + ") has value = (" + res
422                         + ")");
423                 count++;
424             }
425             ci.println("Dumped " + count + " records");
426         } else {
427             ci.println("Cache " + cacheName + " on container " + containerName
428                     + " not existant!");
429         }
430     }
431
432     public void _get(CommandInterpreter ci) {
433         ConcurrentMap<Integer, StringContainer> c;
434         if (this.icluster == null) {
435             ci.println("\nNo Clustering services available");
436             return;
437         }
438         String containerName = ci.nextArgument();
439         if (containerName == null) {
440             ci.println("containerName not supplied");
441             return;
442         }
443         String cacheName = ci.nextArgument();
444         if (cacheName == null) {
445             ci.println("Cache not supplied");
446             return;
447         }
448         String sKey = ci.nextArgument();
449         if (sKey == null) {
450             ci.println("Key not supplied");
451             return;
452         }
453         Integer key = null;
454         try {
455             key = Integer.valueOf(sKey);
456         } catch (NumberFormatException nfe) {
457             ci.println("Key is not a valid integer: " + sKey);
458         }
459         c = (ConcurrentMap<Integer, StringContainer>) this.icluster.getCache(
460                 containerName, cacheName);
461         if (c != null) {
462             ci.println("\nGet key (" + key + ")=(" + c.get(key) + ")");
463         } else {
464             ci.println("Cache " + cacheName + " on container " + containerName
465                     + " not existant!");
466         }
467     }
468
469     @SuppressWarnings("deprecation") //TODO: remove use of deprecated listenRoleChange
470     public void _listenActive(CommandInterpreter ci) {
471         if (this.icluster == null) {
472             ci.println("\nNo Clustering services available");
473             return;
474         }
475         this.doListen = new DoListenRoleChanged();
476         try {
477             this.icluster.listenRoleChange(this.doListen);
478         } catch (ListenRoleChangeAddException e) {
479             ci.println("Exception while registering the listener");
480             return;
481         }
482         ci.println("Register listenRoleChanges");
483     }
484
485     @SuppressWarnings("deprecation") //TODO: remove deprecated call to unlistenRoleChange
486     public void _unlistenActive(CommandInterpreter ci) {
487         if (this.icluster == null) {
488             ci.println("\nNo Clustering services available");
489             return;
490         }
491         if (this.doListen != null) {
492             this.icluster.unlistenRoleChange(this.doListen);
493             ci.println("Unregistered Active notifications");
494         }
495     }
496
497     class DoListenRoleChanged implements IListenRoleChange {
498         @Override
499         public void newActiveAvailable() {
500             logger.debug("New Active is available");
501         }
502     }
503
504     public void _putComplex(CommandInterpreter ci) {
505         ConcurrentMap<StringContainer, ComplexContainer> c;
506         if (this.icluster == null) {
507             ci.println("\nNo Clustering services available");
508             return;
509         }
510         String containerName = ci.nextArgument();
511         if (containerName == null) {
512             ci.println("containerName not supplied");
513             return;
514         }
515         String cacheName = ci.nextArgument();
516         if (cacheName == null) {
517             ci.println("Cache not supplied");
518             return;
519         }
520         String key = ci.nextArgument();
521         if (key == null) {
522             ci.println("Key not supplied (String)");
523             return;
524         }
525         String valueIdentity = ci.nextArgument();
526         if (valueIdentity == null) {
527             ci.println("Value for Identity not supplied (String)");
528             return;
529         }
530         String sValueState = ci.nextArgument();
531         if (sValueState == null) {
532             ci.println("Value for State not supplied (Integer)");
533             return;
534         }
535         Integer valueState = null;
536         try {
537             valueState = Integer.valueOf(sValueState);
538         } catch (NumberFormatException nfe) {
539             ci.println("Value State is not a valid integer: " + sValueState);
540             return;
541         }
542         c = (ConcurrentMap<StringContainer, ComplexContainer>) this.icluster
543                 .getCache(containerName, cacheName);
544         if (c != null) {
545             c.put(new StringContainer(key), new ComplexContainer(valueIdentity,
546                     valueState));
547             ci.println("\nPut in key (" + key + ")={String:" + valueIdentity
548                     + ",Integer:" + valueState + "}");
549         } else {
550             ci.println("Cache " + cacheName + " on container " + containerName
551                     + " not existant!");
552         }
553     }
554
555     public void _updateComplex(CommandInterpreter ci) {
556         ConcurrentMap<StringContainer, ComplexContainer> c;
557         if (this.icluster == null) {
558             ci.println("\nNo Clustering services available");
559             return;
560         }
561         String containerName = ci.nextArgument();
562         if (containerName == null) {
563             ci.println("containerName not supplied");
564             return;
565         }
566         String cacheName = ci.nextArgument();
567         if (cacheName == null) {
568             ci.println("Cache not supplied");
569             return;
570         }
571         String key = ci.nextArgument();
572         if (key == null) {
573             ci.println("Key not supplied (String)");
574             return;
575         }
576         String valueIdentity = ci.nextArgument();
577         if (valueIdentity == null) {
578             ci.println("Value for Identity not supplied (String)");
579             return;
580         }
581         c = (ConcurrentMap<StringContainer, ComplexContainer>) this.icluster
582                 .getCache(containerName, cacheName);
583         if (c != null) {
584             StringContainer k = new StringContainer(key);
585             ComplexContainer v = c.get(k);
586             if (v != null) {
587                 v.setIdentity(valueIdentity);
588                 ci.println("\nUpdate key (" + key + ")={String:"
589                         + valueIdentity + "}");
590
591                 // IMPORTANT ON UPDATING ANY FIELD OF THE CHILD MAKE
592                 // SURE TO PUT THE NEW VALUE IN THE CACHE ELSE THE
593                 // VALUE WILL NOT PROPAGATE!!
594                 c.put(k, v);
595             } else {
596                 ci.println("\nCannot Update key (" + key
597                         + ") doesn't exist in the database");
598             }
599         } else {
600             ci.println("Cache " + cacheName + " on container " + containerName
601                     + " not existant!");
602         }
603     }
604
605     public void setIClusterServices(IClusterServices i) {
606         this.icluster = i;
607         logger.debug("IClusterServices set");
608     }
609
610     public void unsetIClusterServices(IClusterServices i) {
611         if (this.icluster == i) {
612             this.icluster = null;
613             logger.debug("IClusterServices UNset");
614         }
615     }
616
617     public void startUp() {
618         logger.debug("Started clustering test plugin");
619     }
620
621     public void shutDown() {
622         logger.debug("Stopped clustering test plugin");
623     }
624
625     @Override
626     public String getHelp() {
627         StringBuffer help = new StringBuffer();
628         help.append("---Clustering Service Testing---\n");
629         help.append("\tput              - Put a key,value in the cache\n");
630         help.append("\tremove           - Delete a key from the cache\n");
631         help.append("\tget              - Get a key from the cache\n");
632         help.append("\tdumper           - Dump the cache\n");
633         help
634                 .append("\tcacheinfo        - Dump the configuration for a cache\n");
635         help.append("\ttbegin           - Transaction begin\n");
636         help.append("\ttcommit          - Transaction Commit\n");
637         help.append("\ttrollback        - Transaction Rollback\n");
638         help.append("\tlistcaches       - List all the Caches\n");
639         help.append("\tlisten           - Listen to cache updates\n");
640         help.append("\tunlisten         - UNListen to cache updates\n");
641         help.append("\tlistenActive     - Listen to Active updates\n");
642         help.append("\tunlistenActive   - UNListen to Active updates\n");
643         help.append("\tdestroy          - Destroy a cache\n");
644         help.append("\tcreate           - Create a cache\n");
645         help.append("\tmyController     - Print this controller's Cluster identifier\n");
646         help.append("\tgetClusterNodes  - Print all the controllers that make this cluster\n");
647         help.append("\tputComplex       - Fill a more complex data structure\n");
648         help.append("\tupdateComplex    - Update the value of a more complex data structure\n");
649         help.append("\tgetLogLevel      - Get the loglevel for the logger specified\n");
650         help.append("\tsetLogLevel      - Set the loglevel for the logger specified\n");
651         return help.toString();
652     }
653 }