Add new revision for pcep types model
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPSecondaryExplicitRouteObjecParser.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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
9 package org.opendaylight.protocol.pcep.parser.object;
10
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.EROSubobjectRegistry;
17 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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;
25
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.";
30
31     public PCEPSecondaryExplicitRouteObjecParser(final EROSubobjectRegistry subobjReg) {
32         super(subobjReg, CLASS, TYPE);
33     }
34
35     @Override
36     public Sero parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
37         Preconditions.checkArgument(buffer != null && buffer.isReadable(), EMPTY_ARRAY_ERROR);
38
39         final SeroBuilder builder = new SeroBuilder();
40         builder.setIgnore(header.isIgnore());
41         builder.setProcessingRule(header.isProcessingRule());
42         final List<Subobject> subObjects = parseSubobjects(buffer);
43
44         builder.setSubobject(subObjects.stream()
45             .map(so -> new SubobjectBuilder().setLoose(so.isLoose()).setSubobjectType(so.getSubobjectType()).build())
46             .collect(Collectors.toList()));
47
48         return builder.build();
49     }
50
51     @Override
52     public void serializeObject(final Object object, final ByteBuf buffer) {
53         Preconditions.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.getSubobject().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.isLoose())
61                     .setSubobjectType(so.getSubobjectType())
62                     .build())
63             .collect(Collectors.toList());
64         serializeSubobject(subObjects, body);
65         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
66     }
67 }