Remove netconf from commons/opendaylight pom
[controller.git] / opendaylight / netconf / tools / netconf-cli / src / main / java / org / opendaylight / controller / netconf / cli / reader / impl / GenericReader.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 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.schema.NormalizedNode;
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;
34
35 public class GenericReader extends AbstractReader<DataSchemaNode> {
36
37     private final CommandArgHandlerRegistry argumentHandlerRegistry;
38
39     public GenericReader(final ConsoleIO console, final CommandArgHandlerRegistry argumentHandlerRegistry,
40             final SchemaContext schemaContext) {
41         super(console, schemaContext);
42         this.argumentHandlerRegistry = argumentHandlerRegistry;
43     }
44
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;
49     }
50
51     @Override
52     protected List<NormalizedNode<?, ?>> readWithContext(final DataSchemaNode schemaNode) throws IOException, ReadingException {
53         final Optional<Class<? extends Reader<DataSchemaNode>>> customReaderClassOpt = tryGetCustomHandler(schemaNode);
54
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);
61         } else {
62             return readGeneric(schemaNode);
63         }
64
65         // TODO reuse instances
66     }
67
68     private List<NormalizedNode<?, ?>> readGeneric(final DataSchemaNode schemaNode) throws ReadingException, IOException {
69         final List<NormalizedNode<?, ?>> newNodes = new ArrayList<>();
70         boolean isRedCorrectly = false;
71         do {
72             try {
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);
95                 }
96                 isRedCorrectly = true;
97             } catch (final ReadingException e) {
98                 console.writeLn(e.getMessage());
99             }
100         } while (!isRedCorrectly);
101         return newNodes;
102     }
103
104     @Override
105     protected ConsoleContext getContext(final DataSchemaNode schemaNode) {
106         // return null context, leave context to specific implementations
107         return NULL_CONTEXT;
108     }
109
110     private <T> Optional<Class<? extends T>> tryGetCustomHandler(final DataSchemaNode dataSchemaNode) {
111
112         for (final UnknownSchemaNode unknownSchemaNode : dataSchemaNode.getUnknownSchemaNodes()) {
113
114             if (isExtenstionForCustomHandler(unknownSchemaNode)) {
115                 final String argumentHandlerClassName = unknownSchemaNode.getNodeParameter();
116                 try {
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());
123                 }
124             }
125         }
126
127         return Optional.absent();
128     }
129
130     private boolean isExtenstionForCustomHandler(final UnknownSchemaNode unknownSchemaNode) {
131         final QName qName = unknownSchemaNode.getExtensionDefinition().getQName();
132         return qName.equals(CommandConstants.ARG_HANDLER_EXT_QNAME);
133     }
134 }