X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-cli%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fcli%2Freader%2Fimpl%2FDecisionReader.java;fp=opendaylight%2Fnetconf%2Fnetconf-cli%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fcli%2Freader%2Fimpl%2FDecisionReader.java;h=b1e9a43298a8d67759ea6a8a7e7feb9ae9c657e9;hp=0000000000000000000000000000000000000000;hb=b3d2a00776a1a5e3a139d73ced859aa557c931af;hpb=d04e0863b86415749a8437241c57df0d32a3b133;ds=sidebyside diff --git a/opendaylight/netconf/netconf-cli/src/main/java/org/opendaylight/controller/netconf/cli/reader/impl/DecisionReader.java b/opendaylight/netconf/netconf-cli/src/main/java/org/opendaylight/controller/netconf/cli/reader/impl/DecisionReader.java new file mode 100644 index 0000000000..b1e9a43298 --- /dev/null +++ b/opendaylight/netconf/netconf-cli/src/main/java/org/opendaylight/controller/netconf/cli/reader/impl/DecisionReader.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.netconf.cli.reader.impl; + +import static org.opendaylight.controller.netconf.cli.io.IOUtil.SKIP; + +import com.google.common.base.Optional; +import java.io.IOException; +import jline.console.completer.AggregateCompleter; +import jline.console.completer.Completer; +import jline.console.completer.StringsCompleter; +import jline.internal.Log; +import org.opendaylight.controller.netconf.cli.io.ConsoleContext; +import org.opendaylight.controller.netconf.cli.io.ConsoleIO; +import org.opendaylight.controller.netconf.cli.io.IOUtil; +import org.opendaylight.controller.netconf.cli.reader.ReadingException; + +public class DecisionReader { + + private static final String YES = "Y"; + private static final String NO = "N"; + public static final Completer YES_NO_COMPLETER = new StringsCompleter(YES, NO); + + public Optional read(final ConsoleIO console, final String questionMessageBlueprint, + final Object... questionMessageArgs) throws IOException, ReadingException { + final ConsoleContext ctx = getContext(); + console.enterContext(ctx); + try { + console.formatLn(questionMessageBlueprint, questionMessageArgs); + final String rawValue = console.read(); + if (YES.equals(rawValue.toUpperCase())) { + return Optional.of(Boolean.TRUE); + } else if (NO.equals(rawValue.toUpperCase())) { + return Optional.of(Boolean.FALSE); + } else if (SKIP.equals(rawValue)) { + return Optional.absent(); + } else { + final String message = String.format("Incorrect possibility (%s) was selected", rawValue); + Log.error(message); + throw new ReadingException(message); + } + } finally { + console.leaveContext(); + } + } + + private static ConsoleContext getContext() { + return new ConsoleContext() { + + @Override + public Optional getPrompt() { + return Optional.absent(); + } + + @Override + public Completer getCompleter() { + return new AggregateCompleter(YES_NO_COMPLETER, new StringsCompleter(IOUtil.SKIP)); + } + + }; + } + +}