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