334a6c741388d91073dfac920664886d929ad3cd
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / restconf / impl / DataNormalizer.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.sal.restconf.impl;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.ImmutableList;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
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;
23
24 class DataNormalizer {
25     private final DataNormalizationOperation<?> operation;
26     private final EffectiveModelContext context;
27
28     DataNormalizer(final EffectiveModelContext ctx) {
29         context = requireNonNull(ctx);
30         operation = DataNormalizationOperation.from(ctx);
31     }
32
33     Entry<YangInstanceIdentifier, SchemaInferenceStack> toNormalized(final YangInstanceIdentifier legacy) {
34         List<PathArgument> normalizedArgs = new ArrayList<>();
35
36         DataNormalizationOperation<?> currentOp = operation;
37         Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
38         SchemaInferenceStack stack = SchemaInferenceStack.of(context);
39
40         try {
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);
50                 }
51                 normalizedArgs.add(legacyArg);
52             }
53         } catch (DataNormalizationException e) {
54             throw new IllegalArgumentException("Failed to normalize path " + legacy, e);
55         }
56
57         return Map.entry(YangInstanceIdentifier.create(normalizedArgs), stack);
58     }
59
60     DataNormalizationOperation<?> getOperation(final YangInstanceIdentifier legacy)
61             throws DataNormalizationException {
62         DataNormalizationOperation<?> currentOp = operation;
63
64         for (PathArgument pathArgument : legacy.getPathArguments()) {
65             currentOp = currentOp.getChild(pathArgument);
66         }
67         return currentOp;
68     }
69
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);
77             }
78         }
79         return YangInstanceIdentifier.create(legacyArgs.build());
80     }
81 }