5d141a175076d452b3a717bb4fe15fc0b9c30248
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / 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.impl.object;
9
10 import com.google.common.base.Preconditions;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
19 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.Iro;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.IroBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.iro.Subobject;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.iro.SubobjectBuilder;
27
28 /**
29  * Parser for {@link Iro}
30  */
31 public class PCEPIncludeRouteObjectParser extends AbstractEROWithSubobjectsParser {
32
33     public static final int CLASS = 10;
34
35     public static final int TYPE = 1;
36
37     public PCEPIncludeRouteObjectParser(final EROSubobjectRegistry subobjReg) {
38         super(subobjReg);
39     }
40
41     @Override
42     public Iro parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
43         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
44         final IroBuilder builder = new IroBuilder();
45         builder.setIgnore(header.isIgnore());
46         builder.setProcessingRule(header.isProcessingRule());
47         final List<Subobject> subs = new ArrayList<>();
48         for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject s : parseSubobjects(bytes)) {
49             subs.add(new SubobjectBuilder().setLoose(s.isLoose()).setSubobjectType(s.getSubobjectType()).build());
50         }
51         builder.setSubobject(subs);
52         return builder.build();
53     }
54
55     @Override
56     public void serializeObject(final Object object, final ByteBuf buffer) {
57         Preconditions.checkArgument(object instanceof Iro, "Wrong instance of PCEPObject. Passed %s. Needed IroObject.", object.getClass());
58         final Iro iro = ((Iro) object);
59         final ByteBuf body = Unpooled.buffer();
60         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject> subs = new ArrayList<>();
61         for (final Subobject s : iro.getSubobject()) {
62             subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder().setLoose(
63                     s.isLoose()).setSubobjectType(s.getSubobjectType()).build());
64         }
65         serializeSubobject(subs, body);
66         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
67     }
68 }