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