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.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 class DataNormalizer {
21 private final DataNormalizationOperation<?> operation;
23 DataNormalizer(final EffectiveModelContext ctx) {
24 operation = DataNormalizationOperation.from(ctx);
27 YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) {
28 List<PathArgument> normalizedArgs = new ArrayList<>();
30 DataNormalizationOperation<?> currentOp = operation;
31 Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
34 while (arguments.hasNext()) {
35 PathArgument legacyArg = arguments.next();
36 currentOp = currentOp.getChild(legacyArg);
37 checkArgument(currentOp != null,
38 "Legacy Instance Identifier %s is not correct. Normalized Instance Identifier so far %s",
39 legacy, normalizedArgs);
40 while (currentOp.isMixin()) {
41 normalizedArgs.add(currentOp.getIdentifier());
42 currentOp = currentOp.getChild(legacyArg.getNodeType());
44 normalizedArgs.add(legacyArg);
46 } catch (DataNormalizationException e) {
47 throw new IllegalArgumentException("Failed to normalize path " + legacy, e);
50 return YangInstanceIdentifier.create(normalizedArgs);
53 DataNormalizationOperation<?> getOperation(final YangInstanceIdentifier legacy)
54 throws DataNormalizationException {
55 DataNormalizationOperation<?> currentOp = operation;
57 for (PathArgument pathArgument : legacy.getPathArguments()) {
58 currentOp = currentOp.getChild(pathArgument);
63 YangInstanceIdentifier toLegacy(final YangInstanceIdentifier normalized) throws DataNormalizationException {
64 ImmutableList.Builder<PathArgument> legacyArgs = ImmutableList.builder();
65 DataNormalizationOperation<?> currentOp = operation;
66 for (PathArgument normalizedArg : normalized.getPathArguments()) {
67 currentOp = currentOp.getChild(normalizedArg);
68 if (!currentOp.isMixin()) {
69 legacyArgs.add(normalizedArg);
72 return YangInstanceIdentifier.create(legacyArgs.build());