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