diff --git a/corefx/src/main/java/com/core/client/ICoreClient.java b/corefx/src/main/java/com/core/client/ICoreClient.java index 5635e615..283ed107 100644 --- a/corefx/src/main/java/com/core/client/ICoreClient.java +++ b/corefx/src/main/java/com/core/client/ICoreClient.java @@ -42,6 +42,14 @@ public interface ICoreClient { String getServiceFile(CoreNode node, String serviceName, String fileName) throws IOException; + boolean startService(CoreNode node, String serviceName) throws IOException; + + boolean stopService(CoreNode node, String serviceName) throws IOException; + + boolean restartService(CoreNode node, String serviceName) throws IOException; + + boolean validateService(CoreNode node, String serviceName) throws IOException; + boolean setServiceFile(CoreNode node, String serviceName, ServiceFile serviceFile) throws IOException; List getEmaneConfig(CoreNode node) throws IOException; diff --git a/corefx/src/main/java/com/core/client/rest/CoreRestClient.java b/corefx/src/main/java/com/core/client/rest/CoreRestClient.java index 55f3cbff..b79c690f 100644 --- a/corefx/src/main/java/com/core/client/rest/CoreRestClient.java +++ b/corefx/src/main/java/com/core/client/rest/CoreRestClient.java @@ -159,6 +159,34 @@ public class CoreRestClient implements ICoreClient { return WebUtils.getJson(url, String.class, args); } + @Override + public boolean startService(CoreNode node, String serviceName) throws IOException { + String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s/start", sessionId, node.getId(), + serviceName)); + return WebUtils.putJson(url); + } + + @Override + public boolean stopService(CoreNode node, String serviceName) throws IOException { + String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s/stop", sessionId, node.getId(), + serviceName)); + return WebUtils.putJson(url); + } + + @Override + public boolean restartService(CoreNode node, String serviceName) throws IOException { + String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s/restart", sessionId, node.getId(), + serviceName)); + return WebUtils.putJson(url); + } + + @Override + public boolean validateService(CoreNode node, String serviceName) throws IOException { + String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s/validate", sessionId, node.getId(), + serviceName)); + return WebUtils.putJson(url); + } + @Override public boolean setServiceFile(CoreNode node, String serviceName, ServiceFile serviceFile) throws IOException { String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s/file", sessionId, node.getId(), diff --git a/corefx/src/main/java/com/core/graph/NodeContextMenu.java b/corefx/src/main/java/com/core/graph/NodeContextMenu.java index a328b3da..d50cd58a 100644 --- a/corefx/src/main/java/com/core/graph/NodeContextMenu.java +++ b/corefx/src/main/java/com/core/graph/NodeContextMenu.java @@ -2,17 +2,112 @@ package com.core.graph; import com.core.Controller; import com.core.data.CoreNode; +import com.core.ui.Toast; +import javafx.scene.control.Menu; +import javafx.scene.control.MenuItem; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.IOException; +import java.util.Collections; +import java.util.Set; class NodeContextMenu extends AbstractNodeContextMenu { + private static final Logger logger = LogManager.getLogger(); + NodeContextMenu(Controller controller, CoreNode coreNode) { super(controller, coreNode); setup(); } + private MenuItem createStartItem(String service) { + MenuItem menuItem = new MenuItem("Start"); + menuItem.setOnAction(event -> { + try { + boolean result = controller.getCoreClient().startService(coreNode, service); + if (result) { + Toast.success("Started " + service); + } else { + Toast.error("Failure to start " + service); + } + } catch (IOException ex) { + Toast.error("Error starting " + service, ex); + } + }); + return menuItem; + } + + private MenuItem createStopItem(String service) { + MenuItem menuItem = new MenuItem("Stop"); + menuItem.setOnAction(event -> { + try { + boolean result = controller.getCoreClient().stopService(coreNode, service); + if (result) { + Toast.success("Stopped " + service); + } else { + Toast.error("Failure to stop " + service); + } + } catch (IOException ex) { + Toast.error("Error stopping " + service, ex); + } + }); + return menuItem; + } + + private MenuItem createRestartItem(String service) { + MenuItem menuItem = new MenuItem("Restart"); + menuItem.setOnAction(event -> { + try { + boolean result = controller.getCoreClient().restartService(coreNode, service); + if (result) { + Toast.success("Restarted " + service); + } else { + Toast.error("Failure to restart " + service); + } + } catch (IOException ex) { + Toast.error("Error restarting " + service, ex); + } + }); + return menuItem; + } + + private MenuItem createValidateItem(String service) { + MenuItem menuItem = new MenuItem("Validate"); + menuItem.setOnAction(event -> { + try { + boolean result = controller.getCoreClient().validateService(coreNode, service); + if (result) { + Toast.success("Validated " + service); + } else { + Toast.error("Validation failed for " + service); + } + } catch (IOException ex) { + Toast.error("Error validating " + service, ex); + } + }); + return menuItem; + } + private void setup() { if (controller.getCoreClient().isRunning()) { - addMenuItem("Manage Services", event -> { - }); + Set services = coreNode.getServices(); + if (services.isEmpty()) { + services = controller.getDefaultServices().getOrDefault(coreNode.getModel(), Collections.emptySet()); + } + + if (!services.isEmpty()) { + Menu menu = new Menu("Manage Services"); + for (String service : services) { + Menu serviceMenu = new Menu(service); + MenuItem startItem = createStartItem(service); + MenuItem stopItem = createStopItem(service); + MenuItem restartItem = createRestartItem(service); + MenuItem validateItem = createValidateItem(service); + serviceMenu.getItems().addAll(startItem, stopItem, restartItem, validateItem); + menu.getItems().add(serviceMenu); + } + getItems().add(menu); + } } else { addMenuItem("Services", event -> controller.getNodeServicesDialog().showDialog(coreNode)); addMenuItem("Delete Node", event -> controller.deleteNode(coreNode));