All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bidib.wizard.gateway.support.GatewayUtils Maven / Gradle / Ivy

package org.bidib.wizard.gateway.support;

import java.util.ArrayList;
import java.util.List;

import org.bidib.jbidibc.messages.Feature;
import org.bidib.jbidibc.messages.utils.NodeUtils;
import org.bidib.wizard.api.model.NodeInterface;
import org.bidib.wizard.api.model.connection.BidibConnection;
import org.bidib.wizard.api.model.event.NodeStatusEvent.StatusIdentifier;
import org.bidib.wizard.common.labels.WizardLabelWrapper;
import org.bidib.wizard.common.node.ConnectionNodeAwarePublisher;
import org.bidib.wizard.gateway.config.GatewayConfig;
import org.bidib.wizard.gateway.model.node.ProxyInterfaceNode;
import org.bidib.wizard.gateway.model.node.ProxyNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GatewayUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(GatewayUtils.class);

    /**
     * Clone the provided node.
     * 
     * @param connection
     *            the connection
     * @param node
     *            the node
     * @return the cloned node
     */
    public static NodeInterface cloneNode(
        final BidibConnection connection, final WizardLabelWrapper wizardLabelWrapper, final NodeInterface node,
        final org.bidib.jbidibc.messages.logger.Logger nodeLogger) {
        LOGGER.info("Clone the node: {}, connection: {}", node, connection);

        // the code is from NodeFactory

        final org.bidib.jbidibc.messages.Node coreNode = node.getNode();
        org.bidib.jbidibc.messages.Node coreNodeClone =
            org.bidib.jbidibc.messages.Node
                .createNode(coreNode.getVersion(), coreNode.getAddr(), coreNode.getUniqueId());
        coreNodeClone.setLogger(nodeLogger);
        coreNodeClone.setMagic(coreNode.getMagic());

        // copy features
        List features = new ArrayList<>();
        for (Feature feature : coreNode.getFeatures()) {
            features.add(Feature.clone(feature));
        }
        coreNodeClone.setFeatures(features);

        ProxyNode clonedNode = null;
        if (NodeUtils.hasSubNodesFunctions(node.getUniqueId())) {
            clonedNode = new ProxyInterfaceNode(coreNodeClone);
        }
        else {
            clonedNode = new ProxyNode(coreNodeClone);
        }

        // create the publisher that encapsulates the subject for the port events
        final ConnectionNodeAwarePublisher publisher =
            new ConnectionNodeAwarePublisher(GatewayConfig.CONNECTION_ID_GATEWAY_SERVICE, clonedNode.getUniqueId(),
                connection.getSubjectPortEvents(), connection.getSubjectNodeEvents());
        clonedNode.setEventPublisher(publisher);
        clonedNode.setWizardLabelWrapper(wizardLabelWrapper);
        clonedNode.initialize();

        // TODO check
        if (StatusIdentifier.InitialLoadFinished == node.getNodeLoadStatusIdentifier()) {
            clonedNode.signalInitialLoadFinished();
        }
        return clonedNode;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy