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