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