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
8 package org.opendaylight.protocol.pcep.parser.object;
10 import static com.google.common.base.Preconditions.checkArgument;
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.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
18 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
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.explicit.route.object.ero.Subobject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.secondary.explicit.route.object.Sero;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.secondary.explicit.route.object.SeroBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.secondary.explicit.route.object.sero.SubobjectBuilder;
26 public final class PCEPSecondaryExplicitRouteObjecParser extends AbstractEROWithSubobjectsParser {
27 private static final int CLASS = 29;
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 PCEPSecondaryExplicitRouteObjecParser(final EROSubobjectRegistry subobjReg) {
32 super(subobjReg, CLASS, TYPE);
36 public Sero parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
37 checkArgument(buffer != null && buffer.isReadable(), EMPTY_ARRAY_ERROR);
39 final SeroBuilder builder = new SeroBuilder();
40 builder.setIgnore(header.getIgnore());
41 builder.setProcessingRule(header.getProcessingRule());
42 final List<Subobject> subObjects = parseSubobjects(buffer);
44 builder.setSubobject(subObjects.stream()
45 .map(so -> new SubobjectBuilder().setLoose(so.getLoose()).setSubobjectType(so.getSubobjectType()).build())
46 .collect(Collectors.toList()));
48 return builder.build();
52 public void serializeObject(final Object object, final ByteBuf buffer) {
53 checkArgument(object instanceof Sero,
54 "Wrong instance of PCEPObject. Passed %s. Needed EroObject.", object.getClass());
55 final Sero sero = (Sero) object;
56 final ByteBuf body = Unpooled.buffer();
57 final List<Subobject> subObjects = sero.nonnullSubobject().stream()
58 .map(so -> new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109
59 .explicit.route.object.ero.SubobjectBuilder()
60 .setLoose(so.getLoose())
61 .setSubobjectType(so.getSubobjectType())
63 .collect(Collectors.toList());
64 serializeSubobject(subObjects, body);
65 ObjectUtil.formatSubobject(TYPE, CLASS, object.getProcessingRule(), object.getIgnore(), body, buffer);