Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
private static final int DEFAULT_MAX_EVENTS = 1000;

@Property(name = "maxEvents", intValue = DEFAULT_MAX_EVENTS,
        label = "Maximum number of events to accumulate")
private int maxEvents = DEFAULT_MAX_EVENTS;


private static final int DEFAULT_EMAIL = "onos-dev@onosproject.org";


@Property(name = "notificationEmail", value = DEFAULT_EMAIL,
          label = "Notification email address")
private String notificationEmail = DEFAULT_EMAIL;

...

Code Block
@Modified
public boolean modified(ComponentContext context) {
    Dictionary<?, ?> properties = context.getProperties();


    String s = Tools.get(properties, "newMaxEvents");
    maxEvents = isNullOrEmpty(s) ? DEFAULT_MAX_EVENTS : Integer.parseInt(s.trim())

    s = Tools.get(properties, "notificationEmail");
    notificationEmail = isNullOrEmpty(s) ? DEFAULT_EMAIL : s;
    ...
}

...

Note : There that there is a slight anomaly in the OSGi framework where sometimes the parameters for fields of type other than String are contained in the map returned from the properties dictionary as their specific type, i.e. Integer, Boolean, and sometime the value for the same value parameter will be returned as a String. This is why the above example uses the recommended Tools.get() method for retrieving the property value via the Tools. get() method. This method forces all values to be returned as a String, allowing the component code in the modified method to be simpler and safer. It is recommended that all configurable components use the same method.

Finally, the component should register and unregister the configuration properties from its activate and deactivate methods:

Code Block
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected ComponentConfigService cfgService;


@Activate
public void activate(ComponentContext context) {
    cfgService.registerProperties(getClass());
    modified(context);
    ...
    log.info("Started");
}

@Deactivate
public void deactivate(ComponentContext context) {
    cfgService.unregisterProperties(getClass(), false);
    ...
    log.info("Stopped");
}

 

 

...

}

 

Maven Example

Modules that contain configurable components should contain the following configuration of onos-maven-plugin:

Code Block
<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.onosproject</groupId>
            <artifactId>onos-maven-plugin</artifactId>
            <version>1.1.0</version>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>cfg</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        ...
    </plugins>
</build>

This Maven plugin will generate the jar/bundle resource containing the dictionary of configurable properties using the annotations. This is then used by the CLI, and in the future GUI, to present the ONOS administrator with a list of configurable parameters, description of their purpose, their current and default values.

The component configuration subsystem will then make sure that when a change is made on one ONOS instance, the same change will be propagated across the entire ONOS cluster and reflected on all ONOS instances in that cluster.