Merge "Fix modules Restconf call for mounted devices"
[controller.git] / opendaylight / md-sal / sal-common-impl / src / main / java / org / opendaylight / controller / md / sal / common / impl / util / compat / 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.controller.md.sal.common.impl.util.compat;
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.SchemaContext;
17
18 /**
19  * @deprecated This class provides compatibility between XML semantics
20  * and {@link org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree}
21  */
22 @Deprecated
23 public class DataNormalizer {
24
25     private final DataNormalizationOperation<?> operation;
26
27     public DataNormalizer(final SchemaContext ctx) {
28         operation = DataNormalizationOperation.from(ctx);
29     }
30
31     public YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) {
32         ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
33
34         DataNormalizationOperation<?> currentOp = operation;
35         Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
36
37         try {
38             while (arguments.hasNext()) {
39                 PathArgument legacyArg = arguments.next();
40                 currentOp = currentOp.getChild(legacyArg);
41                 checkArgument(currentOp != null,
42                         "Legacy Instance Identifier %s is not correct. Normalized Instance Identifier so far %s",
43                         legacy, normalizedArgs.build());
44                 while (currentOp.isMixin()) {
45                     normalizedArgs.add(currentOp.getIdentifier());
46                     currentOp = currentOp.getChild(legacyArg.getNodeType());
47                 }
48                 normalizedArgs.add(legacyArg);
49             }
50         } catch (DataNormalizationException e) {
51             throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
52         }
53
54         return YangInstanceIdentifier.create(normalizedArgs.build());
55     }
56
57     public DataNormalizationOperation<?> getOperation(final YangInstanceIdentifier legacy) throws DataNormalizationException {
58         DataNormalizationOperation<?> currentOp = operation;
59         Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
60
61         while (arguments.hasNext()) {
62             currentOp = currentOp.getChild(arguments.next());
63         }
64         return currentOp;
65     }
66
67     public YangInstanceIdentifier toLegacy(final YangInstanceIdentifier normalized) throws DataNormalizationException {
68         ImmutableList.Builder<PathArgument> legacyArgs = ImmutableList.builder();
69         DataNormalizationOperation<?> currentOp = operation;
70         for (PathArgument normalizedArg : normalized.getPathArguments()) {
71             currentOp = currentOp.getChild(normalizedArg);
72             if (!currentOp.isMixin()) {
73                 legacyArgs.add(normalizedArg);
74             }
75         }
76         return YangInstanceIdentifier.create(legacyArgs.build());
77     }
78
79     public DataNormalizationOperation<?> getRootOperation() {
80         return operation;
81     }
82
83 }