+/*
+ * 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.controller.md.sal.common.impl.util.compat;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.Iterator;
import java.util.Map;
+import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.CompositeNode;
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.Node;
import org.opendaylight.yangtools.yang.data.api.SimpleNode;
DataNormalizationOperation<?> currentOp = operation;
Iterator<PathArgument> arguments = legacy.getPath().iterator();
- while ( arguments.hasNext() ) {
- PathArgument legacyArg = arguments.next();
- currentOp = currentOp.getChild(legacyArg);
- checkArgument(currentOp != null, "Legacy Instance Identifier %s is not correct. Normalized Instance Identifier so far %s",legacy,normalizedArgs.build());
- while (currentOp.isMixin()) {
- normalizedArgs.add(currentOp.getIdentifier());
- currentOp = currentOp.getChild(legacyArg.getNodeType());
- }
- if(arguments.hasNext() || (!currentOp.isKeyedEntry() || legacyArg instanceof NodeIdentifierWithPredicates || legacyArg instanceof NodeWithValue)) {
+
+ try {
+ while (arguments.hasNext()) {
+ PathArgument legacyArg = arguments.next();
+ currentOp = currentOp.getChild(legacyArg);
+ checkArgument(currentOp != null,
+ "Legacy Instance Identifier %s is not correct. Normalized Instance Identifier so far %s",
+ legacy, normalizedArgs.build());
+ while (currentOp.isMixin()) {
+ normalizedArgs.add(currentOp.getIdentifier());
+ currentOp = currentOp.getChild(legacyArg.getNodeType());
+ }
normalizedArgs.add(legacyArg);
}
+ } catch (DataNormalizationException e) {
+ throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
}
+
return new InstanceIdentifier(normalizedArgs.build());
}
- public Map.Entry<InstanceIdentifier,NormalizedNode<?, ?>> toNormalized(final Map.Entry<InstanceIdentifier,CompositeNode> legacy) {
+ public Map.Entry<InstanceIdentifier, NormalizedNode<?, ?>> toNormalized(
+ final Map.Entry<InstanceIdentifier, CompositeNode> legacy) {
return toNormalized(legacy.getKey(), legacy.getValue());
}
- public Map.Entry<InstanceIdentifier,NormalizedNode<?, ?>> toNormalized(final InstanceIdentifier legacyPath, final CompositeNode legacyData) {
+ public Map.Entry<InstanceIdentifier, NormalizedNode<?, ?>> toNormalized(final InstanceIdentifier legacyPath,
+ final CompositeNode legacyData) {
InstanceIdentifier normalizedPath = toNormalized(legacyPath);
DataNormalizationOperation<?> currentOp = operation;
for (PathArgument arg : normalizedPath.getPath()) {
- currentOp = currentOp.getChild(arg);
+ try {
+ currentOp = currentOp.getChild(arg);
+ } catch (DataNormalizationException e) {
+ throw new IllegalArgumentException(String.format("Failed to validate normalized path %s",
+ normalizedPath), e);
+ }
}
- // Write Augmentaiton data resolution
+
+ // Write Augmentation data resolution
if (legacyData.getChildren().size() == 1) {
- DataNormalizationOperation<?> potentialOp = currentOp.getChild(legacyData.getChildren().get(0)
- .getNodeType());
- if(potentialOp.getIdentifier() instanceof AugmentationIdentifier) {
+ final DataNormalizationOperation<?> potentialOp;
+
+ try {
+ final QName childType = legacyData.getChildren().get(0).getNodeType();
+ potentialOp = currentOp.getChild(childType);
+ } catch (DataNormalizationException e) {
+ throw new IllegalArgumentException(String.format("Failed to get child operation for %s", legacyData), e);
+ }
+
+ if (potentialOp.getIdentifier() instanceof AugmentationIdentifier) {
currentOp = potentialOp;
ArrayList<PathArgument> reworkedArgs = new ArrayList<>(normalizedPath.getPath());
reworkedArgs.add(potentialOp.getIdentifier());
Preconditions.checkArgument(currentOp != null,
"Instance Identifier %s does not reference correct schema Node.", normalizedPath);
- return new AbstractMap.SimpleEntry<InstanceIdentifier,NormalizedNode<?, ?>>(normalizedPath,currentOp.normalize(legacyData));
+ return new AbstractMap.SimpleEntry<InstanceIdentifier, NormalizedNode<?, ?>>(normalizedPath,
+ currentOp.normalize(legacyData));
}
- public InstanceIdentifier toLegacy(final InstanceIdentifier normalized) {
+ public InstanceIdentifier toLegacy(final InstanceIdentifier normalized) throws DataNormalizationException {
ImmutableList.Builder<PathArgument> legacyArgs = ImmutableList.builder();
PathArgument previous = null;
+ DataNormalizationOperation<?> currentOp = operation;
for (PathArgument normalizedArg : normalized.getPath()) {
- if (normalizedArg instanceof NodeIdentifier) {
- if (previous != null) {
- legacyArgs.add(previous);
- }
- previous = normalizedArg;
- } else if (normalizedArg instanceof NodeIdentifierWithPredicates) {
- // We skip previous node, which was mixin.
- previous = normalizedArg;
- } else if (normalizedArg instanceof AugmentationIdentifier) {
- // We ignore argument
+ currentOp = currentOp.getChild(normalizedArg);
+ if(!currentOp.isMixin()) {
+ legacyArgs.add(normalizedArg);
}
- // FIXME : Add option for reading choice
- }
- if (previous != null) {
- legacyArgs.add(previous);
}
return new InstanceIdentifier(legacyArgs.build());
}
public static Node<?> toLegacy(final NormalizedNode<?, ?> node) {
if (node instanceof MixinNode) {
/**
- * Direct reading of MixinNodes is not supported,
- * since it is not possible in legacy APIs create pointer
- * to Mixin Nodes.
+ * Direct reading of MixinNodes is not supported, since it is not
+ * possible in legacy APIs create pointer to Mixin Nodes.
*
*/
return null;
final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> mixin) {
ArrayList<Node<?>> ret = new ArrayList<>();
for (NormalizedNode<?, ?> child : mixin.getValue()) {
- if(child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
- Iterables.addAll(ret,toLegacyNodesFromMixin((NormalizedNodeContainer) child));
+ if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
+ Iterables.addAll(ret, toLegacyNodesFromMixin((NormalizedNodeContainer) child));
} else {
ret.add(toLegacy(child));
}