Added response data to operational datastore, refactored data validation
[packetcable.git] / packetcable-policy-server / src / main / java / org / opendaylight / controller / packetcable / provider / validation / impl / ValidatorProviderImpl.java
1 /*
2  * Copyright (c) 2015 CableLabs 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
9 package org.opendaylight.controller.packetcable.provider.validation.impl;
10
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13 import com.google.common.collect.Maps;
14 import java.util.Map;
15 import java.util.NoSuchElementException;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.packetcable.provider.validation.ValidationException;
18 import org.opendaylight.controller.packetcable.provider.validation.Validator;
19 import org.opendaylight.controller.packetcable.provider.validation.ValidatorProvider;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21
22
23 /**
24  * {@inheritDoc}
25  *
26  * @author rvail
27  */
28 public class ValidatorProviderImpl implements ValidatorProvider {
29
30     private final Map<Class<? extends DataObject>, Validator<? extends DataObject>> validatorMap;
31
32     public ValidatorProviderImpl() {
33         this.validatorMap = Maps.newHashMap();
34     }
35
36     /**
37      * {@inheritDoc}
38      */
39     @Override
40     public <T extends DataObject> void put(@Nonnull final Class<T> tClass, @Nonnull final Validator<T> validator) {
41         checkNotNull(tClass);
42         checkNotNull(validator);
43         validatorMap.put(tClass, validator);
44     }
45
46     /**
47      * {@inheritDoc}
48      */
49     @Override
50     public <T extends DataObject> void validate(@Nonnull final Class<T> tClass, @Nonnull final DataObject data,
51             @Nonnull final Validator.Extent extent) throws ValidationException {
52         if (!tClass.isAssignableFrom(data.getClass())) {
53             throw new IllegalArgumentException(
54                     String.format("data must be the same type as tClass, got=%s : expected=%s",
55                             data.getImplementedInterface(), tClass));
56         }
57         // We are checking the type of data above
58         @SuppressWarnings("unchecked") T tData = (T) data;
59         validatorFor(tClass).validate(tData, extent);
60     }
61
62     /**
63      * {@inheritDoc}
64      */
65     @Override
66     public <T extends DataObject> Validator<T> validatorFor(@Nonnull final Class<T> tClass) {
67         checkNotNull(tClass);
68         if (validatorMap.containsKey(tClass)) {
69             // validation is done via the put method all key/value pairs are for the same type T
70             @SuppressWarnings("unchecked") Validator<T> result = (Validator<T>) validatorMap.get(tClass);
71             return result;
72         }
73         throw new NoSuchElementException("Entry not found for key: " + tClass);
74     }
75
76
77 }