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