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.impl;
10 import static org.opendaylight.controller.netconf.cli.io.IOUtil.isSkipInput;
12 import com.google.common.base.Optional;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.List;
20 import jline.console.completer.AggregateCompleter;
21 import jline.console.completer.Completer;
22 import jline.console.completer.StringsCompleter;
23 import org.opendaylight.controller.netconf.cli.io.ConsoleContext;
24 import org.opendaylight.controller.netconf.cli.io.ConsoleIO;
25 import org.opendaylight.controller.netconf.cli.io.IOUtil;
26 import org.opendaylight.controller.netconf.cli.reader.ReadingException;
27 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 class UnionTypeReader {
33 private static final Logger LOG = LoggerFactory.getLogger(UnionTypeReader.class);
35 private final ConsoleIO console;
37 public UnionTypeReader(final ConsoleIO console) {
38 this.console = console;
41 public Optional<TypeDefinition<?>> read(final TypeDefinition<?> unionTypeDefinition) throws IOException,
43 final ConsoleContext context = getContext(unionTypeDefinition);
44 console.enterContext(context);
46 final Map<String, TypeDefinition<?>> mapping = ((UnionConsoleContext) context).getMenuItemMapping();
47 console.formatLn("The element is of type union. Choose concrete type from: %s", mapping.keySet());
49 final String rawValue = console.read();
50 if (isSkipInput(rawValue)) {
51 return Optional.absent();
53 final TypeDefinition<?> value = mapping.get(rawValue);
55 return Optional.<TypeDefinition<?>> of(value);
57 final String message = String.format("Incorrect type (%s) was specified for union type definition", rawValue);
59 throw new ReadingException(message);
62 console.leaveContext();
66 private UnionConsoleContext getContext(final TypeDefinition<?> typeDefinition) {
67 return new UnionConsoleContext(typeDefinition);
70 private class UnionConsoleContext implements ConsoleContext {
72 private final TypeDefinition<?> typeDef;
73 private final Map<String, TypeDefinition<?>> menuItemsToTypeDefinitions = new HashMap<>();
75 public UnionConsoleContext(final TypeDefinition<?> typeDef) {
76 this.typeDef = typeDef;
80 public Optional<String> getPrompt() {
81 return Optional.of("type[" + typeDef.getQName().getLocalName() + "]");
85 public Completer getCompleter() {
86 List<TypeDefinition<?>> subtypesForMenu = resolveSubtypesFrom(typeDef);
87 if (subtypesForMenu.isEmpty()) {
88 subtypesForMenu = Collections.<TypeDefinition<?>> singletonList(typeDef);
90 final Collection<String> menuItems = toMenuItem(subtypesForMenu);
91 return new AggregateCompleter(new StringsCompleter(menuItems), new StringsCompleter(IOUtil.SKIP));
94 public Map<String, TypeDefinition<?>> getMenuItemMapping() {
95 return menuItemsToTypeDefinitions;
98 private Collection<String> toMenuItem(final List<TypeDefinition<?>> allTypesBehindUnion) {
99 final List<String> result = new ArrayList<String>();
100 for (final TypeDefinition<?> type : allTypesBehindUnion) {
101 final String menuItem = type.getQName().getLocalName();
102 menuItemsToTypeDefinitions.put(menuItem, type);
103 result.add(menuItem);
110 * If union type is found in potentialEndTypeCandidate as subtype then
111 * it these subtypes become candidates.
113 * @param potentialEndTypeCandidate
114 * candidate to node which has no union subtype
116 private List<TypeDefinition<?>> resolveSubtypesFrom(final TypeDefinition<?> potentialEndTypeCandidate) {
117 if (potentialEndTypeCandidate instanceof UnionTypeDefinition) {
118 return ((UnionTypeDefinition) potentialEndTypeCandidate).getTypes();
120 if (potentialEndTypeCandidate.getBaseType() == null) {
121 return Collections.emptyList();
123 return resolveSubtypesFrom(potentialEndTypeCandidate.getBaseType());