Versions Compared

Key

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

...

Code Block
languagejava
titleNullLinkProvider.java
collapsetrue
// new imports
import static com.google.common.base.Strings.isNullOrEmpty;

import org.osgi.service.component.ComponentContext;
import org.apache.felix.scr.annotations.Modified;
import java.util.Dictionary;

// ...<snip>...

public class NullLinkProvider extends AbstractProvider implements LinkProvider {

    private final Logger log = getLogger(getClass()); 
 
	// Attention - we need register and unregister our Class (NullLinkProvider),
	// before and after we load the config file
    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
    protected ComponentConfigService componentConfigService;  

    // The Manager serving as communication point between the ONOS core and this Provider
    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
    protected LinkProviderRegistry providerRegistry;

    // The interface that defines how this Provider can interact with the core
    private LinkProviderService providerService;

    public NullLinkProvider() {
        // ProviderId is used by the core to identify this Provider
        // The schema "null" and FQDN of the Null southbound comprise this ID
        super(new ProviderId("null", "org.onosproject.provider.nil"));
    }

    @Activate
    public void activate(ComponentContext context) {
 
		//Attention - register for system-wide configurations
		componentConfigService.registerProperties(getClass());
        providerService = providerRegistry.register(this);
        deviceService.addListener(linkProvider);

        // launch exactly one LinkDriver
        if (!modified(context)) {
            log.info("Using defaults: flicker={}, eventRate={}",
                    FLICKER, DEFAULT_RATE);
            linkDriver.submit(new LinkDriver());
        }
        log.info("started");
    }

    @Deactivate
    public void deactivate(ComponentContext context) {
 
		//Attention - just unregister when deactivate our Class
		componentConfigService.unregisterProperties(getClass(),false);
        // ...
    }

    // The method called when configuration changes are made
    @Modified
    public boolean modified(ComponentContext context) {
        return false;
    }
	
    // ...<snip>...
 }

...

Code Block
languagejava
titleNullLinkProvider.java - modified()
collapsetrue
    @Modified
    public boolean modified(ComponentContext context) {
        // if there's no file, fall back to defaults
        if (context == null) {
            log.info("No configs, using defaults: flicker={}, eventRate={}",
                    FLICKER, DEFAULT_RATE);
            return false;
        }

        Dictionary<?, ?> properties = context.getProperties();
        boolean flickSetting;
        int newRate;

 
 
        // try to extract values keyed on the parameters' names
        try {
			// Attention!!! properties.get() already return the object of corresponding Class 
  String          Object s = (String) properties.get("flicker");
            flickSetting = isNullOrEmpty(s)s == null ? flicker : Boolean.valueOf(s.trim())(Boolean)s;
            s = (String) properties.get("eventRate");
            newRate = isNullOrEmpty(s)s == null ? eventRate : (Integer.valueOf(s.trim()))s;
        } catch (Exception e) {
            log.warn(e.getMessage());
            flickSetting = flicker;
            newRate = eventRate;
        }

        if (flicker != flickSetting) {
            flicker = flickSetting;
        }

        // launch a LinkDriver with the new values, if they were changed
        if (flicker) {
            if (eventRate != newRate) {
                eventRate = newRate;
            }
            linkDriver.submit(new LinkDriver());
            log.info("Using settings: flicker={}, eventRate={}", flicker,
                    eventRate);
            return true;
        }
        return false;
    } 

...