BUG-47 : removed PCEPMessage interface, switched to generated Message.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / PCEPReportMessageValidator.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.impl.message;
9
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.List;
13
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
16 import org.opendaylight.protocol.pcep.PCEPErrors;
17 import org.opendaylight.protocol.pcep.PCEPObject;
18 import org.opendaylight.protocol.pcep.impl.PCEPMessageValidator;
19 import org.opendaylight.protocol.pcep.impl.object.UnknownObject;
20 import org.opendaylight.protocol.pcep.message.PCEPErrorMessage;
21 import org.opendaylight.protocol.pcep.message.PCEPReportMessage;
22 import org.opendaylight.protocol.pcep.object.CompositeRptPathObject;
23 import org.opendaylight.protocol.pcep.object.CompositeStateReportObject;
24 import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
25 import org.opendaylight.protocol.pcep.object.PCEPExistingPathBandwidthObject;
26 import org.opendaylight.protocol.pcep.object.PCEPExplicitRouteObject;
27 import org.opendaylight.protocol.pcep.object.PCEPLspObject;
28 import org.opendaylight.protocol.pcep.object.PCEPLspaObject;
29 import org.opendaylight.protocol.pcep.object.PCEPMetricObject;
30 import org.opendaylight.protocol.pcep.object.PCEPReportedRouteObject;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
32
33 /**
34  * PCEPReportMessage validator. Validates message integrity.
35  */
36 public class PCEPReportMessageValidator extends PCEPMessageValidator {
37
38         @Override
39         public List<Message> validate(final List<PCEPObject> objects) throws PCEPDeserializerException {
40                 if (objects == null)
41                         throw new IllegalArgumentException("Passed list can't be null.");
42
43                 final List<CompositeStateReportObject> report = new ArrayList<CompositeStateReportObject>();
44
45                 while (!objects.isEmpty()) {
46                         if (objects.get(0) instanceof UnknownObject)
47                                 return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(((UnknownObject) objects.get(0)).getError())));
48
49                         if (!(objects.get(0) instanceof PCEPLspObject))
50                                 return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(PCEPErrors.LSP_MISSING)));
51
52                         final PCEPLspObject lsp = (PCEPLspObject) objects.get(0);
53                         objects.remove(0);
54
55                         final List<CompositeRptPathObject> paths = new ArrayList<CompositeRptPathObject>();
56
57                         if (!objects.isEmpty()) {
58                                 try {
59                                         CompositeRptPathObject path;
60                                         path = this.getValidCompositePath(objects);
61                                         while (path != null) {
62                                                 paths.add(path);
63                                                 if (objects.isEmpty())
64                                                         break;
65                                                 path = this.getValidCompositePath(objects);
66                                         }
67                                 } catch (final PCEPDocumentedException e) {
68                                         return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(e.getError())));
69                                 }
70                         }
71
72                         report.add(new CompositeStateReportObject(lsp, paths));
73                 }
74
75                 if (report.isEmpty())
76                         throw new PCEPDeserializerException("Atleast one CompositeStateReportObject is mandatory.");
77
78                 if (!objects.isEmpty()) {
79                         if (objects.get(0) instanceof UnknownObject)
80                                 return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(((UnknownObject) objects.get(0)).getError())));
81                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
82                 }
83
84                 return Arrays.asList((Message) new PCEPReportMessage(report));
85         }
86
87         private CompositeRptPathObject getValidCompositePath(final List<PCEPObject> objects) throws PCEPDocumentedException {
88                 if (objects.get(0) instanceof UnknownObject)
89                         throw new PCEPDocumentedException("Unknown object", ((UnknownObject) objects.get(0)).getError());
90                 if (!(objects.get(0) instanceof PCEPExplicitRouteObject))
91                         return null;
92                 final PCEPExplicitRouteObject explicitRoute = (PCEPExplicitRouteObject) objects.get(0);
93                 objects.remove(0);
94
95                 PCEPLspaObject pathLspa = null;
96                 PCEPExistingPathBandwidthObject pathBandwidth = null;
97                 PCEPReportedRouteObject pathRro = null;
98                 final List<PCEPMetricObject> pathMetrics = new ArrayList<PCEPMetricObject>();
99
100                 PCEPObject obj;
101                 int state = 1;
102                 while (!objects.isEmpty()) {
103                         obj = objects.get(0);
104                         if (obj instanceof UnknownObject) {
105                                 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
106                         }
107
108                         switch (state) {
109                         case 1:
110                                 state = 2;
111                                 if (obj instanceof PCEPLspaObject) {
112                                         pathLspa = (PCEPLspaObject) obj;
113                                         break;
114                                 }
115                         case 2:
116                                 state = 3;
117                                 if (obj instanceof PCEPExistingPathBandwidthObject) {
118                                         pathBandwidth = (PCEPExistingPathBandwidthObject) obj;
119                                         break;
120                                 }
121
122                         case 3:
123                                 state = 4;
124                                 if (obj instanceof PCEPReportedRouteObject) {
125                                         pathRro = (PCEPReportedRouteObject) obj;
126                                         break;
127                                 }
128                         case 4:
129                                 state = 5;
130                                 if (obj instanceof PCEPMetricObject) {
131                                         pathMetrics.add((PCEPMetricObject) obj);
132                                         state = 4;
133                                         break;
134                                 }
135                         }
136
137                         if (state == 5)
138                                 break;
139
140                         objects.remove(0);
141                 }
142
143                 return new CompositeRptPathObject(explicitRoute, pathLspa, pathBandwidth, pathRro, pathMetrics);
144         }
145 }