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.io;
10 import java.text.ParseException;
11 import java.text.SimpleDateFormat;
12 import java.util.Date;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
16 import org.opendaylight.controller.netconf.cli.reader.ReadingException;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
25 public static final String SKIP = "skip";
26 public static final String PROMPT_SUFIX = ">";
27 public static final String PATH_SEPARATOR = "/";
32 public static boolean isQName(final String qName) {
33 final Matcher matcher = patternNew.matcher(qName);
34 return matcher.matches();
37 public static Date parseDate(final String revision) {
38 final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
40 return formatter.parse(revision);
41 } catch (final ParseException e) {
42 throw new IllegalArgumentException("Date not valid", e);
46 public static String listType(final SchemaNode schemaNode) {
47 if (schemaNode instanceof LeafListSchemaNode) {
49 } else if (schemaNode instanceof ListSchemaNode) {
51 } else if (schemaNode instanceof LeafSchemaNode) {
54 // FIXME throw exception on unexpected state, not null/emptyString
58 public static String qNameToKeyString(final QName qName, final String moduleName) {
59 return String.format("%s(%s)", qName.getLocalName(), moduleName);
62 // TODO test and check regex + review format of string for QName
63 final static Pattern patternNew = Pattern.compile("([^\\)]+)\\(([^\\)]+)\\)");
65 public static QName qNameFromKeyString(final String qName, final Map<String, QName> mappedModules)
66 throws ReadingException {
67 final Matcher matcher = patternNew.matcher(qName);
68 if (!matcher.matches()) {
69 final String message = String.format("QName in wrong format: %s should be: %s", qName, patternNew);
70 throw new ReadingException(message);
72 final QName base = mappedModules.get(matcher.group(2));
74 final String message = String.format("Module %s cannot be found", matcher.group(2));
75 throw new ReadingException(message);
77 return QName.create(base, matcher.group(1));
80 public static boolean isSkipInput(final String rawValue) {
81 return rawValue.equals(SKIP);