blob: 29a244eaf6134ccf0312f796ff3b538b408ff36d [file] [log] [blame]
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -07003 *
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 Milkeya4122362015-08-18 15:19:08 -070016package org.onosproject.net.config;
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070017
18
Thomas Vachuskae2b7e7e2015-05-20 11:11:31 -070019import com.google.common.annotations.Beta;
20
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070021/**
22 * Base abstract factory for creating configurations for the specified subject type.
23 *
Thomas Vachuska96d55b12015-05-11 08:52:03 -070024 * @param <S> type of subject
25 * @param <C> type of configuration
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070026 */
Thomas Vachuskae2b7e7e2015-05-20 11:11:31 -070027@Beta
Thomas Vachuska96d55b12015-05-11 08:52:03 -070028public abstract class ConfigFactory<S, C extends Config<S>> {
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070029
Thomas Vachuska96d55b12015-05-11 08:52:03 -070030 private final SubjectFactory<S> subjectFactory;
31 private final Class<C> configClass;
32 private final String configKey;
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070033 private final boolean isList;
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070034
35 /**
36 * Creates a new configuration factory for the specified class of subjects
Thomas Vachuska96d55b12015-05-11 08:52:03 -070037 * 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 Vachuskaf0e1fae2015-04-24 00:51:51 -070040 *
Thomas Vachuska96d55b12015-05-11 08:52:03 -070041 * @param subjectFactory subject factory
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070042 * @param configClass configuration class
43 * @param configKey configuration class key
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070044 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -070045 protected ConfigFactory(SubjectFactory<S> subjectFactory,
46 Class<C> configClass, String configKey) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070047 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 Vachuska96d55b12015-05-11 08:52:03 -070069 this.subjectFactory = subjectFactory;
70 this.configClass = configClass;
71 this.configKey = configKey;
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070072 this.isList = isList;
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070073 }
74
75 /**
76 * Returns the class of the subject to which this factory applies.
77 *
78 * @return subject type
79 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -070080 public SubjectFactory<S> subjectFactory() {
81 return subjectFactory;
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070082 }
83
84 /**
Thomas Vachuska96d55b12015-05-11 08:52:03 -070085 * Returns the class of the configuration which this factory generates.
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070086 *
Thomas Vachuska96d55b12015-05-11 08:52:03 -070087 * @return configuration type
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070088 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -070089 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 Vachuskaf0e1fae2015-04-24 00:51:51 -0700102 }
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 Vachuska96d55b12015-05-11 08:52:03 -0700110 public abstract C createConfig();
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -0700111
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700112 /**
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 Vachuskaf0e1fae2015-04-24 00:51:51 -0700122}