org.bidib.wizard.common.node.SwitchPairPortHandler 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.SwitchPairPort;
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 SwitchPairPortHandler extends AbstractPortHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(SwitchPairPortHandler.class);
public SwitchPairPortHandler(final NodeInterface node, final ConnectionNodeAwarePublisher publisher,
final WizardLabelWrapper wizardLabelWrapper) {
super(node, publisher, wizardLabelWrapper);
}
@Override
public void setPorts(List switchPairPorts, final NodeChangePublisher nodeChangePublisher) {
LOGGER.info("Set the switchPair ports on the node: {}", switchPairPorts);
synchronized (ports) {
ports.clear();
if (CollectionUtils.isNotEmpty(switchPairPorts)) {
ports.addAll(switchPairPorts);
final NodeLabels nodeLabels =
getWizardLabelWrapper().getWizardLabelFactory().loadLabels(node.getUniqueId());
// set the port labels
BidibLabelUtils.applyPortLabels(nodeLabels, WizardLabelFactory.LabelTypes.switchPairPort, ports);
}
portsProcessed = false;
}
nodeChangePublisher.firePortListChanged(SwitchPairPort.class);
// TODO change to use the subject
getPublisher()
.getSubjectPortEvents()
.onNext(new PortListEvent(getPublisher().getConnectionId(), getPublisher().getUniqueId(), ports));
}
/**
* Get the switchPair ports.
*
* @return the switchPair ports
* @param nodeChangePublisher
* the node change publisher
*/
@Override
protected void cachePorts(List genericPorts) {
LOGGER.debug("Prepare the switchPair ports.");
for (GenericPort genericPort : genericPorts) {
// fetch the values from the generic port
if (genericPort.isSupportsSwitchPairPort()) {
LOGGER.trace("The current port supports switchPair port: {}", genericPort);
SwitchPairPort switchPort = new SwitchPairPort(genericPort);
switchPort.setId(genericPort.getPortNumber());
ports.add(switchPort);
}
else {
LOGGER.trace("The current port does not support switchPair port: {}", genericPort);
}
}
final NodeLabels nodeLabels = getWizardLabelWrapper().getWizardLabelFactory().loadLabels(node.getUniqueId());
// set the port labels
BidibLabelUtils.applyPortLabels(nodeLabels, WizardLabelFactory.LabelTypes.switchPairPort, ports);
}
@Override
public List getEnabledPorts() {
// TODO make sure that the switchPairPorts are filled already
synchronized (ports) {
List enabledPorts = new LinkedList<>();
for (SwitchPairPort port : ports) {
if (port.isEnabled()) {
enabledPorts.add(port);
}
}
return Collections.unmodifiableList(enabledPorts);
}
}
public void setPortStatus(final int portNumber, int portState, final NodeChangePublisher nodeChangePublisher) {
SwitchPairPort 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.isSupportsSwitchPairPort() && genericPort.getPortNumber() == portNumber) {
LOGGER.info("The current port supports switchPair port: {}", genericPort);
genericPort.setPortStatus(ByteUtils.getLowByte(portState));
if (genericPort.isInactive()) {
genericPort.setInactive(false);
portConfigChanged = true;
}
port = new SwitchPairPort(genericPort);
break;
}
else {
LOGGER.trace("The current port does not support switchPair port: {}", genericPort);
}
}
}
}
if (port == null) {
// make sure the switchPair ports are available
if (CollectionUtils.isNotEmpty(ports)) {
SwitchPairPort switchPairPort = PortListUtils.findPortByPortNumber(ports, portNumber);
if (switchPairPort != null) {
SwitchPortEnum type = SwitchPortEnum.valueOf(ByteUtils.getLowByte(portState));
SwitchPortStatus status = SwitchPortStatus.valueOf(type);
LOGGER.debug("SwitchPort status has changed, port: {}, status: {}", switchPairPort, status);
switchPairPort.setStatus(status);
if (switchPairPort.isInactive()) {
switchPairPort.setInactive(false);
portConfigChanged = true;
}
port = switchPairPort;
}
else {
LOGGER.warn("No switchPair port available for portNumber: {}", portNumber);
}
}
}
else {
LOGGER.debug("No switchPair ports or generic ports available.");
}
}
if (port != null) {
nodeChangePublisher.firePortStatusChanged(SwitchPairPort.class, port);
if (portConfigChanged) {
nodeChangePublisher.firePortConfigChanged(port);
}
getPublisher()
.getSubjectPortEvents()
.onNext(new PortStatusEvent(getPublisher().getConnectionId(), getPublisher().getUniqueId(), port));
}
}
}