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