+ /**
+ * Searches module with name {@code searchedModuleName} in {@code modules}. If module name isn't specified and
+ * module set has only one element then this element is returned.
+ *
+ */
+ public static Module resolveModule(final String searchedModuleName, final Set<Module> modules) {
+ assertNotNull("Modules can't be null.", modules);
+ if (searchedModuleName != null) {
+ for (final Module m : modules) {
+ if (m.getName().equals(searchedModuleName)) {
+ return m;
+ }
+ }
+ } else if (modules.size() == 1) {
+ return modules.iterator().next();
+ }
+ return null;
+ }
+
+ public static DataSchemaNode resolveDataSchemaNode(final String searchedDataSchemaName, final Module module) {
+ assertNotNull("Module can't be null", module);
+
+ if (searchedDataSchemaName != null) {
+ for (final DataSchemaNode dsn : module.getChildNodes()) {
+ if (dsn.getQName().getLocalName().equals(searchedDataSchemaName)) {
+ return dsn;
+ }
+ }
+ } else if (module.getChildNodes().size() == 1) {
+ return module.getChildNodes().iterator().next();
+ }
+ return null;
+ }
+
+ public static QName buildQName(final String name, final String uri, final String date, final String prefix) {
+ try {
+ final URI u = new URI(uri);
+ Date dt = null;
+ if (date != null) {
+ dt = Date.valueOf(date);
+ }
+ return QName.create(u, dt, name);
+ } catch (final URISyntaxException e) {
+ return null;
+ }
+ }
+
+ public static QName buildQName(final String name, final String uri, final String date) {
+ return buildQName(name, uri, date, null);
+ }
+
+ public static QName buildQName(final String name) {
+ return buildQName(name, "", null);
+ }
+
+ public static String loadTextFile(final String filePath) throws IOException {
+ final FileReader fileReader = new FileReader(filePath);
+ final BufferedReader bufReader = new BufferedReader(fileReader);
+
+ String line = null;
+ final StringBuilder result = new StringBuilder();
+ while ((line = bufReader.readLine()) != null) {
+ result.append(line);
+ }
+ bufReader.close();
+ return result.toString();
+ }
+
+ private static Pattern patternForStringsSeparatedByWhiteChars(final String... substrings) {
+ final StringBuilder pattern = new StringBuilder();
+ pattern.append(".*");
+ for (final String substring : substrings) {
+ pattern.append(substring);
+ pattern.append("\\s*");
+ }
+ pattern.append(".*");
+ return Pattern.compile(pattern.toString(), Pattern.DOTALL);
+ }
+
+ public static boolean containsStringData(final String jsonOutput, final String... substrings) {
+ final Pattern pattern = patternForStringsSeparatedByWhiteChars(substrings);
+ final Matcher matcher = pattern.matcher(jsonOutput);
+ return matcher.matches();
+ }
+
+ public static YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(final String localName, final String namespace,
+ final String revision) throws ParseException {
+ return new YangInstanceIdentifier.NodeIdentifier(QName.create(namespace, revision, localName));
+ }
+
+ public static YangInstanceIdentifier.NodeIdentifierWithPredicates getNodeIdentifierPredicate(final String localName,
+ final String namespace, final String revision, final Map<String, Object> keys) throws ParseException {
+ final Map<QName, Object> predicate = new HashMap<>();
+ for (final String key : keys.keySet()) {
+ predicate.put(QName.create(namespace, revision, key), keys.get(key));
+ }
+
+ return new YangInstanceIdentifier.NodeIdentifierWithPredicates(
+
+ QName.create(namespace, revision, localName), predicate);
+ }
+
+ public static YangInstanceIdentifier.NodeIdentifierWithPredicates getNodeIdentifierPredicate(final String localName,
+ final String namespace, final String revision, final String... keysAndValues) throws ParseException {
+ if (keysAndValues.length % 2 != 0) {
+ new IllegalArgumentException("number of keys argument have to be divisible by 2 (map)");
+ }
+ final Map<QName, Object> predicate = new HashMap<>();
+
+ int i = 0;
+ while (i < keysAndValues.length) {
+ predicate.put(QName.create(namespace, revision, keysAndValues[i++]), keysAndValues[i++]);
+ }
+
+ return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(namespace, revision, localName),
+ predicate);
+ }
+
+ static NormalizedNode<?,?> prepareNormalizedNodeWithIetfInterfacesInterfacesData() throws ParseException {
+ final String ietfInterfacesDate = "2013-07-04";
+ final String namespace = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
+ final DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> mapEntryNode = ImmutableMapEntryNodeBuilder.create();
+
+ final Map<String, Object> predicates = new HashMap<>();
+ predicates.put("name", "eth0");
+
+ mapEntryNode.withNodeIdentifier(getNodeIdentifierPredicate("interface", namespace, ietfInterfacesDate,
+ predicates));
+ mapEntryNode
+ .withChild(new ImmutableLeafNodeBuilder<String>()
+ .withNodeIdentifier(getNodeIdentifier("name", namespace, ietfInterfacesDate)).withValue("eth0")
+ .build());
+ mapEntryNode.withChild(new ImmutableLeafNodeBuilder<String>()
+ .withNodeIdentifier(getNodeIdentifier("type", namespace, ietfInterfacesDate))
+ .withValue("ethernetCsmacd").build());
+ mapEntryNode.withChild(new ImmutableLeafNodeBuilder<Boolean>()
+ .withNodeIdentifier(getNodeIdentifier("enabled", namespace, ietfInterfacesDate))
+ .withValue(Boolean.FALSE).build());
+ mapEntryNode.withChild(new ImmutableLeafNodeBuilder<String>()
+ .withNodeIdentifier(getNodeIdentifier("description", namespace, ietfInterfacesDate))
+ .withValue("some interface").build());
+
+ return mapEntryNode.build();
+ }