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;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.collect.ImmutableList;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
18 import java.util.Map.Entry;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
24 class DataNormalizer {
25 private final DataNormalizationOperation<?> operation;
26 private final EffectiveModelContext context;
28 DataNormalizer(final EffectiveModelContext ctx) {
29 context = requireNonNull(ctx);
30 operation = DataNormalizationOperation.from(ctx);
33 Entry<YangInstanceIdentifier, SchemaInferenceStack> toNormalized(final YangInstanceIdentifier legacy) {
34 List<PathArgument> normalizedArgs = new ArrayList<>();
36 DataNormalizationOperation<?> currentOp = operation;
37 Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
38 SchemaInferenceStack stack = SchemaInferenceStack.of(context);
41 while (arguments.hasNext()) {
42 PathArgument legacyArg = arguments.next();
43 currentOp = currentOp.enterChild(legacyArg, stack);
44 checkArgument(currentOp != null,
45 "Legacy Instance Identifier %s is not correct. Normalized Instance Identifier so far %s",
46 legacy, normalizedArgs);
47 while (currentOp.isMixin()) {
48 normalizedArgs.add(currentOp.getIdentifier());
49 currentOp = currentOp.enterChild(legacyArg.getNodeType(), stack);
51 normalizedArgs.add(legacyArg);
53 } catch (DataNormalizationException e) {
54 throw new IllegalArgumentException("Failed to normalize path " + legacy, e);
57 return Map.entry(YangInstanceIdentifier.create(normalizedArgs), stack);
60 DataNormalizationOperation<?> getOperation(final YangInstanceIdentifier legacy)
61 throws DataNormalizationException {
62 DataNormalizationOperation<?> currentOp = operation;
64 for (PathArgument pathArgument : legacy.getPathArguments()) {
65 currentOp = currentOp.getChild(pathArgument);
70 YangInstanceIdentifier toLegacy(final YangInstanceIdentifier normalized) throws DataNormalizationException {
71 ImmutableList.Builder<PathArgument> legacyArgs = ImmutableList.builder();
72 DataNormalizationOperation<?> currentOp = operation;
73 for (PathArgument normalizedArg : normalized.getPathArguments()) {
74 currentOp = currentOp.getChild(normalizedArg);
75 if (!currentOp.isMixin()) {
76 legacyArgs.add(normalizedArg);
79 return YangInstanceIdentifier.create(legacyArgs.build());