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