package org.opendaylight.controller.topologymanager.internal;
-import org.junit.Assert;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentMap;
import org.junit.After;
+import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.controller.sal.core.Bandwidth;
import org.opendaylight.controller.switchmanager.SwitchConfig;
import org.opendaylight.controller.topologymanager.TopologyUserLinkConfig;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ConcurrentMap;
-
public class TopologyManagerImplTest {
private TopologyManagerImpl topoManagerImpl;
}
Assert.assertTrue(topoManagerImpl.flushUpdateQueue(5000));
- // Give TopologyManger time to update its edges DB.
- Thread.sleep(1000);
- Assert.assertEquals(1, topoManagerImpl.getEdges().size());
+ verifyEdgesSize(1);
Assert.assertNotNull(topoManagerImpl.getEdges().get(edge));
}
+ private void verifyEdgesSize(int expSize) throws InterruptedException {
+ int timeout = 5000;
+ for(int i = 0; i < timeout / 50; i++) {
+ if(topoManagerImpl.getEdges().size() == expSize) {
+ return;
+ }
+
+ Thread.sleep(50);
+ }
+
+ Assert.fail(String.format("Expected edges size %d. Actual was %d",
+ topoManagerImpl.getEdges().size(), expSize));
+ }
+
@Test
- public void testNotifyNodeConnector() throws ConstructionException,
- InterruptedException {
+ public void testNotifyNodeConnector() throws Exception {
TestSwitchManager swMgr = new TestSwitchManager();
topoManagerImpl.setSwitchManager(swMgr);
topoManagerImpl.nonClusterObjectCreate();
Map<String, Property> propMap = new HashMap<>();
swMgr.addNodeConnectors(nc1);
topoManagerImpl.notifyNodeConnector(nc1, UpdateType.ADDED, propMap);
- Assert.assertEquals(0, topoManagerImpl.getEdges().size());
+ verifyEdgesSize(0);
topoManagerImpl.notifyNodeConnector(nc1, UpdateType.CHANGED, propMap);
- Assert.assertEquals(0, topoManagerImpl.getEdges().size());
+ verifyEdgesSize(0);
swMgr.clear();
topoManagerImpl.notifyNodeConnector(nc1, UpdateType.REMOVED, propMap);
- Assert.assertEquals(0, topoManagerImpl.getEdges().size());
+ verifyEdgesSize(0);
// Test NodeConnector notification in the case that there is a related
// edge update just before the notification.
swMgr.addNodeConnectors(nc2);
topoManagerImpl.notifyNodeConnector(nc2, UpdateType.CHANGED, propMap);
Assert.assertTrue(topoManagerImpl.flushUpdateQueue(5000));
- // Give TopologyManger time to update its edges DB.
- Thread.sleep(1000);
- Assert.assertEquals(2, topoManagerImpl.getEdges().size());
+ verifyEdgesSize(2);
teu1 = new TopoEdgeUpdate(edge1, props, UpdateType.REMOVED);
teu2 = new TopoEdgeUpdate(edge2, props, UpdateType.REMOVED);
topoedgeupdateList.add(teu2);
topoManagerImpl.edgeUpdate(topoedgeupdateList);
Assert.assertTrue(topoManagerImpl.flushUpdateQueue(5000));
- // Give TopologyManger time to update its edges DB.
- Thread.sleep(1000);
- Assert.assertEquals(0, topoManagerImpl.getEdges().size());
+ verifyEdgesSize(0);
topoManagerImpl.notifyNodeConnector(nc1, UpdateType.REMOVED, propMap);
topoManagerImpl.notifyNodeConnector(nc2, UpdateType.REMOVED, propMap);
swMgr.addNodeConnectors(nc2);
topoManagerImpl.notifyNodeConnector(nc2, UpdateType.CHANGED, propMap);
Assert.assertTrue(topoManagerImpl.flushUpdateQueue(5000));
- // Give TopologyManger time to update its edges DB.
- Thread.sleep(1000);
- Assert.assertEquals(0, topoManagerImpl.getEdges().size());
+ verifyEdgesSize(0);
topoManagerImpl.notifyNodeConnector(nc1, UpdateType.REMOVED, propMap);
topoManagerImpl.notifyNodeConnector(nc2, UpdateType.REMOVED, propMap);
Assert.assertTrue(topoManagerImpl.flushUpdateQueue(5000));
- // Give TopologyManger time to update its edges DB.
- Thread.sleep(1000);
- Assert.assertEquals(0, topoManagerImpl.getEdges().size());
+ verifyEdgesSize(0);
}
}
+++ /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.controller.sal.connect.netconf.util;
-
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
-
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.FluentIterable;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Iterables;
-import com.google.common.collect.Lists;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import javax.xml.transform.dom.DOMSource;
-import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationException;
-import org.opendaylight.yangtools.concepts.Identifiable;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.ModifyAction;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
-import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
-import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
-import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.AttributesBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
-import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
-import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
-import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
-import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-
-/**
- * Transforms an instance of yang instance identifier to a filter like structure in normalized node format. Can be also used to nest the edit-config rpc content.
- * For each argument of the id, a specific normalized node is created to ensure schema context conformance.
- */
-public abstract class InstanceIdToNodes<T extends PathArgument> implements Identifiable<T> {
-
- private final T identifier;
-
- @Override
- public T getIdentifier() {
- return identifier;
- }
-
- protected InstanceIdToNodes(final T identifier) {
- this.identifier = identifier;
- }
-
- abstract InstanceIdToNodes<?> getChild(final PathArgument child) throws DataNormalizationException;
-
- public abstract NormalizedNode<?, ?> create(YangInstanceIdentifier legacyData, Optional<NormalizedNode<?, ?>> deepestChild, Optional<ModifyAction> operation);
-
- private static abstract class SimpleTypeNormalization<T extends PathArgument> extends InstanceIdToNodes<T> {
-
- protected SimpleTypeNormalization(final T identifier) {
- super(identifier);
- }
-
- @Override
- public NormalizedNode<?, ?> create(final YangInstanceIdentifier id, final Optional<NormalizedNode<?, ?>> deepestChild, final Optional<ModifyAction> operation) {
- checkNotNull(id);
- final PathArgument pathArgument = Iterables.get(id.getPathArguments(), 0);
- final NormalizedNodeAttrBuilder<? extends PathArgument, Object, ? extends NormalizedNode<? extends PathArgument, Object>> builder = getBuilder(pathArgument);
-
- if(deepestChild.isPresent()) {
- builder.withValue(deepestChild.get().getValue());
- }
-
- addModifyOpIfPresent(operation, builder);
- return builder.build();
- }
-
- protected abstract NormalizedNodeAttrBuilder<? extends PathArgument, Object, ? extends NormalizedNode<? extends PathArgument, Object>> getBuilder(PathArgument node);
-
- @Override
- public InstanceIdToNodes<?> getChild(final PathArgument child) {
- return null;
- }
- }
-
-
- public void addModifyOpIfPresent(final Optional<ModifyAction> operation, final AttributesBuilder<?> builder) {
- if(operation.isPresent()) {
- builder.withAttributes(Collections.singletonMap(NetconfMessageTransformUtil.NETCONF_OPERATION_QNAME, NetconfMessageTransformUtil.modifyOperationToXmlString(operation.get())));
- }
- }
-
- private static final class LeafNormalization extends SimpleTypeNormalization<NodeIdentifier> {
-
- protected LeafNormalization(final LeafSchemaNode potential) {
- super(new NodeIdentifier(potential.getQName()));
- }
-
- @Override
- protected NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> getBuilder(final PathArgument node) {
- return Builders.leafBuilder().withNodeIdentifier(getIdentifier());
- }
- }
-
- private static final class LeafListEntryNormalization extends SimpleTypeNormalization<NodeWithValue> {
-
- public LeafListEntryNormalization(final LeafListSchemaNode potential) {
- super(new NodeWithValue(potential.getQName(), null));
- }
-
- @Override
- protected NormalizedNodeAttrBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> getBuilder(final PathArgument node) {
- Preconditions.checkArgument(node instanceof NodeWithValue);
- return Builders.leafSetEntryBuilder().withNodeIdentifier((NodeWithValue) node).withValue(((NodeWithValue) node).getValue());
- }
-
- }
-
- private static abstract class CompositeNodeNormalizationOperation<T extends PathArgument> extends
- InstanceIdToNodes<T> {
-
- protected CompositeNodeNormalizationOperation(final T identifier) {
- super(identifier);
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public final NormalizedNode<?, ?> create(final YangInstanceIdentifier id, final Optional<NormalizedNode<?, ?>> lastChild, final Optional<ModifyAction> operation) {
- checkNotNull(id);
- final Iterator<PathArgument> iterator = id.getPathArguments().iterator();
- final PathArgument legacyData = iterator.next();
-
- if (!isMixin(this) && getIdentifier().getNodeType() != null) {
- checkArgument(getIdentifier().getNodeType().equals(legacyData.getNodeType()),
- "Node QName must be %s was %s", getIdentifier().getNodeType(), legacyData.getNodeType());
- }
- final NormalizedNodeContainerBuilder builder = createBuilder(legacyData);
-
- if (iterator.hasNext()) {
- final PathArgument childPath = iterator.next();
- final InstanceIdToNodes childOp = getChildOperation(childPath);
-
- final YangInstanceIdentifier childId = YangInstanceIdentifier.create(Iterables.skip(id.getPathArguments(), 1));
- builder.addChild(childOp.create(childId, lastChild, operation));
- } else {
- if(lastChild.isPresent()) {
- builder.withValue(Lists.newArrayList((Collection<?>) lastChild.get().getValue()));
- }
- if(operation.isPresent()) {
- Preconditions.checkArgument(builder instanceof AttributesBuilder<?>);
- addModifyOpIfPresent(operation, ((AttributesBuilder<?>) builder));
- }
- }
-
- return builder.build();
- }
-
- private InstanceIdToNodes getChildOperation(final PathArgument childPath) {
- final InstanceIdToNodes childOp;
- try {
- childOp = getChild(childPath);
- } catch (final DataNormalizationException e) {
- throw new IllegalArgumentException(String.format("Failed to process child node %s", childPath), e);
- }
- checkArgument(childOp != null, "Node %s is not allowed inside %s", childPath, getIdentifier());
- return childOp;
- }
-
- @SuppressWarnings("rawtypes")
- protected abstract NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final PathArgument compositeNode);
- }
-
- static boolean isMixin(final InstanceIdToNodes<?> op) {
- return op instanceof MixinNormalizationOp;
- }
-
- private static abstract class DataContainerNormalizationOperation<T extends PathArgument> extends
- CompositeNodeNormalizationOperation<T> {
-
- private final DataNodeContainer schema;
- private final Map<PathArgument, InstanceIdToNodes<?>> byArg;
-
- protected DataContainerNormalizationOperation(final T identifier, final DataNodeContainer schema) {
- super(identifier);
- this.schema = schema;
- this.byArg = new ConcurrentHashMap<>();
- }
-
- @Override
- public InstanceIdToNodes<?> getChild(final PathArgument child) throws DataNormalizationException {
- InstanceIdToNodes<?> potential = byArg.get(child);
- if (potential != null) {
- return potential;
- }
- potential = fromLocalSchema(child);
- return register(potential);
- }
-
- private InstanceIdToNodes<?> fromLocalSchema(final PathArgument child) throws DataNormalizationException {
- if (child instanceof AugmentationIdentifier) {
- return fromSchemaAndQNameChecked(schema, ((AugmentationIdentifier) child).getPossibleChildNames()
- .iterator().next());
- }
- return fromSchemaAndQNameChecked(schema, child.getNodeType());
- }
-
- private InstanceIdToNodes<?> register(final InstanceIdToNodes<?> potential) {
- if (potential != null) {
- byArg.put(potential.getIdentifier(), potential);
- }
- return potential;
- }
- }
-
- private static final class ListItemNormalization extends
- DataContainerNormalizationOperation<NodeIdentifierWithPredicates> {
-
- protected ListItemNormalization(final NodeIdentifierWithPredicates identifier, final ListSchemaNode schema) {
- super(identifier, schema);
- }
-
- @Override
- protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final PathArgument currentArg) {
- final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = Builders
- .mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) currentArg);
- for (final Entry<QName, Object> keyValue : ((NodeIdentifierWithPredicates) currentArg).getKeyValues().entrySet()) {
- builder.addChild(Builders.leafBuilder()
- //
- .withNodeIdentifier(new NodeIdentifier(keyValue.getKey())).withValue(keyValue.getValue())
- .build());
- }
- return builder;
- }
-
- }
-
- private static final class UnkeyedListItemNormalization extends DataContainerNormalizationOperation<NodeIdentifier> {
-
- protected UnkeyedListItemNormalization(final ListSchemaNode schema) {
- super(new NodeIdentifier(schema.getQName()), schema);
- }
-
- @Override
- protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final PathArgument compositeNode) {
- return Builders.unkeyedListEntryBuilder().withNodeIdentifier(getIdentifier());
- }
-
- }
-
- private static final class ContainerTransformation extends DataContainerNormalizationOperation<NodeIdentifier> {
-
- protected ContainerTransformation(final ContainerSchemaNode schema) {
- super(new NodeIdentifier(schema.getQName()), schema);
- }
-
- @Override
- protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final PathArgument compositeNode) {
- return Builders.containerBuilder().withNodeIdentifier(getIdentifier());
- }
- }
-
- /**
- * Marker interface for Mixin nodes normalization operations
- */
- private interface MixinNormalizationOp {}
-
-
- private static final class OrderedLeafListMixinNormalization extends UnorderedLeafListMixinNormalization {
-
-
- public OrderedLeafListMixinNormalization(final LeafListSchemaNode potential) {
- super(potential);
- }
-
- @Override
- protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final PathArgument compositeNode) {
- return Builders.orderedLeafSetBuilder().withNodeIdentifier(getIdentifier());
- }
- }
-
- private static class UnorderedLeafListMixinNormalization extends CompositeNodeNormalizationOperation<NodeIdentifier> implements MixinNormalizationOp {
-
- private final InstanceIdToNodes<?> innerOp;
-
- public UnorderedLeafListMixinNormalization(final LeafListSchemaNode potential) {
- super(new NodeIdentifier(potential.getQName()));
- innerOp = new LeafListEntryNormalization(potential);
- }
-
- @Override
- protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final PathArgument compositeNode) {
- return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier());
- }
-
- @Override
- public InstanceIdToNodes<?> getChild(final PathArgument child) {
- if (child instanceof NodeWithValue) {
- return innerOp;
- }
- return null;
- }
- }
-
- private static final class AugmentationNormalization extends DataContainerNormalizationOperation<AugmentationIdentifier> implements MixinNormalizationOp {
-
- public AugmentationNormalization(final AugmentationSchema augmentation, final DataNodeContainer schema) {
- //super();
- super(augmentationIdentifierFrom(augmentation), augmentationProxy(augmentation, schema));
- }
-
- @Override
- protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final PathArgument compositeNode) {
- return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier());
- }
- }
-
- private static class UnorderedMapMixinNormalization extends CompositeNodeNormalizationOperation<NodeIdentifier> implements MixinNormalizationOp {
-
- private final ListItemNormalization innerNode;
-
- public UnorderedMapMixinNormalization(final ListSchemaNode list) {
- super(new NodeIdentifier(list.getQName()));
- this.innerNode = new ListItemNormalization(new NodeIdentifierWithPredicates(list.getQName(),
- Collections.<QName, Object>emptyMap()), list);
- }
-
- @Override
- protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final PathArgument compositeNode) {
- return Builders.mapBuilder().withNodeIdentifier(getIdentifier());
- }
-
- @Override
- public InstanceIdToNodes<?> getChild(final PathArgument child) {
- if (child.getNodeType().equals(getIdentifier().getNodeType())) {
- return innerNode;
- }
- return null;
- }
- }
-
- private static class UnkeyedListMixinNormalization extends CompositeNodeNormalizationOperation<NodeIdentifier> implements MixinNormalizationOp {
-
- private final UnkeyedListItemNormalization innerNode;
-
- public UnkeyedListMixinNormalization(final ListSchemaNode list) {
- super(new NodeIdentifier(list.getQName()));
- this.innerNode = new UnkeyedListItemNormalization(list);
- }
-
- @Override
- protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final PathArgument compositeNode) {
- return Builders.unkeyedListBuilder().withNodeIdentifier(getIdentifier());
- }
-
- @Override
- public InstanceIdToNodes<?> getChild(final PathArgument child) {
- if (child.getNodeType().equals(getIdentifier().getNodeType())) {
- return innerNode;
- }
- return null;
- }
-
- }
-
- private static final class OrderedMapMixinNormalization extends UnorderedMapMixinNormalization {
-
- public OrderedMapMixinNormalization(final ListSchemaNode list) {
- super(list);
- }
-
- @Override
- protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final PathArgument compositeNode) {
- return Builders.orderedMapBuilder().withNodeIdentifier(getIdentifier());
- }
-
- }
-
- private static class ChoiceNodeNormalization extends CompositeNodeNormalizationOperation<NodeIdentifier> implements MixinNormalizationOp {
-
- private final ImmutableMap<PathArgument, InstanceIdToNodes<?>> byArg;
-
- protected ChoiceNodeNormalization(final ChoiceSchemaNode schema) {
- super(new NodeIdentifier(schema.getQName()));
- final ImmutableMap.Builder<PathArgument, InstanceIdToNodes<?>> byArgBuilder = ImmutableMap.builder();
-
- for (final ChoiceCaseNode caze : schema.getCases()) {
- for (final DataSchemaNode cazeChild : caze.getChildNodes()) {
- final InstanceIdToNodes<?> childOp = fromDataSchemaNode(cazeChild);
- byArgBuilder.put(childOp.getIdentifier(), childOp);
- }
- }
- byArg = byArgBuilder.build();
- }
-
- @Override
- public InstanceIdToNodes<?> getChild(final PathArgument child) {
- return byArg.get(child);
- }
-
- @Override
- protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final PathArgument compositeNode) {
- return Builders.choiceBuilder().withNodeIdentifier(getIdentifier());
- }
- }
-
- private static class AnyXmlNormalization extends InstanceIdToNodes<NodeIdentifier> {
-
- protected AnyXmlNormalization(final AnyXmlSchemaNode schema) {
- super(new NodeIdentifier(schema.getQName()));
- }
-
- @Override
- public InstanceIdToNodes<?> getChild(final PathArgument child) throws DataNormalizationException {
- return null;
- }
-
- @Override
- public NormalizedNode<?, ?> create(final YangInstanceIdentifier legacyData, final Optional<NormalizedNode<?, ?>> deepestChild, final Optional<ModifyAction> operation) {
- if(deepestChild.isPresent()) {
- Preconditions.checkState(deepestChild instanceof AnyXmlNode);
- final NormalizedNodeAttrBuilder<NodeIdentifier, DOMSource, AnyXmlNode> anyXmlBuilder =
- Builders.anyXmlBuilder().withNodeIdentifier(getIdentifier()).withValue(((AnyXmlNode) deepestChild).getValue());
- addModifyOpIfPresent(operation, anyXmlBuilder);
- return anyXmlBuilder.build();
- }
-
- final NormalizedNodeAttrBuilder<NodeIdentifier, DOMSource, AnyXmlNode> builder =
- Builders.anyXmlBuilder().withNodeIdentifier(getIdentifier());
- addModifyOpIfPresent(operation, builder);
- return builder.build();
- }
-
- }
-
- private static Optional<DataSchemaNode> findChildSchemaNode(final DataNodeContainer parent, final QName child) {
- DataSchemaNode potential = parent.getDataChildByName(child);
- if (potential == null) {
- final Iterable<ChoiceSchemaNode> choices = FluentIterable.from(parent.getChildNodes()).filter(ChoiceSchemaNode.class);
- potential = findChoice(choices, child);
- }
- return Optional.fromNullable(potential);
- }
-
- private static InstanceIdToNodes<?> fromSchemaAndQNameChecked(final DataNodeContainer schema, final QName child) throws DataNormalizationException {
- final Optional<DataSchemaNode> potential = findChildSchemaNode(schema, child);
- if (!potential.isPresent()) {
- throw new DataNormalizationException(String.format("Supplied QName %s is not valid according to schema %s, potential children nodes: %s", child, schema, schema.getChildNodes()));
- }
-
- final DataSchemaNode result = potential.get();
- // We try to look up if this node was added by augmentation
- if ((schema instanceof DataSchemaNode) && result.isAugmenting()) {
- return fromAugmentation(schema, (AugmentationTarget) schema, result);
- }
- return fromDataSchemaNode(result);
- }
-
- private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) {
- org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode foundChoice = null;
- choiceLoop:
- for (final ChoiceSchemaNode choice : choices) {
- for (final ChoiceCaseNode caze : choice.getCases()) {
- if (findChildSchemaNode(caze, child).isPresent()) {
- foundChoice = choice;
- break choiceLoop;
- }
- }
- }
- return foundChoice;
- }
-
- private static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchema augmentation) {
- final ImmutableSet.Builder<QName> potentialChildren = ImmutableSet.builder();
- for (final DataSchemaNode child : augmentation.getChildNodes()) {
- potentialChildren.add(child.getQName());
- }
- return new AugmentationIdentifier(potentialChildren.build());
- }
-
- private static DataNodeContainer augmentationProxy(final AugmentationSchema augmentation, final DataNodeContainer schema) {
- final Set<DataSchemaNode> children = new HashSet<>();
- for (final DataSchemaNode augNode : augmentation.getChildNodes()) {
- children.add(schema.getDataChildByName(augNode.getQName()));
- }
- return new NodeContainerProxy(null, children);
- }
-
- /**
- * Returns a SchemaPathUtil for provided child node
- * <p/>
- * If supplied child is added by Augmentation this operation returns
- * a SchemaPathUtil for augmentation,
- * otherwise returns a SchemaPathUtil for child as
- * call for {@link #fromDataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode)}.
- */
- private static InstanceIdToNodes<?> fromAugmentation(final DataNodeContainer parent,
- final AugmentationTarget parentAug, final DataSchemaNode child) {
- AugmentationSchema augmentation = null;
- for (final AugmentationSchema aug : parentAug.getAvailableAugmentations()) {
- final DataSchemaNode potential = aug.getDataChildByName(child.getQName());
- if (potential != null) {
- augmentation = aug;
- break;
- }
-
- }
- if (augmentation != null) {
- return new AugmentationNormalization(augmentation, parent);
- } else {
- return fromDataSchemaNode(child);
- }
- }
-
- private static InstanceIdToNodes<?> fromDataSchemaNode(final DataSchemaNode potential) {
- if (potential instanceof ContainerSchemaNode) {
- return new ContainerTransformation((ContainerSchemaNode) potential);
- } else if (potential instanceof ListSchemaNode) {
- return fromListSchemaNode((ListSchemaNode) potential);
- } else if (potential instanceof LeafSchemaNode) {
- return new LeafNormalization((LeafSchemaNode) potential);
- } else if (potential instanceof ChoiceSchemaNode) {
- return new ChoiceNodeNormalization((ChoiceSchemaNode) potential);
- } else if (potential instanceof LeafListSchemaNode) {
- return fromLeafListSchemaNode((LeafListSchemaNode) potential);
- } else if (potential instanceof AnyXmlSchemaNode) {
- return new AnyXmlNormalization((AnyXmlSchemaNode) potential);
- }
- return null;
- }
-
- private static InstanceIdToNodes<?> fromListSchemaNode(final ListSchemaNode potential) {
- final List<QName> keyDefinition = potential.getKeyDefinition();
- if (keyDefinition == null || keyDefinition.isEmpty()) {
- return new UnkeyedListMixinNormalization(potential);
- }
- if (potential.isUserOrdered()) {
- return new OrderedMapMixinNormalization(potential);
- }
- return new UnorderedMapMixinNormalization(potential);
- }
-
- private static InstanceIdToNodes<?> fromLeafListSchemaNode(final LeafListSchemaNode potential) {
- if (potential.isUserOrdered()) {
- return new OrderedLeafListMixinNormalization(potential);
- }
- return new UnorderedLeafListMixinNormalization(potential);
- }
-
- public static NormalizedNode<?, ?> serialize(final SchemaContext ctx, final YangInstanceIdentifier id) {
- return serialize(ctx, id, Optional.<NormalizedNode<?, ?>>absent(), Optional.<ModifyAction>absent());
- }
-
- public static NormalizedNode<?, ?> serialize(final SchemaContext ctx, final YangInstanceIdentifier id, final NormalizedNode<?, ?> deepestElement) {
- return serialize(ctx, id, Optional.<NormalizedNode<?, ?>>of(deepestElement), Optional.<ModifyAction>absent());
- }
-
- public static NormalizedNode<?, ?> serialize(final SchemaContext ctx, final YangInstanceIdentifier id, final Optional<NormalizedNode<?, ?>> deepestElement, final Optional<ModifyAction> operation) {
- Preconditions.checkNotNull(ctx);
- Preconditions.checkNotNull(id);
- final PathArgument topLevelElement = id.getPathArguments().iterator().next();
- final DataSchemaNode dataChildByName = ctx.getDataChildByName(topLevelElement.getNodeType());
- Preconditions.checkNotNull(dataChildByName, "Cannot find %s node in schema context. Instance identifier has to start from root", topLevelElement);
- try {
- final InstanceIdToNodes<?> instanceIdToNodes = fromSchemaAndQNameChecked(ctx, topLevelElement.getNodeType());
- return instanceIdToNodes.create(id, deepestElement, operation);
- } catch (final DataNormalizationException e) {
- throw new IllegalArgumentException("Unable to serialize: " + id, e);
- }
- }
-}
+++ /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.controller.sal.connect.netconf.util;
-
-import static org.junit.Assert.assertEquals;
-
-import com.google.common.base.Function;
-import com.google.common.collect.Collections2;
-import com.google.common.io.ByteSource;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Collections;
-import org.junit.Before;
-import org.junit.Test;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
-import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
-import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
-import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
-
-public class InstanceIdToNodesTest {
-
- private static final String NS = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:normalization:test";
- private static final String REVISION = "2014-03-13";
- private static final QName ID = QName.create(NS, REVISION, "id");
- private SchemaContext ctx;
-
- private final YangInstanceIdentifier.NodeIdentifier rootContainer = new YangInstanceIdentifier.NodeIdentifier(QName.create(NS, REVISION, "test"));
- private final YangInstanceIdentifier.NodeIdentifier outerContainer = new YangInstanceIdentifier.NodeIdentifier(QName.create(NS, REVISION, "outer-container"));
- private final YangInstanceIdentifier.NodeIdentifier augmentedLeaf = new YangInstanceIdentifier.NodeIdentifier(QName.create(NS, REVISION, "augmented-leaf"));
- private final YangInstanceIdentifier.AugmentationIdentifier augmentation = new YangInstanceIdentifier.AugmentationIdentifier(Collections.singleton(augmentedLeaf.getNodeType()));
-
- private final YangInstanceIdentifier.NodeIdentifier outerList = new YangInstanceIdentifier.NodeIdentifier(QName.create(NS, REVISION, "outer-list"));
- private final YangInstanceIdentifier.NodeIdentifierWithPredicates outerListWithKey = new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(NS, REVISION, "outer-list"), ID, 1);
- private final YangInstanceIdentifier.NodeIdentifier choice = new YangInstanceIdentifier.NodeIdentifier(QName.create(NS, REVISION, "outer-choice"));
- private final YangInstanceIdentifier.NodeIdentifier leafFromCase = new YangInstanceIdentifier.NodeIdentifier(QName.create(NS, REVISION, "one"));
-
- private final YangInstanceIdentifier.NodeIdentifier leafList = new YangInstanceIdentifier.NodeIdentifier(QName.create(NS, REVISION, "ordered-leaf-list"));
- private final YangInstanceIdentifier.NodeWithValue leafListWithValue = new YangInstanceIdentifier.NodeWithValue(leafList.getNodeType(), "abcd");
-
- static SchemaContext createTestContext() throws IOException, YangSyntaxErrorException {
- final YangParserImpl parser = new YangParserImpl();
- return parser.parseSources(Collections2.transform(Collections.singletonList("/schemas/filter-test.yang"), new Function<String, ByteSource>() {
- @Override
- public ByteSource apply(final String input) {
- return new ByteSource() {
- @Override
- public InputStream openStream() throws IOException {
- return InstanceIdToNodesTest.class.getResourceAsStream(input);
- }
- };
- }
- }));
- }
-
- @Before
- public void setUp() throws Exception {
- ctx = createTestContext();
-
- }
-
- @Test
- public void testInAugment() throws Exception {
- final ContainerNode expectedFilter = Builders.containerBuilder().withNodeIdentifier(rootContainer).withChild(
- Builders.containerBuilder().withNodeIdentifier(outerContainer).withChild(
- Builders.augmentationBuilder().withNodeIdentifier(augmentation).withChild(
- Builders.leafBuilder().withNodeIdentifier(augmentedLeaf).build()
- ).build()
- ).build()
- ).build();
-
- final NormalizedNode<?, ?> filter = InstanceIdToNodes.serialize(ctx, YangInstanceIdentifier.create(rootContainer, outerContainer, augmentation, augmentedLeaf));
- assertEquals(expectedFilter, filter);
- }
-
- @Test
- public void testInAugmentLeafOverride() throws Exception {
- final LeafNode<Object> lastLeaf = Builders.leafBuilder().withNodeIdentifier(augmentedLeaf).withValue("randomValue").build();
-
- final ContainerNode expectedFilter = Builders.containerBuilder().withNodeIdentifier(rootContainer).withChild(
- Builders.containerBuilder().withNodeIdentifier(outerContainer).withChild(
- Builders.augmentationBuilder().withNodeIdentifier(augmentation).withChild(
- lastLeaf
- ).build()
- ).build()
- ).build();
-
- final NormalizedNode<?, ?> filter = InstanceIdToNodes.serialize(ctx, YangInstanceIdentifier.create(rootContainer, outerContainer, augmentation, augmentedLeaf), lastLeaf);
- assertEquals(expectedFilter, filter);
- }
-
- @Test
- public void testListChoice() throws Exception {
- final ContainerNode expectedFilter = Builders.containerBuilder().withNodeIdentifier(rootContainer).withChild(
- Builders.mapBuilder().withNodeIdentifier(outerList).withChild(
- Builders.mapEntryBuilder().withNodeIdentifier(outerListWithKey).withChild(
- Builders.leafBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(ID)).withValue(1).build()
- ).withChild(
- Builders.choiceBuilder().withNodeIdentifier(choice).withChild(
- Builders.leafBuilder().withNodeIdentifier(leafFromCase).build()
- ).build()
- ).build()
- ).build()
- ).build();
-
- final NormalizedNode<?, ?> filter = InstanceIdToNodes.serialize(ctx, YangInstanceIdentifier.create(rootContainer, outerList, outerListWithKey, choice, leafFromCase));
- assertEquals(expectedFilter, filter);
- }
-
- @Test
- public void testTopContainerLastChildOverride() throws Exception {
- final ContainerNode expectedStructure = Builders.containerBuilder().withNodeIdentifier(rootContainer).withChild(
- Builders.mapBuilder().withNodeIdentifier(outerList).withChild(
- Builders.mapEntryBuilder().withNodeIdentifier(outerListWithKey).withChild(
- Builders.leafBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(ID)).withValue(1).build()
- ).withChild(
- Builders.choiceBuilder().withNodeIdentifier(choice).withChild(
- Builders.leafBuilder().withNodeIdentifier(leafFromCase).build()
- ).build()
- ).build()
- ).build()
- ).build();
-
- final NormalizedNode<?, ?> filter = InstanceIdToNodes.serialize(ctx, YangInstanceIdentifier.create(rootContainer), expectedStructure);
- assertEquals(expectedStructure, filter);
- }
-
- @Test
- public void testListLastChildOverride() throws Exception {
- final MapEntryNode outerListEntry = Builders.mapEntryBuilder().withNodeIdentifier(outerListWithKey).withChild(
- Builders.leafBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(ID)).withValue(1).build()
- ).build();
- final MapNode lastChild = Builders.mapBuilder().withNodeIdentifier(this.outerList).withChild(
- outerListEntry
- ).build();
- final ContainerNode expectedStructure = Builders.containerBuilder().withNodeIdentifier(rootContainer).withChild(
- lastChild
- ).build();
-
- NormalizedNode<?, ?> filter = InstanceIdToNodes.serialize(ctx, YangInstanceIdentifier.create(rootContainer, outerList, outerListWithKey), outerListEntry);
- assertEquals(expectedStructure, filter);
- filter = InstanceIdToNodes.serialize(ctx, YangInstanceIdentifier.create(rootContainer, outerList, outerListWithKey));
- assertEquals(expectedStructure, filter);
- }
-
- @Test
- public void testLeafList() throws Exception {
- final ContainerNode expectedFilter = Builders.containerBuilder().withNodeIdentifier(rootContainer).withChild(
- Builders.orderedLeafSetBuilder().withNodeIdentifier(leafList).withChild(
- Builders.leafSetEntryBuilder().withNodeIdentifier(leafListWithValue).withValue(leafListWithValue.getValue()).build()
- ).build()
- ).build();
-
- final NormalizedNode<?, ?> filter = InstanceIdToNodes.serialize(ctx, YangInstanceIdentifier.create(rootContainer, leafList, leafListWithValue));
- assertEquals(expectedFilter, filter);
- }
-}
\ No newline at end of file