Initial code drop of netconf protocol implementation
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / messages / NetconfMessageHeader.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.util.messages;
10
11 import com.google.common.base.Charsets;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.protocol.util.ByteArray;
14
15 import java.nio.ByteBuffer;
16
17 /**
18  * Netconf message header is used only when chunked framing mechanism is
19  * supported. The header consists of only the length field.
20  */
21 public final class NetconfMessageHeader {
22
23     private long length;
24
25     // \n#<length>\n
26     private static final byte[] headerBegin = new byte[] { (byte) 0x0a, (byte) 0x23 };
27
28     private static final byte headerEnd = (byte) 0x0a;
29
30     public static final int MIN_HEADER_LENGTH = 4; // bytes
31
32     public static final int MAX_HEADER_LENGTH = 13; // bytes
33
34     private boolean parsed = false;
35
36     public NetconfMessageHeader() {
37
38     }
39
40     public NetconfMessageHeader fromBytes(final byte[] bytes) {
41         // the length is variable therefore bytes between headerBegin and
42         // headerEnd mark the length
43         // the length should be only numbers and therefore easily parsed with
44         // ASCII
45         this.length = Long.parseLong(Charsets.US_ASCII.decode(
46                 ByteBuffer.wrap(ByteArray.subByte(bytes, headerBegin.length, bytes.length - headerBegin.length - 1)))
47                 .toString());
48         Preconditions.checkState(this.length < Integer.MAX_VALUE && this.length > 0);
49         this.parsed = true;
50         return this;
51     }
52
53     public byte[] toBytes() {
54         final byte[] l = String.valueOf(this.length).getBytes(Charsets.US_ASCII);
55         final byte[] h = new byte[headerBegin.length + l.length + 1];
56         System.arraycopy(headerBegin, 0, h, 0, headerBegin.length);
57         System.arraycopy(l, 0, h, headerBegin.length, l.length);
58         System.arraycopy(new byte[] { headerEnd }, 0, h, headerBegin.length + l.length, 1);
59         return h;
60     }
61
62     // FIXME: improve precision to long
63     public int getLength() {
64         return (int) this.length;
65     }
66
67     public void setLength(final int length) {
68         this.length = length;
69     }
70
71     /**
72      * @return the parsed
73      */
74     public boolean isParsed() {
75         return this.parsed;
76     }
77
78     /**
79      * @param parsed
80      *            the parsed to set
81      */
82     public void setParsed() {
83         this.parsed = false;
84     }
85 }