2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
10 import com.google.common.base.Throwables;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableMap;
13 import java.lang.invoke.MethodHandle;
14 import java.lang.invoke.MethodHandles;
15 import java.lang.reflect.Constructor;
16 import java.util.ArrayList;
17 import java.util.Comparator;
18 import java.util.LinkedHashMap;
19 import java.util.List;
21 import java.util.Map.Entry;
22 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
23 import org.opendaylight.yangtools.concepts.Codec;
24 import org.opendaylight.yangtools.yang.binding.Identifier;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
28 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
30 final class IdentifiableItemCodec implements Codec<NodeIdentifierWithPredicates, IdentifiableItem<?, ?>> {
31 private final Map<QName, ValueContext> keyValueContexts;
32 private final List<QName> keysInBindingOrder;
33 private final ListSchemaNode schema;
34 private final Class<?> identifiable;
35 private final MethodHandle ctorInvoker;
36 private final MethodHandle ctor;
38 IdentifiableItemCodec(final ListSchemaNode schema, final Class<? extends Identifier<?>> keyClass,
39 final Class<?> identifiable, final Map<QName, ValueContext> keyValueContexts) {
41 this.identifiable = identifiable;
44 ctor = MethodHandles.publicLookup().unreflectConstructor(getConstructor(keyClass));
45 } catch (IllegalAccessException e) {
46 throw new IllegalArgumentException("Missing constructor in class " + keyClass, e);
48 final MethodHandle inv = MethodHandles.spreadInvoker(ctor.type(), 0);
49 this.ctorInvoker = inv.asType(inv.type().changeReturnType(Identifier.class));
52 * We need to re-index to make sure we instantiate nodes in the order in which
55 final Map<QName, ValueContext> keys = new LinkedHashMap<>();
56 for (final QName qname : schema.getKeyDefinition()) {
57 keys.put(qname, keyValueContexts.get(qname));
59 this.keyValueContexts = ImmutableMap.copyOf(keys);
62 * When instantiating binding objects we need to specify constructor arguments
63 * in alphabetic order. We play a couple of tricks here to optimize CPU/memory
66 * We do not have to perform a sort if the source collection has less than two
69 * We always perform an ImmutableList.copyOf(), as that will turn into a no-op
70 * if the source is already immutable. It will also produce optimized implementations
71 * for empty and singleton collections.
73 * BUG-2755: remove this if order is made declaration-order-dependent
75 final List<QName> unsortedKeys = schema.getKeyDefinition();
76 final List<QName> sortedKeys;
77 if (unsortedKeys.size() > 1) {
78 final List<QName> tmp = new ArrayList<>(unsortedKeys);
79 // This is not terribly efficient but gets the job done
80 tmp.sort(Comparator.comparing(qname -> BindingMapping.getPropertyName(qname.getLocalName())));
83 sortedKeys = unsortedKeys;
86 this.keysInBindingOrder = ImmutableList.copyOf(sortedKeys);
90 @SuppressWarnings("checkstyle:illegalCatch")
91 public IdentifiableItem<?, ?> deserialize(final NodeIdentifierWithPredicates input) {
92 final Object[] bindingValues = new Object[keysInBindingOrder.size()];
95 for (final QName key : keysInBindingOrder) {
96 final Object yangValue = input.getKeyValues().get(key);
97 bindingValues[offset++] = keyValueContexts.get(key).deserialize(yangValue);
100 final Identifier<?> identifier;
102 identifier = (Identifier<?>) ctorInvoker.invokeExact(ctor, bindingValues);
103 } catch (Throwable e) {
104 Throwables.throwIfUnchecked(e);
105 throw new RuntimeException(e);
108 @SuppressWarnings({ "rawtypes", "unchecked" })
109 final IdentifiableItem identifiableItem = IdentifiableItem.of((Class) identifiable, (Identifier) identifier);
110 return identifiableItem;
114 public NodeIdentifierWithPredicates serialize(final IdentifiableItem<?, ?> input) {
115 final Object value = input.getKey();
117 final Map<QName, Object> values = new LinkedHashMap<>();
118 for (final Entry<QName, ValueContext> valueCtx : keyValueContexts.entrySet()) {
119 values.put(valueCtx.getKey(), valueCtx.getValue().getAndSerialize(value));
121 return new NodeIdentifierWithPredicates(schema.getQName(), values);
124 @SuppressWarnings("unchecked")
125 private static Constructor<? extends Identifier<?>> getConstructor(final Class<? extends Identifier<?>> clazz) {
126 for (@SuppressWarnings("rawtypes") final Constructor constr : clazz.getConstructors()) {
127 final Class<?>[] parameters = constr.getParameterTypes();
128 if (!clazz.equals(parameters[0])) {
129 // It is not copy constructor;
133 throw new IllegalArgumentException("Supplied class " + clazz + "does not have required constructor.");