e883c7a29f2fde0f8ffc1737f70b8e31a3124796
[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 org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
17 import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
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
27 /**
28  * Parser for {@link PathKey}
29  */
30 public class XROPathKey128SubobjectParser implements XROSubobjectParser {
31
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         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
41         if (buffer.readableBytes() != CONTENT128_LENGTH) {
42             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >"
43                     + CONTENT128_LENGTH + ".");
44         }
45         final int pathKey = buffer.readUnsignedShort();
46         final byte[] pceId = ByteArray.readBytes(buffer, PCE128_ID_F_LENGTH);
47         final SubobjectBuilder builder = new SubobjectBuilder();
48         final PathKeyBuilder pBuilder = new PathKeyBuilder();
49         pBuilder.setPceId(new PceId(pceId));
50         pBuilder.setPathKey(new PathKey(pathKey));
51         builder.setMandatory(mandatory);
52         builder.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
53         return builder.build();
54     }
55
56     public static void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
57         Preconditions.checkArgument(subobject.getSubobjectType() instanceof PathKeyCase, "Unknown subobject instance. Passed %s. Needed PathKey.", subobject.getSubobjectType().getClass());
58         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
59             .subobjects.subobject.type.path.key._case.PathKey pk = ((PathKeyCase) subobject.getSubobjectType()).getPathKey();
60         final ByteBuf body = Unpooled.buffer();
61         Preconditions.checkArgument(pk.getPathKey() != null, "PathKey is mandatory.");
62         writeUnsignedShort(pk.getPathKey().getValue(), body);
63         Preconditions.checkArgument(pk.getPceId() != null, "PceId is mandatory.");
64         body.writeBytes(pk.getPceId().getValue());
65         XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
66     }
67 }