*/
public final class YangConstants {
/**
- * YANG namespace, as defined in RFC 6020.
+ * YANG File Extension, as defined in https://tools.ietf.org/html/rfc6020#section-14.1.
*/
- public static final URI RFC6020_YANG_NAMESPACE = URI.create("urn:ietf:params:xml:ns:yang:1");
+ public static final String RFC6020_YANG_FILE_EXTENSION = ".yang";
/**
- * YIN namespace, as defined in RFC 6020.
+ * YANG Media Type, as defined in https://tools.ietf.org/html/rfc6020#section-14.1.
*/
- public static final URI RFC6020_YIN_NAMESPACE = URI.create("urn:ietf:params:xml:ns:yang:yin:1");
+ public static final String RFC6020_YANG_MAC_FILE_TYPE = "TEXT";
+
+
+ /**
+ * YANG Media Type, as defined in https://tools.ietf.org/html/rfc6020#section-14.1.
+ */
+ public static final String RFC6020_YANG_MEDIA_TYPE = "application/yang";
+
+ /**
+ * YANG namespace, as defined in https://tools.ietf.org/html/rfc6020#section-14.
+ */
+ public static final URI RFC6020_YANG_NAMESPACE = URI.create("urn:ietf:params:xml:ns:yang:1");
/**
* Base QNameModule for all YANG statements.
*/
public static final QNameModule RFC6020_YANG_MODULE = QNameModule.create(RFC6020_YANG_NAMESPACE, null).intern();
+ /**
+ * YIN File Extension, as defined in https://tools.ietf.org/html/rfc6020#section-14.2.
+ */
+ public static final String RFC6020_YIN_FILE_EXTENSION = ".yin";
+
+ /**
+ * YANG Media Type, as defined in https://tools.ietf.org/html/rfc6020#section-14.1.
+ */
+ public static final String RFC6020_MAC_FILE_TYPE = "TEXT";
+
+ /**
+ * YANG Media Type, as defined in https://tools.ietf.org/html/rfc6020#section-14.1.
+ */
+ public static final String RFC6020_YIN_MEDIA_TYPE = "application/xml+yin";
+
+ /**
+ * YIN namespace, as defined in https://tools.ietf.org/html/rfc6020#section-14.
+ */
+ public static final URI RFC6020_YIN_NAMESPACE = URI.create("urn:ietf:params:xml:ns:yang:yin:1");
+
/**
* Base QNameModule for all YIN statements.
*/
private YangConstants() {
throw new UnsupportedOperationException("Utility class");
}
-
}
--- /dev/null
+/*
+ * Copyright (c) 2016 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.common;
+
+import com.google.common.annotations.Beta;
+import java.util.AbstractMap.SimpleEntry;
+import java.util.Map.Entry;
+
+/**
+ * Utility class for handling various naming conventions mentioned in YANG and related specifications.
+ *
+ * @author Robert Varga
+ */
+@Beta
+public final class YangNames {
+ private YangNames() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * Parse a file name according to rules outlined in https://tools.ietf.org/html/rfc6020#section-5.2. Input string
+ * should be the base path with file extension stripped.
+ *
+ * @param baseName file base name
+ * @return A tuple containing the module name and parsed revision, if present.
+ */
+ public static Entry<String, String> parseFilename(final String baseName) {
+ final int zavinac = baseName.lastIndexOf('@');
+ if (zavinac < 0) {
+ return new SimpleEntry<>(baseName, null);
+ }
+
+ return new SimpleEntry<>(baseName.substring(0, zavinac), baseName.substring(zavinac + 1));
+ }
+}
package org.opendaylight.yangtools.yang.model.repo.api;
import static com.google.common.base.Preconditions.checkArgument;
+
import com.google.common.annotations.Beta;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.io.ByteSource;
import java.io.IOException;
import java.io.InputStream;
+import java.util.Map.Entry;
import org.opendaylight.yangtools.concepts.Delegator;
+import org.opendaylight.yangtools.yang.common.YangConstants;
+import org.opendaylight.yangtools.yang.common.YangNames;
/**
* YANG text schema source representation. Exposes an RFC6020 text representation
}
public static SourceIdentifier identifierFromFilename(final String name) {
- checkArgument(name.endsWith(".yang"), "Filename %s does not have a .yang extension", name);
- // FIXME: add revision-awareness
- return SourceIdentifier.create(name.substring(0, name.length() - 5), Optional.absent());
+ checkArgument(name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION), "Filename %s does not have a .yang extension",
+ name);
+
+ final String baseName = name.substring(0, name.length() - YangConstants.RFC6020_YANG_FILE_EXTENSION.length());
+ final Entry<String, String> parsed = YangNames.parseFilename(baseName);
+ return RevisionSourceIdentifier.create(parsed.getKey(), Optional.fromNullable(parsed.getValue()));
}
/**
import com.google.common.io.ByteSource;
import java.io.IOException;
import java.io.InputStream;
+import java.util.Map.Entry;
import org.opendaylight.yangtools.concepts.Delegator;
+import org.opendaylight.yangtools.yang.common.YangConstants;
+import org.opendaylight.yangtools.yang.common.YangNames;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* YIN text schema source representation. Exposes an RFC6020 XML representation as an {@link InputStream}.
*/
@Beta
public abstract class YinTextSchemaSource extends ByteSource implements YinSchemaSourceRepresentation {
+ private static final Logger LOG = LoggerFactory.getLogger(YinTextSchemaSource.class);
+ private static final String XML_EXTENSION = ".xml";
+
private final SourceIdentifier identifier;
protected YinTextSchemaSource(final SourceIdentifier identifier) {
public static SourceIdentifier identifierFromFilename(final String name) {
final String baseName;
- if (name.endsWith(".xml") || name.endsWith(".yin")) {
- baseName = name.substring(0, name.length() - 4);
+ if (name.endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION)) {
+ baseName = name.substring(0, name.length() - YangConstants.RFC6020_YIN_FILE_EXTENSION.length());
+ } else if (name.endsWith(XML_EXTENSION)) {
+ // FIXME: BUG-7061: remove this once we do not need it
+ LOG.warn("XML file {} being loaded as YIN", name);
+ baseName = name.substring(0, name.length() - XML_EXTENSION.length());
} else {
throw new IllegalArgumentException("Filename " + name + " does not have a .yin or .xml extension");
}
- // FIXME: add revision-awareness
- return SourceIdentifier.create(baseName, Optional.absent());
+ final Entry<String, String> parsed = YangNames.parseFilename(baseName);
+ return RevisionSourceIdentifier.create(parsed.getKey(), Optional.fromNullable(parsed.getValue()));
}
/**