Create common parent for extensions families
[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
9 package org.opendaylight.protocol.bgp.linkstate.impl.attribute;
10
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
14 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
15 import org.opendaylight.protocol.rsvp.parser.spi.RSVPTeObjectRegistry;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.LinkStateAttribute;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.TeLspAttributesCase;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.TeLspAttributesCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.te.lsp.attributes._case.TeLspAttributes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.te.lsp.attributes._case.TeLspAttributesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.RsvpTeObject;
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.BasicBandwidthObject;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.bandwidth.object.bandwidth.object.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.BasicFastRerouteObject;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.fast.reroute.object.fast.reroute.object.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.BasicProtectionObject;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.protection.object.protection.object.DynamicControlProtectionObject;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.object.RecordRouteObject;
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.secondary.record.route.object.SecondaryRecordRouteObject;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.session.attribute.object.SessionAttributeObject;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.session.attribute.object.session.attribute.object.BasicSessionAttributeObject;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.session.attribute.object.session.attribute.object.SessionAttributeObjectWithResourcesAffinities;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.tspec.object.TspecObject;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 final class TeLspAttributesParser {
54
55     private static final Logger LOG = LoggerFactory.getLogger(TeLspAttributesParser.class);
56     //TBD BY IANA
57     private static final int MAGIC_NUMBER = 99;
58
59     private TeLspAttributesParser() {
60         throw new UnsupportedOperationException();
61     }
62
63     static LinkStateAttribute parseTeLspAttributes(final RSVPTeObjectRegistry registry, final ByteBuf attributes) throws BGPParsingException {
64
65         final TeLspAttributesBuilder builder = new TeLspAttributesBuilder();
66         LOG.trace("Initiated parsing TE LSP Objects.");
67         while (attributes.isReadable()) {
68             final int length = attributes.readUnsignedShort();
69             final int classNum = attributes.readUnsignedByte();
70             final int cType = attributes.readUnsignedByte();
71             final ByteBuf value = attributes.readSlice(length);
72             try {
73                 addObject(builder, registry.parseRSPVTe(classNum, cType, value));
74             } catch (final RSVPParsingException e) {
75                 LOG.debug("Parsering TE LSP Object error. class number: {} cType: {} value: {}", classNum, cType, value, e);
76                 throw new BGPParsingException(e.getMessage());
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, final ByteBuf output) {
127         LOG.trace("Started serializing TE LSP Objects");
128         final ByteBuf byteBuf = Unpooled.buffer();
129
130         final TeLspAttributes teLspAttribute = linkState.getTeLspAttributes();
131         final TspecObject tSpec = teLspAttribute.getTspecObject();
132         registry.serializeRSPVTe(tSpec, byteBuf);
133
134         final FlowSpecObject flow = teLspAttribute.getFlowSpecObject();
135         registry.serializeRSPVTe(flow, byteBuf);
136
137         final SessionAttributeObject sao = teLspAttribute.getSessionAttributeObject();
138         if (sao instanceof BasicSessionAttributeObject) {
139             registry.serializeRSPVTe((BasicSessionAttributeObject) sao, byteBuf);
140         } else if (sao instanceof SessionAttributeObjectWithResourcesAffinities) {
141             registry.serializeRSPVTe((SessionAttributeObjectWithResourcesAffinities) sao, byteBuf);
142         }
143
144         final ExplicitRouteObject ero = teLspAttribute.getExplicitRouteObject();
145         registry.serializeRSPVTe(ero, byteBuf);
146
147         final RecordRouteObject rro = teLspAttribute.getRecordRouteObject();
148         registry.serializeRSPVTe(rro, byteBuf);
149
150         final FastRerouteObject fro = teLspAttribute.getFastRerouteObject();
151         if (fro instanceof BasicFastRerouteObject) {
152             registry.serializeRSPVTe((BasicFastRerouteObject) fro, byteBuf);
153         } else if (fro instanceof LegacyFastRerouteObject) {
154             registry.serializeRSPVTe((LegacyFastRerouteObject) fro, byteBuf);
155         }
156
157         final DetourObject dto = teLspAttribute.getDetourObject();
158         if (dto instanceof Ipv4DetourObject) {
159             registry.serializeRSPVTe((Ipv4DetourObject) dto, byteBuf);
160         } else if (dto instanceof Ipv6DetourObject) {
161             registry.serializeRSPVTe((Ipv6DetourObject) dto, byteBuf);
162         }
163
164         final ExcludeRouteObject exro = teLspAttribute.getExcludeRouteObject();
165         registry.serializeRSPVTe(exro, byteBuf);
166
167         final SecondaryExplicitRouteObject sero = teLspAttribute.getSecondaryExplicitRouteObject();
168         registry.serializeRSPVTe(sero, byteBuf);
169
170         final SecondaryRecordRouteObject srro = teLspAttribute.getSecondaryRecordRouteObject();
171         registry.serializeRSPVTe(srro, byteBuf);
172
173         final LspAttributesObject lspAtt = teLspAttribute.getLspAttributesObject();
174         registry.serializeRSPVTe(lspAtt, byteBuf);
175
176         final LspRequiredAttributesObject rao = teLspAttribute.getLspRequiredAttributesObject();
177         registry.serializeRSPVTe(rao, byteBuf);
178
179         final ProtectionObject po = teLspAttribute.getProtectionObject();
180         if (po instanceof DynamicControlProtectionObject) {
181             registry.serializeRSPVTe((DynamicControlProtectionObject) po, byteBuf);
182         } else if (po instanceof BasicProtectionObject) {
183             registry.serializeRSPVTe((BasicProtectionObject) po, byteBuf);
184         }
185
186         final AssociationObject aso = teLspAttribute.getAssociationObject();
187         registry.serializeRSPVTe(aso, byteBuf);
188
189         final PrimaryPathRouteObject ppr = teLspAttribute.getPrimaryPathRouteObject();
190         registry.serializeRSPVTe(ppr, byteBuf);
191
192         final AdminStatusObject adso = teLspAttribute.getAdminStatusObject();
193         registry.serializeRSPVTe(adso, byteBuf);
194
195         final BandwidthObject bo = teLspAttribute.getBandwidthObject();
196         if (bo instanceof BasicBandwidthObject) {
197             registry.serializeRSPVTe((BasicBandwidthObject) bo, byteBuf);
198         } else if (bo instanceof ReoptimizationBandwidthObject) {
199             registry.serializeRSPVTe((ReoptimizationBandwidthObject) bo, byteBuf);
200         }
201
202         final MetricObject mo = teLspAttribute.getMetricObject();
203         registry.serializeRSPVTe(mo, byteBuf);
204
205
206         output.writeShort(MAGIC_NUMBER);
207         output.writeShort(byteBuf.readableBytes());
208         output.writeBytes(byteBuf);
209         LOG.trace("Finished serializing TE LSP Objects");
210     }
211 }