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 Map<Class, PathArgumentAttributesGetter> pathArgumentAttributesGetters = new HashMap<>();
+ private static final String REVISION_ARG = "?revision=";
+ 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
// that was not serialized using the PathArgumentSerializer
- Preconditions.checkNotNull(qName, "qName should not be null");
- Preconditions.checkArgument(!"".equals(qName.getLocalName()),
- "qName.localName cannot be empty qName = " + qName.toString());
- Preconditions.checkArgument(qName.getNamespace() != -1, "qName.namespace should be valid");
+// Preconditions.checkNotNull(qName, "qName should not be null");
+// Preconditions.checkArgument(qName.getNamespace() != -1, "qName.namespace should be valid");
- StringBuilder sb = new StringBuilder();
String namespace = context.getNamespace(qName.getNamespace());
- String revision = "";
String localName = context.getLocalName(qName.getLocalName());
+ StringBuilder sb;
if(qName.getRevision() != -1){
- revision = context.getRevision(qName.getRevision());
- sb.append("(").append(namespace).append("?revision=").append(
- revision).append(")").append(
- localName);
+ String revision = context.getRevision(qName.getRevision());
+ sb = new StringBuilder(namespace.length() + REVISION_ARG.length() + revision.length() +
+ localName.length() + 2);
+ sb.append('(').append(namespace).append(REVISION_ARG).append(
+ revision).append(')').append(localName);
} else {
- sb.append("(").append(namespace).append(")").append(
- localName);
+ sb = new StringBuilder(namespace.length() + localName.length() + 2);
+ sb.append('(').append(namespace).append(')').append(localName);
}
return sb.toString();
-
}
/**
* @return MD-SAL PathArgument
*/
private static YangInstanceIdentifier.PathArgument parsePathArgument(
- NormalizedNodeDeSerializationContext context,
- NormalizedNodeMessages.PathArgument pathArgument) {
-
- Preconditions.checkArgument(pathArgument.getIntType() >= 0
- && pathArgument.getIntType() < PathArgumentType.values().length,
- "Illegal PathArgumentType " + pathArgument.getIntType());
+ 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) {
-
- Map<QName, Object> map = new HashMap<>();
+ QNameDeSerializationContext context,
+ List<NormalizedNodeMessages.PathArgumentAttribute> attributesList) {
- for(NormalizedNodeMessages.PathArgumentAttribute attribute : attributesList){
+ Map<QName, Object> map;
+ if(attributesList.size() == 1) {
+ NormalizedNodeMessages.PathArgumentAttribute attribute = attributesList.get(0);
NormalizedNodeMessages.QName name = attribute.getName();
Object value = parseAttribute(context, attribute);
+ map = Collections.singletonMap(QNameFactory.create(qNameToString(context, name)), value);
+ } else {
+ map = new HashMap<>();
+
+ for(NormalizedNodeMessages.PathArgumentAttribute attribute : attributesList){
+ NormalizedNodeMessages.QName name = attribute.getName();
+ Object value = parseAttribute(context, attribute);
- map.put(QNameFactory.create(qNameToString(context, name)), value);
+ map.put(QNameFactory.create(qNameToString(context, name)), value);
+ }
}
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);
}