2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.netconf.cli;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
14 import org.opendaylight.controller.netconf.cli.io.ConsoleIO;
15 import org.opendaylight.controller.netconf.cli.reader.Reader;
16 import org.opendaylight.controller.netconf.cli.reader.custom.ConfigReader;
17 import org.opendaylight.controller.netconf.cli.reader.custom.EditContentReader;
18 import org.opendaylight.controller.netconf.cli.reader.custom.FilterReader;
19 import org.opendaylight.controller.netconf.cli.reader.custom.PasswordReader;
20 import org.opendaylight.controller.netconf.cli.reader.impl.GenericReader;
21 import org.opendaylight.controller.netconf.cli.writer.OutFormatter;
22 import org.opendaylight.controller.netconf.cli.writer.Writer;
23 import org.opendaylight.controller.netconf.cli.writer.custom.DataWriter;
24 import org.opendaylight.controller.netconf.cli.writer.impl.NormalizedNodeWriter;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 * Keeps custom and generic input/output arguments handlers. Custom handlers are
30 * constructed lazily, due to remote schema context acquisition.
32 public class CommandArgHandlerRegistry {
34 private final ConsoleIO consoleIO;
35 private final SchemaContextRegistry schemaContextRegistry;
37 private final Map<Class<? extends Reader<? extends DataSchemaNode>>, ReaderProvider> customReaders = Maps
39 private final Map<Class<? extends Writer<DataSchemaNode>>, WriterProvider> customWriters = Maps.newHashMap();
41 public CommandArgHandlerRegistry(final ConsoleIO consoleIO, final SchemaContextRegistry schemaContextRegistry) {
42 this.consoleIO = consoleIO;
43 this.schemaContextRegistry = schemaContextRegistry;
49 private void setUpWriters() {
50 customWriters.put(DataWriter.class, new DataWriterProvider());
53 private void setUpReaders() {
54 customReaders.put(PasswordReader.class, new PasswordReaderProvider());
55 customReaders.put(FilterReader.class, new FilterReaderProvider());
56 customReaders.put(ConfigReader.class, new ConfigReaderProvider());
57 customReaders.put(EditContentReader.class, new EditContentReaderProvider());
60 public synchronized Reader<? extends DataSchemaNode> getCustomReader(
61 final Class<? extends Reader<DataSchemaNode>> readerType) {
62 return customReaders.get(readerType).provide(consoleIO, this, schemaContextRegistry);
65 private static SchemaContext getRemoteSchema(final Class<?> handlerType,
66 final SchemaContextRegistry schemaContextRegistry) {
67 final Optional<SchemaContext> remoteSchemaContext = schemaContextRegistry.getRemoteSchemaContext();
68 Preconditions.checkState(remoteSchemaContext.isPresent(),
69 "Remote schema context not acquired yet, cannot get handler %s", handlerType);
70 return remoteSchemaContext.get();
73 public synchronized Reader<DataSchemaNode> getGenericReader(final SchemaContext schemaContext) {
74 return new GenericReader(consoleIO, this, schemaContext);
77 public synchronized Reader<DataSchemaNode> getGenericReader(final SchemaContext schemaContext,
78 final boolean readConfigNode) {
79 return new GenericReader(consoleIO, this, schemaContext, readConfigNode);
82 public synchronized Writer<DataSchemaNode> getCustomWriter(final Class<? extends Writer<DataSchemaNode>> writerType) {
83 return customWriters.get(writerType).provide(consoleIO, getRemoteSchema(writerType, schemaContextRegistry),
87 public synchronized Writer<DataSchemaNode> getGenericWriter() {
88 return new NormalizedNodeWriter(consoleIO, new OutFormatter());
92 * Reader providers, in order to construct readers lazily
94 private static interface ReaderProvider {
95 Reader<? extends DataSchemaNode> provide(ConsoleIO consoleIO,
96 final CommandArgHandlerRegistry commandArgHandlerRegistry,
97 final SchemaContextRegistry schemaContextRegistry);
100 private static final class FilterReaderProvider implements ReaderProvider {
102 public Reader<? extends DataSchemaNode> provide(final ConsoleIO consoleIO,
103 final CommandArgHandlerRegistry commandArgHandlerRegistry,
104 final SchemaContextRegistry schemaContextRegistry) {
105 return new FilterReader(consoleIO, getRemoteSchema(FilterReader.class, schemaContextRegistry));
109 private static final class ConfigReaderProvider implements ReaderProvider {
111 public Reader<? extends DataSchemaNode> provide(final ConsoleIO consoleIO,
112 final CommandArgHandlerRegistry commandArgHandlerRegistry,
113 final SchemaContextRegistry schemaContextRegistry) {
114 return new ConfigReader(consoleIO, getRemoteSchema(ConfigReader.class, schemaContextRegistry),
115 commandArgHandlerRegistry);
119 private static final class EditContentReaderProvider implements ReaderProvider {
121 public Reader<? extends DataSchemaNode> provide(final ConsoleIO consoleIO,
122 final CommandArgHandlerRegistry commandArgHandlerRegistry,
123 final SchemaContextRegistry schemaContextRegistry) {
124 return new EditContentReader(consoleIO, commandArgHandlerRegistry, getRemoteSchema(EditContentReader.class,
125 schemaContextRegistry));
129 private static final class PasswordReaderProvider implements ReaderProvider {
131 public Reader<? extends DataSchemaNode> provide(final ConsoleIO consoleIO,
132 final CommandArgHandlerRegistry commandArgHandlerRegistry,
133 final SchemaContextRegistry schemaContextRegistry) {
134 return new PasswordReader(consoleIO, schemaContextRegistry.getLocalSchemaContext());
139 * Writer providers, in order to construct readers lazily
141 private static interface WriterProvider {
142 Writer<DataSchemaNode> provide(ConsoleIO consoleIO, SchemaContext schema,
143 final CommandArgHandlerRegistry commandArgHandlerRegistry);
146 private class DataWriterProvider implements WriterProvider {
148 public Writer<DataSchemaNode> provide(final ConsoleIO consoleIO, final SchemaContext schema,
149 final CommandArgHandlerRegistry commandArgHandlerRegistry) {
150 return new DataWriter(consoleIO, new OutFormatter(), schema);