Migrate to use yangtools' ByteBufUtils
[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 io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.ArrayList;
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 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
28
29 /**
30  * Parser for {@link Svec}.
31  */
32 public final class PCEPSvecObjectParser extends CommonObjectParser implements ObjectSerializer {
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 P_FLAG_OFFSET = 19;
50     private static final int D_FLAG_OFFSET = 20;
51     private static final int S_FLAG_OFFSET = 21;
52     private static final int N_FLAG_OFFSET = 22;
53     private static final int L_FLAG_OFFSET = 23;
54
55     /*
56      * min size in bytes
57      */
58     private static final int MIN_SIZE = FLAGS_SIZE / Byte.SIZE + FLAGS_F_OFFSET;
59
60     public PCEPSvecObjectParser() {
61         super(CLASS, TYPE);
62     }
63
64     @Override
65     public Svec parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
66         Preconditions.checkArgument(bytes != null && bytes.isReadable(),
67             "Array of bytes is mandatory. Can't be null or empty.");
68         if (bytes.readableBytes() < MIN_SIZE) {
69             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: "
70                 + bytes.readableBytes() + "; Expected: >=" + MIN_SIZE + ".");
71         }
72         bytes.skipBytes(FLAGS_F_OFFSET);
73         final BitArray flags = BitArray.valueOf(bytes, FLAGS_SIZE);
74         final List<RequestId> requestIDs = new ArrayList<>();
75
76         while (bytes.isReadable()) {
77             requestIDs.add(new RequestId(ByteBufUtils.readUint32(bytes)));
78         }
79         if (requestIDs.isEmpty()) {
80             throw new PCEPDeserializerException("Empty Svec Object - no request ids.");
81         }
82         final SvecBuilder builder = new SvecBuilder()
83                 .setIgnore(header.isIgnore())
84                 .setProcessingRule(header.isProcessingRule())
85                 .setLinkDiverse(flags.get(L_FLAG_OFFSET))
86                 .setNodeDiverse(flags.get(N_FLAG_OFFSET))
87                 .setSrlgDiverse(flags.get(S_FLAG_OFFSET))
88                 .setLinkDirectionDiverse(flags.get(D_FLAG_OFFSET))
89                 .setPartialPathDiverse(flags.get(P_FLAG_OFFSET))
90                 .setRequestsIds(requestIDs);
91         return builder.build();
92     }
93
94     @Override
95     public void serializeObject(final Object object, final ByteBuf buffer) {
96         Preconditions.checkArgument(object instanceof Svec,
97             "Wrong instance of PCEPObject. Passed %s. Needed SvecObject.", object.getClass());
98         final Svec svecObj = (Svec) object;
99         final ByteBuf body = Unpooled.buffer();
100         body.writeZero(FLAGS_F_OFFSET);
101         final BitArray flags = new BitArray(FLAGS_SIZE);
102         flags.set(L_FLAG_OFFSET, svecObj.isLinkDiverse());
103         flags.set(N_FLAG_OFFSET, svecObj.isNodeDiverse());
104         flags.set(S_FLAG_OFFSET, svecObj.isSrlgDiverse());
105         flags.set(D_FLAG_OFFSET, svecObj.isLinkDirectionDiverse());
106         flags.set(P_FLAG_OFFSET, svecObj.isPartialPathDiverse());
107         flags.toByteBuf(body);
108
109         final List<RequestId> requestIDs = svecObj.getRequestsIds();
110         assert !requestIDs.isEmpty() : "Empty Svec Object - no request ids.";
111         for (final RequestId requestId : requestIDs) {
112             writeUnsignedInt(requestId.getValue(), body);
113         }
114         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
115     }
116 }