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.netconf.cli.reader;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Strings;
12 import java.io.IOException;
13 import java.util.Collections;
14 import java.util.List;
15 import jline.console.completer.Completer;
16 import jline.console.completer.NullCompleter;
17 import org.opendaylight.netconf.cli.io.ConsoleContext;
18 import org.opendaylight.netconf.cli.io.ConsoleIO;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
27 public abstract class AbstractReader<T extends DataSchemaNode> implements Reader<T> {
29 public static final NullContext NULL_CONTEXT = new NullContext();
31 // TODO make console private add protected getter
32 protected ConsoleIO console;
33 private final SchemaContext context;
34 private boolean readConfigNode = false;
36 public AbstractReader(final ConsoleIO console, final SchemaContext context) {
37 this.console = console;
38 this.context = context;
41 public AbstractReader(final ConsoleIO console, final SchemaContext context, final boolean readConfigNode) {
42 this(console, context);
43 this.readConfigNode = readConfigNode;
46 protected SchemaContext getSchemaContext() {
50 protected ConsoleIO getConsole() {
54 protected boolean getReadConfigNode() {
55 return readConfigNode;
59 public List<NormalizedNode<?, ?>> read(final T schemaNode) throws ReadingException {
60 if (isReadingWanted(schemaNode)) {
61 final ConsoleContext ctx = getContext(schemaNode);
62 console.enterContext(ctx);
64 return readWithContext(schemaNode);
65 } catch (final IOException e) {
66 throw new ReadingException("Unable to read data from input for " + schemaNode.getQName(), e);
68 console.leaveContext();
71 return Collections.emptyList();
74 private boolean isReadingWanted(final DataSchemaNode node) {
75 return !readConfigNode || node.isConfiguration();
80 protected abstract List<NormalizedNode<?, ?>> readWithContext(T schemaNode) throws IOException, ReadingException;
82 protected abstract ConsoleContext getContext(T schemaNode);
84 protected Optional<String> getDefaultValue(final T schemaNode) {
85 String defaultValue = null;
86 if (schemaNode instanceof LeafSchemaNode) {
87 defaultValue = ((LeafSchemaNode) schemaNode).getDefault();
88 } else if (schemaNode instanceof ChoiceSchemaNode) {
89 defaultValue = ((ChoiceSchemaNode) schemaNode).getDefaultCase();
92 return Optional.fromNullable(defaultValue);
95 protected boolean isEmptyInput(final String rawValue) {
96 return Strings.isNullOrEmpty(rawValue);
99 protected static boolean isEmptyType(final TypeDefinition<?> type) {
100 return type instanceof EmptyTypeDefinition;
103 private static class NullContext implements ConsoleContext {
105 public Completer getCompleter() {
106 return new NullCompleter();
110 public Optional<String> getPrompt() {
111 return Optional.absent();