Remove Objects.{is,non}Null abuse
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / match / MatchDeserializer.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.openflowplugin.impl.protocol.deserialization.match;
9
10 import io.netty.buffer.ByteBuf;
11 import java.util.HashMap;
12 import java.util.Map;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
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.MatchEntryDeserializerKey;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.openflowplugin.api.openflow.protocol.deserialization.MatchEntryDeserializer;
20 import org.opendaylight.openflowplugin.api.openflow.protocol.deserialization.MatchEntryDeserializerRegistry;
21 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
22 import org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class MatchDeserializer implements OFDeserializer<Match>, HeaderDeserializer<Match>,
30         MatchEntryDeserializerRegistry, MatchEntryDeserializer, DeserializerRegistryInjector {
31
32     private static final Logger LOG = LoggerFactory.getLogger(MatchDeserializer.class);
33     private final Map<MatchEntryDeserializerKey, MatchEntryDeserializer> entryRegistry = new HashMap<>();
34     private final MatchPath matchPath;
35     private DeserializerRegistry registry;
36
37     public MatchDeserializer(final MatchPath matchPath) {
38         this.matchPath = matchPath;
39     }
40
41     @Override
42     public Match deserialize(ByteBuf inBuffer) {
43         if (inBuffer.readableBytes() <= 0) {
44             return null;
45         }
46
47         final MatchBuilder builder = new MatchBuilder();
48
49         // OFP do not have any method to differentiate between OXM and standard match, so we do not care about type
50         inBuffer.readUnsignedShort();
51         final int length = inBuffer.readUnsignedShort();
52
53         final int startIndex = inBuffer.readerIndex();
54         final int entriesLength = length - 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES;
55
56         while (inBuffer.readerIndex() - startIndex < entriesLength) {
57             deserializeEntry(inBuffer, builder);
58         }
59
60         int paddingRemainder = length % EncodeConstants.PADDING;
61
62         if (paddingRemainder != 0) {
63             inBuffer.skipBytes(EncodeConstants.PADDING - paddingRemainder);
64         }
65
66         return builder.build();
67     }
68
69     @Override
70     public Match deserializeHeader(ByteBuf inBuffer) {
71         final MatchBuilder builder = new MatchBuilder();
72         deserializeEntry(inBuffer, builder);
73         return builder.build();
74     }
75
76     @Override
77     public void deserializeEntry(ByteBuf inBuffer, MatchBuilder builder) {
78         if (inBuffer.readableBytes() <= 0) {
79             return;
80         }
81         int oxmClass = inBuffer.getUnsignedShort(inBuffer.readerIndex());
82         int oxmField = inBuffer.getUnsignedByte(inBuffer.readerIndex()
83                 + EncodeConstants.SIZE_OF_SHORT_IN_BYTES) >>> 1;
84
85         final MatchEntryDeserializerKey key = new MatchEntryDeserializerKey(
86                 EncodeConstants.OF13_VERSION_ID, oxmClass, oxmField);
87
88         if (oxmClass == EncodeConstants.EXPERIMENTER_VALUE) {
89             long expId = inBuffer.getUnsignedInt(inBuffer.readerIndex()
90                     + EncodeConstants.SIZE_OF_SHORT_IN_BYTES
91                     + 2 * EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
92
93             key.setExperimenterId(expId);
94         }
95
96         final MatchEntryDeserializer entryDeserializer = entryRegistry.get(key);
97
98         if (entryDeserializer != null) {
99             entryDeserializer.deserializeEntry(inBuffer, builder);
100         } else {
101             final OFDeserializer<MatchEntry> deserializer = registry.getDeserializer(key);
102             MatchExtensionHelper.injectExtension(EncodeConstants.OF13_VERSION_ID,
103                     deserializer.deserialize(inBuffer), builder, matchPath);
104         }
105     }
106
107     @Override
108     public void registerEntryDeserializer(MatchEntryDeserializerKey key, MatchEntryDeserializer deserializer) {
109         if (key == null || deserializer == null) {
110             throw new IllegalArgumentException("MatchEntryDeserializerKey or Deserializer is null");
111         }
112
113         final MatchEntryDeserializer desInRegistry = entryRegistry.put(key, deserializer);
114
115         if (desInRegistry != null) {
116             LOG.debug("Deserializer for key {} overwritten. Old deserializer: {}, new deserializer: {}", key,
117                     desInRegistry.getClass().getName(), deserializer.getClass().getName());
118         }
119     }
120
121     @Override
122     public boolean unregisterEntryDeserializer(MatchEntryDeserializerKey key) {
123         if (key == null) {
124             throw new IllegalArgumentException("MatchEntryDeserializerKey is null");
125         }
126
127         return entryRegistry.remove(key) != null;
128     }
129
130     @Override
131     public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
132         registry = deserializerRegistry;
133     }
134 }