--- /dev/null
+package org.opendaylight.yangtools.concepts;
+
+public interface ValueWrapper<T> {
+
+ T getValue();
+
+}
--- /dev/null
+package org.opendaylight.yangtools.yang.data.api;
+
+import java.util.Map;
+
+import org.opendaylight.yangtools.yang.common.QName;
+
+public interface AttributesContainer {
+
+
+ Map<QName,String> getAttributes();
+
+ Object getAttributeValue(QName value);
+
+}
--- /dev/null
+/*
+ * Copyright (c) 2013 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.yangtools.yang.data.impl;
+
+import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.AttributesContainer;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.ModifyAction;
+import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
+import org.opendaylight.yangtools.yang.data.api.Node;
+import org.opendaylight.yangtools.yang.data.api.SimpleNode;
+import org.opendaylight.yangtools.yang.data.impl.util.AbstractCompositeNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.util.NodeBuilder;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public final class ImmutableCompositeNode extends AbstractNodeTO<List<Node<?>>> implements //
+ Immutable, //
+ CompositeNode, //
+ AttributesContainer, //
+ Serializable {
+
+ private static final long serialVersionUID = 100L;
+
+ private Map<QName, List<Node<?>>> nodeMap = new HashMap<>();
+
+ private Map<QName, String> attributes;
+
+
+
+ /**
+ * @param qname
+ * @param parent
+ * use null to create top composite node (without parent)
+ * @param value
+ */
+ private ImmutableCompositeNode(QName qname, Map<QName,String> attributes,List<Node<?>> value) {
+ super(qname, null, ImmutableList.copyOf(value));
+ attributes = ImmutableMap.copyOf(attributes);
+ init();
+ }
+
+ /**
+ * @param qname
+ * @param parent
+ * use null to create top composite node (without parent)
+ * @param value
+ */
+ private ImmutableCompositeNode(QName qname, List<Node<?>> value, QName a1, String av) {
+ super(qname, null, value);
+ attributes = ImmutableMap.of(a1, av);
+ init();
+ }
+
+ /**
+ * @param qname
+ * @param parent
+ * use null to create top composite node (without parent)
+ * @param value
+ * @param modifyAction
+ */
+ public ImmutableCompositeNode(QName qname, List<Node<?>> value, ModifyAction modifyAction) {
+ super(qname, null, value, modifyAction);
+ init();
+ }
+
+ protected void init() {
+ if (getValue() != null) {
+ nodeMap = NodeUtils.buildNodeMap(getValue());
+ }
+ }
+
+ protected Map<QName, List<Node<?>>> getNodeMap() {
+ return nodeMap;
+ }
+
+ @Override
+ public List<Node<?>> getChildren() {
+ return Collections.unmodifiableList(getValue() == null ? new ArrayList<Node<?>>() : getValue());
+ }
+
+ @Override
+ public SimpleNode<?> getFirstSimpleByName(QName leafQName) {
+ List<SimpleNode<?>> list = getSimpleNodesByName(leafQName);
+ if (list.isEmpty()) {
+ return null;
+ }
+ return list.get(0);
+ }
+
+ @Override
+ public List<CompositeNode> getCompositesByName(QName children) {
+ List<Node<?>> toFilter = getNodeMap().get(children);
+ if (toFilter == null) {
+ return Collections.emptyList();
+ }
+ List<CompositeNode> list = new ArrayList<CompositeNode>();
+ for (Node<?> node : toFilter) {
+ if (node instanceof CompositeNode) {
+ list.add((CompositeNode) node);
+ }
+ }
+ return list;
+ }
+
+ @Override
+ public List<SimpleNode<?>> getSimpleNodesByName(QName children) {
+ List<Node<?>> toFilter = getNodeMap().get(children);
+ if (toFilter == null) {
+ return Collections.emptyList();
+ }
+ List<SimpleNode<?>> list = new ArrayList<SimpleNode<?>>();
+
+ for (Node<?> node : toFilter) {
+ if (node instanceof SimpleNode<?>) {
+ list.add((SimpleNode<?>) node);
+ }
+ }
+ return list;
+ }
+
+ @Override
+ public CompositeNode getFirstCompositeByName(QName container) {
+ List<CompositeNode> list = getCompositesByName(container);
+ if (list.isEmpty()) {
+ return null;
+ }
+ return list.get(0);
+ }
+
+ @Override
+ public Map<QName, String> getAttributes() {
+ return attributes;
+ }
+
+ @Override
+ public String getAttributeValue(QName key) {
+ return attributes.get(key);
+ }
+
+ /**
+ * @param leaf
+ * @return TODO:: do we need this method?
+ */
+ public SimpleNode<?> getFirstLeafByName(QName leaf) {
+ List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
+ if (list.isEmpty()) {
+ return null;
+ }
+ return list.get(0);
+ }
+
+ @Override
+ public List<CompositeNode> getCompositesByName(String children) {
+ return getCompositesByName(new QName(getNodeType(), children));
+ }
+
+ @Override
+ public List<SimpleNode<?>> getSimpleNodesByName(String children) {
+ return getSimpleNodesByName(new QName(getNodeType(), children));
+ }
+
+ @Override
+ public MutableCompositeNode asMutable() {
+ throw new IllegalAccessError("cast to mutable is not supported - " + getClass().getSimpleName());
+ }
+
+ @Override
+ public String toString() {
+ return super.toString() + ", children.size = " + (getChildren() != null ? getChildren().size() : "n/a");
+ }
+
+ @Override
+ public void clear() {
+ nodeMap.clear();
+ }
+
+ @Override
+ public boolean containsKey(Object key) {
+ return nodeMap.containsKey(key);
+ }
+
+ @Override
+ public boolean containsValue(Object value) {
+ return nodeMap.containsValue(value);
+ }
+
+ @Override
+ public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
+ return nodeMap.entrySet();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return super.equals(obj);
+ }
+
+ @Override
+ public int size() {
+ return nodeMap.size();
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return nodeMap.isEmpty();
+ }
+
+ @Override
+ public List<Node<?>> get(Object key) {
+ return nodeMap.get(key);
+ }
+
+ @Override
+ public List<Node<?>> put(QName key, List<Node<?>> value) {
+ return nodeMap.put(key, value);
+ }
+
+ @Override
+ public List<Node<?>> remove(Object key) {
+ return nodeMap.remove(key);
+ }
+
+ @Override
+ public void putAll(Map<? extends QName, ? extends List<Node<?>>> m) {
+ nodeMap.putAll(m);
+ }
+
+ @Override
+ public Set<QName> keySet() {
+ return nodeMap.keySet();
+ }
+
+ @Override
+ public Collection<List<Node<?>>> values() {
+ return nodeMap.values();
+ }
+
+ // Serialization related
+
+ private void readObject(ObjectInputStream aStream) throws IOException, ClassNotFoundException {
+ aStream.defaultReadObject();
+ QName qName = (QName) aStream.readObject();
+ CompositeNode parent = (CompositeNode) aStream.readObject();
+ List<Node<?>> value = (List<Node<?>>) aStream.readObject();
+ ModifyAction modifyAction = (ModifyAction) aStream.readObject();
+
+ init(qName, parent, value, modifyAction);
+ }
+
+ private void writeObject(ObjectOutputStream aStream) throws IOException {
+ aStream.defaultWriteObject();
+ // manually serialize superclass
+ aStream.writeObject(getQName());
+ aStream.writeObject(getParent());
+ aStream.writeObject(getValue());
+ aStream.writeObject(getModificationAction());
+ }
+
+ public static CompositeNodeBuilder<ImmutableCompositeNode> builder() {
+ return new ImmutableCompositeNodeBuilder();
+ }
+
+ private static class ImmutableCompositeNodeBuilder extends AbstractCompositeNodeBuilder<ImmutableCompositeNode> {
+
+ @Override
+ public AbstractCompositeNodeBuilder<ImmutableCompositeNode> addLeaf(QName leafName, Object leafValue) {
+ add(new SimpleNodeTOImpl<Object>(leafName, null, leafValue));
+ return this;
+ }
+
+ @Override
+ public ImmutableCompositeNode toInstance() {
+ return ImmutableCompositeNode.create(this.getQName(), this.getAttributes(), this.getChildNodes());
+ }
+
+ }
+
+ public static ImmutableCompositeNode create(QName qName, List<Node<?>> childNodes) {
+ return new ImmutableCompositeNode(qName, ImmutableMap.<QName, String>of(),childNodes);
+ }
+
+ public static ImmutableCompositeNode create(QName qName, Map<QName, String> attributes, List<Node<?>> childNodes) {
+ return new ImmutableCompositeNode(qName, attributes,childNodes);
+ }
+}
--- /dev/null
+package org.opendaylight.yangtools.yang.data.impl.util;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.Node;
+
+import com.google.common.collect.Iterables;
+
+import static com.google.common.base.Preconditions.*;
+
+public abstract class AbstractCompositeNodeBuilder<P extends CompositeNode> //
+ extends AbstractNodeBuilder<P, CompositeNodeBuilder<P>> //
+ implements CompositeNodeBuilder<P> {
+
+ final List<Node<?>> childNodes;
+
+ public AbstractCompositeNodeBuilder() {
+ super();
+ childNodes = new ArrayList<>();
+ }
+
+ public AbstractCompositeNodeBuilder(QName nodeType, Map<QName, String> attributes) {
+ super(nodeType, attributes);
+ childNodes = new ArrayList<>();
+ }
+
+ public AbstractCompositeNodeBuilder(QName nodeType, Iterable<? extends Node<?>> nodes) {
+ super(nodeType);
+ childNodes = new ArrayList<>();
+ }
+
+ @Override
+ public AbstractCompositeNodeBuilder<P> add(Node<?> node) {
+ childNodes.add(checkNotNull(node, "Node should not be null"));
+ return this;
+ }
+
+ @Override
+ public AbstractCompositeNodeBuilder<P> addAll(Iterable<? extends Node<?>> nodes) {
+ Iterables.addAll(childNodes, checkNotNull(nodes, "Node should not be null"));
+ return this;
+ }
+
+ @Override
+ public CompositeNodeBuilder<P> addLeaf(String leafLocalName, String leafValue) {
+ return addLeaf(QName.create(getQName(), leafLocalName), leafValue);
+ }
+
+ public List<Node<?>> getChildNodes() {
+ return childNodes;
+ }
+}
--- /dev/null
+package org.opendaylight.yangtools.yang.data.impl.util;
+
+import java.net.URI;
+import java.util.Date;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.Node;
+
+public abstract class AbstractNodeBuilder<P extends Node<?>, T extends NodeBuilder<P, T>> implements NodeBuilder<P, T> {
+
+ private final Map<QName, String> attributes;
+ private QName qName;
+
+ public AbstractNodeBuilder() {
+ this.attributes = new ConcurrentHashMap<>();
+ }
+
+ public AbstractNodeBuilder(QName nodeType, Map<QName, String> attributes) {
+ super();
+ this.qName = nodeType;
+ this.attributes = new ConcurrentHashMap<>(attributes);
+ }
+
+ public AbstractNodeBuilder(QName nodeType) {
+ this.qName = nodeType;
+ this.attributes = new ConcurrentHashMap<>();
+ }
+
+ @SuppressWarnings("unchecked")
+ protected final T thisInstance() {
+ return (T) this;
+ }
+
+ @Override
+ public final T setQName(QName name) {
+ this.qName = name;
+ return thisInstance();
+ }
+
+ public QName getQName() {
+ return qName;
+ }
+
+ @Override
+ public final T setAttribute(QName attrName, String attrValue) {
+ attributes.put(attrName, attrValue);
+ return thisInstance();
+ }
+
+ public Map<QName, String> getAttributes() {
+ return attributes;
+ }
+
+ @Override
+ public T setAttribute(String attrName, String attrValue) {
+ attributes.put(QName.create(qName, attrName), attrValue);
+ return thisInstance();
+ }
+
+}
--- /dev/null
+package org.opendaylight.yangtools.yang.data.impl.util;
+
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.Node;
+import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
+
+public interface CompositeNodeBuilder<P extends CompositeNode> extends NodeBuilder<P,CompositeNodeBuilder<P>> {
+
+ CompositeNodeBuilder<P> addLeaf(QName leafName,Object leafValue);
+ CompositeNodeBuilder<P> add(Node<?> node);
+ CompositeNodeBuilder<P> addAll(Iterable<? extends Node<?>> nodes);
+ CompositeNodeBuilder<P> addLeaf(String leafLocalName, String leafValue);
+}
--- /dev/null
+package org.opendaylight.yangtools.yang.data.impl.util;
+
+import java.net.URI;
+import java.util.Date;
+
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.Node;
+
+public interface NodeBuilder<P extends Node<?>, T extends NodeBuilder<P, T>> extends Builder<P> {
+
+ T setQName(QName name);
+
+ QName getQName();
+
+ T setAttribute(QName attrName, String attrValue);
+
+ T setAttribute(String attrName, String attrValue);
+
+}
--- /dev/null
+package org.opendaylight.yangtools.yang.model.util.repo;
+
+import org.opendaylight.yangtools.concepts.Delegator;
+import org.opendaylight.yangtools.concepts.Registration;
+
+import com.google.common.base.Optional;
+
+import static com.google.common.base.Preconditions.*;
+
+public abstract class AbstractCachingSchemaSourceProvider<I, O> implements SchemaSourceProvider<O>,
+ Delegator<SchemaSourceProvider<I>> {
+
+ private final SchemaSourceProvider<I> defaultDelegate;
+
+ protected AbstractCachingSchemaSourceProvider(SchemaSourceProvider<I> delegate) {
+ this.defaultDelegate = delegate;
+ }
+
+ @Override
+ public Optional<O> getSchemaSource(String moduleName, Optional<String> revision) {
+ return getSchemaSourceImpl(moduleName, revision, defaultDelegate);
+ }
+
+ private Optional<O> getSchemaSourceImpl(String moduleName, Optional<String> revision,
+ SchemaSourceProvider<I> delegate) {
+ checkNotNull(moduleName, "Module name should not be null.");
+ checkNotNull(revision, "Revision should not be null");
+
+ Optional<O> cached = getCachedSchemaSource(moduleName, revision);
+ if (cached.isPresent()) {
+ return cached;
+ }
+ Optional<I> live = delegate.getSchemaSource(moduleName, revision);
+ return cacheSchemaSource(moduleName, revision, live);
+ }
+
+ abstract protected Optional<O> cacheSchemaSource(String moduleName, Optional<String> revision, Optional<I> stream);
+
+ abstract protected Optional<O> getCachedSchemaSource(String moduleName, Optional<String> revision);
+
+ public SchemaSourceProvider<I> getDelegate() {
+ return defaultDelegate;
+ }
+
+ public SchemaSourceProvider<O> createInstanceFor(SchemaSourceProvider<I> delegate) {
+ checkNotNull(delegate, "Delegate should not be null");
+ return new SchemaSourceProviderInstance(delegate);
+ }
+
+ private class SchemaSourceProviderInstance implements SchemaSourceProvider<O>, Delegator<SchemaSourceProvider<I>> {
+
+ private final SchemaSourceProvider<I> delegate;
+
+ protected SchemaSourceProviderInstance(SchemaSourceProvider<I> delegate) {
+ super();
+ this.delegate = delegate;
+ }
+
+ @Override
+ public Optional<O> getSchemaSource(String moduleName, Optional<String> revision) {
+ return getSchemaSourceImpl(moduleName, revision, getDelegate());
+ }
+
+ @Override
+ public SchemaSourceProvider<I> getDelegate() {
+ return delegate;
+ }
+ }
+}
--- /dev/null
+package org.opendaylight.yangtools.yang.model.util.repo;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.io.StringBufferInputStream;
+
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+
+public class FilesystemSchemaCachingProvider<I> extends AbstractCachingSchemaSourceProvider<I, InputStream> {
+
+ private final File storageDirectory;
+ private final Function<I, String> transformationFunction;
+
+ public FilesystemSchemaCachingProvider(SchemaSourceProvider<I> delegate, File directory,
+ Function<I, String> transformationFunction) {
+ super(delegate);
+ this.storageDirectory = directory;
+ this.transformationFunction = transformationFunction;
+ }
+
+ @Override
+ protected synchronized Optional<InputStream> cacheSchemaSource(String moduleName, Optional<String> revision, Optional<I> source) {
+ File schemaFile = toFile(moduleName, revision);
+ try {
+ if(source.isPresent() && schemaFile.createNewFile()) {
+ try (
+ FileOutputStream outStream = new FileOutputStream(schemaFile);
+ OutputStreamWriter writer = new OutputStreamWriter(outStream);
+ ) {
+ writer.write(transformToString(source.get()));
+ writer.flush();
+ } catch (IOException e) {
+
+ }
+ }
+ } catch (IOException e){
+
+ }
+ return transformToStream(source);
+ }
+
+ @SuppressWarnings("deprecation")
+ private Optional<InputStream> transformToStream(Optional<I> source) {
+ if(source.isPresent()) {
+ return Optional.<InputStream>of(new StringBufferInputStream(transformToString(source.get())));
+ }
+ return Optional.absent();
+ }
+
+ private String transformToString(I input) {
+ return transformationFunction.apply(input);
+ }
+
+ @Override
+ protected Optional<InputStream> getCachedSchemaSource(String moduleName, Optional<String> revision) {
+ File inputFile = toFile(moduleName, revision);
+ try {
+ if (inputFile.exists() && inputFile.canRead()) {
+ InputStream stream = new FileInputStream(inputFile);
+ return Optional.of(stream);
+ }
+ } catch (FileNotFoundException e) {
+ return Optional.absent();
+ }
+ return Optional.absent();
+ }
+
+ private File toFile(String moduleName, Optional<String> revision) {
+ return new File(storageDirectory, toYangFileName(moduleName, revision));
+ }
+
+ public static final String toYangFileName(String moduleName, Optional<String> revision) {
+ StringBuilder filename = new StringBuilder(moduleName);
+ if (revision.isPresent()) {
+ filename.append("@");
+ filename.append(revision.get());
+ }
+ filename.append(".yang");
+ return filename.toString();
+ }
+
+ private static final Function<String, String> NOOP_TRANSFORMATION = new Function<String, String>() {
+ @Override
+ public String apply(String input) {
+ return input;
+ }
+ };
+
+ public static FilesystemSchemaCachingProvider<String> createFromStringSourceProvider(
+ SchemaSourceProvider<String> liveProvider, File directory) {
+ Preconditions.checkNotNull(liveProvider);
+ Preconditions.checkNotNull(directory);
+ directory.mkdirs();
+ return new FilesystemSchemaCachingProvider<String>(liveProvider, directory,NOOP_TRANSFORMATION);
+ }
+}
--- /dev/null
+package org.opendaylight.yangtools.yang.model.util.repo;
+
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+public interface SchemaService {
+
+
+ SchemaContext getSchemaContext();
+}
--- /dev/null
+package org.opendaylight.yangtools.yang.model.util.repo;
+
+import java.io.InputStream;
+
+import com.google.common.base.Optional;
+
+public interface SchemaSourceProvider<F> {
+
+ Optional<F> getSchemaSource(String moduleName, Optional<String> revision);
+
+}
--- /dev/null
+package org.opendaylight.yangtools.yang.model.util.repo;
+
+import com.google.common.base.Optional;
+
+public class SchemaSourceProviders {
+
+ @SuppressWarnings("rawtypes")
+ private static final SchemaSourceProvider NOOP_PROVIDER = new SchemaSourceProvider() {
+
+ @Override
+ public Optional getSchemaSource(String moduleName, Optional revision) {
+ return Optional.absent();
+ }
+
+ };
+
+ @SuppressWarnings("unchecked")
+ public static <T> SchemaSourceProvider<T> noopProvider() {
+ return (SchemaSourceProvider<T>) NOOP_PROVIDER;
+ }
+
+}