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.netconf.sal.restconf.impl;
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.EffectiveModelContext;
18 class DataNormalizer {
19 private final DataNormalizationOperation<?> operation;
21 DataNormalizer(final EffectiveModelContext ctx) {
22 operation = DataNormalizationOperation.from(ctx);
25 YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) {
26 ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
28 DataNormalizationOperation<?> currentOp = operation;
29 Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
32 while (arguments.hasNext()) {
33 PathArgument legacyArg = arguments.next();
34 currentOp = currentOp.getChild(legacyArg);
35 checkArgument(currentOp != null,
36 "Legacy Instance Identifier %s is not correct. Normalized Instance Identifier so far %s",
37 legacy, normalizedArgs.build());
38 while (currentOp.isMixin()) {
39 normalizedArgs.add(currentOp.getIdentifier());
40 currentOp = currentOp.getChild(legacyArg.getNodeType());
42 normalizedArgs.add(legacyArg);
44 } catch (DataNormalizationException e) {
45 throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
48 return YangInstanceIdentifier.create(normalizedArgs.build());
51 DataNormalizationOperation<?> getOperation(final YangInstanceIdentifier legacy)
52 throws DataNormalizationException {
53 DataNormalizationOperation<?> currentOp = operation;
55 for (PathArgument pathArgument : legacy.getPathArguments()) {
56 currentOp = currentOp.getChild(pathArgument);
61 YangInstanceIdentifier toLegacy(final YangInstanceIdentifier normalized) throws DataNormalizationException {
62 ImmutableList.Builder<PathArgument> legacyArgs = ImmutableList.builder();
63 DataNormalizationOperation<?> currentOp = operation;
64 for (PathArgument normalizedArg : normalized.getPathArguments()) {
65 currentOp = currentOp.getChild(normalizedArg);
66 if (!currentOp.isMixin()) {
67 legacyArgs.add(normalizedArg);
70 return YangInstanceIdentifier.create(legacyArgs.build());