Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPIncludeRouteObjectParser.java
1 /*
2  * Copyright (c) 2013 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.parser.object;
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 java.util.ArrayList;
15 import java.util.List;
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.include.route.object.Iro;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.include.route.object.IroBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.include.route.object.iro.Subobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.include.route.object.iro.SubobjectBuilder;
25
26 /**
27  * Parser for {@link Iro}.
28  */
29 public final class PCEPIncludeRouteObjectParser extends AbstractEROWithSubobjectsParser {
30
31     private static final int CLASS = 10;
32     private static final int TYPE = 1;
33
34     public PCEPIncludeRouteObjectParser(final EROSubobjectRegistry subobjReg) {
35         super(subobjReg, CLASS, TYPE);
36     }
37
38     @Override
39     public Iro parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
40         checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
41         final List<Subobject> subs = new ArrayList<>();
42         for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit
43                 .route.object.ero.Subobject s : parseSubobjects(bytes)) {
44             subs.add(new SubobjectBuilder()
45                 .setLoose(s.getLoose())
46                 .setSubobjectType(s.getSubobjectType())
47                 .build());
48         }
49         return new IroBuilder()
50                 .setIgnore(header.getIgnore())
51                 .setProcessingRule(header.getProcessingRule())
52                 .setSubobject(subs)
53                 .build();
54     }
55
56     @Override
57     public void serializeObject(final Object object, final ByteBuf buffer) {
58         checkArgument(object instanceof Iro, "Wrong instance of PCEPObject. Passed %s. Needed IroObject.",
59             object.getClass());
60         final Iro iro = (Iro) object;
61         final ByteBuf body = Unpooled.buffer();
62         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit
63             .route.object.ero.Subobject> subs = new ArrayList<>();
64         for (final Subobject s : iro.nonnullSubobject()) {
65             subs.add(
66                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109
67                     .explicit.route.object.ero.SubobjectBuilder()
68                         .setLoose(s.getLoose())
69                         .setSubobjectType(s.getSubobjectType())
70                         .build());
71         }
72         serializeSubobject(subs, body);
73         ObjectUtil.formatSubobject(TYPE, CLASS, object.getProcessingRule(), object.getIgnore(), body, buffer);
74     }
75 }