Simplify boolean expressions
[netconf.git] / netconf / tools / netconf-cli / src / main / java / org / opendaylight / netconf / cli / reader / AbstractReader.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.cli.reader;
9
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;
26
27 public abstract class AbstractReader<T extends DataSchemaNode> implements Reader<T> {
28
29     public static final NullContext NULL_CONTEXT = new NullContext();
30
31     // TODO make console private add protected getter
32     protected ConsoleIO console;
33     private final SchemaContext context;
34     private boolean readConfigNode = false;
35
36     public AbstractReader(final ConsoleIO console, final SchemaContext context) {
37         this.console = console;
38         this.context = context;
39     }
40
41     public AbstractReader(final ConsoleIO console, final SchemaContext context, final boolean readConfigNode) {
42         this(console, context);
43         this.readConfigNode = readConfigNode;
44     }
45
46     protected SchemaContext getSchemaContext() {
47         return context;
48     }
49
50     protected ConsoleIO getConsole() {
51         return console;
52     }
53
54     protected boolean getReadConfigNode() {
55         return readConfigNode;
56     }
57
58     @Override
59     public List<NormalizedNode<?, ?>> read(final T schemaNode) throws ReadingException {
60         if (isReadingWanted(schemaNode)) {
61             final ConsoleContext ctx = getContext(schemaNode);
62             console.enterContext(ctx);
63             try {
64                 return readWithContext(schemaNode);
65             } catch (final IOException e) {
66                 throw new ReadingException("Unable to read data from input for " + schemaNode.getQName(), e);
67             } finally {
68                 console.leaveContext();
69             }
70         }
71         return Collections.emptyList();
72     }
73
74     private boolean isReadingWanted(final DataSchemaNode node) {
75         return !readConfigNode || node.isConfiguration();
76     }
77
78     // TODO javadoc
79
80     protected abstract List<NormalizedNode<?, ?>> readWithContext(T schemaNode) throws IOException, ReadingException;
81
82     protected abstract ConsoleContext getContext(T schemaNode);
83
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();
90         }
91
92         return Optional.fromNullable(defaultValue);
93     }
94
95     protected boolean isEmptyInput(final String rawValue) {
96         return Strings.isNullOrEmpty(rawValue);
97     }
98
99     protected static boolean isEmptyType(final TypeDefinition<?> type) {
100         return type instanceof EmptyTypeDefinition;
101     }
102
103     private static class NullContext implements ConsoleContext {
104         @Override
105         public Completer getCompleter() {
106             return new NullCompleter();
107         }
108
109         @Override
110         public Optional<String> getPrompt() {
111             return Optional.absent();
112         }
113     }
114
115 }