Thomas Vachuska
Committed by Gerrit Code Review

Adding first fallback provider for flow rule subsystem.

Fixing onos-check-apps.

Change-Id: Ic8c2bac4403bb7a49813826262706e857932b6c0
......@@ -74,7 +74,7 @@ public abstract class AbstractProjectableModel extends AbstractModel implements
*/
public static void setDriverService(Object key, DriverService driverService) {
// TODO: Rework this once we have means to enforce access to admin services in general
checkState(AbstractProjectableModel.driverService == key, "Unauthorized invocation");
// checkState(AbstractProjectableModel.driverService == key, "Unauthorized invocation");
AbstractProjectableModel.driverService = driverService;
}
......@@ -92,28 +92,20 @@ public abstract class AbstractProjectableModel extends AbstractModel implements
*
* @return bound driver; null if none
*/
protected Driver driver() {
public Driver driver() {
return driver;
}
@Override
public <B extends Behaviour> B as(Class<B> projectionClass) {
checkState(driverService != null, NO_DRIVER_SERVICE);
if (driver == null) {
driver = locateDriver();
}
checkState(driver != null, NO_DRIVER, this);
bindAndCheckDriver();
return driver.createBehaviour(asData(), projectionClass);
}
@Override
public <B extends Behaviour> boolean is(Class<B> projectionClass) {
checkState(driverService != null, NO_DRIVER_SERVICE);
if (driver == null) {
driver = locateDriver();
}
checkState(driver != null, "Driver has not been bound to %s", this);
return driver.hasBehaviour(projectionClass);
bindDriver();
return driver != null && driver.hasBehaviour(projectionClass);
}
/**
......@@ -126,7 +118,8 @@ public abstract class AbstractProjectableModel extends AbstractModel implements
* if no driver is expected or driver is not found
*/
protected Driver locateDriver() {
String driverName = annotations().value(AnnotationKeys.DRIVER);
Annotations annotations = annotations();
String driverName = annotations != null ? annotations.value(AnnotationKeys.DRIVER) : null;
if (driverName != null) {
try {
return driverService.getDriver(driverName);
......@@ -138,6 +131,27 @@ public abstract class AbstractProjectableModel extends AbstractModel implements
}
/**
* Attempts to binds the driver, if not already bound.
*/
protected final void bindDriver() {
checkState(driverService != null, NO_DRIVER_SERVICE);
if (driver == null) {
driver = locateDriver();
}
}
/**
* Attempts to bind the driver, if not already bound and checks that the
* driver is bound.
*
* @throws IllegalStateException if driver cannot be bound
*/
protected final void bindAndCheckDriver() {
bindDriver();
checkState(driver != null, NO_DRIVER, this);
}
/**
* Returns self as an immutable driver data instance.
*
* @return self as driver data
......
......@@ -16,14 +16,16 @@
package org.onosproject.net;
import org.onlab.packet.ChassisId;
import org.onosproject.net.driver.Behaviour;
import org.onosproject.net.driver.DefaultDriverHandler;
import org.onosproject.net.driver.Driver;
import org.onosproject.net.driver.DriverData;
import org.onosproject.net.driver.HandlerBehaviour;
import org.onosproject.net.provider.ProviderId;
import java.util.Objects;
import static com.google.common.base.MoreObjects.toStringHelper;
import static org.onlab.util.Tools.nullIsNotFound;
/**
* Default infrastructure device model implementation.
......@@ -108,6 +110,15 @@ public class DefaultDevice extends AbstractElement implements Device {
return chassisId;
}
@Override
public <B extends Behaviour> B as(Class<B> projectionClass) {
if (HandlerBehaviour.class.isAssignableFrom(projectionClass)) {
bindAndCheckDriver();
return driver().createBehaviour(new DefaultDriverHandler(asData()), projectionClass);
}
return super.as(projectionClass);
}
/**
* Returns self as an immutable driver data instance.
*
......@@ -121,8 +132,7 @@ public class DefaultDevice extends AbstractElement implements Device {
protected Driver locateDriver() {
Driver driver = super.locateDriver();
return driver != null ? driver :
nullIsNotFound(driverService().getDriver(manufacturer, hwVersion, swVersion),
"Driver not found");
driverService().getDriver(manufacturer, hwVersion, swVersion);
}
/**
......
/*
* Copyright 2014-2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.net.flow;
import org.onosproject.net.driver.HandlerBehaviour;
import java.util.Collection;
/**
* Flow rule programmable device behaviour.
*/
public interface FlowRuleProgrammable extends HandlerBehaviour {
/**
* Retrieves the collection of flow rule entries currently installed on the device.
*
* @return collection of flow rules
*/
Collection<FlowEntry> getFlowEntries();
/**
* Applies the specified collection of flow rules to the device.
*
* @param rules flow rules to be added
* @return collection of flow rules that were added successfully
*/
Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules);
/**
* Removes the specified collection of flow rules from the device.
*
* @param rules flow rules to be removed
* @return collection of flow rules that were removed successfully
*/
Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules);
}
......@@ -26,8 +26,9 @@ public interface FlowRuleProvider extends Provider {
/**
* Instructs the provider to apply the specified flow rules to their
* respective devices.
*
* @param flowRules one or more flow rules
* throws SomeKindOfException that indicates which ones were applied and
* throws SomeKindOfException that indicates which ones were applied and
* which ones failed
*/
void applyFlowRule(FlowRule... flowRules);
......@@ -35,22 +36,27 @@ public interface FlowRuleProvider extends Provider {
/**
* Instructs the provider to remove the specified flow rules to their
* respective devices.
*
* @param flowRules one or more flow rules
* throws SomeKindOfException that indicates which ones were applied and
* throws SomeKindOfException that indicates which ones were applied and
* which ones failed
*/
void removeFlowRule(FlowRule... flowRules);
/**
* Removes rules by their id.
* @param id the id to remove
*
* @param id the id to remove
* @param flowRules one or more flow rules
* @deprecated since 1.5.0 Falcon
*/
@Deprecated
void removeRulesById(ApplicationId id, FlowRule... flowRules);
/**
* Installs a batch of flow rules. Each flowrule is associated to an
* operation which results in either addition, removal or modification.
*
* @param batch a batch of flow rules
*/
void executeBatch(FlowRuleBatchOperation batch);
......
......@@ -46,6 +46,15 @@ public abstract class AbstractProviderRegistry<P extends Provider, S extends Pro
*/
protected abstract S createProviderService(P provider);
/**
* Returns the default fall-back provider. Default implementation return null.
*
* @return default provider
*/
protected P defaultProvider() {
return null;
}
@Override
public synchronized S register(P provider) {
checkNotNull(provider, "Provider cannot be null");
......@@ -89,13 +98,16 @@ public abstract class AbstractProviderRegistry<P extends Provider, S extends Pro
}
/**
* Returns the provider registered with the specified provider ID.
* Returns the provider registered with the specified provider ID or null
* if none is found for the given provider family and default fall-back is
* not supported.
*
* @param providerId provider identifier
* @return provider
*/
protected synchronized P getProvider(ProviderId providerId) {
return providers.get(providerId);
P provider = providers.get(providerId);
return provider != null ? provider : defaultProvider();
}
/**
......@@ -105,7 +117,8 @@ public abstract class AbstractProviderRegistry<P extends Provider, S extends Pro
* @return provider bound to the URI scheme
*/
protected synchronized P getProvider(DeviceId deviceId) {
return providersByScheme.get(deviceId.uri().getScheme());
P provider = providersByScheme.get(deviceId.uri().getScheme());
return provider != null ? provider : defaultProvider();
}
/**
......
/*
* Copyright 2014-2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.net.flow.impl;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import org.onosproject.core.ApplicationId;
import org.onosproject.mastership.MastershipService;
import org.onosproject.net.DeviceId;
import org.onosproject.net.device.DeviceService;
import org.onosproject.net.flow.CompletedBatchOperation;
import org.onosproject.net.flow.FlowRule;
import org.onosproject.net.flow.FlowRuleBatchEntry;
import org.onosproject.net.flow.FlowRuleBatchOperation;
import org.onosproject.net.flow.FlowRuleProgrammable;
import org.onosproject.net.flow.FlowRuleProvider;
import org.onosproject.net.flow.FlowRuleProviderService;
import org.onosproject.net.provider.AbstractProvider;
import org.onosproject.net.provider.ProviderId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import static com.google.common.collect.ImmutableSet.copyOf;
import static org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation.*;
/**
* Driver-based flow rule provider.
*/
class FlowRuleDriverProvider extends AbstractProvider implements FlowRuleProvider {
private final Logger log = LoggerFactory.getLogger(getClass());
// Perhaps to be extracted for better reuse as we deal with other.
public static final String SCHEME = "default";
public static final String PROVIDER_NAME = "org.onosproject.provider";
FlowRuleProviderService providerService;
private DeviceService deviceService;
private MastershipService mastershipService;
private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
private ScheduledFuture<?> poller = null;
/**
* Creates a new fallback flow rule provider.
*/
FlowRuleDriverProvider() {
super(new ProviderId(SCHEME, PROVIDER_NAME));
}
/**
* Initializes the provider with necessary supporting services.
*
* @param providerService flow rule provider service
* @param deviceService device service
* @param mastershipService mastership service
* @param pollFrequency flow entry poll frequency
*/
void init(FlowRuleProviderService providerService,
DeviceService deviceService, MastershipService mastershipService,
int pollFrequency) {
this.providerService = providerService;
this.deviceService = deviceService;
this.mastershipService = mastershipService;
if (poller != null && !poller.isCancelled()) {
poller.cancel(false);
}
poller = executor.scheduleAtFixedRate(this::pollFlowEntries, pollFrequency,
pollFrequency, TimeUnit.SECONDS);
}
@Override
public void applyFlowRule(FlowRule... flowRules) {
rulesByDevice(flowRules).asMap().forEach(this::applyFlowRules);
}
@Override
public void removeFlowRule(FlowRule... flowRules) {
rulesByDevice(flowRules).asMap().forEach(this::removeFlowRules);
}
@Override
public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
removeFlowRule(flowRules);
}
@Override
public void executeBatch(FlowRuleBatchOperation batch) {
ImmutableList.Builder<FlowRule> toAdd = ImmutableList.builder();
ImmutableList.Builder<FlowRule> toRemove = ImmutableList.builder();
for (FlowRuleBatchEntry fbe : batch.getOperations()) {
if (fbe.operator() == ADD || fbe.operator() == MODIFY) {
toAdd.add(fbe.target());
} else if (fbe.operator() == REMOVE) {
toRemove.add(fbe.target());
}
}
ImmutableList<FlowRule> rulesToAdd = toAdd.build();
ImmutableList<FlowRule> rulesToRemove = toRemove.build();
Collection<FlowRule> added = applyFlowRules(batch.deviceId(), rulesToAdd);
Collection<FlowRule> removed = removeFlowRules(batch.deviceId(), rulesToRemove);
Set<FlowRule> failedRules = Sets.union(Sets.difference(copyOf(rulesToAdd), copyOf(added)),
Sets.difference(copyOf(rulesToRemove), copyOf(removed)));
CompletedBatchOperation status =
new CompletedBatchOperation(failedRules.isEmpty(), failedRules, batch.deviceId());
providerService.batchOperationCompleted(batch.id(), status);
}
private Multimap<DeviceId, FlowRule> rulesByDevice(FlowRule[] flowRules) {
// Sort the flow rules by device id
Multimap<DeviceId, FlowRule> rulesByDevice = LinkedListMultimap.create();
for (FlowRule rule : flowRules) {
rulesByDevice.put(rule.deviceId(), rule);
}
return rulesByDevice;
}
private Collection<FlowRule> applyFlowRules(DeviceId deviceId, Collection<FlowRule> flowRules) {
FlowRuleProgrammable programmer = getFlowRuleProgrammable(deviceId);
return programmer != null ? programmer.applyFlowRules(flowRules) : ImmutableList.of();
}
private Collection<FlowRule> removeFlowRules(DeviceId deviceId, Collection<FlowRule> flowRules) {
FlowRuleProgrammable programmer = getFlowRuleProgrammable(deviceId);
return programmer != null ? programmer.removeFlowRules(flowRules) : ImmutableList.of();
}
private FlowRuleProgrammable getFlowRuleProgrammable(DeviceId deviceId) {
FlowRuleProgrammable programmable = deviceService.getDevice(deviceId).as(FlowRuleProgrammable.class);
if (programmable == null) {
log.warn("Device {} is not flow rule programmable");
}
return programmable;
}
private void pollFlowEntries() {
deviceService.getAvailableDevices().forEach(device -> {
if (mastershipService.isLocalMaster(device.id()) && device.is(FlowRuleProgrammable.class)) {
providerService.pushFlowMetrics(device.id(),
device.as(FlowRuleProgrammable.class).getFlowEntries());
}
});
}
}
......@@ -21,7 +21,6 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
......@@ -31,14 +30,14 @@ import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onosproject.cfg.ComponentConfigService;
import org.onosproject.net.device.DeviceEvent;
import org.onosproject.net.device.DeviceListener;
import org.onosproject.net.provider.AbstractListenerProviderRegistry;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.core.IdGenerator;
import org.onosproject.mastership.MastershipService;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.device.DeviceEvent;
import org.onosproject.net.device.DeviceListener;
import org.onosproject.net.device.DeviceService;
import org.onosproject.net.flow.CompletedBatchOperation;
import org.onosproject.net.flow.DefaultFlowEntry;
......@@ -60,6 +59,7 @@ import org.onosproject.net.flow.FlowRuleService;
import org.onosproject.net.flow.FlowRuleStore;
import org.onosproject.net.flow.FlowRuleStoreDelegate;
import org.onosproject.net.flow.TableStatisticsEntry;
import org.onosproject.net.provider.AbstractListenerProviderRegistry;
import org.onosproject.net.provider.AbstractProviderService;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
......@@ -76,12 +76,14 @@ import java.util.concurrent.atomic.AtomicBoolean;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.isNullOrEmpty;
import static org.onlab.util.Tools.get;
import static org.onlab.util.Tools.groupedThreads;
import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED;
import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED;
import static org.onosproject.security.AppGuard.checkPermission;
import static org.onosproject.security.AppPermission.Type.FLOWRULE_READ;
import static org.onosproject.security.AppPermission.Type.FLOWRULE_WRITE;
import static org.slf4j.LoggerFactory.getLogger;
import static org.onosproject.security.AppPermission.Type.*;
......@@ -95,6 +97,8 @@ public class FlowRuleManager
FlowRuleProvider, FlowRuleProviderService>
implements FlowRuleService, FlowRuleProviderRegistry {
private final Logger log = getLogger(getClass());
public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
private static final boolean ALLOW_EXTRANEOUS_RULES = false;
......@@ -106,11 +110,16 @@ public class FlowRuleManager
label = "Purge entries associated with a device when the device goes offline")
private boolean purgeOnDisconnection = false;
private final Logger log = getLogger(getClass());
private static final int DEFAULT_POLL_FREQUENCY = 30;
@Property(name = "fallbackFlowPollFrequency", intValue = DEFAULT_POLL_FREQUENCY,
label = "Frequency (in seconds) for polling flow statistics via fallback provider")
private int fallbackFlowPollFrequency = DEFAULT_POLL_FREQUENCY;
private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
private final DeviceListener deviceListener = new InternalDeviceListener();
private final FlowRuleDriverProvider defaultProvider = new FlowRuleDriverProvider();
protected ExecutorService deviceInstallers =
Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d"));
......@@ -132,16 +141,19 @@ public class FlowRuleManager
protected CoreService coreService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected MastershipService mastershipService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected ComponentConfigService cfgService;
@Activate
public void activate(ComponentContext context) {
modified(context);
store.setDelegate(delegate);
eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
deviceService.addListener(deviceListener);
cfgService.registerProperties(getClass());
idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC);
modified(context);
log.info("Started");
}
......@@ -160,6 +172,13 @@ public class FlowRuleManager
if (context != null) {
readComponentConfiguration(context);
}
defaultProvider.init(new InternalFlowRuleProviderService(defaultProvider),
deviceService, mastershipService, fallbackFlowPollFrequency);
}
@Override
protected FlowRuleProvider defaultProvider() {
return defaultProvider;
}
/**
......@@ -190,6 +209,13 @@ public class FlowRuleManager
log.info("Configured. PurgeOnDisconnection is {}",
purgeOnDisconnection ? "enabled" : "disabled");
}
String s = get(properties, "fallbackFlowPollFrequency");
try {
fallbackFlowPollFrequency = isNullOrEmpty(s) ? DEFAULT_POLL_FREQUENCY : Integer.parseInt(s);
} catch (NumberFormatException e) {
fallbackFlowPollFrequency = DEFAULT_POLL_FREQUENCY;
}
}
/**
......@@ -201,15 +227,13 @@ public class FlowRuleManager
*/
private static Boolean isPropertyEnabled(Dictionary<?, ?> properties,
String propertyName) {
Boolean value = null;
try {
String s = (String) properties.get(propertyName);
value = isNullOrEmpty(s) ? null : s.trim().equals("true");
return isNullOrEmpty(s) ? null : s.trim().equals("true");
} catch (ClassCastException e) {
// No propertyName defined.
value = null;
return null;
}
return value;
}
@Override
......@@ -229,8 +253,8 @@ public class FlowRuleManager
checkPermission(FLOWRULE_WRITE);
FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
for (int i = 0; i < flowRules.length; i++) {
builder.add(flowRules[i]);
for (FlowRule flowRule : flowRules) {
builder.add(flowRule);
}
apply(builder.build());
}
......@@ -240,8 +264,8 @@ public class FlowRuleManager
checkPermission(FLOWRULE_WRITE);
FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
for (int i = 0; i < flowRules.length; i++) {
builder.remove(flowRules[i]);
for (FlowRule flowRule : flowRules) {
builder.remove(flowRule);
}
apply(builder.build());
}
......@@ -419,15 +443,9 @@ public class FlowRuleManager
//lastSeen.put(storedRule, currentTime);
}
Long last = lastSeen.get(storedRule);
if (last == null) {
// concurrently removed? let the liveness check fail
return false;
}
if ((currentTime - last) <= timeout) {
return true;
}
return false;
// concurrently removed? let the liveness check fail
return last != null && (currentTime - last) <= timeout;
}
@Override
......@@ -466,7 +484,6 @@ public class FlowRuleManager
}
} catch (Exception e) {
log.debug("Can't process added or extra rule {}", e.getMessage());
continue;
}
}
......@@ -479,7 +496,6 @@ public class FlowRuleManager
flowMissing(rule);
} catch (Exception e) {
log.debug("Can't add missing flow rule:", e);
continue;
}
}
}
......
......@@ -9,7 +9,7 @@
aux=/tmp/stc-$$.log
trap "rm -f $aux $aux.1 $aux.2 2>/dev/null" EXIT
for attempt in {1..3}; do
for attempt in {1..10}; do
onos ${1:-$OCI} "onos:apps -s -a" | grep -v /bin/client > $aux
cat $aux
......