Merge "Randomize port to allow concurrent execution"
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / PCEPNotificationMessageValidator.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.PCEPNotificationMessage;
22 import org.opendaylight.protocol.pcep.object.CompositeNotifyObject;
23 import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
24 import org.opendaylight.protocol.pcep.object.PCEPNotificationObject;
25 import org.opendaylight.protocol.pcep.object.PCEPRequestParameterObject;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
27
28 /**
29  * PCEPNotificationMessage validator. Validates message integrity.
30  */
31 public class PCEPNotificationMessageValidator extends PCEPMessageValidator {
32
33         @Override
34         public List<Message> validate(final List<PCEPObject> objects) throws PCEPDeserializerException {
35                 if (objects == null)
36                         throw new IllegalArgumentException("Passed list can't be null.");
37
38                 final List<CompositeNotifyObject> compositeNotifications = new ArrayList<CompositeNotifyObject>();
39
40                 while (!objects.isEmpty()) {
41                         CompositeNotifyObject comObj;
42                         try {
43                                 comObj = getValidNotificationComposite(objects);
44                         } catch (final PCEPDocumentedException e) {
45                                 return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(e.getError())));
46                         }
47
48                         if (comObj == null)
49                                 break;
50
51                         compositeNotifications.add(comObj);
52                 }
53
54                 if (compositeNotifications.isEmpty())
55                         throw new PCEPDeserializerException("Atleast one CompositeNotifiObject is mandatory.");
56
57                 if (!objects.isEmpty())
58                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
59
60                 return Arrays.asList((Message) new PCEPNotificationMessage(compositeNotifications));
61         }
62
63         private static CompositeNotifyObject getValidNotificationComposite(final List<PCEPObject> objects) throws PCEPDocumentedException {
64                 final List<PCEPRequestParameterObject> requestParameters = new ArrayList<PCEPRequestParameterObject>();
65                 final List<PCEPNotificationObject> notifications = new ArrayList<PCEPNotificationObject>();
66                 PCEPObject obj;
67
68                 int state = 1;
69                 while (!objects.isEmpty()) {
70                         obj = objects.get(0);
71
72                         if (obj instanceof UnknownObject)
73                                 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
74
75                         switch (state) {
76                         case 1:
77                                 state = 2;
78                                 if (obj instanceof PCEPRequestParameterObject) {
79                                         if (((PCEPRequestParameterObject) obj).isProcessed())
80                                                 throw new PCEPDocumentedException("Invalid setting of P flag.", PCEPErrors.P_FLAG_NOT_SET);
81                                         requestParameters.add((PCEPRequestParameterObject) obj);
82                                         state = 1;
83                                         break;
84                                 }
85                         case 2:
86                                 if (obj instanceof PCEPNotificationObject) {
87                                         notifications.add((PCEPNotificationObject) obj);
88                                         state = 2;
89                                         break;
90                                 }
91                                 state = 3;
92                         }
93
94                         if (state == 3)
95                                 break;
96
97                         objects.remove(obj);
98                 }
99
100                 if (notifications.isEmpty())
101                         return null;
102
103                 return new CompositeNotifyObject(requestParameters, notifications);
104         }
105 }