a5ab8f55d9235fa783e355e130ba5c7b19a5fa1e
[vtn.git] /
1 /*
2  * Copyright (c) 2012-2013 NEC Corporation
3  * All rights reserved.
4  * 
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this
7  * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.vtn.javaapi.validation.physical;
10
11 import com.google.gson.JsonObject;
12 import org.opendaylight.vtn.core.util.Logger;
13 import org.opendaylight.vtn.javaapi.constants.VtnServiceConsts;
14 import org.opendaylight.vtn.javaapi.constants.VtnServiceJsonConsts;
15 import org.opendaylight.vtn.javaapi.exception.VtnServiceException;
16 import org.opendaylight.vtn.javaapi.ipc.enums.UncJavaAPIErrorCode;
17 import org.opendaylight.vtn.javaapi.resources.AbstractResource;
18 import org.opendaylight.vtn.javaapi.resources.physical.LinkResource;
19 import org.opendaylight.vtn.javaapi.resources.physical.LinksResource;
20 import org.opendaylight.vtn.javaapi.validation.CommonValidator;
21 import org.opendaylight.vtn.javaapi.validation.VtnServiceValidator;
22
23 /**
24  * The Class LinkResourceValidator validates get request Json object for Link
25  * API.
26  */
27 public class LinkResourceValidator extends VtnServiceValidator {
28
29         private static final Logger LOG = Logger
30                         .getLogger(LinkResourceValidator.class.getName());
31
32         private final AbstractResource resource;
33         final CommonValidator validator = new CommonValidator();
34
35         /**
36          * Instantiates a new link resource validator.
37          * 
38          * @param switchResource
39          *            the instance of AbstractResource
40          */
41         public LinkResourceValidator(final AbstractResource resource) {
42                 this.resource = resource;
43         }
44
45         /**
46          * Validate uri parameters for Link API
47          * 
48          * @return true, if successful
49          */
50         @Override
51         public boolean validateUri() {
52                 LOG.trace("Start LinkResourceValidator#validateUri()");
53                 boolean isValid = false;
54                 setInvalidParameter(VtnServiceJsonConsts.URI
55                                 + VtnServiceJsonConsts.CONTROLLERID);
56                 if (resource instanceof LinkResource
57                                 && ((LinkResource) resource).getControllerId() != null
58                                 && !((LinkResource) resource).getControllerId().trim()
59                                                 .isEmpty()) {
60                         isValid = validator.isValidMaxLengthAlphaNum(
61                                         ((LinkResource) resource).getControllerId().trim(),
62                                         VtnServiceJsonConsts.LEN_31);
63                         if (isValid) {
64                                 setInvalidParameter(VtnServiceJsonConsts.URI
65                                                 + VtnServiceJsonConsts.LINKNAME);
66                                 if (((LinkResource) resource).getLinkName() != null
67                                                 && !((LinkResource) resource).getLinkName().trim()
68                                                                 .isEmpty()) {
69                                         isValid = linkNameValidator(((LinkResource) resource)
70                                                         .getLinkName().trim());
71                                 } else {
72                                         isValid = false;
73                                 }
74                         }
75                         setListOpFlag(false);
76                 } else if (resource instanceof LinksResource
77                                 && ((LinksResource) resource).getControllerId() != null
78                                 && !((LinksResource) resource).getControllerId().trim()
79                                                 .isEmpty()) {
80                         isValid = validator.isValidMaxLengthAlphaNum(
81                                         ((LinksResource) resource).getControllerId(),
82                                         VtnServiceJsonConsts.LEN_31);
83                         setListOpFlag(true);
84                 }
85                 LOG.trace("Complete LinkResourceValidator#validateUri()");
86                 return isValid;
87         }
88
89         /**
90          * Validate link name
91          * 
92          * @param link
93          *            value to be validated
94          * 
95          * @return true, if successful
96          */
97         private boolean linkNameValidator(final String link) {
98                 boolean isValid = false;
99                 final String[] uri = link.split(VtnServiceJsonConsts.LINKSAPERATOR);
100                 if (uri.length == VtnServiceJsonConsts.LEN_4) {
101                         isValid = validator.isValidMaxLength(
102                                         uri[VtnServiceJsonConsts.VAL_0],
103                                         VtnServiceJsonConsts.LEN_255);
104                         if (isValid) {
105                                 isValid = validator.isValidMaxLength(
106                                                 uri[VtnServiceJsonConsts.VAL_1],
107                                                 VtnServiceJsonConsts.LEN_31);
108                         }
109                         if (isValid) {
110                                 isValid = validator.isValidMaxLength(
111                                                 uri[VtnServiceJsonConsts.VAL_2],
112                                                 VtnServiceJsonConsts.LEN_255);
113                         }
114                         if (isValid) {
115                                 isValid = validator.isValidMaxLength(
116                                                 uri[VtnServiceJsonConsts.VAL_3],
117                                                 VtnServiceJsonConsts.LEN_31);
118                         }
119                 }
120                 return isValid;
121         }
122
123         /**
124          * Validate request json get method of Link API
125          */
126         @Override
127         public void validate(final String method, final JsonObject requestBody)
128                         throws VtnServiceException {
129                 LOG.trace("Start LinkResourceValidator#validate()");
130                 LOG.info("Validating request for " + method
131                                 + " of LinkResourceValidator");
132                 boolean isValid = false;
133                 try {
134                         isValid = validateUri();
135                         if (isValid && requestBody != null
136                                         && VtnServiceConsts.GET.equals(method)) {
137                                 isValid = validateGet(requestBody, isListOpFlag());
138                                 updateOpParameterForList(requestBody);
139                         } else {
140                                 isValid = false;
141                         }
142                 } catch (final NumberFormatException e) {
143                         LOG.error("Inside catch:NumberFormatException");
144                         isValid = false;
145                 }
146                 // Throws exception if validation fails
147                 if (!isValid) {
148                         LOG.error("Validation failed");
149                         throw new VtnServiceException(Thread.currentThread()
150                                         .getStackTrace()[1].getMethodName(),
151                                         UncJavaAPIErrorCode.VALIDATION_ERROR.getErrorCode(),
152                                         UncJavaAPIErrorCode.VALIDATION_ERROR.getErrorMessage());
153                 }
154                 LOG.info("Validation successful");
155                 LOG.trace("Complete LinkResourceValidator#validate()");
156         }
157
158         /**
159          * Validate get request json for Link API
160          * 
161          * @param requestBody
162          *            the request Json object
163          * 
164          * @return true, if is valid get
165          */
166         public boolean validateGet(final JsonObject requestBody,
167                         final boolean opFlag) {
168                 LOG.trace("Start LinkResourceValidator#ValidGet");
169                 boolean isValid = true;
170                 // validation for key: targetdb
171                 setInvalidParameter(VtnServiceJsonConsts.TARGETDB);
172                 if (requestBody.has(VtnServiceJsonConsts.TARGETDB)
173                                 && requestBody
174                                                 .getAsJsonPrimitive(VtnServiceJsonConsts.TARGETDB)
175                                                 .getAsString() != null
176                                 && !requestBody
177                                                 .getAsJsonPrimitive(VtnServiceJsonConsts.TARGETDB)
178                                                 .getAsString().trim().isEmpty()) {
179                         isValid = VtnServiceJsonConsts.STATE.equalsIgnoreCase(requestBody
180                                         .getAsJsonPrimitive(VtnServiceJsonConsts.TARGETDB)
181                                         .getAsString().trim());
182                 } else {
183                         requestBody.remove(VtnServiceJsonConsts.TARGETDB);
184                         requestBody.addProperty(VtnServiceJsonConsts.TARGETDB,
185                                         VtnServiceJsonConsts.STATE);
186                 }
187                 if (!opFlag) {
188                         if (requestBody.has(VtnServiceJsonConsts.OP)) {
189                                 requestBody.remove(VtnServiceJsonConsts.OP);
190                         } else {
191                                 LOG.debug("No need to remove");
192                         }
193                         if (requestBody.has(VtnServiceJsonConsts.INDEX)) {
194                                 requestBody.remove(VtnServiceJsonConsts.INDEX);
195                         } else {
196                                 LOG.debug("No need to remove");
197                         }
198                         if (requestBody.has(VtnServiceJsonConsts.MAX)) {
199                                 requestBody.remove(VtnServiceJsonConsts.MAX);
200                         } else {
201                                 LOG.debug("No need to remove");
202                         }
203                 } else {
204                         // validation for key: op
205                         if (isValid) {
206                                 setInvalidParameter(VtnServiceJsonConsts.OP);
207                                 isValid = validator.isValidOperation(requestBody);
208                         }
209                         // validation for key: index
210                         if (isValid) {
211                                 setInvalidParameter(VtnServiceJsonConsts.INDEX);
212                                 if (requestBody.has(VtnServiceJsonConsts.INDEX)
213                                                 && requestBody.getAsJsonPrimitive(
214                                                                 VtnServiceJsonConsts.INDEX).getAsString() != null
215                                                 && !requestBody
216                                                                 .getAsJsonPrimitive(VtnServiceJsonConsts.INDEX)
217                                                                 .getAsString().isEmpty()) {
218                                         isValid = linkNameValidator(requestBody
219                                                         .getAsJsonPrimitive(VtnServiceJsonConsts.INDEX)
220                                                         .getAsString().trim());
221                                 }
222                         }
223                         // validation for key: max_repitition
224                         if (isValid) {
225                                 setInvalidParameter(VtnServiceJsonConsts.MAX);
226                                 isValid = validator.isValidMaxRepetition(requestBody);
227                                 /*
228                                  * if(requestBody.has(VtnServiceJsonConsts.MAX) &&
229                                  * requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.MAX)
230                                  * .getAsString() != null) { isValid =
231                                  * validator.isValidMaxLengthNumber(requestBody
232                                  * .getAsJsonPrimitive(VtnServiceJsonConsts.MAX).getAsString()
233                                  * .trim()); }
234                                  */
235                         }
236                 }
237                 // validation for key: switch_id
238                 if (isValid) {
239                         setInvalidParameter(VtnServiceJsonConsts.SWITCH1ID);
240                         if (requestBody.has(VtnServiceJsonConsts.SWITCH1ID)
241                                         && requestBody.getAsJsonPrimitive(
242                                                         VtnServiceJsonConsts.SWITCH1ID).getAsString() != null
243                                         && !requestBody
244                                                         .getAsJsonPrimitive(VtnServiceJsonConsts.SWITCH1ID)
245                                                         .getAsString().isEmpty()) {
246                                 isValid = validator.isValidMaxLength(requestBody
247                                                 .getAsJsonPrimitive(VtnServiceJsonConsts.SWITCH1ID)
248                                                 .getAsString().trim(), VtnServiceJsonConsts.LEN_255);
249                         }
250                 }
251
252                 if (isValid) {
253                         setInvalidParameter(VtnServiceJsonConsts.SWITCH2ID);
254                         if (requestBody.has(VtnServiceJsonConsts.SWITCH2ID)
255                                         && requestBody.getAsJsonPrimitive(
256                                                         VtnServiceJsonConsts.SWITCH2ID).getAsString() != null
257                                         && !requestBody
258                                                         .getAsJsonPrimitive(VtnServiceJsonConsts.SWITCH2ID)
259                                                         .getAsString().isEmpty()) {
260                                 isValid = validator.isValidMaxLength(requestBody
261                                                 .getAsJsonPrimitive(VtnServiceJsonConsts.SWITCH2ID)
262                                                 .getAsString().trim(), VtnServiceJsonConsts.LEN_255);
263                         }
264                 }
265                 LOG.trace("Complete LinkResourceValidator#isValidGet");
266                 return isValid;
267         }
268
269 }