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