use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.
the class ECFlowRuleStore method getTableStatistics.
@Override
public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) {
NodeId master = mastershipService.getMasterFor(deviceId);
if (master == null && deviceService.isAvailable(deviceId)) {
log.warn("Failed to getTableStats: No master for {}", deviceId);
return Collections.emptyList();
}
List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId);
if (tableStats == null) {
return Collections.emptyList();
}
return ImmutableList.copyOf(tableStats);
}
use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.
the class ECFlowRuleStore method getFlowRuleCount.
@Override
public int getFlowRuleCount(DeviceId deviceId, FlowEntryState state) {
NodeId master = mastershipService.getMasterFor(deviceId);
if (master == null && deviceService.isAvailable(deviceId)) {
log.warn("Failed to getFlowRuleCount: No master for {}", deviceId);
return 0;
}
if (Objects.equals(local, master) || master == null) {
return flowTable.getFlowRuleCount(deviceId, state);
}
log.trace("Forwarding getFlowRuleCount to master {} for device {}", master, deviceId);
return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(Pair.of(deviceId, state), GET_DEVICE_FLOW_COUNT, serializer::encode, serializer::decode, master), FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, 0);
}
use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.
the class ECFlowRuleStore method getFlowEntry.
@Override
public FlowEntry getFlowEntry(FlowRule rule) {
NodeId master = mastershipService.getMasterFor(rule.deviceId());
if (master == null && deviceService.isAvailable(rule.deviceId())) {
log.warn("Failed to getFlowEntry: No master for {}", rule.deviceId());
return null;
}
if (Objects.equals(local, master) || master == null) {
return flowTable.getFlowEntry(rule);
}
log.trace("Forwarding getFlowEntry to {}, which is the primary (master) for device {}", master, rule.deviceId());
return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(rule, ECFlowRuleStoreMessageSubjects.GET_FLOW_ENTRY, serializer::encode, serializer::decode, master), FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, null);
}
use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.
the class GossipDeviceStore method removeDevice.
@Override
public synchronized DeviceEvent removeDevice(DeviceId deviceId) {
final NodeId myId = clusterService.getLocalNode().id();
NodeId master = mastershipService.getMasterFor(deviceId);
// if there exist a master, forward
// if there is no master, try to become one and process
boolean relinquishAtEnd = false;
if (master == null) {
final MastershipRole myRole = mastershipService.getLocalRole(deviceId);
if (myRole != MastershipRole.NONE) {
relinquishAtEnd = true;
}
log.debug("Temporarily requesting role for {} to remove", deviceId);
if (mastershipService.requestRoleFor(deviceId).join() == MastershipRole.MASTER) {
master = myId;
}
}
boolean isMaster = myId.equals(master);
// If this node is not the master, forward the request.
if (!isMaster) {
log.debug("{} has control of {}, forwarding remove request", master, deviceId);
// TODO check unicast return value
clusterCommunicator.unicast(deviceId, DEVICE_REMOVE_REQ, SERIALIZER::encode, master);
/* error log:
log.error("Failed to forward {} remove request to {}", deviceId, master, e);
*/
}
// If this node is the master, get a timestamp. Otherwise, default to the current device timestamp.
Timestamp timestamp = isMaster ? deviceClockService.getTimestamp(deviceId) : null;
DeviceEvent event = removeDeviceInternal(deviceId, timestamp);
// If this node is the master, update peers.
if (isMaster && event != null) {
log.debug("Notifying peers of a device removed topology event for deviceId: {}", deviceId);
notifyPeers(new InternalDeviceRemovedEvent(deviceId, timestamp));
}
notifyDelegateIfNotNull(event);
// Relinquish mastership if acquired to remove the device.
if (relinquishAtEnd) {
log.debug("Relinquishing temporary role acquired for {}", deviceId);
mastershipService.relinquishMastership(deviceId);
}
return event;
}
use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.
the class UpgradeManagerTest method createUpgradeManager.
/**
* Creates a new upgrade manager to test.
*
* @param version the local node version
* @param state the initial upgrade state
* @param versions a list of controller node versions
* @return the activated upgrade manager
*/
@SuppressWarnings("unchecked")
private UpgradeManager createUpgradeManager(Version version, Upgrade state, List<Version> versions) {
UpgradeManager upgradeManager = new UpgradeManager();
injectEventDispatcher(upgradeManager, new TestEventDispatcher());
upgradeManager.membershipService = new MembershipServiceAdapter() {
@Override
public MembershipGroup getLocalGroup() {
return getGroups().stream().filter(group -> group.version().equals(version)).findFirst().get();
}
@Override
public Collection<MembershipGroup> getGroups() {
AtomicInteger nodeCounter = new AtomicInteger();
Map<Version, Set<Member>> groups = Maps.newHashMap();
versions.stream().forEach(version -> {
groups.computeIfAbsent(version, k -> Sets.newHashSet()).add(new Member(NodeId.nodeId(String.valueOf(nodeCounter.getAndIncrement())), version));
});
return Maps.transformEntries(groups, MembershipGroup::new).values();
}
};
upgradeManager.clusterService = new ClusterServiceAdapter() {
@Override
public Set<ControllerNode> getNodes() {
AtomicInteger nodeCounter = new AtomicInteger();
return versions.stream().map(v -> {
int nodeId = nodeCounter.getAndIncrement();
return new DefaultControllerNode(NodeId.nodeId(String.valueOf(nodeId)), IpAddress.valueOf("127.0.0.1"), nodeId);
}).collect(Collectors.toSet());
}
@Override
public ControllerNode getNode(NodeId nodeId) {
return getNodes().stream().filter(node -> node.id().equals(nodeId)).findFirst().orElse(null);
}
@Override
public Version getVersion(NodeId nodeId) {
return versions.get(Integer.parseInt(nodeId.id()));
}
};
upgradeManager.versionService = new VersionServiceAdapter() {
@Override
public Version version() {
return version;
}
};
upgradeManager.coordinationService = new CoordinationServiceAdapter() {
@Override
public <V> AtomicValueBuilder<V> atomicValueBuilder() {
return new AtomicValueBuilder<V>() {
@Override
public AsyncAtomicValue<V> build() {
return new AsyncAtomicValueAdapter() {
@Override
public AtomicValue asAtomicValue() {
return new AtomicValueAdapter() {
private Object value = state;
@Override
public void set(Object value) {
this.value = value;
}
@Override
public Object get() {
return value;
}
@Override
public boolean compareAndSet(Object expect, Object update) {
if ((value == null && expect == null) || (value != null && value.equals(expect))) {
value = update;
return true;
}
return false;
}
};
}
};
}
};
}
};
upgradeManager.activate();
return upgradeManager;
}
Aggregations