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;
19 * @deprecated This class provides compatibility between XML semantics
20 * and {@link org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree}
23 public class DataNormalizer {
25 private final DataNormalizationOperation<?> operation;
27 public DataNormalizer(final SchemaContext ctx) {
28 operation = DataNormalizationOperation.from(ctx);
31 public YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) {
32 ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
34 DataNormalizationOperation<?> currentOp = operation;
35 Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
38 while (arguments.hasNext()) {
39 PathArgument legacyArg = arguments.next();
40 currentOp = currentOp.getChild(legacyArg);
41 checkArgument(currentOp != null,
42 "Legacy Instance Identifier %s is not correct. Normalized Instance Identifier so far %s",
43 legacy, normalizedArgs.build());
44 while (currentOp.isMixin()) {
45 normalizedArgs.add(currentOp.getIdentifier());
46 currentOp = currentOp.getChild(legacyArg.getNodeType());
48 normalizedArgs.add(legacyArg);
50 } catch (DataNormalizationException e) {
51 throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
54 return YangInstanceIdentifier.create(normalizedArgs.build());
57 public DataNormalizationOperation<?> getOperation(final YangInstanceIdentifier legacy) throws DataNormalizationException {
58 DataNormalizationOperation<?> currentOp = operation;
60 for (PathArgument pathArgument : legacy.getPathArguments()) {
61 currentOp = currentOp.getChild(pathArgument);
66 public YangInstanceIdentifier toLegacy(final YangInstanceIdentifier normalized) throws DataNormalizationException {
67 ImmutableList.Builder<PathArgument> legacyArgs = ImmutableList.builder();
68 DataNormalizationOperation<?> currentOp = operation;
69 for (PathArgument normalizedArg : normalized.getPathArguments()) {
70 currentOp = currentOp.getChild(normalizedArg);
71 if (!currentOp.isMixin()) {
72 legacyArgs.add(normalizedArg);
75 return YangInstanceIdentifier.create(legacyArgs.build());
78 public DataNormalizationOperation<?> getRootOperation() {