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.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.controller.netconf.cli.io.ConsoleContext;
18 import org.opendaylight.controller.netconf.cli.io.ConsoleIO;
19 import org.opendaylight.yangtools.yang.data.api.Node;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
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<Node<?>> 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 if (readConfigNode && !node.isConfiguration()) {
83 protected abstract List<Node<?>> readWithContext(T schemaNode) throws IOException, ReadingException;
85 protected abstract ConsoleContext getContext(T schemaNode);
87 protected Optional<String> getDefaultValue(final T schemaNode) {
88 String defaultValue = null;
89 if (schemaNode instanceof LeafSchemaNode) {
90 defaultValue = ((LeafSchemaNode) schemaNode).getDefault();
91 } else if (schemaNode instanceof ChoiceNode) {
92 defaultValue = ((ChoiceNode) schemaNode).getDefaultCase();
95 return Optional.fromNullable(defaultValue);
98 protected boolean isEmptyInput(final String rawValue) {
99 return Strings.isNullOrEmpty(rawValue);
102 protected static boolean isEmptyType(final TypeDefinition<?> type) {
103 return type instanceof EmptyTypeDefinition;
106 private static class NullContext implements ConsoleContext {
108 public Completer getCompleter() {
109 return new NullCompleter();
113 public Optional<String> getPrompt() {
114 return Optional.absent();