Improve DataNormalizer performance
[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
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;
19
20 class DataNormalizer {
21     private final DataNormalizationOperation<?> operation;
22
23     DataNormalizer(final EffectiveModelContext ctx) {
24         operation = DataNormalizationOperation.from(ctx);
25     }
26
27     YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) {
28         List<PathArgument> normalizedArgs = new ArrayList<>();
29
30         DataNormalizationOperation<?> currentOp = operation;
31         Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
32
33         try {
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());
43                 }
44                 normalizedArgs.add(legacyArg);
45             }
46         } catch (DataNormalizationException e) {
47             throw new IllegalArgumentException("Failed to normalize path " + legacy, e);
48         }
49
50         return YangInstanceIdentifier.create(normalizedArgs);
51     }
52
53     DataNormalizationOperation<?> getOperation(final YangInstanceIdentifier legacy)
54             throws DataNormalizationException {
55         DataNormalizationOperation<?> currentOp = operation;
56
57         for (PathArgument pathArgument : legacy.getPathArguments()) {
58             currentOp = currentOp.getChild(pathArgument);
59         }
60         return currentOp;
61     }
62
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);
70             }
71         }
72         return YangInstanceIdentifier.create(legacyArgs.build());
73     }
74 }