+
+ public static String loadTextFile(String filePath) throws IOException {
+ FileReader fileReader = new FileReader(filePath);
+ BufferedReader bufReader = new BufferedReader(fileReader);
+
+ String line = null;
+ StringBuilder result = new StringBuilder();
+ while ((line = bufReader.readLine()) != null) {
+ result.append(line);
+ }
+ bufReader.close();
+ return result.toString();
+ }
+
+ private static Pattern patternForStringsSeparatedByWhiteChars(String... substrings) {
+ StringBuilder pattern = new StringBuilder();
+ pattern.append(".*");
+ for (String substring : substrings) {
+ pattern.append(substring);
+ pattern.append("\\s*");
+ }
+ pattern.append(".*");
+ return Pattern.compile(pattern.toString(), Pattern.DOTALL);
+ }
+
+ public static boolean containsStringData(String jsonOutput, String... substrings) {
+ Pattern pattern = patternForStringsSeparatedByWhiteChars(substrings);
+ Matcher matcher = pattern.matcher(jsonOutput);
+ return matcher.matches();
+ }
+
+ public static NormalizedNode compositeNodeToDatastoreNormalizedNode(final CompositeNode compositeNode,
+ final DataSchemaNode schema) {
+ List<Node<?>> lst = new ArrayList<Node<?>>();
+ lst.add(compositeNode);
+ if (schema instanceof ContainerSchemaNode) {
+ return CnSnToNormalizedNodeParserFactory.getInstance().getContainerNodeParser()
+ .parse(lst, (ContainerSchemaNode) schema);
+ } else if (schema instanceof ListSchemaNode) {
+ return CnSnToNormalizedNodeParserFactory.getInstance().getMapNodeParser()
+ .parse(lst, (ListSchemaNode) schema);
+ }
+
+ LOG.error("Top level isn't of type container, list, leaf schema node but " + schema.getClass().getSimpleName());
+
+ throw new RestconfDocumentedException(new RestconfError(ErrorType.APPLICATION, ErrorTag.INVALID_VALUE,
+ "It wasn't possible to translate specified data to datastore readable form."));
+ }
+
+ public static YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(String localName, String namespace,
+ String revision) throws ParseException {
+ return new YangInstanceIdentifier.NodeIdentifier(QName.create(namespace, revision, localName));
+ }
+
+ public static YangInstanceIdentifier.NodeIdentifierWithPredicates getNodeIdentifierPredicate(String localName,
+ String namespace, String revision, Map<String, Object> keys) throws ParseException {
+ Map<QName, Object> predicate = new HashMap<>();
+ for (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(String localName,
+ String namespace, String revision, String... keysAndValues) throws ParseException {
+ java.util.Date date = new SimpleDateFormat("yyyy-MM-dd").parse(revision);
+ if (keysAndValues.length % 2 != 0) {
+ new IllegalArgumentException("number of keys argument have to be divisible by 2 (map)");
+ }
+ 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);
+ }
+
+ public static CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
+ CompositeNodeBuilder<ImmutableCompositeNode> interfaceBuilder = ImmutableCompositeNode.builder();
+ interfaceBuilder.addLeaf(buildQName("name", "dummy", "2014-07-29"), "eth0");
+ interfaceBuilder.addLeaf(buildQName("type", "dummy", "2014-07-29"), "ethernetCsmacd");
+ interfaceBuilder.addLeaf(buildQName("enabled", "dummy", "2014-07-29"), "false");
+ interfaceBuilder.addLeaf(buildQName("description", "dummy", "2014-07-29"), "some interface");
+ return interfaceBuilder.toInstance();
+ }
+
+ static NormalizedNode prepareNormalizedNodeWithIetfInterfacesInterfacesData() throws ParseException {
+ String ietfInterfacesDate = "2013-07-04";
+ String namespace = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
+ DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> mapEntryNode = ImmutableMapEntryNodeBuilder.create();
+
+ 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();
+ }