Add new revision for pcep types model
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPSvecObjectParser.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 package org.opendaylight.protocol.pcep.parser.object;
9
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
11
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Lists;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.List;
17 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
18 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
19 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.protocol.util.BitArray;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.RequestId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.svec.object.Svec;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.svec.object.SvecBuilder;
27
28 /**
29  * Parser for {@link Svec}
30  */
31 public final class PCEPSvecObjectParser extends CommonObjectParser implements ObjectSerializer {
32
33     private static final int CLASS = 11;
34     private static final int TYPE = 1;
35
36     /*
37      * field lengths in bytes
38      */
39     private static final int FLAGS_SIZE = 24;
40
41     /*
42      * fields offsets in bytes
43      */
44     private static final int FLAGS_F_OFFSET = 1;
45
46     /*
47      * flags offsets inside flags field in bits
48      */
49     private static final int S_FLAG_OFFSET = 21;
50     private static final int N_FLAG_OFFSET = 22;
51     private static final int L_FLAG_OFFSET = 23;
52
53     /*
54      * min size in bytes
55      */
56     private static final int MIN_SIZE = FLAGS_SIZE / Byte.SIZE + FLAGS_F_OFFSET;
57
58     public PCEPSvecObjectParser() {
59         super(CLASS, TYPE);
60     }
61
62     @Override
63     public Svec parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
64         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
65         if (bytes.readableBytes() < MIN_SIZE) {
66             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes() + "; Expected: >="
67                     + MIN_SIZE + ".");
68         }
69         bytes.skipBytes(FLAGS_F_OFFSET);
70         final BitArray flags = BitArray.valueOf(bytes, FLAGS_SIZE);
71         final List<RequestId> requestIDs = Lists.newArrayList();
72
73         while (bytes.isReadable()) {
74             requestIDs.add(new RequestId(bytes.readUnsignedInt()));
75         }
76         if (requestIDs.isEmpty()) {
77             throw new PCEPDeserializerException("Empty Svec Object - no request ids.");
78         }
79         final SvecBuilder builder = new SvecBuilder();
80
81         builder.setIgnore(header.isIgnore());
82         builder.setProcessingRule(header.isProcessingRule());
83
84         builder.setLinkDiverse(flags.get(L_FLAG_OFFSET));
85         builder.setNodeDiverse(flags.get(N_FLAG_OFFSET));
86         builder.setSrlgDiverse(flags.get(S_FLAG_OFFSET));
87         builder.setRequestsIds(requestIDs);
88         return builder.build();
89     }
90
91     @Override
92     public void serializeObject(final Object object, final ByteBuf buffer) {
93         Preconditions.checkArgument(object instanceof Svec, "Wrong instance of PCEPObject. Passed %s. Needed SvecObject.", object.getClass());
94         final Svec svecObj = (Svec) object;
95         final ByteBuf body = Unpooled.buffer();
96         body.writeZero(FLAGS_F_OFFSET);
97         final BitArray flags = new BitArray(FLAGS_SIZE);
98         flags.set(L_FLAG_OFFSET, svecObj.isLinkDiverse());
99         flags.set(N_FLAG_OFFSET, svecObj.isNodeDiverse());
100         flags.set(S_FLAG_OFFSET, svecObj.isSrlgDiverse());
101         flags.toByteBuf(body);
102
103         final List<RequestId> requestIDs = svecObj.getRequestsIds();
104         assert !(requestIDs.isEmpty()) : "Empty Svec Object - no request ids.";
105         for(final RequestId requestId : requestIDs) {
106             writeUnsignedInt(requestId.getValue(), body);
107         }
108         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
109     }
110 }