Merge "Set root pom.xml <name> for Sonar"
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPRequestParameterObjectParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.protocol.pcep.impl.object;
10
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeBitSet;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
13
14 import com.google.common.base.Preconditions;
15 import com.google.common.primitives.UnsignedBytes;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.util.BitSet;
19 import java.util.List;
20 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
21 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
22 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
23 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
24 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
25 import org.opendaylight.protocol.util.ByteArray;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RequestId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.Order;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.setup.type.tlv.PathSetupType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.RpBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.rp.Tlvs;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.rp.TlvsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
37
38 /**
39  * Parser for {@link Rp}
40  */
41 public class PCEPRequestParameterObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
42
43     public static final int CLASS = 2;
44
45     public static final int TYPE = 1;
46
47     /*
48      * lengths of fields in bytes
49      */
50     private static final int FLAGS_PRI_MF_LENGTH = 4;
51
52     /*
53      * lengths of subfields inside multi-field in bits
54      */
55     private static final int FLAGS_SF_LENGTH = 29;
56
57     /*
58      * offsets of subfields inside multi-field in bits
59      */
60
61     private static final int FLAGS_SF_OFFSET = 0;
62     private static final int PRI_SF_OFFSET = FLAGS_SF_OFFSET + FLAGS_SF_LENGTH;
63
64     /*
65      * flags offsets inside flags sub-field in bits
66      */
67
68     private static final int O_FLAG_OFFSET = 26;
69     private static final int B_FLAG_OFFSET = 27;
70     private static final int R_FLAG_OFFSET = 28;
71
72     /*
73      * GCO extension flags offsets inside flags sub-field in bits
74      */
75     private static final int M_FLAG_OFFSET = 21;
76     private static final int D_FLAG_OFFSET = 22;
77
78     /*
79      * Path-key bit (RFC5520)
80      */
81     private static final int P_FLAG_OFFSET = 23;
82     /*
83      * OF extension flags offsets inside flags sub.field in bits
84      */
85     private static final int S_FLAG_OFFSET = 24;
86     /*
87      * RFC6006 flags
88      */
89     private static final int F_FLAG_OFFSET = 18;
90
91     private static final int N_FLAG_OFFSET = 19;
92
93     private static final int E_FLAG_OFFSET = 20;
94
95     public PCEPRequestParameterObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
96         super(tlvReg, viTlvReg);
97     }
98
99     @Override
100     public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
101         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
102         final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(bytes, FLAGS_PRI_MF_LENGTH));
103
104         final RpBuilder builder = new RpBuilder();
105         builder.setIgnore(header.isIgnore());
106         builder.setProcessingRule(header.isProcessingRule());
107
108         short priority = 0;
109         priority |= flags.get(PRI_SF_OFFSET + 2) ? 1 : 0;
110         priority |= (flags.get(PRI_SF_OFFSET + 1) ? 1 : 0) << 1;
111         priority |= (flags.get(PRI_SF_OFFSET) ? 1 : 0) << 2;
112         builder.setPriority(priority);
113         builder.setFragmentation(flags.get(F_FLAG_OFFSET));
114         builder.setP2mp(flags.get(N_FLAG_OFFSET));
115         builder.setEroCompression(flags.get(E_FLAG_OFFSET));
116         builder.setMakeBeforeBreak(flags.get(M_FLAG_OFFSET));
117         builder.setOrder(flags.get(D_FLAG_OFFSET));
118         builder.setPathKey(flags.get(P_FLAG_OFFSET));
119         builder.setSupplyOf(flags.get(S_FLAG_OFFSET));
120         builder.setLoose(flags.get(O_FLAG_OFFSET));
121         builder.setBiDirectional(flags.get(B_FLAG_OFFSET));
122         builder.setReoptimization(flags.get(R_FLAG_OFFSET));
123
124         builder.setRequestId(new RequestId(bytes.readUnsignedInt()));
125         final TlvsBuilder tlvsBuilder = new TlvsBuilder();
126         parseTlvs(tlvsBuilder, bytes.slice());
127         builder.setTlvs(tlvsBuilder.build());
128         return builder.build();
129     }
130
131     @Override
132     public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
133         if (tlv instanceof Order) {
134             builder.setOrder((Order) tlv);
135         }
136         if (tlv instanceof PathSetupType) {
137             builder.setPathSetupType((PathSetupType) tlv);
138         }
139     }
140
141     @Override
142     public void serializeObject(final Object object, final ByteBuf buffer) {
143         Preconditions.checkArgument(object instanceof Rp, "Wrong instance of PCEPObject. Passed %s. Needed RPObject.", object.getClass());
144         final ByteBuf body = Unpooled.buffer();
145         final Rp rpObj = (Rp) object;
146         final BitSet flags = new BitSet(FLAGS_PRI_MF_LENGTH * Byte.SIZE);
147         if (rpObj.isReoptimization() != null) {
148             flags.set(R_FLAG_OFFSET, rpObj.isReoptimization());
149         }
150         if (rpObj.isBiDirectional() != null) {
151             flags.set(B_FLAG_OFFSET, rpObj.isBiDirectional());
152         }
153         if (rpObj.isLoose() != null) {
154             flags.set(O_FLAG_OFFSET, rpObj.isLoose());
155         }
156         if (rpObj.isMakeBeforeBreak() != null) {
157             flags.set(M_FLAG_OFFSET, rpObj.isMakeBeforeBreak());
158         }
159         if (rpObj.isOrder() != null) {
160             flags.set(D_FLAG_OFFSET, rpObj.isOrder());
161         }
162         if (rpObj.isPathKey() != null) {
163             flags.set(P_FLAG_OFFSET, rpObj.isPathKey());
164         }
165         if (rpObj.isSupplyOf() != null) {
166             flags.set(S_FLAG_OFFSET, rpObj.isSupplyOf());
167         }
168         if (rpObj.isFragmentation() != null) {
169             flags.set(F_FLAG_OFFSET, rpObj.isFragmentation());
170         }
171         if (rpObj.isP2mp() != null) {
172             flags.set(N_FLAG_OFFSET, rpObj.isP2mp());
173         }
174         if (rpObj.isEroCompression() != null) {
175             flags.set(E_FLAG_OFFSET, rpObj.isEroCompression());
176         }
177         if (rpObj.getPriority() != null) {
178             final byte[] p = { 0, 0, 0, UnsignedBytes.checkedCast(rpObj.getPriority().shortValue())};
179             flags.or(ByteArray.bytesToBitSet(p));
180         }
181         writeBitSet(flags, FLAGS_PRI_MF_LENGTH, body);
182         Preconditions.checkArgument(rpObj.getRequestId() != null, "RequestId is mandatory");
183         writeUnsignedInt(rpObj.getRequestId().getValue(), body);
184         serializeTlvs(rpObj.getTlvs(), body);
185         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
186     }
187
188     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
189         if (tlvs == null) {
190             return;
191         }
192         if (tlvs.getOrder() != null) {
193             serializeTlv(tlvs.getOrder(), body);
194         }
195         if (tlvs.getPathSetupType() != null) {
196             serializeTlv(tlvs.getPathSetupType(), body);
197         }
198         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
199     }
200
201     @Override
202     protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
203         if (!tlvs.isEmpty()) {
204             builder.setVendorInformationTlv(tlvs);
205         }
206     }
207 }