Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 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 | */ |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 16 | package org.onosproject.net.config; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 17 | |
| 18 | |
Thomas Vachuska | e2b7e7e | 2015-05-20 11:11:31 -0700 | [diff] [blame] | 19 | import com.google.common.annotations.Beta; |
| 20 | |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 21 | /** |
| 22 | * Base abstract factory for creating configurations for the specified subject type. |
| 23 | * |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 24 | * @param <S> type of subject |
| 25 | * @param <C> type of configuration |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 26 | */ |
Thomas Vachuska | e2b7e7e | 2015-05-20 11:11:31 -0700 | [diff] [blame] | 27 | @Beta |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 28 | public abstract class ConfigFactory<S, C extends Config<S>> { |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 29 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 30 | private final SubjectFactory<S> subjectFactory; |
| 31 | private final Class<C> configClass; |
| 32 | private final String configKey; |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 33 | private final boolean isList; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Creates a new configuration factory for the specified class of subjects |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 37 | * capable of generating the configurations of the specified class. The |
| 38 | * subject and configuration class keys are used merely as keys for use in |
| 39 | * composite JSON trees. |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 40 | * |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 41 | * @param subjectFactory subject factory |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 42 | * @param configClass configuration class |
| 43 | * @param configKey configuration class key |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 44 | */ |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 45 | protected ConfigFactory(SubjectFactory<S> subjectFactory, |
| 46 | Class<C> configClass, String configKey) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 47 | this(subjectFactory, configClass, configKey, false); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Creates a new configuration factory for the specified class of subjects |
| 52 | * capable of generating the configurations of the specified class. The |
| 53 | * subject and configuration class keys are used merely as keys for use in |
| 54 | * composite JSON trees. |
| 55 | * <p> |
| 56 | * Note that configurations backed by JSON array are not easily extensible |
| 57 | * at the top-level as they are inherently limited to holding an ordered |
| 58 | * list of items. |
| 59 | * </p> |
| 60 | * |
| 61 | * @param subjectFactory subject factory |
| 62 | * @param configClass configuration class |
| 63 | * @param configKey configuration class key |
| 64 | * @param isList true to indicate backing by JSON array |
| 65 | */ |
| 66 | protected ConfigFactory(SubjectFactory<S> subjectFactory, |
| 67 | Class<C> configClass, String configKey, |
| 68 | boolean isList) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 69 | this.subjectFactory = subjectFactory; |
| 70 | this.configClass = configClass; |
| 71 | this.configKey = configKey; |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 72 | this.isList = isList; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the class of the subject to which this factory applies. |
| 77 | * |
| 78 | * @return subject type |
| 79 | */ |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 80 | public SubjectFactory<S> subjectFactory() { |
| 81 | return subjectFactory; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 85 | * Returns the class of the configuration which this factory generates. |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 86 | * |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 87 | * @return configuration type |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 88 | */ |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 89 | public Class<C> configClass() { |
| 90 | return configClass; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns the unique key (within subject class) of this configuration. |
| 95 | * This is primarily aimed for use in composite JSON trees in external |
| 96 | * representations and has no bearing on the internal behaviours. |
| 97 | * |
| 98 | * @return configuration key |
| 99 | */ |
| 100 | public String configKey() { |
| 101 | return configKey; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Creates a new but uninitialized configuration. Framework will initialize |
| 106 | * the configuration via {@link Config#init} method. |
| 107 | * |
| 108 | * @return new uninitialized configuration |
| 109 | */ |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 110 | public abstract C createConfig(); |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 111 | |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Indicates whether the configuration is a list and should be backed by |
| 114 | * a JSON array rather than JSON object. |
| 115 | * |
| 116 | * @return true if backed by JSON array |
| 117 | */ |
| 118 | public boolean isList() { |
| 119 | return isList; |
| 120 | } |
| 121 | |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 122 | } |