Do not support unions with complex types
[yangtools.git] / data / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / UnionStringCodec.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.codec;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableList;
13 import java.util.ArrayList;
14 import java.util.Base64;
15 import java.util.List;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.data.api.codec.UnionCodec;
18 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 final class UnionStringCodec extends TypeDefinitionAwareCodec<Object, UnionTypeDefinition>
23         implements UnionCodec<String> {
24     private static final Logger LOG = LoggerFactory.getLogger(UnionStringCodec.class);
25
26     private final ImmutableList<TypeDefinitionAwareCodec<Object, ?>> codecs;
27
28     private UnionStringCodec(final UnionTypeDefinition typeDef,
29             final ImmutableList<TypeDefinitionAwareCodec<Object, ?>> codecs) {
30         super(requireNonNull(typeDef), Object.class);
31         this.codecs = requireNonNull(codecs);
32     }
33
34     static @Nullable TypeDefinitionAwareCodec<?, UnionTypeDefinition> from(final UnionTypeDefinition typeDef) {
35         final var types = typeDef.getTypes();
36         final var builder = ImmutableList.<TypeDefinitionAwareCodec<Object, ?>>builderWithExpectedSize(types.size());
37         for (var type : types) {
38             final var codec = from(type);
39             if (codec == null) {
40                 LOG.debug("Cannot handle {} because of unhandled component {}", typeDef, type);
41                 return null;
42             }
43             builder.add(codec);
44         }
45         return new UnionStringCodec(typeDef, builder.build());
46     }
47
48     @Override
49     protected Object deserializeImpl(final String stringRepresentation) {
50         List<IllegalArgumentException> suppressed = null;
51         for (var codec : codecs) {
52             try {
53                 return codec.deserialize(stringRepresentation);
54             } catch (final IllegalArgumentException e) {
55                 // invalid - try the next union type.
56                 LOG.debug("Value {} did not match codec {}", stringRepresentation, codec, e);
57                 if (suppressed == null) {
58                     suppressed = new ArrayList<>();
59                 }
60                 suppressed.add(e);
61             }
62         }
63
64         final var ex = new IllegalArgumentException("Invalid value \"" + stringRepresentation + "\" for union type.");
65         if (suppressed != null) {
66             suppressed.forEach(ex::addSuppressed);
67         }
68         throw ex;
69     }
70
71     @Override
72     protected String serializeImpl(final Object data) {
73         return data instanceof byte[] ? Base64.getEncoder().encodeToString((byte[]) data) : data.toString();
74     }
75 }