Bug 2756 - Action model update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / action / OF13SetFieldActionDeserializer.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.deserialization.action;
10
11 import io.netty.buffer.ByteBuf;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
19 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntryDeserializerKey;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.ActionChoice;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetFieldCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.field._case.SetFieldActionBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
27
28 /**
29  * @author michal.polkorab
30  *
31  */
32 public class OF13SetFieldActionDeserializer extends AbstractActionDeserializer
33         implements DeserializerRegistryInjector {
34
35     private DeserializerRegistry registry;
36
37     @Override
38     public Action deserialize(ByteBuf input) {
39         org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder builder = new ActionBuilder();
40         int startIndex = input.readerIndex();
41         input.skipBytes(2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
42         SetFieldCaseBuilder caseBuilder = new SetFieldCaseBuilder();
43         SetFieldActionBuilder actionBuilder = new SetFieldActionBuilder();
44         int oxmClass = input.getUnsignedShort(input.readerIndex());
45         // get oxm_field & hasMask byte and extract the field value
46         int oxmField = input.getUnsignedByte(input.readerIndex()
47                 + EncodeConstants.SIZE_OF_SHORT_IN_BYTES) >>> 1;
48         MatchEntryDeserializerKey key = new MatchEntryDeserializerKey(EncodeConstants.OF13_VERSION_ID,
49                 oxmClass, oxmField);
50         if (oxmClass == EncodeConstants.EXPERIMENTER_VALUE) {
51             long expId = input.getUnsignedInt(input.readerIndex() + EncodeConstants.SIZE_OF_SHORT_IN_BYTES
52                     + 2 * EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
53             key.setExperimenterId(expId);
54         }
55         OFDeserializer<MatchEntry> matchDeserializer = registry.getDeserializer(key);
56         List<MatchEntry> entry = new ArrayList<>();
57         entry.add(matchDeserializer.deserialize(input));
58         actionBuilder.setMatchEntry(entry);
59         caseBuilder.setSetFieldAction(actionBuilder.build());
60         builder.setActionChoice(caseBuilder.build());
61         int paddingRemainder = (input.readerIndex() - startIndex) % EncodeConstants.PADDING;
62         if (paddingRemainder != 0) {
63             input.skipBytes(EncodeConstants.PADDING - paddingRemainder);
64         }
65         return builder.build();
66     }
67
68     @Override
69     protected ActionChoice getType() {
70         return new SetFieldCaseBuilder().build();
71     }
72
73     @Override
74     public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
75         this.registry = deserializerRegistry;
76     }
77
78 }