/** * Handle a JSON control request. * * The control action is executed directly * using the calling thread. * * @param request the request * @return JSON response, JSON null if the request was not recognized. * @throws Exception on failure */ public JsonElement controlRequest(JsonObject request) throws Exception { if (request.has(OP_KEY)) return controlOperation(request); return JsonNull.INSTANCE; }
/** * Submit the specified application previously registered * via {@link #registerTopology(String, BiConsumer) registerTopology}. * @param appName name of registered application * @param config See {@link #submit(Topology, JsonObject) submit}. May be null. * * @throws Exception on failure starting applications. */ private void submitApplication(String appName, JsonObject config) throws Exception { if (systemApps.contains(appName)) { throw new IllegalArgumentException("appName"); } ApplicationServiceMXBean bean = getControlService().getControl(ApplicationServiceMXBean.TYPE, ApplicationService.ALIAS, ApplicationServiceMXBean.class); bean.submit(appName, config==null ? null : config.toString()); } }
@Override public <T> String getControlId(String type, String alias, Class<T> controlInterface) { String controlId = getControlId(type, null, alias); return getControlMBean(controlId, controlInterface) != null ? controlId : null; }
@Test public void test1ArgString() throws Exception { JsonControlService control = new JsonControlService(); assertNull(cb2.getDoneS()); control.registerControl("myb", "1s", null, MyBean.class, cb1); control.registerControl("myb", "2s", null, MyBean.class, cb2); args.add(new JsonPrimitive("ABC")); req.add(JsonControlService.ARGS_KEY, args); control.controlRequest(req); args.add(new JsonPrimitive("DEF")); req.add(JsonControlService.ARGS_KEY, args); control.controlRequest(req);
@Test //@Ignore("This test relies on an existing war in a given location ... need to refactor this") public void testAppServiceJar() throws Exception { DirectProvider provider = new DirectProvider(); JsonControlService control = new JsonControlService(); provider.getServices().addService(ControlService.class, control); AppService.createAndRegister(provider, provider); File testAppsJar = getServerJar(); assertNotNull(testAppsJar); assertTrue(testAppsJar.exists()); System.out.println("Using server jar at: " + testAppsJar.toString()); URL testAppsJarURL = testAppsJar.toURI().toURL(); JsonObject registerJar = newRegisterJarRequest(testAppsJarURL.toExternalForm()); JsonElement crr = control.controlRequest(registerJar); assertTrue(crr.getAsBoolean()); Thread.sleep(500); JsonObject submitAppTwo = newSubmitRequest("SecondJarApp"); crr = control.controlRequest(submitAppTwo); assertTrue(crr.getAsBoolean()); Thread.sleep(500); }
final String type = request.get(TYPE_KEY).getAsString(); String alias = request.get(ALIAS_KEY).getAsString(); final String controlId = getControlId(type, null, alias); Method method = findMethod(mbean.getControlInterface(), methodName, argumentCount); executeMethod(method, mbean.getControl(), getArguments(method, args));
/** * Create application connects {@code edgentControl} device commands * to the control service. * * Subscribes to device * commands of type {@link Commands#CONTROL_SERVICE} * and sends the payload into the JSON control service * to invoke the control operation. */ protected void createIotCommandToControlApp() { this.registerTopology(CONTROL_APP_NAME, (iotDevice, config) -> { TStream<JsonObject> controlCommands = iotDevice.commands(Commands.CONTROL_SERVICE); controlCommands.sink(cmd -> { try { getControlService().controlRequest(cmd.getAsJsonObject(IotDevice.CMD_PAYLOAD)); } catch (Exception re) { // If the command fails then don't stop this application, // just process the next command. LoggerFactory.getLogger(ControlService.class).error("Control request failed: {}", cmd, re); } }); }); systemApps.add(CONTROL_APP_NAME); }
public DirectProvider() { this.services = new ServiceContainer(); getServices().addService(ControlService.class, new JsonControlService()); }
/** * {@inheritDoc} * <P> * All control service MBeans must be valid according * to {@link Controls#isControlServiceMBean(Class)}. * </P> * * @see Controls#isControlServiceMBean(Class) */ @Override public synchronized <T> String registerControl(String type, String id, String alias, Class<T> controlInterface, T control) { if (!Controls.isControlServiceMBean(controlInterface)) throw new IllegalArgumentException(); final String controlId = getControlId(type, id, alias); if (mbeans.containsKey(controlId)) { logger.error("Control id: {} already exists", controlId); throw new IllegalStateException(); } logger.trace("Register control id: {}", controlId); mbeans.put(controlId, new ControlMBean<T>(controlInterface, control)); return controlId; }
@Test public void test1ArgInt() throws Exception { JsonControlService control = new JsonControlService(); assertEquals(0, cb2.getDoneI()); control.registerControl("myb", "1i", null, MyBean.class, cb1); control.registerControl("myb", "2i", null, MyBean.class, cb2); args.add(new JsonPrimitive(726)); req.add(JsonControlService.ARGS_KEY, args); control.controlRequest(req); args.add(new JsonPrimitive(2924)); req.add(JsonControlService.ARGS_KEY, args); control.controlRequest(req);
@Test public void testAppService() throws Exception { DirectProvider provider = new DirectProvider(); JsonControlService control = new JsonControlService(); provider.getServices().addService(ControlService.class, control); AppService.createAndRegister(provider, provider); IotTestApps.registerApplications(provider); JsonObject submitAppOne = newSubmitRequest("AppOne"); JsonElement crr = control.controlRequest(submitAppOne); assertTrue(crr.getAsBoolean()); }
final String type = request.get(TYPE_KEY).getAsString(); String alias = request.get(ALIAS_KEY).getAsString(); final String controlId = getControlId(type, null, alias); Method method = findMethod(mbean.getControlInterface(), methodName, argumentCount); executeMethod(method, mbean.getControl(), getArguments(method, args));
/** * Create application connects {@code edgentControl} device commands * to the control service. * * Subscribes to device * commands of type {@link Commands#CONTROL_SERVICE} * and sends the payload into the JSON control service * to invoke the control operation. */ protected void createIotCommandToControlApp() { this.registerTopology(CONTROL_APP_NAME, (iotDevice, config) -> { TStream<JsonObject> controlCommands = iotDevice.commands(Commands.CONTROL_SERVICE); controlCommands.sink(cmd -> { try { getControlService().controlRequest(cmd.getAsJsonObject(IotDevice.CMD_PAYLOAD)); } catch (Exception re) { // If the command fails then don't stop this application, // just process the next command. LoggerFactory.getLogger(ControlService.class).error("Control request failed: {}", cmd, re); } }); }); systemApps.add(CONTROL_APP_NAME); }
public DirectProvider() { this.services = new ServiceContainer(); getServices().addService(ControlService.class, new JsonControlService()); }
/** * {@inheritDoc} * <P> * All control service MBeans must be valid according * to {@link Controls#isControlServiceMBean(Class)}. * </P> * * @see Controls#isControlServiceMBean(Class) */ @Override public synchronized <T> String registerControl(String type, String id, String alias, Class<T> controlInterface, T control) { if (!Controls.isControlServiceMBean(controlInterface)) throw new IllegalArgumentException(); final String controlId = getControlId(type, id, alias); if (mbeans.containsKey(controlId)) { logger.error("Control id: {} already exists", controlId); throw new IllegalStateException(); } logger.trace("Register control id: {}", controlId); mbeans.put(controlId, new ControlMBean<T>(controlInterface, control)); return controlId; }
@Test public void testNoArg() throws Exception { JsonControlService control = new JsonControlService(); MyBeanImpl cb1 = new MyBeanImpl(); MyBeanImpl cb2 = new MyBeanImpl(); assertFalse(cb1.isDoneIt()); assertFalse(cb2.isDoneIt()); control.registerControl("myb", "1", null, MyBean.class, cb1); control.registerControl("myb", "2", null, MyBean.class, cb2); assertFalse(cb1.isDoneIt()); assertFalse(cb2.isDoneIt()); JsonObject req = new JsonObject(); req.addProperty(JsonControlService.TYPE_KEY, "myb"); req.addProperty(JsonControlService.ALIAS_KEY, "1"); req.addProperty(JsonControlService.OP_KEY, "doIt"); control.controlRequest(req); assertTrue(cb1.isDoneIt()); assertFalse(cb2.isDoneIt()); req = new JsonObject(); req.addProperty(JsonControlService.TYPE_KEY, "myb"); req.addProperty(JsonControlService.ALIAS_KEY, "2"); req.addProperty(JsonControlService.OP_KEY, "doIt"); control.controlRequest(req); assertTrue(cb1.isDoneIt()); assertTrue(cb2.isDoneIt()); }
assertEquals(Job.State.RUNNING, jobMbean.getCurrentState()); jsc.controlRequest(closeJob);
@Override public <T> String getControlId(String type, String alias, Class<T> controlInterface) { String controlId = getControlId(type, null, alias); return getControlMBean(controlId, controlInterface) != null ? controlId : null; }
/** * Submit the specified application previously registered * via {@link #registerTopology(String, BiConsumer) registerTopology}. * @param appName name of registered application * @param config See {@link #submit(Topology, JsonObject) submit}. May be null. * * @throws Exception on failure starting applications. */ private void submitApplication(String appName, JsonObject config) throws Exception { if (systemApps.contains(appName)) { throw new IllegalArgumentException("appName"); } ApplicationServiceMXBean bean = getControlService().getControl(ApplicationServiceMXBean.TYPE, ApplicationService.ALIAS, ApplicationServiceMXBean.class); bean.submit(appName, config==null ? null : config.toString()); } }
/** * Handle a JSON control request. * * The control action is executed directly * using the calling thread. * * @param request the request * @return JSON response, JSON null if the request was not recognized. * @throws Exception on failure */ public JsonElement controlRequest(JsonObject request) throws Exception { if (request.has(OP_KEY)) return controlOperation(request); return JsonNull.INSTANCE; }