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 java.util.List;
15 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
16 import org.opendaylight.yangtools.concepts.Codec;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 final class InstanceIdentifierCodec implements Codec<YangInstanceIdentifier, InstanceIdentifier<?>> {
22 private final BindingCodecContext context;
24 InstanceIdentifierCodec(final BindingCodecContext context) {
25 this.context = requireNonNull(context);
29 public YangInstanceIdentifier serialize(final InstanceIdentifier<?> input) {
30 final List<PathArgument> domArgs = new ArrayList<>();
31 context.getCodecContextNode(input, domArgs);
32 return YangInstanceIdentifier.create(domArgs);
36 public InstanceIdentifier<?> deserialize(final YangInstanceIdentifier input) {
37 final List<InstanceIdentifier.PathArgument> builder = new ArrayList<>();
38 final BindingDataObjectCodecTreeNode<?> codec = context.getCodecContextNode(input, builder);
42 if (codec instanceof ListNodeCodecContext && Iterables.getLast(builder) instanceof InstanceIdentifier.Item) {
43 // We ended up in list, but without key, which means it represent list as a whole,
44 // which is not binding representable.
47 return InstanceIdentifier.create(builder);