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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.io.IOException;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.controller.netconf.cli.CommandArgHandlerRegistry;
16 import org.opendaylight.controller.netconf.cli.commands.CommandConstants;
17 import org.opendaylight.controller.netconf.cli.io.ConsoleContext;
18 import org.opendaylight.controller.netconf.cli.io.ConsoleIO;
19 import org.opendaylight.controller.netconf.cli.reader.AbstractReader;
20 import org.opendaylight.controller.netconf.cli.reader.GenericListEntryReader;
21 import org.opendaylight.controller.netconf.cli.reader.Reader;
22 import org.opendaylight.controller.netconf.cli.reader.ReadingException;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.Node;
25 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
35 public class GenericReader extends AbstractReader<DataSchemaNode> {
37 private final CommandArgHandlerRegistry argumentHandlerRegistry;
39 public GenericReader(final ConsoleIO console, final CommandArgHandlerRegistry argumentHandlerRegistry,
40 final SchemaContext schemaContext) {
41 super(console, schemaContext);
42 this.argumentHandlerRegistry = argumentHandlerRegistry;
45 public GenericReader(final ConsoleIO console, final CommandArgHandlerRegistry argumentHandlerRegistry,
46 final SchemaContext schemaContext, final boolean readConfigNode) {
47 super(console, schemaContext, readConfigNode);
48 this.argumentHandlerRegistry = argumentHandlerRegistry;
52 protected List<Node<?>> readWithContext(final DataSchemaNode schemaNode) throws IOException, ReadingException {
53 final Optional<Class<? extends Reader<DataSchemaNode>>> customReaderClassOpt = tryGetCustomHandler(schemaNode);
55 if (customReaderClassOpt.isPresent()) {
56 // TODO resolve class cast of generic custom readers
57 final Reader<DataSchemaNode> customReaderInstance = (Reader<DataSchemaNode>) argumentHandlerRegistry
58 .getCustomReader(customReaderClassOpt.get());
59 Preconditions.checkNotNull(customReaderInstance, "Unknown custom reader: %s", customReaderClassOpt.get());
60 return customReaderInstance.read(schemaNode);
62 return readGeneric(schemaNode);
65 // TODO reuse instances
68 private List<Node<?>> readGeneric(final DataSchemaNode schemaNode) throws ReadingException, IOException {
69 final List<Node<?>> newNodes = new ArrayList<>();
70 boolean isRedCorrectly = false;
73 if (schemaNode instanceof LeafSchemaNode) {
74 return new LeafReader(console, getSchemaContext(), getReadConfigNode())
75 .read((LeafSchemaNode) schemaNode);
76 } else if (schemaNode instanceof ContainerSchemaNode) {
77 return new ContainerReader(console, argumentHandlerRegistry, getSchemaContext(),
78 getReadConfigNode()).read((ContainerSchemaNode) schemaNode);
79 } else if (schemaNode instanceof ListSchemaNode) {
80 final GenericListEntryReader<ListSchemaNode> entryReader = new ListEntryReader(console,
81 argumentHandlerRegistry, getSchemaContext(), getReadConfigNode());
82 return new GenericListReader<>(console, entryReader, getSchemaContext(), getReadConfigNode())
83 .read((ListSchemaNode) schemaNode);
84 } else if (schemaNode instanceof LeafListSchemaNode) {
85 final GenericListEntryReader<LeafListSchemaNode> entryReader = new LeafListEntryReader(console,
86 getSchemaContext(), getReadConfigNode());
87 return new GenericListReader<>(console, entryReader, getSchemaContext(), getReadConfigNode())
88 .read((LeafListSchemaNode) schemaNode);
89 } else if (schemaNode instanceof ChoiceSchemaNode) {
90 return new ChoiceReader(console, argumentHandlerRegistry, getSchemaContext(), getReadConfigNode())
91 .read((ChoiceSchemaNode) schemaNode);
92 } else if (schemaNode instanceof AnyXmlSchemaNode) {
93 return new AnyXmlReader(console, getSchemaContext(), getReadConfigNode())
94 .read((AnyXmlSchemaNode) schemaNode);
96 isRedCorrectly = true;
97 } catch (final ReadingException e) {
98 console.writeLn(e.getMessage());
100 } while (!isRedCorrectly);
105 protected ConsoleContext getContext(final DataSchemaNode schemaNode) {
106 // return null context, leave context to specific implementations
110 private <T> Optional<Class<? extends T>> tryGetCustomHandler(final DataSchemaNode dataSchemaNode) {
112 for (final UnknownSchemaNode unknownSchemaNode : dataSchemaNode.getUnknownSchemaNodes()) {
114 if (isExtenstionForCustomHandler(unknownSchemaNode)) {
115 final String argumentHandlerClassName = unknownSchemaNode.getNodeParameter();
117 final Class<?> argumentClass = Class.forName(argumentHandlerClassName);
118 // TODO add check before cast
119 return Optional.<Class<? extends T>> of((Class<? extends T>) argumentClass);
120 } catch (final ClassNotFoundException e) {
121 throw new IllegalArgumentException("Unknown custom reader class " + argumentHandlerClassName
122 + " for: " + dataSchemaNode.getQName());
127 return Optional.absent();
130 private boolean isExtenstionForCustomHandler(final UnknownSchemaNode unknownSchemaNode) {
131 final QName qName = unknownSchemaNode.getExtensionDefinition().getQName();
132 return qName.equals(CommandConstants.ARG_HANDLER_EXT_QNAME);