Merge "BUG-730 : added test for bgp/concepts"
[bgpcep.git] / pcep / segment-routing / src / main / java / org / opendaylight / protocol / pcep / segment / routing02 / SrPcUpdMessageParser.java
1 /*
2  * Copyright (c) 2014 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.pcep.segment.routing02;
10
11 import io.netty.buffer.ByteBuf;
12 import java.util.List;
13 import org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07PCUpdateRequestMessageParser;
14 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
15 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.Lsp;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.pcupd.message.Updates;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.pcupd.message.UpdatesBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.pcupd.message.updates.PathBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.Srp;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.Tlvs6;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.Tlvs7;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
26
27 public class SrPcUpdMessageParser extends Stateful07PCUpdateRequestMessageParser {
28
29     public SrPcUpdMessageParser(ObjectRegistry registry) {
30         super(registry);
31     }
32
33     @Override
34     protected void serializeUpdate(Updates update, ByteBuf buffer) {
35         if (isSegmentRoutingPath(update.getSrp())) {
36             serializeObject(update.getSrp(), buffer);
37             if (update.getLsp() != null) {
38                 serializeObject(update.getLsp(), buffer);
39             }
40             final Ero srEro = update.getPath().getEro();
41             if (srEro != null) {
42                 serializeObject(srEro, buffer);
43             }
44         } else {
45             super.serializeUpdate(update, buffer);
46         }
47     }
48
49     @Override
50     protected Updates getValidUpdates(List<Object> objects, List<Message> errors) {
51         if (objects.get(0) instanceof Srp && isSegmentRoutingPath((Srp) objects.get(0))) {
52             boolean isValid = true;
53             final Srp srp = (Srp) objects.get(0);
54             final UpdatesBuilder builder = new UpdatesBuilder();
55             builder.setSrp(srp);
56             objects.remove(0);
57             if (objects.get(0) instanceof Lsp) {
58                 builder.setLsp((Lsp) objects.get(0));
59                 objects.remove(0);
60             } else {
61                 errors.add(createErrorMsg(PCEPErrors.LSP_MISSING));
62                 isValid = false;
63             }
64
65             final Object obj = objects.get(0);
66             if (obj instanceof Ero) {
67                 final Ero ero = (Ero) obj;
68                 final PCEPErrors error = SrEroUtil.validateSrEroSubobjects(ero);
69                 if (error != null) {
70                     errors.add(createErrorMsg(error));
71                     isValid = false;
72                 } else {
73                     builder.setPath(new PathBuilder().setEro(ero).build());
74                     objects.remove(0);
75                 }
76             } else {
77                 errors.add(createErrorMsg(PCEPErrors.ERO_MISSING));
78                 isValid = false;
79             }
80             if (isValid) {
81                 return builder.build();
82             }
83             return null;
84         }
85         return super.getValidUpdates(objects, errors);
86     }
87
88     private boolean isSegmentRoutingPath(final Srp srp) {
89         if (srp != null && srp.getTlvs() != null) {
90             return SrEroUtil.isPst(srp.getTlvs().getAugmentation(Tlvs6.class))
91                     || SrEroUtil.isPst(srp.getTlvs().getAugmentation(Tlvs7.class));
92         }
93         return false;
94     }
95
96 }