2 * Copyright (c) 2015 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 static java.util.Objects.requireNonNull;
12 import com.google.common.collect.Iterables;
13 import java.util.ArrayList;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.mdsal.binding.dom.codec.api.BindingInstanceIdentifierCodec;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.DataObjectStep;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.binding.KeylessStep;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 final class InstanceIdentifierCodec implements BindingInstanceIdentifierCodec,
24 //FIXME: this is not really an IllegalArgumentCodec, as it can legally return null from deserialize()
25 ValueCodec<YangInstanceIdentifier, InstanceIdentifier<?>> {
26 private final BindingCodecContext context;
28 InstanceIdentifierCodec(final BindingCodecContext context) {
29 this.context = requireNonNull(context);
33 public <T extends DataObject> InstanceIdentifier<T> toBinding(final YangInstanceIdentifier domPath) {
34 final var builder = new ArrayList<DataObjectStep<?>>();
35 final var codec = context.getCodecContextNode(domPath, builder);
39 if (codec instanceof ListCodecContext && Iterables.getLast(builder) instanceof KeylessStep) {
40 // We ended up in list, but without key, which means it represent list as a whole,
41 // which is not binding representable.
45 return InstanceIdentifier.unsafeOf(builder);
49 public @NonNull YangInstanceIdentifier fromBinding(@NonNull final InstanceIdentifier<?> bindingPath) {
50 final var domArgs = new ArrayList<PathArgument>();
51 context.getCodecContextNode(bindingPath, domArgs);
52 return YangInstanceIdentifier.of(domArgs);
57 public YangInstanceIdentifier serialize(final InstanceIdentifier<?> input) {
58 return fromBinding(input);
63 public InstanceIdentifier<?> deserialize(final YangInstanceIdentifier input) {
64 return toBinding(input);