Package org.onosproject.net.config
Class Config<S>
- java.lang.Object
-
- org.onosproject.net.config.Config<S>
-
- Type Parameters:
S
- type of subject
- Direct Known Subclasses:
AllowedEntityConfig
,BandwidthCapacity
,BaseConfig
,BasicFeatureConfig
,BasicUiTopoLayoutConfig
,BgpConfig
,HostLearningConfig
,InterfaceConfig
,IntReportConfig
,McastConfig
,PortDescriptionsConfig
,RouterConfig
,RoutersConfig
@Beta public abstract class Config<S> extends java.lang.Object
Base abstraction of a configuration facade for a specific subject. Derived classes should keep all state in the specified JSON tree as that is the only state that will be distributed or persisted; this class is merely a facade for interacting with a particular facet of configuration on a given subject.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Config.FieldPresence
Indicator of whether a configuration JSON field is required.
-
Field Summary
Fields Modifier and Type Field Description protected com.fasterxml.jackson.databind.node.ArrayNode
array
protected ConfigApplyDelegate
delegate
protected java.lang.String
key
protected com.fasterxml.jackson.databind.ObjectMapper
mapper
protected com.fasterxml.jackson.databind.JsonNode
node
protected com.fasterxml.jackson.databind.node.ObjectNode
object
protected S
subject
-
Constructor Summary
Constructors Constructor Description Config()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
apply()
Applies any configuration changes made via this configuration.protected Config<S>
clear(java.lang.String name)
Clears the specified property.protected boolean
get(java.lang.String name, boolean defaultValue)
Gets the specified property as a boolean.protected double
get(java.lang.String name, double defaultValue)
Gets the specified property as a double.protected int
get(java.lang.String name, int defaultValue)
Gets the specified property as an integer.protected long
get(java.lang.String name, long defaultValue)
Gets the specified property as a long.protected <E extends java.lang.Enum<E>>
Eget(java.lang.String name, E defaultValue, java.lang.Class<E> enumClass)
Gets the specified property as an enum.protected java.lang.String
get(java.lang.String name, java.lang.String defaultValue)
Gets the specified property as a string.protected <T> java.util.List<T>
getList(java.lang.String name, java.util.function.Function<java.lang.String,T> function)
Gets the specified array property as a list of items.protected <T> java.util.List<T>
getList(java.lang.String name, java.util.function.Function<java.lang.String,T> function, java.util.List<T> defaultValue)
Gets the specified array property as a list of items.protected boolean
hasField(com.fasterxml.jackson.databind.node.ObjectNode node, java.lang.String name)
Returns true if the given node contains a field with the given name.protected boolean
hasField(java.lang.String name)
Returns true if this config contains a field with the given name.protected boolean
hasFields(com.fasterxml.jackson.databind.node.ObjectNode node, java.lang.String... mandatoryFields)
Indicates whether all specified fields are present in a particular JSON object.protected boolean
hasFields(java.lang.String... mandatoryFields)
Indicates whether all specified fields are present in the backing JSON.protected boolean
hasOnlyFields(com.fasterxml.jackson.databind.node.ObjectNode node, java.lang.String... allowedFields)
Indicates whether only the specified fields are present in a particular JSON object.protected boolean
hasOnlyFields(java.lang.String... allowedFields)
Indicates whether only the specified fields are present in the backing JSON.void
init(S subject, java.lang.String key, com.fasterxml.jackson.databind.JsonNode node, com.fasterxml.jackson.databind.ObjectMapper mapper, ConfigApplyDelegate delegate)
Initializes the configuration behaviour with necessary context.protected boolean
isBoolean(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field of a particular node holds a valid boolean value.protected boolean
isBoolean(java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field holds a valid boolean value.protected boolean
isConnectPoint(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field of a particular node holds a valid connect point string.protected boolean
isConnectPoint(java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field holds a valid connect point string.protected boolean
isDecimal(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence, double... minMax)
Indicates whether the specified field of a particular node holds a valid decimal number.protected boolean
isDecimal(java.lang.String field, Config.FieldPresence presence, double... minMax)
Indicates whether the specified field holds a valid decimal number.protected boolean
isIntegralNumber(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence, long... minMax)
Indicates whether the specified field of a particular node holds a valid integer.protected boolean
isIntegralNumber(java.lang.String field, Config.FieldPresence presence, long... minMax)
Indicates whether the specified field holds a valid integer.protected boolean
isIpAddress(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field of a particular node holds a valid IP address.protected boolean
isIpAddress(java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field holds a valid IP address.protected boolean
isIpPrefix(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field of a particular node holds a valid IP prefix.protected boolean
isIpPrefix(java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field holds a valid IP prefix.protected boolean
isMacAddress(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field of a particular node holds a valid MAC address.protected boolean
isMacAddress(java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field holds a valid MAC address.protected boolean
isNumber(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence, long... minMax)
Indicates whether the specified field of a particular node holds a valid number.protected boolean
isNumber(java.lang.String field, Config.FieldPresence presence, long... minMax)
Indicates whether the specified field holds a valid number.protected boolean
isString(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence, java.lang.String... pattern)
Indicates whether the specified field on a particular node holds a valid string value.protected boolean
isString(java.lang.String field, Config.FieldPresence presence, java.lang.String... pattern)
Indicates whether the specified field holds a valid string value.protected boolean
isTpPort(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field of a particular node holds a valid transport layer port.protected boolean
isTpPort(java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field holds a valid transport layer port.boolean
isValid()
Indicates whether or not the backing JSON node contains valid data.protected boolean
isValidLength(java.lang.String field, int maxLength)
Indicates whether the specified field is of a valid length.java.lang.String
key()
Returns the configuration key.com.fasterxml.jackson.databind.JsonNode
node()
Returns the JSON node that contains the configuration data.protected <T> Config<S>
setList(java.lang.String name, java.util.function.Function<? super T,java.lang.String> function, java.util.List<T> value)
Sets the specified property as an array of items in a given collection transformed into a String with suppliedfunction
.protected <E extends java.lang.Enum>
Config<S>setOrClear(java.lang.String name, E value)
Sets the specified property as a double or clears it if null value given.protected Config<S>
setOrClear(java.lang.String name, java.lang.Boolean value)
Sets the specified property as a boolean or clears it if null value given.protected Config<S>
setOrClear(java.lang.String name, java.lang.Double value)
Sets the specified property as a double or clears it if null value given.protected Config<S>
setOrClear(java.lang.String name, java.lang.Integer value)
Sets the specified property as an integer or clears it if null value given.protected Config<S>
setOrClear(java.lang.String name, java.lang.Long value)
Sets the specified property as a long or clears it if null value given.protected Config<S>
setOrClear(java.lang.String name, java.lang.String value)
Sets the specified property as a string or clears it if null value given.protected <T> Config<S>
setOrClear(java.lang.String name, java.util.Collection<T> collection)
Sets the specified property as an array of items in a given collection or clears it if null is given.S
subject()
Returns the specific subject to which this configuration pertains.java.lang.String
toString()
-
-
-
Field Detail
-
subject
protected S subject
-
key
protected java.lang.String key
-
node
protected com.fasterxml.jackson.databind.JsonNode node
-
object
protected com.fasterxml.jackson.databind.node.ObjectNode object
-
array
protected com.fasterxml.jackson.databind.node.ArrayNode array
-
mapper
protected com.fasterxml.jackson.databind.ObjectMapper mapper
-
delegate
protected ConfigApplyDelegate delegate
-
-
Method Detail
-
init
public final void init(S subject, java.lang.String key, com.fasterxml.jackson.databind.JsonNode node, com.fasterxml.jackson.databind.ObjectMapper mapper, ConfigApplyDelegate delegate)
Initializes the configuration behaviour with necessary context.- Parameters:
subject
- configuration subjectkey
- configuration keynode
- JSON node where configuration data is storedmapper
- JSON object mapperdelegate
- delegate context, or null for detached configs.
-
isValid
public boolean isValid()
Indicates whether or not the backing JSON node contains valid data.Default implementation returns true. Subclasses are expected to override this with their own validation. Implementations are free to throw a RuntimeException if data is invalid.
- Returns:
- true if the data is valid; false otherwise
- Throws:
java.lang.RuntimeException
- if configuration is invalid or completely foobar
-
subject
public S subject()
Returns the specific subject to which this configuration pertains.- Returns:
- configuration subject
-
key
public java.lang.String key()
Returns the configuration key. This is primarily aimed for use in composite JSON trees in external representations and has no bearing on the internal behaviours.- Returns:
- configuration key
-
node
public com.fasterxml.jackson.databind.JsonNode node()
Returns the JSON node that contains the configuration data.- Returns:
- JSON node backing the configuration
-
apply
public void apply()
Applies any configuration changes made via this configuration.Not effective for detached configs.
-
get
protected java.lang.String get(java.lang.String name, java.lang.String defaultValue)
Gets the specified property as a string.- Parameters:
name
- property namedefaultValue
- default value if property not set- Returns:
- property value or default value
-
setOrClear
protected Config<S> setOrClear(java.lang.String name, java.lang.String value)
Sets the specified property as a string or clears it if null value given.- Parameters:
name
- property namevalue
- new value or null to clear the property- Returns:
- self
-
get
protected boolean get(java.lang.String name, boolean defaultValue)
Gets the specified property as a boolean.- Parameters:
name
- property namedefaultValue
- default value if property not set- Returns:
- property value or default value
-
clear
protected Config<S> clear(java.lang.String name)
Clears the specified property.- Parameters:
name
- property name- Returns:
- self
-
setOrClear
protected Config<S> setOrClear(java.lang.String name, java.lang.Boolean value)
Sets the specified property as a boolean or clears it if null value given.- Parameters:
name
- property namevalue
- new value or null to clear the property- Returns:
- self
-
get
protected int get(java.lang.String name, int defaultValue)
Gets the specified property as an integer.- Parameters:
name
- property namedefaultValue
- default value if property not set- Returns:
- property value or default value
-
setOrClear
protected Config<S> setOrClear(java.lang.String name, java.lang.Integer value)
Sets the specified property as an integer or clears it if null value given.- Parameters:
name
- property namevalue
- new value or null to clear the property- Returns:
- self
-
get
protected long get(java.lang.String name, long defaultValue)
Gets the specified property as a long.- Parameters:
name
- property namedefaultValue
- default value if property not set- Returns:
- property value or default value
-
setOrClear
protected Config<S> setOrClear(java.lang.String name, java.lang.Long value)
Sets the specified property as a long or clears it if null value given.- Parameters:
name
- property namevalue
- new value or null to clear the property- Returns:
- self
-
get
protected double get(java.lang.String name, double defaultValue)
Gets the specified property as a double.- Parameters:
name
- property namedefaultValue
- default value if property not set- Returns:
- property value or default value
-
setOrClear
protected Config<S> setOrClear(java.lang.String name, java.lang.Double value)
Sets the specified property as a double or clears it if null value given.- Parameters:
name
- property namevalue
- new value or null to clear the property- Returns:
- self
-
get
protected <E extends java.lang.Enum<E>> E get(java.lang.String name, E defaultValue, java.lang.Class<E> enumClass)
Gets the specified property as an enum.- Type Parameters:
E
- type of enum- Parameters:
name
- property namedefaultValue
- default value if property not setenumClass
- the enum class- Returns:
- property value or default value
-
setOrClear
protected <E extends java.lang.Enum> Config<S> setOrClear(java.lang.String name, E value)
Sets the specified property as a double or clears it if null value given.- Type Parameters:
E
- type of enum- Parameters:
name
- property namevalue
- new value or null to clear the property- Returns:
- self
-
getList
protected <T> java.util.List<T> getList(java.lang.String name, java.util.function.Function<java.lang.String,T> function)
Gets the specified array property as a list of items.- Type Parameters:
T
- type of item- Parameters:
name
- property namefunction
- mapper from string to item- Returns:
- list of items
-
getList
protected <T> java.util.List<T> getList(java.lang.String name, java.util.function.Function<java.lang.String,T> function, java.util.List<T> defaultValue)
Gets the specified array property as a list of items.- Type Parameters:
T
- type of item- Parameters:
name
- property namefunction
- mapper from string to itemdefaultValue
- default value if property not set- Returns:
- list of items
-
setList
protected <T> Config<S> setList(java.lang.String name, java.util.function.Function<? super T,java.lang.String> function, java.util.List<T> value)
Sets the specified property as an array of items in a given collection transformed into a String with suppliedfunction
.- Type Parameters:
T
- type of items- Parameters:
name
- propertyNamefunction
- to transform item to a Stringvalue
- list of items- Returns:
- self
-
setOrClear
protected <T> Config<S> setOrClear(java.lang.String name, java.util.Collection<T> collection)
Sets the specified property as an array of items in a given collection or clears it if null is given.- Type Parameters:
T
- type of items- Parameters:
name
- propertyNamecollection
- collection of items- Returns:
- self
-
isValidLength
protected boolean isValidLength(java.lang.String field, int maxLength)
Indicates whether the specified field is of a valid length.- Parameters:
field
- the field to validatemaxLength
- the maximum allowed length of the field- Returns:
- true if the field lenth is less than the required length
-
hasField
protected boolean hasField(java.lang.String name)
Returns true if this config contains a field with the given name.- Parameters:
name
- the field name- Returns:
- true if field is present, false otherwise
-
hasField
protected boolean hasField(com.fasterxml.jackson.databind.node.ObjectNode node, java.lang.String name)
Returns true if the given node contains a field with the given name.- Parameters:
node
- the node to examinename
- the name to look for- Returns:
- true if the node has a field with the given name, false otherwise
-
hasOnlyFields
protected boolean hasOnlyFields(java.lang.String... allowedFields)
Indicates whether only the specified fields are present in the backing JSON.- Parameters:
allowedFields
- allowed field names- Returns:
- true if only allowedFields are present; false otherwise
-
hasOnlyFields
protected boolean hasOnlyFields(com.fasterxml.jackson.databind.node.ObjectNode node, java.lang.String... allowedFields)
Indicates whether only the specified fields are present in a particular JSON object.- Parameters:
node
- node whose fields to checkallowedFields
- allowed field names- Returns:
- true if only allowedFields are present; false otherwise
-
hasFields
protected boolean hasFields(java.lang.String... mandatoryFields)
Indicates whether all specified fields are present in the backing JSON.- Parameters:
mandatoryFields
- mandatory field names- Returns:
- true if all mandatory fields are present; false otherwise
-
hasFields
protected boolean hasFields(com.fasterxml.jackson.databind.node.ObjectNode node, java.lang.String... mandatoryFields)
Indicates whether all specified fields are present in a particular JSON object.- Parameters:
node
- node whose fields to checkmandatoryFields
- mandatory field names- Returns:
- true if all mandatory fields are present; false otherwise
-
isMacAddress
protected boolean isMacAddress(java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field holds a valid MAC address.- Parameters:
field
- JSON field namepresence
- specifies if field is optional or mandatory- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isMacAddress
protected boolean isMacAddress(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field of a particular node holds a valid MAC address.- Parameters:
objectNode
- JSON nodefield
- JSON field namepresence
- specifies if field is optional or mandatory- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isIpAddress
protected boolean isIpAddress(java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field holds a valid IP address.- Parameters:
field
- JSON field namepresence
- specifies if field is optional or mandatory- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isIpAddress
protected boolean isIpAddress(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field of a particular node holds a valid IP address.- Parameters:
objectNode
- node from whom to access the fieldfield
- JSON field namepresence
- specifies if field is optional or mandatory- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isIpPrefix
protected boolean isIpPrefix(java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field holds a valid IP prefix.- Parameters:
field
- JSON field namepresence
- specifies if field is optional or mandatory- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isIpPrefix
protected boolean isIpPrefix(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field of a particular node holds a valid IP prefix.- Parameters:
objectNode
- node from whom to access the fieldfield
- JSON field namepresence
- specifies if field is optional or mandatory- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isTpPort
protected boolean isTpPort(java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field holds a valid transport layer port.- Parameters:
field
- JSON field namepresence
- specifies if field is optional or mandatory- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isTpPort
protected boolean isTpPort(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field of a particular node holds a valid transport layer port.- Parameters:
objectNode
- node from whom to access the fieldfield
- JSON field namepresence
- specifies if field is optional or mandatory- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isConnectPoint
protected boolean isConnectPoint(java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field holds a valid connect point string.- Parameters:
field
- JSON field namepresence
- specifies if field is optional or mandatory- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isConnectPoint
protected boolean isConnectPoint(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field of a particular node holds a valid connect point string.- Parameters:
objectNode
- JSON nodefield
- JSON field namepresence
- specifies if field is optional or mandatory- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isString
protected boolean isString(java.lang.String field, Config.FieldPresence presence, java.lang.String... pattern)
Indicates whether the specified field holds a valid string value.- Parameters:
field
- JSON field namepresence
- specifies if field is optional or mandatorypattern
- optional regex pattern- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isString
protected boolean isString(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence, java.lang.String... pattern)
Indicates whether the specified field on a particular node holds a valid string value.- Parameters:
objectNode
- JSON nodefield
- JSON field namepresence
- specifies if field is optional or mandatorypattern
- optional regex pattern- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isNumber
protected boolean isNumber(java.lang.String field, Config.FieldPresence presence, long... minMax)
Indicates whether the specified field holds a valid number.- Parameters:
field
- JSON field namepresence
- specifies if field is optional or mandatoryminMax
- optional min/max values- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isNumber
protected boolean isNumber(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence, long... minMax)
Indicates whether the specified field of a particular node holds a valid number.- Parameters:
objectNode
- JSON objectfield
- JSON field namepresence
- specifies if field is optional or mandatoryminMax
- optional min/max values- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isIntegralNumber
protected boolean isIntegralNumber(java.lang.String field, Config.FieldPresence presence, long... minMax)
Indicates whether the specified field holds a valid integer.- Parameters:
field
- JSON field namepresence
- specifies if field is optional or mandatoryminMax
- optional min/max values- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isIntegralNumber
protected boolean isIntegralNumber(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence, long... minMax)
Indicates whether the specified field of a particular node holds a valid integer.- Parameters:
objectNode
- JSON nodefield
- JSON field namepresence
- specifies if field is optional or mandatoryminMax
- optional min/max values- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isDecimal
protected boolean isDecimal(java.lang.String field, Config.FieldPresence presence, double... minMax)
Indicates whether the specified field holds a valid decimal number.- Parameters:
field
- JSON field namepresence
- specifies if field is optional or mandatoryminMax
- optional min/max values- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isDecimal
protected boolean isDecimal(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence, double... minMax)
Indicates whether the specified field of a particular node holds a valid decimal number.- Parameters:
objectNode
- JSON nodefield
- JSON field namepresence
- specifies if field is optional or mandatoryminMax
- optional min/max values- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isBoolean
protected boolean isBoolean(java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field holds a valid boolean value.- Parameters:
field
- JSON field namepresence
- specifies if field is optional or mandatory- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
isBoolean
protected boolean isBoolean(com.fasterxml.jackson.databind.node.ObjectNode objectNode, java.lang.String field, Config.FieldPresence presence)
Indicates whether the specified field of a particular node holds a valid boolean value.- Parameters:
objectNode
- JSON object nodefield
- JSON field namepresence
- specifies if field is optional or mandatory- Returns:
- true if valid; false otherwise
- Throws:
InvalidFieldException
- if the field is present but not valid
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-