Bump versions to 0.21.6-SNAPSHOT
[bgpcep.git] / bgp / extensions / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / impl / attribute / TeLspAttributesParser.java
1 /*
2  * Copyright (c) 2015 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.bgp.linkstate.impl.attribute;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
13 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
14 import org.opendaylight.protocol.rsvp.parser.spi.RSVPTeObjectRegistry;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.LinkStateAttribute;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.link.state.attribute.TeLspAttributesCase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.link.state.attribute.TeLspAttributesCaseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.link.state.attribute.te.lsp.attributes._case.TeLspAttributes;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.link.state.attribute.te.lsp.attributes._case.TeLspAttributesBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.RsvpTeObject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.object.RecordRouteObject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.admin.status.object.AdminStatusObject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.association.object.AssociationObject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.bandwidth.object.BandwidthObject;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.bandwidth.object.bandwidth.object.basic.bandwidth.object._case.BasicBandwidthObject;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.bandwidth.object.bandwidth.object.reoptimization.bandwidth.object._case.ReoptimizationBandwidthObject;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.detour.object.DetourObject;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.detour.object.detour.object.Ipv4DetourObject;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.detour.object.detour.object.Ipv6DetourObject;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.ExcludeRouteObject;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.object.ExplicitRouteObject;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.fast.reroute.object.FastRerouteObject;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.fast.reroute.object.fast.reroute.object.basic.fast.reroute.object._case.BasicFastRerouteObject;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.fast.reroute.object.fast.reroute.object.legacy.fast.reroute.object._case.LegacyFastRerouteObject;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.flow.spec.object.FlowSpecObject;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.lsp.attributes.object.LspAttributesObject;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.lsp.required.attributes.object.LspRequiredAttributesObject;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.metric.object.MetricObject;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.primary.path.route.object.PrimaryPathRouteObject;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.protection.object.ProtectionObject;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.protection.object.protection.object.basic.protection.object._case.BasicProtectionObject;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.protection.object.protection.object.dynamic.control.protection.object._case.DynamicControlProtectionObject;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.secondary._record.route.object.SecondaryRecordRouteObject;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.secondary.explicit.route.object.SecondaryExplicitRouteObject;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.session.attribute.object.SessionAttributeObject;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.session.attribute.object.session.attribute.object.basic.session.attribute.object._case.BasicSessionAttributeObject;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.session.attribute.object.session.attribute.object.session.attribute.object.with.resources.affinities._case.SessionAttributeObjectWithResourcesAffinities;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.tspec.object.TspecObject;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 final class TeLspAttributesParser {
53     private static final Logger LOG = LoggerFactory.getLogger(TeLspAttributesParser.class);
54     //TBD BY IANA
55     private static final int MAGIC_NUMBER = 99;
56
57     private TeLspAttributesParser() {
58         // Hidden on purpose
59     }
60
61     static LinkStateAttribute parseTeLspAttributes(final RSVPTeObjectRegistry registry, final ByteBuf attributes)
62             throws BGPParsingException {
63
64         final TeLspAttributesBuilder builder = new TeLspAttributesBuilder();
65         LOG.trace("Initiated parsing TE LSP Objects.");
66         while (attributes.isReadable()) {
67             final int length = attributes.readUnsignedShort();
68             final int classNum = attributes.readUnsignedByte();
69             final int cType = attributes.readUnsignedByte();
70             final ByteBuf value = attributes.readSlice(length);
71             try {
72                 addObject(builder, registry.parseRSPVTe(classNum, cType, value));
73             } catch (final RSVPParsingException e) {
74                 LOG.debug("Parsering TE LSP Object error. class number: {} cType: {} value: {}", classNum, cType, value,
75                     e);
76                 throw new BGPParsingException(e.getMessage(), e);
77             }
78         }
79         LOG.trace("Finished parsing TE LSP Objects.");
80         return new TeLspAttributesCaseBuilder().setTeLspAttributes(builder.build()).build();
81     }
82
83     private static void addObject(final TeLspAttributesBuilder builder, final RsvpTeObject object) {
84         if (object instanceof TspecObject) {
85             builder.setTspecObject((TspecObject) object);
86         } else if (object instanceof FlowSpecObject) {
87             builder.setFlowSpecObject((FlowSpecObject) object);
88         } else if (object instanceof SessionAttributeObject) {
89             builder.setSessionAttributeObject((SessionAttributeObject) object);
90         } else if (object instanceof ExplicitRouteObject) {
91             builder.setExplicitRouteObject((ExplicitRouteObject) object);
92         } else if (object instanceof RecordRouteObject) {
93             builder.setRecordRouteObject((RecordRouteObject) object);
94         } else if (object instanceof FastRerouteObject) {
95             builder.setFastRerouteObject((FastRerouteObject) object);
96         } else if (object instanceof DetourObject) {
97             builder.setDetourObject((DetourObject) object);
98         } else if (object instanceof ExcludeRouteObject) {
99             builder.setExcludeRouteObject((ExcludeRouteObject) object);
100         } else if (object instanceof SecondaryExplicitRouteObject) {
101             builder.setSecondaryExplicitRouteObject((SecondaryExplicitRouteObject) object);
102         } else if (object instanceof SecondaryRecordRouteObject) {
103             builder.setSecondaryRecordRouteObject((SecondaryRecordRouteObject) object);
104         } else if (object instanceof LspAttributesObject) {
105             builder.setLspAttributesObject((LspAttributesObject) object);
106         } else if (object instanceof LspRequiredAttributesObject) {
107             builder.setLspRequiredAttributesObject((LspRequiredAttributesObject) object);
108         } else if (object instanceof ProtectionObject) {
109             builder.setProtectionObject((ProtectionObject) object);
110         } else if (object instanceof AssociationObject) {
111             builder.setAssociationObject((AssociationObject) object);
112         } else if (object instanceof PrimaryPathRouteObject) {
113             builder.setPrimaryPathRouteObject((PrimaryPathRouteObject) object);
114         } else if (object instanceof AdminStatusObject) {
115             builder.setAdminStatusObject((AdminStatusObject) object);
116         } else if (object instanceof BandwidthObject) {
117             builder.setBandwidthObject((BandwidthObject) object);
118         } else if (object instanceof MetricObject) {
119             builder.setMetricObject((MetricObject) object);
120         } else {
121             throw new IllegalStateException("Unhandled TE LSP Object " + object);
122         }
123     }
124
125
126     static void serializeLspAttributes(final RSVPTeObjectRegistry registry, final TeLspAttributesCase linkState,
127             final ByteBuf output) {
128         LOG.trace("Started serializing TE LSP Objects");
129         final ByteBuf byteBuf = Unpooled.buffer();
130
131         final TeLspAttributes teLspAttribute = linkState.getTeLspAttributes();
132         final TspecObject tSpec = teLspAttribute.getTspecObject();
133         registry.serializeRSPVTe(tSpec, byteBuf);
134
135         final FlowSpecObject flow = teLspAttribute.getFlowSpecObject();
136         registry.serializeRSPVTe(flow, byteBuf);
137
138         final SessionAttributeObject sao = teLspAttribute.getSessionAttributeObject();
139         if (sao instanceof BasicSessionAttributeObject) {
140             registry.serializeRSPVTe((BasicSessionAttributeObject) sao, byteBuf);
141         } else if (sao instanceof SessionAttributeObjectWithResourcesAffinities) {
142             registry.serializeRSPVTe((SessionAttributeObjectWithResourcesAffinities) sao, byteBuf);
143         }
144
145         final ExplicitRouteObject ero = teLspAttribute.getExplicitRouteObject();
146         registry.serializeRSPVTe(ero, byteBuf);
147
148         final RecordRouteObject rro = teLspAttribute.getRecordRouteObject();
149         registry.serializeRSPVTe(rro, byteBuf);
150
151         final FastRerouteObject fro = teLspAttribute.getFastRerouteObject();
152         if (fro instanceof BasicFastRerouteObject) {
153             registry.serializeRSPVTe((BasicFastRerouteObject) fro, byteBuf);
154         } else if (fro instanceof LegacyFastRerouteObject) {
155             registry.serializeRSPVTe((LegacyFastRerouteObject) fro, byteBuf);
156         }
157
158         final DetourObject dto = teLspAttribute.getDetourObject();
159         if (dto instanceof Ipv4DetourObject) {
160             registry.serializeRSPVTe((Ipv4DetourObject) dto, byteBuf);
161         } else if (dto instanceof Ipv6DetourObject) {
162             registry.serializeRSPVTe((Ipv6DetourObject) dto, byteBuf);
163         }
164
165         final ExcludeRouteObject exro = teLspAttribute.getExcludeRouteObject();
166         registry.serializeRSPVTe(exro, byteBuf);
167
168         final SecondaryExplicitRouteObject sero = teLspAttribute.getSecondaryExplicitRouteObject();
169         registry.serializeRSPVTe(sero, byteBuf);
170
171         final SecondaryRecordRouteObject srro = teLspAttribute.getSecondaryRecordRouteObject();
172         registry.serializeRSPVTe(srro, byteBuf);
173
174         final LspAttributesObject lspAtt = teLspAttribute.getLspAttributesObject();
175         registry.serializeRSPVTe(lspAtt, byteBuf);
176
177         final LspRequiredAttributesObject rao = teLspAttribute.getLspRequiredAttributesObject();
178         registry.serializeRSPVTe(rao, byteBuf);
179
180         final ProtectionObject po = teLspAttribute.getProtectionObject();
181         if (po instanceof DynamicControlProtectionObject) {
182             registry.serializeRSPVTe((DynamicControlProtectionObject) po, byteBuf);
183         } else if (po instanceof BasicProtectionObject) {
184             registry.serializeRSPVTe((BasicProtectionObject) po, byteBuf);
185         }
186
187         final AssociationObject aso = teLspAttribute.getAssociationObject();
188         registry.serializeRSPVTe(aso, byteBuf);
189
190         final PrimaryPathRouteObject ppr = teLspAttribute.getPrimaryPathRouteObject();
191         registry.serializeRSPVTe(ppr, byteBuf);
192
193         final AdminStatusObject adso = teLspAttribute.getAdminStatusObject();
194         registry.serializeRSPVTe(adso, byteBuf);
195
196         final BandwidthObject bo = teLspAttribute.getBandwidthObject();
197         if (bo instanceof BasicBandwidthObject) {
198             registry.serializeRSPVTe((BasicBandwidthObject) bo, byteBuf);
199         } else if (bo instanceof ReoptimizationBandwidthObject) {
200             registry.serializeRSPVTe((ReoptimizationBandwidthObject) bo, byteBuf);
201         }
202
203         final MetricObject mo = teLspAttribute.getMetricObject();
204         registry.serializeRSPVTe(mo, byteBuf);
205
206
207         output.writeShort(MAGIC_NUMBER);
208         output.writeShort(byteBuf.readableBytes());
209         output.writeBytes(byteBuf);
210         LOG.trace("Finished serializing TE LSP Objects");
211     }
212 }