BUG-47 : removed PCEPMessage interface, switched to generated Message.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / PCEPErrorMessageValidator.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.object.CompositeErrorObject;
22 import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
23 import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
24 import org.opendaylight.protocol.pcep.object.PCEPRequestParameterObject;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
26
27 /**
28  * PCEPErrorMessage validator. Validates message integrity.
29  */
30 public class PCEPErrorMessageValidator extends PCEPMessageValidator {
31
32         @Override
33         public List<Message> validate(final List<PCEPObject> objects) throws PCEPDeserializerException {
34                 if (objects == null)
35                         throw new IllegalArgumentException("Passed list can't be null.");
36
37                 PCEPOpenObject openObj = null;
38                 final List<CompositeErrorObject> errors = new ArrayList<CompositeErrorObject>();
39                 final List<PCEPErrorObject> errorObjects = new ArrayList<PCEPErrorObject>();
40
41                 PCEPObject obj;
42                 int state = 1;
43                 while (!objects.isEmpty()) {
44                         obj = objects.get(0);
45
46                         if (obj instanceof UnknownObject)
47                                 return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(((UnknownObject) obj).getError())));
48
49                         switch (state) {
50                         case 1:
51                                 if (obj instanceof PCEPErrorObject) {
52                                         errorObjects.add((PCEPErrorObject) obj);
53                                         break;
54                                 }
55                                 state = 2;
56                         case 2:
57                                 state = 3;
58                                 if (obj instanceof PCEPOpenObject) {
59                                         openObj = (PCEPOpenObject) obj;
60                                         break;
61                                 }
62                         case 3:
63                                 while (!objects.isEmpty()) {
64                                         CompositeErrorObject comObj;
65
66                                         try {
67                                                 comObj = getValidErrorComposite(objects);
68                                         } catch (final PCEPDocumentedException e) {
69                                                 return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(e.getError())));
70                                         }
71
72                                         if (comObj == null)
73                                                 break;
74
75                                         errors.add(comObj);
76                                 }
77
78                                 state = 4;
79                                 break;
80                         }
81
82                         if (state == 4) {
83                                 break;
84                         }
85
86                         objects.remove(0);
87                 }
88
89                 if (errors.isEmpty() && errorObjects.isEmpty())
90                         throw new PCEPDeserializerException("At least one PCEPErrorObject is mandatory.");
91
92                 if (!objects.isEmpty())
93                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
94
95                 return Arrays.asList((Message) new PCEPErrorMessage(openObj, errorObjects, errors));
96         }
97
98         private static CompositeErrorObject getValidErrorComposite(final List<PCEPObject> objects) throws PCEPDocumentedException,
99                         PCEPDeserializerException {
100                 final List<PCEPRequestParameterObject> requestParameters = new ArrayList<PCEPRequestParameterObject>();
101                 final List<PCEPErrorObject> errors = new ArrayList<PCEPErrorObject>();
102                 PCEPObject obj;
103                 int state = 1;
104
105                 while (!objects.isEmpty()) {
106                         obj = objects.get(0);
107
108                         if (obj instanceof UnknownObject)
109                                 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
110
111                         switch (state) {
112                         case 1:
113                                 state = 2;
114                                 if (obj instanceof PCEPRequestParameterObject) {
115                                         if (((PCEPRequestParameterObject) obj).isProcessed())
116                                                 throw new PCEPDocumentedException("Invalid setting of P flag.", PCEPErrors.P_FLAG_NOT_SET);
117                                         requestParameters.add((PCEPRequestParameterObject) obj);
118                                         state = 1;
119                                         break;
120                                 }
121                         case 2:
122                                 if (obj instanceof PCEPErrorObject) {
123                                         errors.add((PCEPErrorObject) obj);
124                                         state = 2;
125                                         break;
126                                 }
127                                 state = 3;
128                         }
129
130                         if (state == 3)
131                                 break;
132
133                         objects.remove(0);
134                 }
135
136                 if (errors.isEmpty())
137                         return null;
138
139                 return new CompositeErrorObject(requestParameters, errors);
140         }
141
142 }