2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.md.sal.common.impl.util.compat;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.collect.ImmutableList;
13 import java.util.Iterator;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 * @deprecated This class provides compatibility between XML semantics
22 * and {@link org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree}
25 public class DataNormalizer {
27 private final DataNormalizationOperation<?> operation;
29 public DataNormalizer(final SchemaContext ctx) {
30 operation = DataNormalizationOperation.from(ctx);
33 public YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) {
34 ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
36 DataNormalizationOperation<?> currentOp = operation;
37 Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
40 while (arguments.hasNext()) {
41 PathArgument legacyArg = arguments.next();
42 currentOp = currentOp.getChild(legacyArg);
43 checkArgument(currentOp != null,
44 "Legacy Instance Identifier %s is not correct. Normalized Instance Identifier so far %s",
45 legacy, normalizedArgs.build());
46 while (currentOp.isMixin()) {
47 normalizedArgs.add(currentOp.getIdentifier());
48 currentOp = currentOp.getChild(legacyArg.getNodeType());
50 normalizedArgs.add(legacyArg);
52 } catch (DataNormalizationException e) {
53 throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
56 return YangInstanceIdentifier.create(normalizedArgs.build());
59 public DataNormalizationOperation<?> getOperation(final YangInstanceIdentifier legacy)
60 throws DataNormalizationException {
61 DataNormalizationOperation<?> currentOp = operation;
63 for (PathArgument pathArgument : legacy.getPathArguments()) {
64 currentOp = currentOp.getChild(pathArgument);
69 public YangInstanceIdentifier toLegacy(final YangInstanceIdentifier normalized) throws DataNormalizationException {
70 ImmutableList.Builder<PathArgument> legacyArgs = ImmutableList.builder();
71 DataNormalizationOperation<?> currentOp = operation;
72 for (PathArgument normalizedArg : normalized.getPathArguments()) {
73 currentOp = currentOp.getChild(normalizedArg);
74 if (!currentOp.isMixin()) {
75 legacyArgs.add(normalizedArg);
78 return YangInstanceIdentifier.create(legacyArgs.build());
81 public DataNormalizationOperation<?> getRootOperation() {