Bug-731 Fixed few minor Sonar warnings
[bgpcep.git] / util / src / main / java / org / opendaylight / protocol / util / PCEPHexDumpParser.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 package org.opendaylight.protocol.util;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import com.google.common.io.CharStreams;
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.InputStreamReader;
18 import java.util.List;
19 import org.apache.commons.codec.DecoderException;
20 import org.apache.commons.codec.binary.Hex;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Parses PCEP messages from a text file. Messages need to follow this formatting:
26  *
27  * Received PCEP Open message. Length:28.
28  *
29  * 20 01 00 1c 01 10 00 18 20 1e 78 03 00 10 00 04 00 00 00 05 00 1a 00 04 00 00 00 b4
30  */
31 public final class PCEPHexDumpParser {
32     private static final int MINIMAL_LENGTH = 4;
33     private static final Logger LOG = LoggerFactory.getLogger(PCEPHexDumpParser.class);
34     private static final String LENGTH = "LENGTH:";
35
36     private PCEPHexDumpParser() {
37
38     }
39
40     public static List<byte[]> parseMessages(final File file) throws IOException {
41         Preconditions.checkArgument(file != null, "Filename cannot be null");
42         return parseMessages(new FileInputStream(file));
43     }
44
45     public static List<byte[]> parseMessages(final InputStream is) throws IOException {
46         Preconditions.checkNotNull(is);
47         try (InputStreamReader isr = new InputStreamReader(is)) {
48             return parseMessages(CharStreams.toString(isr));
49         } finally {
50             is.close();
51         }
52     }
53
54     private static List<byte[]> parseMessages(final String c) {
55         final String content = clearWhiteSpaceToUpper(c);
56
57         final List<byte[]> messages = Lists.newLinkedList();
58         int idx = content.indexOf(LENGTH, 0);
59         while (idx > -1) {
60             // next chars are final length, ending with '.'
61             final int lengthIdx = idx + LENGTH.length();
62             final int messageIdx = content.indexOf('.', lengthIdx);
63             final int length = Integer.parseInt(content.substring(lengthIdx, messageIdx));
64             // dot
65             final int messageEndIdx = messageIdx + (length * 2) + 1;
66
67             // Assert that message is longer than minimum 4(header.length == 4)
68             // If length in PCEP message would be 0, loop would never end
69             Preconditions.checkArgument(length >= MINIMAL_LENGTH, "Invalid message at index " + idx + ", length atribute is lower than "
70                 + MINIMAL_LENGTH);
71
72             // dot
73             final String hexMessage = content.substring(messageIdx + 1, messageEndIdx);
74             byte[] message = null;
75             try {
76                 message = Hex.decodeHex(hexMessage.toCharArray());
77             } catch (final DecoderException e) {
78                 throw new IllegalArgumentException("Failed to decode message", e);
79             }
80             messages.add(message);
81             idx = messageEndIdx;
82             idx = content.indexOf(LENGTH, idx);
83         }
84         LOG.info("Succesfully extracted {} messages", messages.size());
85         return messages;
86     }
87
88     private static String clearWhiteSpaceToUpper(final String line) {
89         return line.replaceAll("\\s", "").toUpperCase();
90     }
91 }