Cleanup use of Guava library
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / BinaryStringCodec.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 com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableRangeSet;
14 import com.google.common.collect.Range;
15 import com.google.common.collect.RangeSet;
16 import com.google.common.collect.TreeRangeSet;
17 import com.google.common.io.BaseEncoding;
18 import java.util.Optional;
19 import javax.xml.bind.DatatypeConverter;
20 import org.opendaylight.yangtools.yang.data.api.codec.BinaryCodec;
21 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
23
24 /**
25  * Do not use this class outside of yangtools, its presence does not fall into the API stability contract.
26  */
27 @Beta
28 public abstract class BinaryStringCodec extends TypeDefinitionAwareCodec<byte[], BinaryTypeDefinition>
29         implements BinaryCodec<String> {
30     private static final class Restricted extends BinaryStringCodec {
31         private final RangeSet<Integer> ranges;
32
33         Restricted(final BinaryTypeDefinition typeDef) {
34             super(typeDef);
35
36             final RangeSet<Integer> r = TreeRangeSet.create();
37             for (LengthConstraint c : typeDef.getLengthConstraints()) {
38                 r.add(Range.closed(c.getMin().intValue(), c.getMax().intValue()));
39             }
40
41             ranges = ImmutableRangeSet.copyOf(r);
42         }
43
44         @Override
45         void validate(final byte[] value) {
46             checkArgument(ranges.contains(value.length), "Value length %s does not match constraints %s", value.length,
47                 ranges);
48         }
49     }
50
51     private static final class Unrestricted extends BinaryStringCodec {
52         Unrestricted(final BinaryTypeDefinition typeDef) {
53             super(typeDef);
54         }
55
56         @Override
57         void validate(final byte[] value) {
58             // No-op
59         }
60     }
61
62     BinaryStringCodec(final BinaryTypeDefinition typeDef) {
63         super(Optional.of(typeDef), byte[].class);
64     }
65
66     public static BinaryStringCodec from(final BinaryTypeDefinition type) {
67         return type.getLengthConstraints().isEmpty() ? new Unrestricted(type) : new Restricted(type);
68     }
69
70     @Override
71     public String serialize(final byte[] data) {
72         return data == null ? "" : BaseEncoding.base64().encode(data);
73     }
74
75     @Override
76     public byte[] deserialize(final String stringRepresentation) {
77         if (stringRepresentation == null) {
78             return null;
79         }
80
81         final byte[] ret = DatatypeConverter.parseBase64Binary(stringRepresentation);
82         validate(ret);
83         return ret;
84     }
85
86     abstract void validate(byte[] value);
87 }