+++ /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.mdsal.binding.yang.types;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import org.opendaylight.yangtools.util.TopologicalSort;
-import org.opendaylight.yangtools.util.TopologicalSort.Node;
-import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
-import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
-import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
-import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.UsesNode;
-
-public final class GroupingDefinitionDependencySort {
- private GroupingDefinitionDependencySort() {
- // Hidden on purpose
- }
-
- /**
- * Sorts a set of {@code groupings} according to the mutual dependencies. Elements of {@code groupings} are first
- * transformed to {@link Node} interfaces and then are sorted by {@link TopologicalSort#sort(Set) sort()} method of
- * {@code TopologicalSort}.<br>
- *
- * <i>Definition of dependency relation:<br>
- * The first {@code GroupingDefinition} object (in this context) depends on second {@code GroupingDefinition} object
- * if the first one contains in its set of {@code UsesNode} (obtained through {@link DataNodeContainer#getUses()})
- * a reference to the second one.
- * </i>
- *
- * @param groupings set of grouping definition which should be sorted according to mutual dependencies
- * @return list of grouping definitions which are sorted by mutual dependencies
- * @throws IllegalArgumentException if {@code groupingDefinitions}
- *
- */
- public static List<GroupingDefinition> sort(final Collection<? extends GroupingDefinition> groupings) {
- if (groupings == null) {
- throw new IllegalArgumentException("Set of Type Definitions cannot be NULL!");
- }
-
- final List<Node> sortedNodes = TopologicalSort.sort(groupingDefinitionsToNodes(groupings));
- final List<GroupingDefinition> resultGroupingDefinitions = new ArrayList<>(sortedNodes.size());
- for (Node node : sortedNodes) {
- NodeWrappedType nodeWrappedType = (NodeWrappedType) node;
- resultGroupingDefinitions.add((GroupingDefinition) nodeWrappedType.getWrappedType());
- }
- return resultGroupingDefinitions;
- }
-
- /**
- * Wraps every grouping definition to node type and adds to every node information about dependencies. The map
- * with mapping from schema path (represents grouping definition) to node is created. For every created node
- * (next <i>nodeFrom</i>) is for its wrapped grouping definition passed the set of its <i>uses nodes</i> through.
- * For every uses node is found its wrapping node (next as <i>nodeTo</i>). This dependency relationship between
- * nodeFrom and all found nodesTo is modeled with creating of one edge from nodeFrom to nodeTo.
- *
- * @param groupings set of grouping definitions which will be wrapped to nodes
- * @return set of nodes where every one contains wrapped grouping definition
- */
- private static Set<Node> groupingDefinitionsToNodes(final Collection<? extends GroupingDefinition> groupings) {
- final Map<GroupingDefinition, Node> nodeMap = new HashMap<>();
- final Set<Node> resultNodes = new HashSet<>();
-
- for (final GroupingDefinition grouping : groupings) {
- final Node node = new NodeWrappedType(grouping);
- nodeMap.put(grouping, node);
- resultNodes.add(node);
- }
-
- for (final Node node : resultNodes) {
- final NodeWrappedType nodeWrappedType = (NodeWrappedType) node;
- final GroupingDefinition grouping = (GroupingDefinition) nodeWrappedType.getWrappedType();
-
- Set<UsesNode> usesNodes = getAllUsesNodes(grouping);
-
- for (UsesNode usesNode : usesNodes) {
- Node nodeTo = nodeMap.get(usesNode.getSourceGrouping());
- if (nodeTo != null) {
- nodeWrappedType.addEdge(nodeTo);
- }
- }
- }
-
- return resultNodes;
- }
-
- /**
- * Returns the set of the uses nodes which are get from uses in <code>container</code>, from uses in groupings
- * inside <code>container</code> and from uses inside child nodes of the <code>container</code>.
- *
- * @param container data node container which can contain some uses of grouping
- * @return set of uses nodes which were find in <code>container</code>.
- */
- private static Set<UsesNode> getAllUsesNodes(final DataNodeContainer container) {
- Set<UsesNode> ret = new HashSet<>();
- Collection<? extends UsesNode> usesNodes = container.getUses();
- ret.addAll(usesNodes);
-
- for (UsesNode usesNode : usesNodes) {
- for (AugmentationSchemaNode augment : usesNode.getAugmentations()) {
- ret.addAll(getAllUsesNodes(augment));
- }
- }
- for (GroupingDefinition groupingDefinition : container.getGroupings()) {
- ret.addAll(getAllUsesNodes(groupingDefinition));
- }
- for (DataSchemaNode childNode : container.getChildNodes()) {
- if (childNode instanceof DataNodeContainer) {
- ret.addAll(getAllUsesNodes((DataNodeContainer) childNode));
- } else if (childNode instanceof ChoiceSchemaNode) {
- for (CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) childNode).getCases()) {
- ret.addAll(getAllUsesNodes(choiceCaseNode));
- }
- }
- }
- if (container instanceof ActionNodeContainer) {
- for (ActionDefinition action : ((ActionNodeContainer) container).getActions()) {
- ret.addAll(getAllUsesNodes(action.getInput()));
- ret.addAll(getAllUsesNodes(action.getOutput()));
- }
- }
- if (container instanceof NotificationNodeContainer) {
- for (NotificationDefinition notification : ((NotificationNodeContainer) container).getNotifications()) {
- ret.addAll(getAllUsesNodes(notification));
- }
- }
-
- return ret;
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2014 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.mdsal.binding.yang.types;
-
-import org.opendaylight.yangtools.util.TopologicalSort.NodeImpl;
-
-final class NodeWrappedType extends NodeImpl {
- /**
- * The payload which is saved inside Node.
- */
- private final Object wrappedType;
-
- /**
- * Create new instance of class <code>NodeWrappedType</code>.
- *
- * @param wrappedType object with payload data
- */
- NodeWrappedType(final Object wrappedType) {
- this.wrappedType = wrappedType;
- }
-
- /**
- * Gets payload from class.
- *
- * @return object with <code>wrappedType</code>
- */
- Object getWrappedType() {
- return wrappedType;
- }
-
- @Override
- public boolean equals(final Object obj) {
- if (this == obj) {
- return true;
- }
- if (!(obj instanceof NodeWrappedType)) {
- return false;
- }
- NodeWrappedType nodeWrappedType = (NodeWrappedType) obj;
- return wrappedType.equals(nodeWrappedType.wrappedType);
- }
-
- @Override
- public int hashCode() {
- return wrappedType.hashCode();
- }
-
- @Override
- public String toString() {
- return "NodeWrappedType{" + "wrappedType=" + wrappedType + '}';
- }
-}
+++ /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.mdsal.binding.yang.types;
\ No newline at end of file
+++ /dev/null
-/*
- * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.mdsal.binding.generator.impl;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-
-import com.google.common.collect.Iterables;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-import org.junit.Test;
-import org.opendaylight.mdsal.binding.model.api.GeneratedType;
-import org.opendaylight.mdsal.binding.yang.types.GroupingDefinitionDependencySort;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-
-public class Mdsal448Test {
- @Test
- public void groupingSortIncludesActions() {
- final EffectiveModelContext context = YangParserTestUtils.parseYangResource("/mdsal448.yang");
- final Collection<? extends GroupingDefinition> groupings = context.findModule("mdsal448").get().getGroupings();
- assertEquals(2, groupings.size());
-
- final List<GroupingDefinition> ordered = sortGroupings(Iterables.get(groupings, 0),
- Iterables.get(groupings, 1));
- assertEquals(2, ordered.size());
- // "the-grouping" needs to be first
- assertEquals("the-grouping", ordered.get(0).getQName().getLocalName());
- assertEquals("action-grouping", ordered.get(1).getQName().getLocalName());
-
- // Sort needs to be stable
- final List<GroupingDefinition> reverse = sortGroupings(Iterables.get(groupings, 1),
- Iterables.get(groupings, 0));
- assertEquals(ordered, reverse);
-
- final List<GeneratedType> types = DefaultBindingGenerator.generateFor(context);
- assertNotNull(types);
- assertEquals(9, types.size());
- }
-
- private static List<GroupingDefinition> sortGroupings(final GroupingDefinition... groupings) {
- return GroupingDefinitionDependencySort.sort(Arrays.asList(groupings));
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2014 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.mdsal.binding.yang.types;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertThrows;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import org.junit.Test;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-
-public class GroupingDefinitionDependencySortTest {
- @Test
- public void testSortMethod() {
-
- final List<GroupingDefinition> unsortedGroupingDefs = new ArrayList<>();
-
- GroupingDefinition grp1 = mock(GroupingDefinition.class);
- doReturn(SchemaPath.create(false, QName.create("", "Cont1"), QName.create("", "Cont2"))).when(grp1).getPath();
- doReturn(QName.create("", "leaf1")).when(grp1).getQName();
- doReturn(Collections.emptySet()).when(grp1).getUses();
- doReturn(Collections.emptySet()).when(grp1).getGroupings();
- doReturn(Collections.emptySet()).when(grp1).getChildNodes();
- doReturn(Collections.emptySet()).when(grp1).getActions();
- doReturn(Collections.emptySet()).when(grp1).getNotifications();
-
- GroupingDefinition grp2 = mock(GroupingDefinition.class);
- doReturn(SchemaPath.create(false, QName.create("", "Cont1"))).when(grp2).getPath();
- doReturn(QName.create("", "leaf2")).when(grp2).getQName();
- doReturn(Collections.emptySet()).when(grp2).getUses();
- doReturn(Collections.emptySet()).when(grp2).getGroupings();
- doReturn(Collections.emptySet()).when(grp2).getChildNodes();
- doReturn(Collections.emptySet()).when(grp2).getActions();
- doReturn(Collections.emptySet()).when(grp2).getNotifications();
-
- GroupingDefinition grp3 = mock(GroupingDefinition.class);
- doReturn(SchemaPath.create(false, QName.create("", "Cont1"), QName.create("", "Cont2"))).when(grp3).getPath();
- doReturn(QName.create("", "leaf3")).when(grp3).getQName();
- doReturn(Collections.emptySet()).when(grp3).getUses();
- doReturn(Collections.emptySet()).when(grp3).getGroupings();
- doReturn(Collections.emptySet()).when(grp3).getChildNodes();
- doReturn(Collections.emptySet()).when(grp3).getActions();
- doReturn(Collections.emptySet()).when(grp3).getNotifications();
-
- GroupingDefinition grp4 = mock(GroupingDefinition.class);
- doReturn(SchemaPath.create(false, QName.create("", "Cont1"), QName.create("", "Cont2"),
- QName.create("", "List1"))).when(grp4).getPath();
- doReturn(QName.create("", "leaf4")).when(grp4).getQName();
- doReturn(Collections.emptySet()).when(grp4).getUses();
- doReturn(Collections.emptySet()).when(grp4).getGroupings();
- doReturn(Collections.emptySet()).when(grp4).getChildNodes();
- doReturn(Collections.emptySet()).when(grp4).getActions();
- doReturn(Collections.emptySet()).when(grp4).getNotifications();
-
- GroupingDefinition grp5 = mock(GroupingDefinition.class);
- doReturn(SchemaPath.create(false, QName.create("", "Cont1"))).when(grp5).getPath();
- doReturn(QName.create("", "leaf5")).when(grp5).getQName();
- doReturn(Collections.emptySet()).when(grp5).getUses();
- doReturn(Collections.emptySet()).when(grp5).getGroupings();
- doReturn(Collections.emptySet()).when(grp5).getChildNodes();
- doReturn(Collections.emptySet()).when(grp5).getActions();
- doReturn(Collections.emptySet()).when(grp5).getNotifications();
-
- unsortedGroupingDefs.add(grp1);
- unsortedGroupingDefs.add(grp1);
- unsortedGroupingDefs.add(grp2);
- unsortedGroupingDefs.add(grp3);
- unsortedGroupingDefs.add(grp4);
- unsortedGroupingDefs.add(grp5);
-
- List<GroupingDefinition> sortedGroupingDefs = GroupingDefinitionDependencySort.sort(unsortedGroupingDefs);
- assertNotNull(sortedGroupingDefs);
- }
-
- @Test
- public void testNullSort() {
- final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
- () -> GroupingDefinitionDependencySort.sort(null));
- assertEquals("Set of Type Definitions cannot be NULL!", ex.getMessage());
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2014 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.mdsal.binding.yang.types;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-import org.junit.Test;
-
-public class NodeWrappedTypeTest {
-
- @Test
- public void test() {
- final NodeWrappedType nwt1 = new NodeWrappedType("obj1");
- final NodeWrappedType nwt2 = new NodeWrappedType("obj2");
- final NodeWrappedType nwt3 = new NodeWrappedType("obj1");
- final String str = "obj3";
-
- assertTrue("Node nwt1 should equal to itself.", nwt1.equals(nwt1));
- assertFalse("It can't be possible to compare nwt with string.", nwt1.equals(str));
- assertFalse("nwt1 shouldn't equal to nwt2.", nwt1.equals(nwt2));
- assertTrue("Node nwt1 should equal to nwt3.", nwt1.equals(nwt3));
-
- assertEquals("toString method is returning incorrect value.", "NodeWrappedType{wrappedType=obj1}",
- nwt1.toString());
- }
-}
+++ /dev/null
-module mdsal448 {
- yang-version "1.1";
- namespace "urn:example:test";
- prefix "test";
-
- grouping the-grouping {
- leaf the-leaf {
- type string;
- }
- }
-
- grouping action-grouping {
- action action-with-grouping {
- input {
- leaf leaf1 {
- type string;
- }
-
- uses the-grouping;
- }
- }
- }
-
- container network {
- list node {
- key "id";
-
- leaf id {
- type string;
- }
-
- uses action-grouping;
- }
- }
-}