Merge "Bug 809: Enhancements to the toaster example"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / toxml / SimpleBinaryAttributeWritingStrategy.java
1 /*
2  * Copyright (c) 2013 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
9 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.io.BaseEncoding;
13 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
14 import org.w3c.dom.Document;
15
16 import java.util.List;
17
18 public class SimpleBinaryAttributeWritingStrategy extends SimpleAttributeWritingStrategy {
19
20     /**
21      * @param document
22      * @param key
23      */
24     public SimpleBinaryAttributeWritingStrategy(Document document, String key) {
25         super(document, key);
26     }
27
28     @Override
29     protected Object preprocess(Object value) {
30         Util.checkType(value, List.class);
31         BaseEncoding en = BaseEncoding.base64();
32
33         List<?> list = (List<?>) value;
34         byte[] decoded = new byte[list.size()];
35         int i = 0;
36         for (Object bAsStr : list) {
37             Preconditions.checkArgument(bAsStr instanceof String, "Unexpected inner value for %s, expected string", value);
38             byte b = Byte.parseByte((String) bAsStr);
39             decoded[i++] = b;
40         }
41
42         return en.encode(decoded);
43     }
44
45 }