Merge "BUG 2799: SPI for EventSources"
[controller.git] / opendaylight / netconf / netconf-cli / src / main / java / org / opendaylight / controller / netconf / cli / reader / impl / GenericListReader.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.listType;
11
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.schema.NormalizedNode;
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;
27
28 public class GenericListReader<T extends DataSchemaNode> extends AbstractReader<T> {
29     private static final Logger LOG = LoggerFactory.getLogger(GenericListReader.class);
30
31     private final GenericListEntryReader<T> concreteListEntryReader;
32
33     public GenericListReader(final ConsoleIO console, final GenericListEntryReader<T> concreteListEntryReader,
34             final SchemaContext schemaContext) {
35         super(console, schemaContext);
36         this.concreteListEntryReader = concreteListEntryReader;
37     }
38
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;
43     }
44
45     @Override
46     public List<NormalizedNode<?, ?>> readWithContext(final T schemaNode) throws IOException, ReadingException {
47         final List<NormalizedNode<?, ?>> 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()) {
51             try {
52                 newNodes.addAll(concreteListEntryReader.read(schemaNode));
53             } catch (final ReadingException e) {
54                 console.writeLn(e.getMessage());
55             }
56             readNextListEntry = new DecisionReader().read(console, "Add other entry to " + listType(schemaNode) + " "
57                     + schemaNode.getQName().getLocalName() + " " + " [Y|N]?");
58         }
59         console.formatLn("Collection type argument: %s read finished", schemaNode.getQName().getLocalName());
60
61         return newNodes;
62     }
63
64     @Override
65     protected ConsoleContext getContext(final T schemaNode) {
66         return new BaseConsoleContext<>(schemaNode);
67     }
68
69 }