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.impl;
10 import com.google.common.base.Optional;
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.List;
18 import jline.console.completer.AggregateCompleter;
19 import jline.console.completer.Completer;
20 import jline.console.completer.StringsCompleter;
21 import org.opendaylight.netconf.cli.io.ConsoleContext;
22 import org.opendaylight.netconf.cli.io.ConsoleIO;
23 import org.opendaylight.netconf.cli.io.IOUtil;
24 import org.opendaylight.netconf.cli.reader.ReadingException;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 class UnionTypeReader {
31 private static final Logger LOG = LoggerFactory.getLogger(UnionTypeReader.class);
33 private final ConsoleIO console;
35 public UnionTypeReader(final ConsoleIO console) {
36 this.console = console;
39 public Optional<TypeDefinition<?>> read(final TypeDefinition<?> unionTypeDefinition) throws IOException,
41 final ConsoleContext context = getContext(unionTypeDefinition);
42 console.enterContext(context);
44 final Map<String, TypeDefinition<?>> mapping = ((UnionConsoleContext) context).getMenuItemMapping();
45 console.formatLn("The element is of type union. Choose concrete type from: %s", mapping.keySet());
47 final String rawValue = console.read();
48 if (IOUtil.isSkipInput(rawValue)) {
49 return Optional.absent();
51 final TypeDefinition<?> value = mapping.get(rawValue);
53 return Optional.<TypeDefinition<?>> of(value);
55 final String message = String.format("Incorrect type (%s) was specified for union type definition", rawValue);
57 throw new ReadingException(message);
60 console.leaveContext();
64 private UnionConsoleContext getContext(final TypeDefinition<?> typeDefinition) {
65 return new UnionConsoleContext(typeDefinition);
68 private class UnionConsoleContext implements ConsoleContext {
70 private final TypeDefinition<?> typeDef;
71 private final Map<String, TypeDefinition<?>> menuItemsToTypeDefinitions = new HashMap<>();
73 public UnionConsoleContext(final TypeDefinition<?> typeDef) {
74 this.typeDef = typeDef;
78 public Optional<String> getPrompt() {
79 return Optional.of("type[" + typeDef.getQName().getLocalName() + "]");
83 public Completer getCompleter() {
84 List<TypeDefinition<?>> subtypesForMenu = resolveSubtypesFrom(typeDef);
85 if (subtypesForMenu.isEmpty()) {
86 subtypesForMenu = Collections.<TypeDefinition<?>> singletonList(typeDef);
88 final Collection<String> menuItems = toMenuItem(subtypesForMenu);
89 return new AggregateCompleter(new StringsCompleter(menuItems), new StringsCompleter(IOUtil.SKIP));
92 public Map<String, TypeDefinition<?>> getMenuItemMapping() {
93 return menuItemsToTypeDefinitions;
96 private Collection<String> toMenuItem(final List<TypeDefinition<?>> allTypesBehindUnion) {
97 final List<String> result = new ArrayList<String>();
98 for (final TypeDefinition<?> type : allTypesBehindUnion) {
99 final String menuItem = type.getQName().getLocalName();
100 menuItemsToTypeDefinitions.put(menuItem, type);
101 result.add(menuItem);
108 * If union type is found in potentialEndTypeCandidate as subtype then
109 * it these subtypes become candidates.
111 * @param potentialEndTypeCandidate
112 * candidate to node which has no union subtype
114 private List<TypeDefinition<?>> resolveSubtypesFrom(final TypeDefinition<?> potentialEndTypeCandidate) {
115 if (potentialEndTypeCandidate instanceof UnionTypeDefinition) {
116 return ((UnionTypeDefinition) potentialEndTypeCandidate).getTypes();
118 if (potentialEndTypeCandidate.getBaseType() == null) {
119 return Collections.emptyList();
121 return resolveSubtypesFrom(potentialEndTypeCandidate.getBaseType());