Merge "Kill Dynamic Actors when we're done with them"
[controller.git] / opendaylight / netconf / netconf-cli / src / main / java / org / opendaylight / controller / netconf / cli / io / IOUtil.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.io;
9
10 import java.text.ParseException;
11 import java.text.SimpleDateFormat;
12 import java.util.Date;
13 import java.util.Map;
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;
22
23 public class IOUtil {
24
25     public static final String SKIP = "skip";
26     public static final String PROMPT_SUFIX = ">";
27     public static final String PATH_SEPARATOR = "/";
28
29     private IOUtil() {
30     }
31
32     public static boolean isQName(final String qName) {
33         final Matcher matcher = patternNew.matcher(qName);
34         return matcher.matches();
35     }
36
37     public static Date parseDate(final String revision) {
38         final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
39         try {
40             return formatter.parse(revision);
41         } catch (final ParseException e) {
42             throw new IllegalArgumentException("Date not valid", e);
43         }
44     }
45
46     public static String listType(final SchemaNode schemaNode) {
47         if (schemaNode instanceof LeafListSchemaNode) {
48             return "Leaf-list";
49         } else if (schemaNode instanceof ListSchemaNode) {
50             return "List";
51         } else if (schemaNode instanceof LeafSchemaNode) {
52             return "Leaf";
53         }
54         // FIXME throw exception on unexpected state, not null/emptyString
55         return "";
56     }
57
58     public static String qNameToKeyString(final QName qName, final String moduleName) {
59         return String.format("%s(%s)", qName.getLocalName(), moduleName);
60     }
61
62     // TODO test and check regex + review format of string for QName
63     final static Pattern patternNew = Pattern.compile("([^\\)]+)\\(([^\\)]+)\\)");
64
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);
71         }
72         final QName base = mappedModules.get(matcher.group(2));
73         if (base == null) {
74             final String message = String.format("Module %s cannot be found", matcher.group(2));
75             throw new ReadingException(message);
76         }
77         return QName.create(base, matcher.group(1));
78     }
79
80     public static boolean isSkipInput(final String rawValue) {
81         return rawValue.equals(SKIP);
82     }
83
84 }