039f12d9d0503a6d5dc65cedb8a6709510cd07bc
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / subobject / EROExplicitExclusionRouteSubobjectParser.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.subobject;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.EROSubobjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
20 import org.opendaylight.protocol.util.Values;
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.explicit.route.object.ero.SubobjectBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.ExrsCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.ExrsCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.exrs._case.Exrs;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder;
27
28 public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
29
30     public static final int TYPE = 33;
31
32     private static final int HEADER_LENGTH = 2;
33
34     private final XROSubobjectRegistry registry;
35
36     public EROExplicitExclusionRouteSubobjectParser(final XROSubobjectRegistry registry) {
37         this.registry = registry;
38     }
39
40     @Override
41     public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
42         Preconditions.checkArgument(buffer != null && buffer.isReadable(),
43                 "Array of bytes is mandatory. Can't be null or empty.");
44         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude
45             .route.object.xro.Subobject> xros = parseSubobject(buffer);
46         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit
47             .route.subobjects.subobject.type.exrs._case.exrs.Exrs> exrss = new ArrayList<>();
48         for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude
49                 .route.object.xro.Subobject xro : xros) {
50             final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820
51                 .explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder exrsBuilder =
52                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820
53                     .explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder()
54                         .setAttribute(xro.getAttribute())
55                         .setMandatory(xro.getMandatory())
56                         .setSubobjectType(xro.getSubobjectType());
57             exrss.add(exrsBuilder.build());
58         }
59         final SubobjectBuilder builder = new SubobjectBuilder()
60                 .setLoose(loose)
61                 .setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(exrss).build()).build());
62         return builder.build();
63     }
64
65     private List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude
66         .route.object.xro.Subobject> parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
67         Preconditions.checkArgument(buffer != null && buffer.isReadable(),
68                 "Array of bytes is mandatory. Can't be null or empty.");
69         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude
70             .route.object.xro.Subobject> xros = new ArrayList<>();
71         while (buffer.isReadable()) {
72             final boolean mandatory =
73                     (buffer.getByte(buffer.readerIndex()) & 1 << Values.FIRST_BIT_OFFSET) != 0 ? true : false;
74             final int type =
75                     buffer.readUnsignedByte() & Values.BYTE_MAX_VALUE_BYTES & ~(1 << Values.FIRST_BIT_OFFSET);
76             final int length = buffer.readUnsignedByte() - HEADER_LENGTH;
77             if (length > buffer.readableBytes()) {
78                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
79                         + buffer.readableBytes());
80             }
81             xros.add(this.registry.parseSubobject(type, buffer.readSlice(length), mandatory));
82         }
83         return xros;
84     }
85
86     @Override
87     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
88         Preconditions.checkArgument(subobject.getSubobjectType() instanceof ExrsCase,
89                 "Unknown subobject instance. Passed %s. Needed Exrs.", subobject.getSubobjectType().getClass());
90         final Exrs exrs = ((ExrsCase) subobject.getSubobjectType()).getExrs();
91         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude
92             .route.object.xro.Subobject> xros = new ArrayList<>();
93         for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit
94                 .route.subobjects.subobject.type.exrs._case.exrs.Exrs exr : exrs.nonnullExrs()) {
95             final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109
96                 .exclude.route.object.xro.SubobjectBuilder xroBuilder =
97                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109
98                     .exclude.route.object.xro.SubobjectBuilder()
99                         .setAttribute(exr.getAttribute())
100                         .setMandatory(exr.getMandatory())
101                         .setSubobjectType(exr.getSubobjectType());
102             xros.add(xroBuilder.build());
103         }
104         final ByteBuf body = Unpooled.buffer();
105         serializeSubobject(xros, body);
106         EROSubobjectUtil.formatSubobject(TYPE, subobject.getLoose(), body, buffer);
107     }
108
109     private void serializeSubobject(
110             final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude
111                 .route.object.xro.Subobject> subobjects, final ByteBuf body) {
112         for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude
113                 .route.object.xro.Subobject subobject : subobjects) {
114             this.registry.serializeSubobject(subobject, body);
115         }
116     }
117 }