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