80829a2a03aa9a6e1e10ea8b32e2a8b2ab14bab2
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / subobject / XROPathKey128SubobjectParser.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.subobject;
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 org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.Subobject;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.SubobjectBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.SubobjectType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
26 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
27
28 /**
29  * Parser for {@link PathKey}.
30  */
31 public class XROPathKey128SubobjectParser implements XROSubobjectParser {
32     public static final int TYPE = 65;
33
34     protected static final int PCE128_ID_F_LENGTH = 16;
35
36     private static final int CONTENT128_LENGTH = 2 + PCE128_ID_F_LENGTH;
37
38     @Override
39     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
40         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
41         if (buffer.readableBytes() != CONTENT128_LENGTH) {
42             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
43                 + "; Expected: >" + CONTENT128_LENGTH + ".");
44         }
45         return new SubobjectBuilder()
46                 .setMandatory(mandatory)
47                 .setSubobjectType(new PathKeyCaseBuilder()
48                     .setPathKey(new PathKeyBuilder()
49                         .setPathKey(new PathKey(ByteBufUtils.readUint16(buffer)))
50                         .setPceId(new PceId(ByteArray.readBytes(buffer, PCE128_ID_F_LENGTH)))
51                         .build())
52                     .build())
53                 .build();
54     }
55
56     public static void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
57         final SubobjectType type = subobject.getSubobjectType();
58         checkArgument(type instanceof PathKeyCase, "Unknown subobject instance. Passed %s. Needed PathKey.",
59             type.getClass());
60         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
61             .subobjects.subobject.type.path.key._case.PathKey pk = ((PathKeyCase) type).getPathKey();
62         final ByteBuf body = Unpooled.buffer();
63         final PathKey pathKey = pk.getPathKey();
64         checkArgument(pathKey != null, "PathKey is mandatory.");
65         ByteBufUtils.write(body, pathKey.getValue());
66         final PceId pceId = pk.getPceId();
67         checkArgument(pceId != null, "PceId is mandatory.");
68         body.writeBytes(pceId.getValue());
69         XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
70     }
71 }