X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fsal%2Fyang-prototype%2Fcode-generator%2Fbinding-generator-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fbinding%2Fyang%2Ftypes%2FUnionDependencySort.java;fp=opendaylight%2Fsal%2Fyang-prototype%2Fcode-generator%2Fbinding-generator-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fbinding%2Fyang%2Ftypes%2FUnionDependencySort.java;h=d19ac3f1cbd311505b2a8ffb1064062f9624c4d7;hb=37ff82351675cc5c279dfe88c6daf10cbbf9f48b;hp=0000000000000000000000000000000000000000;hpb=8398f3adb544427642694be13abe9c3bc1a4e192;p=controller.git diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/UnionDependencySort.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/UnionDependencySort.java new file mode 100644 index 0000000000..d19ac3f1cb --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/UnionDependencySort.java @@ -0,0 +1,161 @@ +/* + * 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.controller.sal.binding.yang.types; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Function; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; +import org.opendaylight.controller.yang.model.api.TypeDefinition; +import org.opendaylight.controller.yang.model.api.type.UnionTypeDefinition; +import org.opendaylight.controller.yang.model.util.ExtendedType; +import org.opendaylight.controller.yang.parser.util.TopologicalSort; +import org.opendaylight.controller.yang.parser.util.TopologicalSort.Node; +import org.opendaylight.controller.yang.parser.util.TopologicalSort.NodeImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class UnionDependencySort { + private static final Logger logger = LoggerFactory + .getLogger(UnionDependencySort.class); + + public static List sort( + final Set> typeDefinitions) { + if (typeDefinitions == null) { + logger.error("Set of Type Definitions cannot be NULL!"); + throw new IllegalArgumentException("Set of Type Definitions " + + "cannot be NULL!"); + } + + final Set extUnionTypes = + unionsFromTypeDefinitions(typeDefinitions); + + final Set unsorted = unionTypesToUnionNodes(extUnionTypes); + + final List sortedNodes = TopologicalSort.sort(unsorted); + return Lists.transform(sortedNodes, new Function() { + @Override + public ExtendedType apply(Node input) { + return ((UnionNode) input).getUnionType(); + } + }); + } + + private static Set unionsFromTypeDefinitions( + final Set> typeDefinitions) { + final Set unions = Sets.newHashSet(); + + for (final TypeDefinition typedef : typeDefinitions) { + if ((typedef != null) && (typedef.getBaseType() != null)) { + if (typedef instanceof ExtendedType + && typedef.getBaseType() instanceof UnionTypeDefinition) { + unions.add((ExtendedType) typedef); + } + } + } + return unions; + } + + private static Set unionTypesToUnionNodes( + final Set extUnionTypes) { + final Map nodeMap = Maps.newHashMap(); + final Set resultNodes = Sets.newHashSet(); + + for (final ExtendedType unionType : extUnionTypes) { + final Node node = new UnionNode(unionType); + nodeMap.put(unionType, node); + resultNodes.add(node); + } + + for (final Node node : resultNodes) { + final UnionNode unionNode = (UnionNode) node; + final ExtendedType extUnionType = unionNode.getUnionType(); + + final UnionTypeDefinition unionType = (UnionTypeDefinition) + extUnionType.getBaseType(); + + final List> innerTypes = unionType.getTypes(); + for (final TypeDefinition typedef : innerTypes) { + if (extUnionTypes.contains(typedef)) { + final Node toNode = nodeMap.get(typedef); + unionNode.addEdge(toNode); + } + } + } + + return resultNodes; + } + + private static UnionNode unionTypeToUnionNode( + final ExtendedType extUnionType, + final Set extUnionTypes) { + final UnionNode node = new UnionNode(extUnionType); + + if (extUnionType.getBaseType() instanceof UnionTypeDefinition) { + final UnionTypeDefinition unionType = (UnionTypeDefinition) + extUnionType.getBaseType(); + + final List> innerTypes = unionType.getTypes(); + for (final TypeDefinition typedef : innerTypes) { + if ((typedef != null) && (typedef instanceof ExtendedType) + && (typedef.getBaseType() instanceof UnionTypeDefinition)) { + if (extUnionTypes.contains(typedef)) { + node.addEdge(new UnionNode((ExtendedType) typedef)); + } + } + } + } + + return node; + } + + @VisibleForTesting + static final class UnionNode extends NodeImpl { + private final ExtendedType unionType; + + UnionNode(ExtendedType unionType) { + this.unionType = unionType; + } + + ExtendedType getUnionType() { + return unionType; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof UnionNode)) { + return false; + } + UnionNode unionNode = (UnionNode) o; + if (!unionType.equals(unionNode.unionType)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + return unionType.hashCode(); + } + + @Override + public String toString() { + return "UnionNode{" + + "unionType=" + unionType + + '}'; + } + } +}