Mass replace CRLF->LF
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / action / OF13SetFieldActionSerializer.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.serialization.action;
10
11 import io.netty.buffer.ByteBuf;
12
13 import org.opendaylight.openflowjava.protocol.api.extensibility.HeaderSerializer;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
17 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterIdMatchEntry;
20 import org.opendaylight.openflowjava.protocol.impl.util.ActionConstants;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OxmFieldsAction;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ExperimenterClass;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
25
26 /**
27  * @author michal.polkorab
28  *
29  */
30 public class OF13SetFieldActionSerializer implements OFSerializer<Action>,
31         HeaderSerializer<Action>, SerializerRegistryInjector {
32
33     private SerializerRegistry registry;
34
35     @Override
36     public void serialize(Action action, ByteBuf outBuffer) {
37         int startIndex = outBuffer.writerIndex();
38         outBuffer.writeShort(ActionConstants.SET_FIELD_CODE);
39         int lengthIndex = outBuffer.writerIndex();
40         outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
41         OxmFieldsAction oxmField = action.getAugmentation(OxmFieldsAction.class);
42         MatchEntries entry = oxmField.getMatchEntries().get(0);
43         MatchEntrySerializerKey<?, ?> key = new MatchEntrySerializerKey<>(
44                 EncodeConstants.OF13_VERSION_ID, entry.getOxmClass(), entry.getOxmMatchField());
45         if (entry.getOxmClass().equals(ExperimenterClass.class)) {
46             key.setExperimenterId(entry.getAugmentation(ExperimenterIdMatchEntry.class)
47                     .getExperimenter().getValue());
48         } else {
49             key.setExperimenterId(null);
50         }
51         OFSerializer<MatchEntries> serializer = registry.getSerializer(key);
52         serializer.serialize(entry, outBuffer);
53         int paddingRemainder = (outBuffer.writerIndex() - startIndex) % EncodeConstants.PADDING;
54         if (paddingRemainder != 0) {
55             outBuffer.writeZero(EncodeConstants.PADDING - paddingRemainder);
56         }
57         outBuffer.setShort(lengthIndex, outBuffer.writerIndex() - startIndex);
58     }
59
60     @Override
61     public void serializeHeader(Action input, ByteBuf outBuffer) {
62         outBuffer.writeShort(ActionConstants.SET_FIELD_CODE);
63         outBuffer.writeShort(ActionConstants.ACTION_IDS_LENGTH);
64     }
65
66     @Override
67     public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
68         registry = serializerRegistry;
69     }
70
71 }