753b5154773bec73d45c4446b4773c59852bbdc1
[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.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;
17
18 class DataNormalizer {
19     private final DataNormalizationOperation<?> operation;
20
21     DataNormalizer(final EffectiveModelContext ctx) {
22         operation = DataNormalizationOperation.from(ctx);
23     }
24
25     YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) {
26         ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
27
28         DataNormalizationOperation<?> currentOp = operation;
29         Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
30
31         try {
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());
41                 }
42                 normalizedArgs.add(legacyArg);
43             }
44         } catch (DataNormalizationException e) {
45             throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
46         }
47
48         return YangInstanceIdentifier.create(normalizedArgs.build());
49     }
50
51     DataNormalizationOperation<?> getOperation(final YangInstanceIdentifier legacy)
52             throws DataNormalizationException {
53         DataNormalizationOperation<?> currentOp = operation;
54
55         for (PathArgument pathArgument : legacy.getPathArguments()) {
56             currentOp = currentOp.getChild(pathArgument);
57         }
58         return currentOp;
59     }
60
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);
68             }
69         }
70         return YangInstanceIdentifier.create(legacyArgs.build());
71     }
72 }