org.bidib.wizard.common.node.SwitchPortHandler Maven / Gradle / Ivy
package org.bidib.wizard.common.node;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.bidib.jbidibc.core.schema.bidiblabels.NodeLabels;
import org.bidib.jbidibc.messages.enums.SwitchPortEnum;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.wizard.api.model.NodeChangePublisher;
import org.bidib.wizard.api.model.NodeInterface;
import org.bidib.wizard.api.utils.PortListUtils;
import org.bidib.wizard.common.labels.BidibLabelUtils;
import org.bidib.wizard.common.labels.WizardLabelFactory;
import org.bidib.wizard.common.labels.WizardLabelWrapper;
import org.bidib.wizard.model.ports.GenericPort;
import org.bidib.wizard.model.ports.SwitchPort;
import org.bidib.wizard.model.ports.event.PortListEvent;
import org.bidib.wizard.model.ports.event.PortStatusEvent;
import org.bidib.wizard.model.status.SwitchPortStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SwitchPortHandler extends AbstractPortHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(SwitchPortHandler.class);
public SwitchPortHandler(final NodeInterface node, final ConnectionNodeAwarePublisher publisher,
final WizardLabelWrapper wizardLabelWrapper) {
super(node, publisher, wizardLabelWrapper);
}
@Override
public void setPorts(List switchPorts, final NodeChangePublisher nodeChangePublisher) {
LOGGER.info("Set the switch ports on the node: {}", switchPorts);
synchronized (ports) {
ports.clear();
if (CollectionUtils.isNotEmpty(switchPorts)) {
ports.addAll(switchPorts);
final NodeLabels nodeLabels =
getWizardLabelWrapper().getWizardLabelFactory().loadLabels(node.getUniqueId());
// set the port labels
BidibLabelUtils.applyPortLabels(nodeLabels, WizardLabelFactory.LabelTypes.switchPort, ports);
}
portsProcessed = false;
}
nodeChangePublisher.firePortListChanged(SwitchPort.class);
// TODO change to use the subject
getPublisher()
.getSubjectPortEvents()
.onNext(new PortListEvent(getPublisher().getConnectionId(), getPublisher().getUniqueId(), ports));
}
@Override
protected void cachePorts(List genericPorts) {
LOGGER.info("Prepare the switch ports.");
for (GenericPort genericPort : genericPorts) {
// fetch the values from the generic port
if (genericPort.isSupportsSwitchPort()) {
LOGGER.trace("The current port supports switch port: {}", genericPort);
SwitchPort switchPort = new SwitchPort(genericPort);
switchPort.setId(genericPort.getPortNumber());
ports.add(switchPort);
}
else {
LOGGER.trace("The current port does not support switch port: {}", genericPort);
}
}
LOGGER.info("Prepared the switch ports: {}", ports);
final NodeLabels nodeLabels = getWizardLabelWrapper().getWizardLabelFactory().loadLabels(node.getUniqueId());
// set the port labels
BidibLabelUtils.applyPortLabels(nodeLabels, WizardLabelFactory.LabelTypes.switchPort, ports);
}
@Override
public List getEnabledPorts() {
// TODO make sure that the switchPorts are filled already
synchronized (ports) {
List enabledPorts = new LinkedList<>();
for (SwitchPort port : ports) {
if (port.isEnabled()) {
enabledPorts.add(port);
}
}
return Collections.unmodifiableList(enabledPorts);
}
}
public void setPortStatus(final int portNumber, int portState, final NodeChangePublisher nodeChangePublisher) {
SwitchPort port = null;
boolean portConfigChanged = false;
synchronized (ports) {
// support the flat port model
if (node.getNode().isPortFlatModelAvailable()) {
if (CollectionUtils.isNotEmpty(node.getGenericPorts())) {
for (GenericPort genericPort : node.getGenericPorts()) {
// fetch the values from the generic port
if (genericPort.isSupportsSwitchPort() && genericPort.getPortNumber() == portNumber) {
LOGGER.info("The current port supports switch port: {}", genericPort);
genericPort.setPortStatus(ByteUtils.getLowByte(portState));
if (genericPort.isInactive()) {
genericPort.setInactive(false);
portConfigChanged = true;
}
port = new SwitchPort(genericPort);
break;
}
else {
LOGGER.trace("The current port does not support switch port: {}", genericPort);
}
}
}
}
if (port == null) {
// make sure the switch ports are available
if (CollectionUtils.isNotEmpty(ports)) {
SwitchPort switchPort = PortListUtils.findPortByPortNumber(ports, portNumber);
if (switchPort != null) {
SwitchPortEnum type = SwitchPortEnum.valueOf(ByteUtils.getLowByte(portState));
SwitchPortStatus status = SwitchPortStatus.valueOf(type);
LOGGER.debug("SwitchPort status has changed, port: {}, status: {}", switchPort, status);
switchPort.setStatus(status);
if (switchPort.isInactive()) {
switchPort.setInactive(false);
portConfigChanged = true;
}
port = switchPort;
}
else {
LOGGER.warn("No switch port available for portNumber: {}", portNumber);
}
}
}
else {
LOGGER.debug("No switch ports or generic ports available.");
}
}
if (port != null) {
// TODO remove the node change publisher
nodeChangePublisher.firePortStatusChanged(SwitchPort.class, port);
if (portConfigChanged) {
nodeChangePublisher.firePortConfigChanged(port);
}
getPublisher()
.getSubjectPortEvents()
.onNext(new PortStatusEvent(getPublisher().getConnectionId(), getPublisher().getUniqueId(), port));
}
}
}