Fix raw type warnings
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / xml / codec / XmlUtils.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.xml.codec;
9
10 import java.util.Map;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
18 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlCodecProvider;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20
21 /**
22  * Common XML-related utility methods, which are not specific to a particular
23  * JAXP API.
24  */
25 public class XmlUtils {
26
27     public static final XmlCodecProvider DEFAULT_XML_CODEC_PROVIDER = new XmlCodecProvider() {
28         @Override
29         public TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codecFor(final TypeDefinition<?> baseType) {
30             return TypeDefinitionAwareCodec.from(baseType);
31         }
32     };
33
34     private XmlUtils() {
35     }
36
37     public static TypeDefinition<?> resolveBaseTypeFrom(final @Nonnull TypeDefinition<?> type) {
38         TypeDefinition<?> superType = type;
39         while (superType.getBaseType() != null) {
40             superType = superType.getBaseType();
41         }
42         return superType;
43     }
44
45     /**
46      * This code is picked from yangtools and modified to add type of instance identifier
47      * output of instance identifier something like below for a flow ref composite node of type instance identifier,
48      * which has path arguments with predicates, whose value is of type java.lang.short
49      * <flow-ref xmlns:bgkj="urn:opendaylight:flow:inventory" xmlns:jdlk="urn:opendaylight:inventory">
50      *   /jdlk:nodes/jdlk:node[jdlk:id='openflow:205558455098190@java.lang.String']
51      *   /bgkj:table[bgkj:id='3@java.lang.Short']
52      *   /bgkj:flow[bgkj:id='156@java.lang.String']
53      * </flow-ref>
54      *
55      */
56
57     public static String encodeIdentifier(final RandomPrefix prefixes, final YangInstanceIdentifier id) {
58         final StringBuilder textContent = new StringBuilder();
59         for (final PathArgument pathArgument : id.getPathArguments()) {
60             textContent.append('/');
61             textContent.append(prefixes.encodeQName(pathArgument.getNodeType()));
62             if (pathArgument instanceof NodeIdentifierWithPredicates) {
63                 final Map<QName, Object> predicates = ((NodeIdentifierWithPredicates) pathArgument).getKeyValues();
64
65                 for (final QName keyValue : predicates.keySet()) {
66                     final Object value = predicates.get(keyValue);
67                     final String type = value.getClass().getName();
68                     final String predicateValue = String.valueOf(value);
69                     textContent.append('[');
70                     textContent.append(prefixes.encodeQName(keyValue));
71                     textContent.append("='");
72                     textContent.append(predicateValue);
73                     textContent.append("@");
74                     textContent.append(type);
75                     textContent.append("']");
76                 }
77             } else if (pathArgument instanceof NodeWithValue) {
78                 textContent.append("[.='");
79                 textContent.append(((NodeWithValue<?>) pathArgument).getValue());
80                 textContent.append("']");
81             }
82         }
83
84         return textContent.toString();
85     }
86 }