2 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
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
9 package org.opendaylight.protocol.pcep.parser.object;
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.List;
15 import java.util.stream.Collectors;
16 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.RROSubobjectRegistry;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.Subobject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.secondary.reported.route.object.Srro;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.secondary.reported.route.object.SrroBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.secondary.reported.route.object.srro.SubobjectBuilder;
26 public final class PCEPSecondaryRecordRouteObjectParser extends AbstractRROWithSubobjectsParser {
27 private static final int CLASS = 30;
28 private static final int TYPE = 1;
29 private static String EMPTY_ARRAY_ERROR = "Array of bytes is mandatory. Can't be null or empty.";
31 public PCEPSecondaryRecordRouteObjectParser(final RROSubobjectRegistry subobjReg) {
32 super(subobjReg, CLASS, TYPE);
36 public Srro parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
37 Preconditions.checkArgument(buffer != null && buffer.isReadable(), EMPTY_ARRAY_ERROR);
39 final SrroBuilder builder = new SrroBuilder();
40 builder.setIgnore(header.isIgnore());
41 builder.setProcessingRule(header.isProcessingRule());
42 final List<Subobject> subObjects = parseSubobjects(buffer);
44 builder.setSubobject(subObjects.stream()
45 .map(so -> new SubobjectBuilder()
46 .setSubobjectType(so.getSubobjectType())
47 .setProtectionAvailable(so.isProtectionAvailable())
48 .setProtectionInUse(so.isProtectionInUse())
50 .collect(Collectors.toList()));
52 return builder.build();
56 public void serializeObject(final Object object, final ByteBuf buffer) {
57 Preconditions.checkArgument(object instanceof Srro,
58 "Wrong instance of PCEPObject. Passed %s. Needed EroObject.", object.getClass());
59 final Srro sero = ((Srro) object);
60 final ByteBuf body = Unpooled.buffer();
61 final List<Subobject> subObjects = sero.getSubobject().stream()
62 .map(so -> new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109
63 .reported.route.object.rro.SubobjectBuilder()
64 .setSubobjectType(so.getSubobjectType())
65 .setProtectionAvailable(so.isProtectionAvailable())
66 .setProtectionInUse(so.isProtectionInUse())
68 .collect(Collectors.toList());
69 serializeSubobject(subObjects, body);
70 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);