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.listType;
12 import com.google.common.base.Optional;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.opendaylight.controller.netconf.cli.io.BaseConsoleContext;
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.ReadingException;
22 import org.opendaylight.yangtools.yang.data.api.Node;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class GenericListReader<T extends DataSchemaNode> extends AbstractReader<T> {
29 private static final Logger LOG = LoggerFactory.getLogger(GenericListReader.class);
31 private final GenericListEntryReader<T> concreteListEntryReader;
33 public GenericListReader(final ConsoleIO console, final GenericListEntryReader<T> concreteListEntryReader,
34 final SchemaContext schemaContext) {
35 super(console, schemaContext);
36 this.concreteListEntryReader = concreteListEntryReader;
39 public GenericListReader(final ConsoleIO console, final GenericListEntryReader<T> concreteListEntryReader,
40 final SchemaContext schemaContext, final boolean readConfigNode) {
41 super(console, schemaContext, readConfigNode);
42 this.concreteListEntryReader = concreteListEntryReader;
46 public List<Node<?>> readWithContext(final T schemaNode) throws IOException, ReadingException {
47 final List<Node<?>> newNodes = new ArrayList<>();
48 Optional<Boolean> readNextListEntry = Optional.of(Boolean.TRUE);
49 console.formatLn("Reading collection type argument: %s", schemaNode.getQName().getLocalName());
50 while (readNextListEntry.isPresent() && readNextListEntry.get()) {
52 newNodes.addAll(concreteListEntryReader.read(schemaNode));
53 } catch (final ReadingException e) {
54 console.writeLn(e.getMessage());
56 readNextListEntry = new DecisionReader().read(console, "Add other entry to " + listType(schemaNode) + " "
57 + schemaNode.getQName().getLocalName() + " " + " [Y|N]?");
59 console.formatLn("Collection type argument: %s read finished", schemaNode.getQName().getLocalName());
65 protected ConsoleContext getContext(final T schemaNode) {
66 return new BaseConsoleContext<>(schemaNode);