Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / AbstractEROPathKeySubobjectParser.java
1 /*
2  * Copyright (c) 2015 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.spi;
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.PCEPDeserializerException;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.Subobject;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.SubobjectBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.SubobjectType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
23 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
24
25 /**
26  * Parser for {@link PathKey}.
27  */
28 public abstract class AbstractEROPathKeySubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
29     public static final int TYPE_32 = 64;
30     public static final int TYPE_128 = 65;
31
32     protected static final int PCE_ID_F_LENGTH = 4;
33     protected static final int PCE128_ID_F_LENGTH = 16;
34     protected static final int CONTENT128_LENGTH = 2 + PCE128_ID_F_LENGTH;
35     protected static final int CONTENT_LENGTH = 2 + PCE_ID_F_LENGTH;
36
37     protected abstract byte[] readPceId(ByteBuf buffer);
38
39     protected abstract void checkContentLength(int length) throws PCEPDeserializerException;
40
41     @Override
42     public final Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
43         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
44         checkContentLength(buffer.readableBytes());
45         return new SubobjectBuilder()
46                 .setLoose(loose)
47                 .setSubobjectType(new PathKeyCaseBuilder()
48                     .setPathKey(new PathKeyBuilder()
49                         .setPathKey(new PathKey(ByteBufUtils.readUint16(buffer)))
50                         .setPceId(new PceId(readPceId(buffer)))
51                         .build())
52                     .build())
53                 .build();
54     }
55
56     @Override
57     public final void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
58         final SubobjectType type = subobject.getSubobjectType();
59         checkArgument(type instanceof PathKeyCase, "Unknown subobject instance. Passed %s. Needed PathKey.",
60             type.getClass());
61         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
62             .subobjects.subobject.type.path.key._case.PathKey pk = ((PathKeyCase) type).getPathKey();
63
64         final PathKey pathKey = pk.getPathKey();
65         checkArgument(pathKey != null, "PathKey is mandatory.");
66         final byte[] pceID = pk.getPceId().getValue();
67         checkArgument(pceID.length == PCE_ID_F_LENGTH || pceID.length == PCE128_ID_F_LENGTH,
68                 "PceId 32/128 Bit required.");
69         final ByteBuf body = Unpooled.buffer();
70         ByteBufUtils.write(body, pathKey.getValue());
71         body.writeBytes(pceID);
72         EROSubobjectUtil.formatSubobject(pceID.length == PCE_ID_F_LENGTH ? TYPE_32 : TYPE_128, subobject.getLoose(),
73                 body, buffer);
74     }
75 }