Remove opendaylight directory
[netconf.git] / netconf / tools / netconf-cli / src / main / java / org / opendaylight / netconf / cli / reader / impl / UnionTypeReader.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.impl;
9
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;
17 import java.util.Map;
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;
29
30 class UnionTypeReader {
31     private static final Logger LOG = LoggerFactory.getLogger(UnionTypeReader.class);
32
33     private final ConsoleIO console;
34
35     public UnionTypeReader(final ConsoleIO console) {
36         this.console = console;
37     }
38
39     public Optional<TypeDefinition<?>> read(final TypeDefinition<?> unionTypeDefinition) throws IOException,
40             ReadingException {
41         final ConsoleContext context = getContext(unionTypeDefinition);
42         console.enterContext(context);
43         try {
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());
46
47             final String rawValue = console.read();
48             if (IOUtil.isSkipInput(rawValue)) {
49                 return Optional.absent();
50             }
51             final TypeDefinition<?> value = mapping.get(rawValue);
52             if (value != null) {
53                 return Optional.<TypeDefinition<?>> of(value);
54             } else {
55                 final String message = String.format("Incorrect type (%s) was specified for union type definition", rawValue);
56                 LOG.error(message);
57                 throw new ReadingException(message);
58             }
59         } finally {
60             console.leaveContext();
61         }
62     }
63
64     private UnionConsoleContext getContext(final TypeDefinition<?> typeDefinition) {
65         return new UnionConsoleContext(typeDefinition);
66     }
67
68     private class UnionConsoleContext implements ConsoleContext {
69
70         private final TypeDefinition<?> typeDef;
71         private final Map<String, TypeDefinition<?>> menuItemsToTypeDefinitions = new HashMap<>();
72
73         public UnionConsoleContext(final TypeDefinition<?> typeDef) {
74             this.typeDef = typeDef;
75         }
76
77         @Override
78         public Optional<String> getPrompt() {
79             return Optional.of("type[" + typeDef.getQName().getLocalName()  + "]");
80         }
81
82         @Override
83         public Completer getCompleter() {
84             List<TypeDefinition<?>> subtypesForMenu = resolveSubtypesFrom(typeDef);
85             if (subtypesForMenu.isEmpty()) {
86                 subtypesForMenu = Collections.<TypeDefinition<?>> singletonList(typeDef);
87             }
88             final Collection<String> menuItems = toMenuItem(subtypesForMenu);
89             return new AggregateCompleter(new StringsCompleter(menuItems), new StringsCompleter(IOUtil.SKIP));
90         }
91
92         public Map<String, TypeDefinition<?>> getMenuItemMapping() {
93             return menuItemsToTypeDefinitions;
94         }
95
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);
102             }
103             return result;
104         }
105
106         /**
107          *
108          * If union type is found in potentialEndTypeCandidate as subtype then
109          * it these subtypes become candidates.
110          *
111          * @param potentialEndTypeCandidate
112          *            candidate to node which has no union subtype
113          */
114         private List<TypeDefinition<?>> resolveSubtypesFrom(final TypeDefinition<?> potentialEndTypeCandidate) {
115             if (potentialEndTypeCandidate instanceof UnionTypeDefinition) {
116                 return ((UnionTypeDefinition) potentialEndTypeCandidate).getTypes();
117             }
118             if (potentialEndTypeCandidate.getBaseType() == null) {
119                 return Collections.emptyList();
120             }
121             return resolveSubtypesFrom(potentialEndTypeCandidate.getBaseType());
122         }
123     }
124 }