Merge "Add missing copyright text"
[controller.git] / opendaylight / netconf / netconf-cli / src / main / java / org / opendaylight / controller / 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.controller.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.controller.netconf.cli.io.ConsoleContext;
18 import org.opendaylight.controller.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         if (readConfigNode && !node.isConfiguration()) {
76             return false;
77         }
78         return true;
79     }
80
81     // TODO javadoc
82
83     protected abstract List<NormalizedNode<?, ?>> readWithContext(T schemaNode) throws IOException, ReadingException;
84
85     protected abstract ConsoleContext getContext(T schemaNode);
86
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 ChoiceSchemaNode) {
92             defaultValue = ((ChoiceSchemaNode) schemaNode).getDefaultCase();
93         }
94
95         return Optional.fromNullable(defaultValue);
96     }
97
98     protected boolean isEmptyInput(final String rawValue) {
99         return Strings.isNullOrEmpty(rawValue);
100     }
101
102     protected static boolean isEmptyType(final TypeDefinition<?> type) {
103         return type instanceof EmptyTypeDefinition;
104     }
105
106     private static class NullContext implements ConsoleContext {
107         @Override
108         public Completer getCompleter() {
109             return new NullCompleter();
110         }
111
112         @Override
113         public Optional<String> getPrompt() {
114             return Optional.absent();
115         }
116     }
117
118 }