*/
package org.opendaylight.yangtools.yang.model.api;
+import com.google.common.base.Preconditions;
import java.util.List;
/**
* definition.
*/
enum Deviate {
- NOT_SUPPORTED, ADD, REPLACE, DELETE
+ NOT_SUPPORTED("not-supported"), ADD("add"), REPLACE("replace"), DELETE("delete");
+
+ private final String keyword;
+
+ private Deviate(final String keyword) {
+ this.keyword = Preconditions.checkNotNull(keyword);
+ }
+
+ /**
+ * @return String that corresponds to the yang keyword.
+ */
+ public String getKeyword() {
+ return keyword;
+ }
}
/**
public StatementContextBase build() throws SourceException {
StatementContextBase<?, ?, ?> potential = null;
- if (getDefinition().getPublicView() != Rfc6020Mapping.AUGMENT) {
+ StatementDefinition stmtDef = getDefinition().getPublicView();
+ if (stmtDef != Rfc6020Mapping.AUGMENT && stmtDef != Rfc6020Mapping.DEVIATION) {
potential = substatements.get(createIdentifier());
}
if (potential == null) {
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Iterables;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
-import java.util.Objects;
+import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
import org.opendaylight.yangtools.yang.model.api.Deviation;
+import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
import org.opendaylight.yangtools.yang.model.api.Status;
private static final Splitter SPACE_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
private static final Pattern PATH_ABS = Pattern.compile("/[^/].*");
+ private static final Map<String, Deviate> KEYWORD_TO_DEVIATE_MAP;
+ static {
+ Builder<String, Deviate> keywordToDeviateMapBuilder = ImmutableMap.builder();
+ for (Deviate deviate : Deviation.Deviate.values()) {
+ keywordToDeviateMapBuilder.put(deviate.getKeyword(), deviate);
+ }
+ KEYWORD_TO_DEVIATE_MAP = keywordToDeviateMapBuilder.build();
+ }
+
private static final ThreadLocal<XPathFactory> XPATH_FACTORY = new ThreadLocal<XPathFactory>() {
@Override
protected XPathFactory initialValue() {
.isAssignableFrom(UnknownStatementImpl.class);
}
- public static Deviation.Deviate parseDeviateFromString(final String deviate) {
-
- // Yang constants should be lowercase so we have throw if value does not
- // suit this
- String deviateUpper = deviate.toUpperCase();
- Preconditions.checkArgument(!Objects.equals(deviate, deviateUpper),
- "String %s is not valid deviate argument", deviate);
-
- // but Java enum is uppercase so we cannot use lowercase here
- try {
- return Deviation.Deviate.valueOf(deviateUpper);
- } catch (IllegalArgumentException e) {
- throw new IllegalArgumentException(String.format("String %s is not valid deviate argument", deviate), e);
- }
+ public static Deviation.Deviate parseDeviateFromString(final String deviateKeyword) {
+ return Preconditions.checkNotNull(KEYWORD_TO_DEVIATE_MAP.get(deviateKeyword),
+ "String '%s' is not valid deviate argument", deviateKeyword);
}
public static Status parseStatus(final String value) {
--- /dev/null
+/*
+ * Copyright (c) 2015 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.stmt.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+import java.io.FileNotFoundException;
+import java.net.URISyntaxException;
+import java.util.Set;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.api.Deviation;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+
+public class Bug4933Test {
+
+ @Test
+ public void test() throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
+ SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug4933/correct");
+ assertNotNull(context);
+
+ Set<Deviation> deviations = context.getModules().iterator().next().getDeviations();
+ assertEquals(4, deviations.size());
+ }
+
+ @Test
+ public void incorrectKeywordTest() throws SourceException, ReactorException, FileNotFoundException,
+ URISyntaxException {
+ try {
+ StmtTestUtils.parseYangSources("/bugs/bug4933/incorrect");
+ fail("NullPointerException should be thrown.");
+ } catch (NullPointerException e) {
+ assertEquals("String 'not_supported' is not valid deviate argument", e.getMessage());
+ }
+ }
+}
--- /dev/null
+module foo {
+ yang-version 1;
+ namespace "foo";
+ prefix foo;
+
+ container my-container {
+ leaf-list my-leaf-list {
+ type string;
+ max-elements 5;
+ must "0 != 1";
+ }
+ }
+
+ deviation /my-container/my-leaf-list {
+ deviate add {
+ min-elements 1;
+ }
+ }
+
+ deviation /my-container/my-leaf-list {
+ deviate replace {
+ max-elements 10;
+ }
+ }
+
+ deviation /my-container/my-leaf-list {
+ deviate delete {
+ must "0 != 1";
+ }
+ }
+
+ container my-container2 {
+ }
+
+ deviation /my-container2 {
+ deviate not-supported;
+ }
+}
--- /dev/null
+module foo {
+ yang-version 1;
+ namespace "foo";
+ prefix foo;
+
+ container my-container {
+ leaf-list my-leaf-list {
+ type string;
+ max-elements 5;
+ must "0 != 1";
+ }
+ }
+
+ deviation /my-container/my-leaf-list {
+ deviate add {
+ min-elements 1;
+ }
+ }
+
+ deviation /my-container/my-leaf-list {
+ deviate replace {
+ max-elements 10;
+ }
+ }
+
+ deviation /my-container/my-leaf-list {
+ deviate delete {
+ must "0 != 1";
+ }
+ }
+
+ container my-container2 {
+ }
+
+ deviation /my-container2 {
+ deviate not_supported;
+ }
+}