re-activate FindBugs
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / ListDeserializer.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.util;
10
11 import io.netty.buffer.ByteBuf;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.HeaderDeserializer;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
17 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Deserializes list.
25  *
26  * @author michal.polkorab
27  */
28 public final class ListDeserializer {
29     private static final Logger LOG = LoggerFactory.getLogger(ListDeserializer.class);
30
31     private ListDeserializer() {
32         throw new UnsupportedOperationException("Utility class shouldn't be instantiated");
33     }
34
35     /**
36      * Deserializes items into list.
37      *
38      * @param version openflow wire version
39      * @param length length of list in ByteBuf (bytes)
40      * @param input input buffer
41      * @param keyMaker creates keys for deserializer lookup
42      * @param registry stores deserializers
43      * @return list of items
44      */
45     public static <E extends DataObject> List<E> deserializeList(short version, int length,
46             ByteBuf input, CodeKeyMaker keyMaker, DeserializerRegistry registry) {
47         List<E> items = null;
48         if (input.readableBytes() > 0) {
49             items = new ArrayList<>();
50             int startIndex = input.readerIndex();
51             while (input.readerIndex() - startIndex < length) {
52                 OFDeserializer<E> deserializer = registry.getDeserializer(keyMaker.make(input));
53                 E item = deserializer.deserialize(input);
54                 items.add(item);
55             }
56         }
57         return items;
58     }
59
60     /**
61      * Deserializes headers of items into list (used in MultipartReplyMessage - Table features).
62      *
63      * @param version openflow wire version
64      * @param length length of list in ByteBuf (bytes)
65      * @param input input buffer
66      * @param keyMaker creates keys for deserializer lookup
67      * @param registry stores deserializers
68      * @return list of items
69      */
70     public static <E extends DataObject> List<E> deserializeHeaders(short version, int length,
71             ByteBuf input, CodeKeyMaker keyMaker, DeserializerRegistry registry) {
72         List<E> items = null;
73         if (input.readableBytes() > 0) {
74             items = new ArrayList<>();
75             int startIndex = input.readerIndex();
76             boolean exceptionLogged = false;
77             while (input.readerIndex() - startIndex < length) {
78                 HeaderDeserializer<E> deserializer;
79                 MessageCodeKey key = keyMaker.make(input);
80                 try {
81                     deserializer = registry.getDeserializer(key);
82                 } catch (ClassCastException | IllegalStateException e) {
83                     // Following "if" is only hotfix to prevent log flooding. Log flooding is originally
84                     // caused by using OVS 2.4 which directly uses / reports Nicira extensions. These extensions
85                     // are not yet (2nd February 2016) fully supported by existing OF Plugin.
86                     // TODO - simplify to correctly report exception during deserialization
87                     if (!exceptionLogged) {
88                         LOG.warn("Problem during reading table feature property. Skipping unknown feature property: {}."
89                             + "If more information is needed, set org.opendaylight.openflowjava do DEBUG log level "
90                             + " ({}).", key, e.getMessage());
91                         if (LOG.isDebugEnabled()) {
92                             LOG.debug("Detailed exception is logged only once for each multipart reply (table features)"
93                                 + "to prevent log flooding. There might be more of table features related exceptions.",
94                                 e);
95                         }
96                         exceptionLogged = true;
97                     }
98                     input.skipBytes(2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
99                     continue;
100                 }
101                 E item = deserializer.deserializeHeader(input);
102                 items.add(item);
103             }
104         }
105         return items;
106     }
107 }