Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.exp.net.config; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | /** |
| 24 | * Base abstraction of a configuration facade for a specific subject. Derived |
| 25 | * classes should keep all state in the specified JSON tree. |
| 26 | * |
| 27 | * @param <S> type of subject |
| 28 | */ |
| 29 | public abstract class Config<S> { |
| 30 | |
| 31 | protected ObjectMapper mapper; |
| 32 | protected ObjectNode node; |
| 33 | private S subject; |
| 34 | private ConfigApplyDelegate<S> delegate; |
| 35 | |
| 36 | /** |
| 37 | * Returns the specific subject to which this configuration pertains. |
| 38 | * |
| 39 | * @return configuration subject |
| 40 | */ |
| 41 | S subject() { |
| 42 | return subject; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Initializes the configuration behaviour with necessary context. |
| 47 | * |
Thomas Vachuska | 266b443 | 2015-04-30 18:13:25 -0700 | [diff] [blame^] | 48 | * @param subject configuration subject |
| 49 | * @param node JSON object node where configuration data is stored |
| 50 | * @param mapper JSON object mapper |
| 51 | * @param delegate delegate context |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 52 | */ |
| 53 | public void init(S subject, ObjectNode node, ObjectMapper mapper, |
| 54 | ConfigApplyDelegate<S> delegate) { |
| 55 | this.subject = checkNotNull(subject); |
| 56 | this.node = checkNotNull(node); |
| 57 | this.mapper = checkNotNull(mapper); |
| 58 | this.delegate = checkNotNull(delegate); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Applies any configuration changes made via this configuration. |
| 63 | */ |
| 64 | public void apply() { |
| 65 | delegate.onApply(this); |
| 66 | } |
| 67 | |
| 68 | } |