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

org.bidib.wizard.server.controllers.NodeDetailsController Maven / Gradle / Ivy

There is a newer version: 2.0.25
Show newest version
package org.bidib.wizard.server.controllers;

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

import org.apache.commons.collections4.CollectionUtils;
import org.bidib.api.json.types.NodeAddress;
import org.bidib.api.json.types.NodeInfo;
import org.bidib.api.json.types.feature.FeatureGetAll;
import org.bidib.api.json.types.feature.FeatureResponse;
import org.bidib.api.json.types.feature.FeatureSetRequest;
import org.bidib.api.json.types.node.NodeDetails;
import org.bidib.api.json.types.node.NodeDetailsResponse;
import org.bidib.api.json.types.node.NodeDetailsResquest;
import org.bidib.api.json.types.node.NodeDetailsSetResquest;
import org.bidib.api.json.types.occupancy.AbstractPort;
import org.bidib.api.json.types.occupancy.FeedbackPort;
import org.bidib.api.json.types.occupancy.OccupancyPortQueryRequest;
import org.bidib.api.json.types.occupancy.OccupancyPortQueryResponse;
import org.bidib.api.json.types.occupancy.OccupancyStatusQueryRequest;
import org.bidib.api.json.types.occupancy.OccupancyStatusQueryResponse;
import org.bidib.api.json.types.occupancy.PortIdentifier;
import org.bidib.api.json.types.switching.PcfgType;
import org.bidib.api.json.types.switching.PortConfigRequest;
import org.bidib.api.json.types.switching.PortConfigResponse;
import org.bidib.api.json.types.switching.PortQueryRequest;
import org.bidib.api.json.types.switching.PortQueryResponse;
import org.bidib.api.json.types.switching.PortStatusQueryRequest;
import org.bidib.api.json.types.switching.PortStatusQueryResponse;
import org.bidib.api.json.types.switching.PortStatusRequest;
import org.bidib.api.json.types.switching.PortStatusResponse;
import org.bidib.jbidibc.messages.Feature;
import org.bidib.jbidibc.messages.enums.PortConfigStatus;
import org.bidib.wizard.api.model.NodeInterface;
import org.bidib.wizard.api.service.node.NodeService;
import org.bidib.wizard.api.service.node.SwitchingNodeService;
import org.bidib.wizard.api.utils.JsonNodeUtils;
import org.bidib.wizard.model.ports.Port;
import org.bidib.wizard.server.aspect.LogExecutionTime;
import org.bidib.wizard.server.config.StompDestinations;
import org.bidib.wizard.server.jsontuils.JsonPortMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api/node")
public class NodeDetailsController {

    private static final Logger LOGGER = LoggerFactory.getLogger(NodeDetailsController.class);

    @Autowired
    private NodeService nodeService;

    @Autowired
    private SwitchingNodeService switchingNodeService;

    @Autowired
    private JsonPortMapping jsonPortMapping;

    @Operation(summary = "Get the features")
    @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Features", content = {
        @Content(mediaType = "application/json", schema = @Schema(implementation = FeatureResponse.class)) }) })
    @GetMapping(path = "/features")
    public ResponseEntity getAllFeatures(FeatureGetAll featureQueryAll) {

        LOGGER.info("Get the features: {}", featureQueryAll);

        String connectionId = featureQueryAll.getConnectionId();
        NodeInfo nodeInfo = featureQueryAll.getNode();

        List features = nodeService.queryAllFeatures(connectionId, nodeInfo);
        if (CollectionUtils.isEmpty(features)) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        FeatureResponse featureResponse = new FeatureResponse().withConnectionId(connectionId).withNode(nodeInfo);
        for (Feature feature : features) {
            org.bidib.api.json.types.feature.Feature jsonFeature =
                new org.bidib.api.json.types.feature.Feature(feature.getType(), feature.getValue());
            featureResponse.getFeatures().add(jsonFeature);
        }

        return new ResponseEntity<>(featureResponse, HttpStatus.OK);
    }

    @MessageMapping(StompDestinations.APP_NODE_FEATURES_QUERY_ALL_DESTINATION)
    @SendToUser(destinations = StompDestinations.REPLY_NODE_FEATURES_DESTINATION, broadcast = false)
    @LogExecutionTime
    public FeatureResponse msgQueryAllFeatures(FeatureGetAll featureQueryAll) {
        LOGGER.info("Query the features: {}", featureQueryAll);

        String connectionId = featureQueryAll.getConnectionId();
        NodeInfo nodeInfo = featureQueryAll.getNode();

        List features = nodeService.queryAllFeatures(connectionId, nodeInfo);

        FeatureResponse featureResponse = new FeatureResponse().withConnectionId(connectionId).withNode(nodeInfo);
        for (Feature feature : features) {
            org.bidib.api.json.types.feature.Feature jsonFeature =
                new org.bidib.api.json.types.feature.Feature(feature.getType(), feature.getValue());
            featureResponse.getFeatures().add(jsonFeature);
        }

        return featureResponse;
    }

    @MessageMapping(StompDestinations.APP_NODE_FEATURES_SET_DESTINATION)
    @SendToUser(destinations = StompDestinations.REPLY_NODE_FEATURES_DESTINATION, broadcast = false)
    @LogExecutionTime
    public FeatureResponse msgSetFeatures(FeatureSetRequest featuresSetRequest) {
        LOGGER.info("Set the features: {}", featuresSetRequest);

        String connectionId = featuresSetRequest.getConnectionId();
        NodeInfo nodeInfo = featuresSetRequest.getNode();
        List features = new ArrayList<>();
        // prepare the features to set
        for (org.bidib.api.json.types.feature.Feature jsonFeature : featuresSetRequest.getFeatures()) {
            Feature feature = new Feature(jsonFeature.getType(), jsonFeature.getValue());
            features.add(feature);
        }

        List featuresResult = nodeService.setFeatures(connectionId, nodeInfo, features);

        FeatureResponse featureResponse = new FeatureResponse().withConnectionId(connectionId).withNode(nodeInfo);
        // prepare the features to return
        for (Feature feature : featuresResult) {
            org.bidib.api.json.types.feature.Feature jsonFeature =
                new org.bidib.api.json.types.feature.Feature(feature.getType(), feature.getValue());
            featureResponse.getFeatures().add(jsonFeature);
        }

        return featureResponse;
    }

    @Operation(summary = "Get the ports")
    @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Ports", content = {
        @Content(mediaType = "application/json", schema = @Schema(implementation = PortQueryResponse.class)) }) })
    @GetMapping(path = "/ports")
    public ResponseEntity getPorts(PortQueryRequest portQuery) {

        LOGGER.info("Query the ports: {}", portQuery);

        String connectionId = portQuery.getConnectionId();
        NodeAddress nodeAddress = portQuery.getNode();

        List> ports = switchingNodeService.queryAllPorts(connectionId, nodeAddress);
        if (CollectionUtils.isEmpty(ports)) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }

        PortQueryResponse portQueryResponse =
            new PortQueryResponse().withConnectionId(connectionId).withNode(nodeAddress);
        for (Port port : ports) {
            AbstractPort jsonPort = JsonPortMapping.toJsonPort(port);
            if (jsonPort != null) {
                portQueryResponse.getPorts().add(jsonPort);
            }
        }

        return new ResponseEntity<>(portQueryResponse, HttpStatus.OK);
    }

    @MessageMapping(StompDestinations.APP_NODE_PORTS_QUERY_ALL_DESTINATION)
    @SendToUser(destinations = StompDestinations.REPLY_NODE_PORTS_DESTINATION, broadcast = false)
    @LogExecutionTime
    public PortQueryResponse msgQueryPorts(PortQueryRequest portQuery) {
        LOGGER.info("Query the ports: {}", portQuery);

        String connectionId = portQuery.getConnectionId();
        NodeAddress nodeAddress = portQuery.getNode();

        List> ports = switchingNodeService.queryAllPorts(connectionId, nodeAddress);

        PortQueryResponse portQueryResponse =
            new PortQueryResponse().withConnectionId(connectionId).withNode(nodeAddress);
        for (Port port : ports) {
            AbstractPort jsonPort = JsonPortMapping.toJsonPort(port);
            if (jsonPort != null) {
                portQueryResponse.getPorts().add(jsonPort);
            }
        }

        return portQueryResponse;
    }

    @Operation(summary = "Get the node details")
    @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Node details", content = {
        @Content(mediaType = "application/json", schema = @Schema(implementation = NodeDetailsResponse.class)) }) })
    @GetMapping(path = "/details")
    public ResponseEntity getNodeDetails(NodeDetailsResquest nodeDetailsRequest) {

        LOGGER.info("Get the nodeDetails: {}", nodeDetailsRequest);

        String connectionId = nodeDetailsRequest.getConnectionId();
        NodeAddress nodeAddress = nodeDetailsRequest.getNode();

        final NodeInterface node = nodeService.getNode(connectionId, nodeAddress);
        if (node == null) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }

        final NodeDetails nodeDetails = JsonNodeUtils.toNodeDetails(node);

        NodeDetailsResponse nodeDetailsResponse =
            new NodeDetailsResponse().withConnectionId(connectionId).withNodeDetails(nodeDetails);

        return new ResponseEntity<>(nodeDetailsResponse, HttpStatus.OK);
    }

    @MessageMapping(StompDestinations.APP_NODE_DETAILS_GET_DESTINATION)
    @SendToUser(destinations = StompDestinations.REPLY_NODE_DETAILS_DESTINATION, broadcast = false)
    @LogExecutionTime
    public NodeDetailsResponse msgGetNodeDetails(NodeDetailsResquest nodeDetailsRequest) {

        LOGGER.info("Get the nodeDetails: {}", nodeDetailsRequest);

        String connectionId = nodeDetailsRequest.getConnectionId();
        NodeAddress nodeAddress = nodeDetailsRequest.getNode();

        final NodeDetailsResponse nodeDetailsResponse = new NodeDetailsResponse().withConnectionId(connectionId);
        final NodeInterface node = nodeService.getNode(connectionId, nodeAddress);
        if (node != null) {

            final NodeDetails nodeDetails = JsonNodeUtils.toNodeDetails(node);
            nodeDetailsResponse.withNodeDetails(nodeDetails);
        }

        return nodeDetailsResponse;
    }

    @MessageMapping(StompDestinations.APP_NODE_DETAILS_SET_DESTINATION)
    @SendToUser(destinations = StompDestinations.REPLY_NODE_DETAILS_DESTINATION, broadcast = false)
    @LogExecutionTime
    public NodeDetailsResponse msgSetNodeDetails(NodeDetailsSetResquest nodeDetailsRequest) {

        LOGGER.info("Set the node details: {}", nodeDetailsRequest);

        String connectionId = nodeDetailsRequest.getConnectionId();
        NodeAddress nodeAddress = nodeDetailsRequest.getNode();

        final NodeDetailsResponse nodeDetailsResponse = new NodeDetailsResponse().withConnectionId(connectionId);
        final NodeInterface node =
            nodeService.setNodeDetails(connectionId, nodeAddress, nodeDetailsRequest.getUserName());
        if (node != null) {

            final NodeDetails nodeDetails = JsonNodeUtils.toNodeDetails(node);
            nodeDetailsResponse.withNodeDetails(nodeDetails);
        }

        return nodeDetailsResponse;
    }

    @MessageMapping(StompDestinations.APP_NODE_PORTS_SET_CONFIG_DESTINATION)
    @SendToUser(destinations = StompDestinations.REPLY_NODE_PORT_CONFIG_DESTINATION, broadcast = false)
    @LogExecutionTime
    public PortConfigResponse msgSetPortConfig(PortConfigRequest portConfigRequest) {

        LOGGER.info("Set the port config: {}", portConfigRequest);

        String connectionId = portConfigRequest.getConnectionId();
        NodeAddress nodeAddress = portConfigRequest.getNode();

        final PortConfigResponse portConfigResponse =
            new PortConfigResponse().withConnectionId(connectionId).withNode(nodeAddress);

        // set port config

        PortIdentifier jsonPort = portConfigRequest.getPort();

        // create a new transient port to provide the changed config
        Port port = jsonPortMapping.toPort(jsonPort);

        Set pcfgs = portConfigRequest.getPcfg();

        JsonPortMapping.addPortConfigX(port, pcfgs);

        switchingNodeService.setPortConfig(connectionId, nodeAddress, port);

        // iterate over the ports and check if an error was signaled
        if (port.getConfigStatus() == PortConfigStatus.CONFIG_ERROR) {
            LOGGER.warn("The current port has a config error: {}", port);

            portConfigResponse.setError("configError");
        }
        else {
            portConfigResponse.withPort(jsonPort).withPcfg(pcfgs);
        }

        return portConfigResponse;
    }

    @MessageMapping(StompDestinations.APP_NODE_PORTS_SET_STATE_DESTINATION)
    @SendToUser(destinations = StompDestinations.REPLY_NODE_PORT_STATE_DESTINATION, broadcast = false)
    @LogExecutionTime
    public PortStatusResponse msgSetPortStatus(PortStatusRequest portStatusRequest) {

        LOGGER.info("Set the port status: {}", portStatusRequest);

        String connectionId = portStatusRequest.getConnectionId();
        NodeAddress nodeAddress = portStatusRequest.getNode();

        final PortStatusResponse portStatusResponse =
            new PortStatusResponse().withConnectionId(connectionId).withNode(nodeAddress);

        // set port status
        PortIdentifier jsonPortIdentifier = portStatusRequest.getPort();
        portStatusResponse.withPort(jsonPortIdentifier);

        // deliver the port back to the caller

        try {
            Port port =
                jsonPortMapping
                    .toPortWithStatus(jsonPortIdentifier, portStatusRequest.getStatus(), portStatusRequest.getValue());

            switchingNodeService.setPortStatus(connectionId, nodeAddress, port);

            // set the status on the response
            portStatusResponse.setStatus(portStatusRequest.getStatus());
            portStatusResponse.setValue(portStatusRequest.getValue());
        }
        catch (Exception ex) {
            LOGGER.warn("Set the port status failed for portStatusRequest: {}", portStatusRequest, ex);

            portStatusResponse.setError("Set the port status failed");
        }

        return portStatusResponse;
    }

    @MessageMapping(StompDestinations.APP_NODE_PORTS_QUERY_STATE_DESTINATION)
    @SendToUser(destinations = StompDestinations.REPLY_NODE_PORT_STATE_DESTINATION, broadcast = false)
    @LogExecutionTime
    public PortStatusQueryResponse msgQueryPortStatus(PortStatusQueryRequest portStatusQueryRequest) {

        LOGGER.info("Query the port status: {}", portStatusQueryRequest);

        String connectionId = portStatusQueryRequest.getConnectionId();
        NodeAddress nodeAddress = portStatusQueryRequest.getNode();

        try {
            if (CollectionUtils.isNotEmpty(portStatusQueryRequest.getPorts())) {

                queryPortStatus(connectionId, nodeAddress, portStatusQueryRequest.getPorts());
            }
        }
        catch (Exception ex) {
            LOGGER.warn("Query the port status failed for node: {}", nodeAddress, ex);

            return new PortStatusQueryResponse()
                .withConnectionId(connectionId).withNode(nodeAddress)
                .withError("Query the port status failed for node: " + nodeAddress);
        }

        return new PortStatusQueryResponse().withConnectionId(connectionId).withNode(nodeAddress);
    }

    private void queryPortStatus(
        String connectionId, NodeAddress nodeAddress, List jsonPortIdentifiers) {
        // query port status
        List> ports = new ArrayList<>();
        for (PortIdentifier jsonPortIdentifier : jsonPortIdentifiers) {
            Port port = jsonPortMapping.toPort(jsonPortIdentifier);
            ports.add(port);
        }
        switchingNodeService.queryPortStatus(connectionId, nodeAddress, ports);
    }

    @GetMapping(path = "/occupancyports")
    public ResponseEntity getPorts(OccupancyPortQueryRequest portQuery) {

        LOGGER.info("Query the ports: {}", portQuery);

        String connectionId = portQuery.getConnectionId();
        NodeAddress nodeAddress = portQuery.getNode();

        List ports =
            nodeService.queryAllOccupancyPorts(connectionId, nodeAddress);
        if (CollectionUtils.isEmpty(ports)) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }

        OccupancyPortQueryResponse portQueryResponse =
            new OccupancyPortQueryResponse().withConnectionId(connectionId).withNode(nodeAddress);
        for (Port port : ports) {
            FeedbackPort jsonPort = JsonPortMapping.toJsonOccupancyPort(port);
            if (jsonPort != null) {
                portQueryResponse.getPorts().add(jsonPort);
            }
        }

        return new ResponseEntity<>(portQueryResponse, HttpStatus.OK);
    }

    @MessageMapping(StompDestinations.APP_NODE_OCCUPANCY_PORTS_QUERY_ALL_DESTINATION)
    @SendToUser(destinations = StompDestinations.REPLY_NODE_OCCUPANCY_PORTS_DESTINATION, broadcast = false)
    @LogExecutionTime
    public OccupancyPortQueryResponse msgQueryPorts(OccupancyPortQueryRequest portQuery) {
        LOGGER.info("Query the ports: {}", portQuery);

        String connectionId = portQuery.getConnectionId();
        NodeAddress nodeAddress = portQuery.getNode();

        List ports =
            nodeService.queryAllOccupancyPorts(connectionId, nodeAddress);

        OccupancyPortQueryResponse portQueryResponse =
            new OccupancyPortQueryResponse().withConnectionId(connectionId).withNode(nodeAddress);
        for (Port port : ports) {
            FeedbackPort jsonPort = JsonPortMapping.toJsonOccupancyPort(port);
            if (jsonPort != null) {
                portQueryResponse.getPorts().add(jsonPort);
            }
        }

        return portQueryResponse;
    }

    @GetMapping(path = "/occupancyports/querystatus")
    public ResponseEntity queryOccupancyPortStatus(
        OccupancyStatusQueryRequest occupancyPortStatusQueryRequest) {
        LOGGER.info("Query the feedback port status: {}", occupancyPortStatusQueryRequest);

        String connectionId = occupancyPortStatusQueryRequest.getConnectionId();
        NodeAddress nodeAddress = occupancyPortStatusQueryRequest.getNode();

        try {
            nodeService
                .queryFeedbackPortStatus(connectionId, nodeAddress, occupancyPortStatusQueryRequest.getPortRangeStart(),
                    occupancyPortStatusQueryRequest.getPortRangeEnd());
        }
        catch (Exception ex) {
            LOGGER.warn("Query the feedback port status failed for node: {}", nodeAddress, ex);

            OccupancyStatusQueryResponse portStatusQueryResponse =
                new OccupancyStatusQueryResponse()
                    .withConnectionId(connectionId).withNode(nodeAddress)
                    .withError("Query the feedback port status failed for node: " + nodeAddress);
            return new ResponseEntity<>(portStatusQueryResponse, HttpStatus.BAD_REQUEST);
        }

        OccupancyStatusQueryResponse portStatusQueryResponse =
            new OccupancyStatusQueryResponse().withConnectionId(connectionId).withNode(nodeAddress);
        return new ResponseEntity<>(portStatusQueryResponse, HttpStatus.OK);
    }

    @MessageMapping(StompDestinations.APP_NODE_OCCUPANCY_PORTS_QUERY_STATE_DESTINATION)
    @SendToUser(destinations = StompDestinations.REPLY_NODE_OCCUPANCY_PORT_STATE_DESTINATION, broadcast = false)
    @LogExecutionTime
    public OccupancyStatusQueryResponse msgQueryOccupancyPortStatus(
        OccupancyStatusQueryRequest occupancyPortStatusQueryRequest) {

        LOGGER.info("Query the feedback port status: {}", occupancyPortStatusQueryRequest);

        String connectionId = occupancyPortStatusQueryRequest.getConnectionId();
        NodeAddress nodeAddress = occupancyPortStatusQueryRequest.getNode();

        try {
            nodeService
                .queryFeedbackPortStatus(connectionId, nodeAddress, occupancyPortStatusQueryRequest.getPortRangeStart(),
                    occupancyPortStatusQueryRequest.getPortRangeEnd());
        }
        catch (Exception ex) {
            LOGGER.warn("Query the feedback port status failed for node: {}", nodeAddress, ex);

            return new OccupancyStatusQueryResponse()
                .withConnectionId(connectionId).withNode(nodeAddress)
                .withError("Query the feedback port status failed for node: " + nodeAddress);
        }

        return new OccupancyStatusQueryResponse().withConnectionId(connectionId).withNode(nodeAddress);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy