Merge "Bumped to SNAPSHOT dependencies - some dependencies moved to <dependencyManag...
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / GroupModInputMessageFactory.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.factories;
10
11 import io.netty.buffer.ByteBuf;
12
13 import java.util.List;
14
15 import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;
16 import org.opendaylight.openflowjava.protocol.impl.util.ActionsSerializer;
17 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsList;
20
21 /**
22  * Translates GroupMod messages
23  * @author timotej.kubas
24  * @author michal.polkorab
25  */
26 public class GroupModInputMessageFactory implements OFSerializer<GroupModInput> {
27     private static final byte MESSAGE_TYPE = 15;
28     private static final int MESSAGE_LENGTH = 16;
29     private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1;
30     private static final byte LENGTH_OF_BUCKET_STRUCTURE = 16;
31     private static final byte PADDING_IN_BUCKET = 4;
32     private static GroupModInputMessageFactory instance;
33
34     private GroupModInputMessageFactory() {
35         // singleton
36     }
37     
38     /**
39      * @return singleton factory
40      */
41     public static synchronized GroupModInputMessageFactory getInstance() {
42         if (instance == null) {
43             instance = new GroupModInputMessageFactory();
44         }
45         return instance;
46     }
47     
48     @Override
49     public void messageToBuffer(short version, ByteBuf out,
50             GroupModInput message) {
51         ByteBufUtils.writeOFHeader(instance, message, out);
52         out.writeShort(message.getCommand().getIntValue());
53         out.writeByte(message.getType().getIntValue());
54         ByteBufUtils.padBuffer(PADDING_IN_GROUP_MOD_MESSAGE, out);
55         out.writeInt(message.getGroupId().getValue().intValue());
56         encodeBuckets(message.getBucketsList(), out);
57     }
58
59     @Override
60     public int computeLength(GroupModInput message) {
61         return MESSAGE_LENGTH + computeLengthOfBuckets(message.getBucketsList());
62     }
63
64     @Override
65     public byte getMessageType() {
66         return MESSAGE_TYPE;
67     }
68     
69     private static void encodeBuckets(List<BucketsList> buckets, ByteBuf outBuffer) {
70         if (buckets != null) {
71             for (BucketsList currentBucket : buckets) {
72                 outBuffer.writeShort(computeLengthOfBucket(currentBucket));
73                 outBuffer.writeShort(currentBucket.getWeight().shortValue());
74                 outBuffer.writeInt(currentBucket.getWatchPort().getValue().intValue());
75                 outBuffer.writeInt(currentBucket.getWatchGroup().intValue());
76                 ByteBufUtils.padBuffer(PADDING_IN_BUCKET, outBuffer);
77                 ActionsSerializer.encodeActions(currentBucket.getAction(), outBuffer);
78             }
79         }
80     }
81     
82     private static int computeLengthOfBucket(BucketsList bucket) {
83         int lengthOfBuckets = 0;
84         if (bucket != null) {
85             lengthOfBuckets = LENGTH_OF_BUCKET_STRUCTURE
86                     + ActionsSerializer.computeLengthOfActions(bucket.getAction());
87         }
88         return lengthOfBuckets;
89     }
90     
91     private static int computeLengthOfBuckets(List<BucketsList> buckets) {
92         int lengthOfBuckets = 0;
93         if (buckets != null) {
94             for (BucketsList currentBucket : buckets) {
95                 lengthOfBuckets += LENGTH_OF_BUCKET_STRUCTURE
96                         + ActionsSerializer.computeLengthOfActions(currentBucket.getAction());
97             }
98         }
99         return lengthOfBuckets;
100     }
101
102     
103 }