package org.opendaylight.controller.cluster.datastore.node.utils.serialization;
import com.google.common.base.Preconditions;
-
import org.opendaylight.controller.cluster.datastore.node.utils.NodeIdentifierFactory;
import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory;
import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Set;
-
import static org.opendaylight.controller.cluster.datastore.node.utils.serialization.PathArgumentType.getSerializablePathArgumentType;
public class PathArgumentSerializer {
private static final String REVISION_ARG = "?revision=";
- private static final Map<Class, PathArgumentAttributesGetter> pathArgumentAttributesGetters = new HashMap<>();
+ private static final Map<Class<?>, PathArgumentAttributesGetter> pathArgumentAttributesGetters = new HashMap<>();
- public static NormalizedNodeMessages.PathArgument serialize(NormalizedNodeSerializationContext context, YangInstanceIdentifier.PathArgument pathArgument){
+ public static NormalizedNodeMessages.PathArgument serialize(QNameSerializationContext context,
+ YangInstanceIdentifier.PathArgument pathArgument){
Preconditions.checkNotNull(context, "context should not be null");
Preconditions.checkNotNull(pathArgument, "pathArgument should not be null");
}
- public static YangInstanceIdentifier.PathArgument deSerialize(NormalizedNodeDeSerializationContext context, NormalizedNodeMessages.PathArgument pathArgument){
+ public static YangInstanceIdentifier.PathArgument deSerialize(QNameDeSerializationContext context,
+ NormalizedNodeMessages.PathArgument pathArgument){
Preconditions.checkNotNull(context, "context should not be null");
Preconditions.checkNotNull(pathArgument, "pathArgument should not be null");
private static interface PathArgumentAttributesGetter {
- Iterable<? extends NormalizedNodeMessages.PathArgumentAttribute> get(NormalizedNodeSerializationContext context,
- YangInstanceIdentifier.PathArgument pathArgument);
+ Iterable<? extends NormalizedNodeMessages.PathArgumentAttribute> get(
+ QNameSerializationContext context, YangInstanceIdentifier.PathArgument pathArgument);
}
static {
pathArgumentAttributesGetters.put(YangInstanceIdentifier.NodeWithValue.class, new PathArgumentAttributesGetter() {
@Override
public Iterable<? extends NormalizedNodeMessages.PathArgumentAttribute> get(
- NormalizedNodeSerializationContext context,
- YangInstanceIdentifier.PathArgument pathArgument) {
- List<NormalizedNodeMessages.PathArgumentAttribute> attributes =
- new ArrayList<>();
+ QNameSerializationContext context, YangInstanceIdentifier.PathArgument pathArgument) {
YangInstanceIdentifier.NodeWithValue identifier
= (YangInstanceIdentifier.NodeWithValue) pathArgument;
NormalizedNodeMessages.PathArgumentAttribute attribute =
buildAttribute(context, null, identifier.getValue());
- attributes.add(attribute);
-
- return attributes;
-
+ return Arrays.asList(attribute);
}
});
pathArgumentAttributesGetters.put(YangInstanceIdentifier.NodeIdentifierWithPredicates.class, new PathArgumentAttributesGetter() {
@Override
public Iterable<? extends NormalizedNodeMessages.PathArgumentAttribute> get(
- NormalizedNodeSerializationContext context,
- YangInstanceIdentifier.PathArgument pathArgument) {
-
- List<NormalizedNodeMessages.PathArgumentAttribute> attributes =
- new ArrayList<>();
+ QNameSerializationContext context, YangInstanceIdentifier.PathArgument pathArgument) {
YangInstanceIdentifier.NodeIdentifierWithPredicates identifier
= (YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument;
- for (QName key : identifier.getKeyValues().keySet()) {
- Object value = identifier.getKeyValues().get(key);
+ Map<QName, Object> keyValues = identifier.getKeyValues();
+ List<NormalizedNodeMessages.PathArgumentAttribute> attributes =
+ new ArrayList<>(keyValues.size());
+ for (Entry<QName, Object> e : keyValues.entrySet()) {
NormalizedNodeMessages.PathArgumentAttribute attribute =
- buildAttribute(context, key, value);
+ buildAttribute(context, e.getKey(), e.getValue());
attributes.add(attribute);
-
}
return attributes;
-
}
});
pathArgumentAttributesGetters.put(YangInstanceIdentifier.AugmentationIdentifier.class, new PathArgumentAttributesGetter() {
@Override
public Iterable<? extends NormalizedNodeMessages.PathArgumentAttribute> get(
- NormalizedNodeSerializationContext context,
- YangInstanceIdentifier.PathArgument pathArgument) {
-
- List<NormalizedNodeMessages.PathArgumentAttribute> attributes =
- new ArrayList<>();
+ QNameSerializationContext context, YangInstanceIdentifier.PathArgument pathArgument) {
YangInstanceIdentifier.AugmentationIdentifier identifier
= (YangInstanceIdentifier.AugmentationIdentifier) pathArgument;
- for (QName key : identifier.getPossibleChildNames()) {
+ Set<QName> possibleChildNames = identifier.getPossibleChildNames();
+ List<NormalizedNodeMessages.PathArgumentAttribute> attributes =
+ new ArrayList<>(possibleChildNames.size());
+ for (QName key : possibleChildNames) {
Object value = key;
NormalizedNodeMessages.PathArgumentAttribute attribute =
buildAttribute(context, key, value);
attributes.add(attribute);
-
}
return attributes;
-
}
});
pathArgumentAttributesGetters.put(YangInstanceIdentifier.NodeIdentifier.class, new PathArgumentAttributesGetter() {
@Override
public Iterable<? extends NormalizedNodeMessages.PathArgumentAttribute> get(
- NormalizedNodeSerializationContext context,
- YangInstanceIdentifier.PathArgument pathArgument) {
+ QNameSerializationContext context, YangInstanceIdentifier.PathArgument pathArgument) {
return Collections.emptyList();
}
});
}
- private static NormalizedNodeMessages.PathArgumentAttribute buildAttribute(NormalizedNodeSerializationContext context,QName name, Object value){
+ private static NormalizedNodeMessages.PathArgumentAttribute buildAttribute(
+ QNameSerializationContext context, QName name, Object value) {
NormalizedNodeMessages.PathArgumentAttribute.Builder builder =
NormalizedNodeMessages.PathArgumentAttribute.newBuilder();
}
- private static NormalizedNodeMessages.QName.Builder encodeQName(NormalizedNodeSerializationContext context, QName qName){
- if(qName == null){
+ private static NormalizedNodeMessages.QName.Builder encodeQName(QNameSerializationContext context,
+ QName qName) {
+ if(qName == null) {
return NormalizedNodeMessages.QName.getDefaultInstance().toBuilder();
}
NormalizedNodeMessages.QName.Builder qNameBuilder =
}
private static Iterable<? extends NormalizedNodeMessages.PathArgumentAttribute> getPathArgumentAttributes(
- NormalizedNodeSerializationContext context,
- YangInstanceIdentifier.PathArgument pathArgument) {
+ QNameSerializationContext context, YangInstanceIdentifier.PathArgument pathArgument) {
return pathArgumentAttributesGetters.get(pathArgument.getClass()).get(context, pathArgument);
-
}
- private static String qNameToString(NormalizedNodeDeSerializationContext context,
+ private static String qNameToString(QNameDeSerializationContext context,
NormalizedNodeMessages.QName qName){
// If this serializer is used qName cannot be null (see encodeQName)
// adding null check only in case someone tried to deSerialize a protocol buffer node
* @return MD-SAL PathArgument
*/
private static YangInstanceIdentifier.PathArgument parsePathArgument(
- NormalizedNodeDeSerializationContext context,
- NormalizedNodeMessages.PathArgument pathArgument) {
+ QNameDeSerializationContext context, NormalizedNodeMessages.PathArgument pathArgument) {
switch(PathArgumentType.values()[pathArgument.getIntType()]){
case NODE_IDENTIFIER_WITH_VALUE : {
}
private static Map<QName, Object> toAttributesMap(
- NormalizedNodeDeSerializationContext context,
- List<NormalizedNodeMessages.PathArgumentAttribute> attributesList) {
+ QNameDeSerializationContext context,
+ List<NormalizedNodeMessages.PathArgumentAttribute> attributesList) {
Map<QName, Object> map;
if(attributesList.size() == 1) {
return map;
}
- private static Object parseAttribute(NormalizedNodeDeSerializationContext context, NormalizedNodeMessages.PathArgumentAttribute attribute){
+ private static Object parseAttribute(QNameDeSerializationContext context,
+ NormalizedNodeMessages.PathArgumentAttribute attribute){
return ValueSerializer.deSerialize(context, attribute);
}