ActionDefinitionManager { public void readCustomActionDefinitions(File customActionDefinitionRoot) throws JAXBException, AmbariException { if (customActionDefinitionRoot == null || !customActionDefinitionRoot.exists() || !customActionDefinitionRoot.canRead()) { LOG.warn("Cannot read custom action definitions. " + customActionDefinitionRoot == null ? "" : "Check path " + customActionDefinitionRoot.getAbsolutePath()); } File[] customActionDefinitionFiles = customActionDefinitionRoot.listFiles(AmbariMetaInfo.FILENAME_FILTER); if (customActionDefinitionFiles != null) { for (File definitionFile : customActionDefinitionFiles) { ActionDefinitionXml adx = null; try { adx = unmarshal(ActionDefinitionXml.class, definitionFile); } catch (UnmarshalException uex) { LOG.warn("Encountered badly formed action definition file - " + definitionFile.getAbsolutePath()); continue; } for (ActionDefinitionSpec ad : adx.actionDefinitions()) { LOG.debug("Read action definition = " + ad.toString()); StringBuilder errorReason = new StringBuilder("Error while parsing action definition. ").append(ad.toString()).append(" --- "); TargetHostType targetType = safeValueOf(TargetHostType.class, ad.getTargetType(), errorReason); ActionType actionType = safeValueOf(ActionType.class, ad.getActionType(), errorReason); Short defaultTimeout = MIN_TIMEOUT; if (ad.getDefaultTimeout() != null && !ad.getDefaultTimeout().isEmpty()) { defaultTimeout = Short.parseShort(ad.getDefaultTimeout()); } if (isValidActionDefinition(ad, actionType, defaultTimeout, errorReason)) { String actionName = ad.getActionName(); if (actionDefinitionMap.containsKey(actionName)) { LOG.warn("Ignoring action definition as a different definition by that name already exists. " + ad.toString()); continue; } actionDefinitionMap.put(ad.getActionName(), new ActionDefinition(ad.getActionName(), actionType, ad.getInputs(), ad.getTargetService(), ad.getTargetComponent(), ad.getDescription(), targetType, defaultTimeout, translatePermissions(ad.getPermissions()))); LOG.info("Added custom action definition for " + ad.getActionName()); } else { LOG.warn(errorReason.toString()); } } } } } ActionDefinitionManager(); static T unmarshal(Class<T> clz, File file); void readCustomActionDefinitions(File customActionDefinitionRoot); List<ActionDefinition> getAllActionDefinition(); ActionDefinition getActionDefinition(String name); void addActionDefinition(ActionDefinition ad); static final Short MIN_TIMEOUT; }
@Test public void testReadCustomActionDefinitions() throws Exception { ActionDefinitionManager manager = new ActionDefinitionManager(); manager.readCustomActionDefinitions(new File(CUSTOM_ACTION_DEFINITION_ROOT)); Assert.assertEquals(3, manager.getAllActionDefinition().size()); ActionDefinition ad = manager.getActionDefinition("customAction1"); Assert.assertNotNull(ad); Assert.assertEquals("customAction1", ad.getActionName()); Assert.assertEquals("A random test", ad.getDescription()); Assert.assertEquals("threshold", ad.getInputs()); Assert.assertEquals("TASKTRACKER", ad.getTargetComponent()); Assert.assertEquals("MAPREDUCE", ad.getTargetService()); Assert.assertEquals(60, (int)ad.getDefaultTimeout()); Assert.assertEquals(TargetHostType.ALL, ad.getTargetType()); Assert.assertEquals(ActionType.USER, ad.getActionType()); Assert.assertEquals(EnumSet.of(RoleAuthorization.HOST_ADD_DELETE_COMPONENTS, RoleAuthorization.HOST_ADD_DELETE_HOSTS), ad.getPermissions()); ad = manager.getActionDefinition("customAction2"); Assert.assertNotNull(ad); Assert.assertEquals("customAction2", ad.getActionName()); Assert.assertEquals("A random test", ad.getDescription()); Assert.assertEquals(null, ad.getInputs()); Assert.assertEquals("TASKTRACKER", ad.getTargetComponent()); Assert.assertEquals("MAPREDUCE", ad.getTargetService()); Assert.assertEquals(60, (int)ad.getDefaultTimeout()); Assert.assertEquals(null, ad.getTargetType()); Assert.assertEquals(ActionType.USER, ad.getActionType()); Assert.assertEquals(EnumSet.of(RoleAuthorization.HOST_ADD_DELETE_COMPONENTS, RoleAuthorization.HOST_ADD_DELETE_HOSTS), ad.getPermissions()); ad = manager.getActionDefinition("customAction3"); Assert.assertNotNull(ad); Assert.assertEquals("customAction3", ad.getActionName()); Assert.assertEquals("A random test", ad.getDescription()); Assert.assertEquals(null, ad.getInputs()); Assert.assertEquals("TASKTRACKER", ad.getTargetComponent()); Assert.assertEquals("MAPREDUCE", ad.getTargetService()); Assert.assertEquals(60, (int)ad.getDefaultTimeout()); Assert.assertEquals(null, ad.getTargetType()); Assert.assertEquals(ActionType.USER, ad.getActionType()); Assert.assertNull(ad.getPermissions()); }
@Test(expected = IllegalArgumentException.class) public void testReadInvalidCustomActionDefinitions() throws Exception { ActionDefinitionManager manager = new ActionDefinitionManager(); manager.readCustomActionDefinitions(new File(CUSTOM_ACTION_DEFINITION_INVALID_ROOT)); }