private void registerOperationAccessConstraints(OperationDefinition od) {
   if (constraintUtilizationRegistry != null) {
     for (AccessConstraintDefinition acd : od.getAccessConstraints()) {
       constraintUtilizationRegistry.registerAccessConstraintOperationUtilization(
           acd.getKey(), getPathAddress(), od.getName());
     }
   }
 }
 @Override
 public void registerOperationHandler(
     OperationDefinition definition, OperationStepHandler handler, boolean inherited) {
   checkPermission();
   if (operationsUpdater.putIfAbsent(
           this,
           definition.getName(),
           new OperationEntry(
               handler,
               definition.getDescriptionProvider(),
               inherited,
               definition.getEntryType(),
               definition.getFlags(),
               definition.getAccessConstraints()))
       != null) {
     throw alreadyRegistered("operation handler", definition.getName());
   }
   registerOperationAccessConstraints(definition);
 }
/** @author Tomaz Cerar (c) 2014 Red Hat Inc. */
public class MapOperations {
  public static final OperationDefinition MAP_CLEAR_DEFINITION =
      new SimpleOperationDefinitionBuilder("map-clear", ControllerResolver.getResolver("global"))
          .setParameters(AbstractMapHandler.NAME)
          .setRuntimeOnly()
          .build();
  public static final OperationDefinition MAP_REMOVE_DEFINITION =
      new SimpleOperationDefinitionBuilder("map-remove", ControllerResolver.getResolver("global"))
          .setParameters(AbstractMapHandler.NAME, AbstractMapHandler.KEY)
          .setRuntimeOnly()
          .build();
  public static final OperationDefinition MAP_GET_DEFINITION =
      new SimpleOperationDefinitionBuilder("map-get", ControllerResolver.getResolver("global"))
          .setParameters(AbstractMapHandler.NAME, AbstractMapHandler.KEY)
          .setRuntimeOnly()
          .setReadOnly()
          .build();
  public static final OperationDefinition MAP_PUT_DEFINITION =
      new SimpleOperationDefinitionBuilder("map-put", ControllerResolver.getResolver("global"))
          .setParameters(AbstractMapHandler.NAME, AbstractMapHandler.KEY, AbstractMapHandler.VALUE)
          .setRuntimeOnly()
          .build();

  public static final OperationStepHandler MAP_CLEAR_HANDLER = new MapClearHandler();
  public static final OperationStepHandler MAP_GET_HANDLER = new MapGetHandler();
  public static final OperationStepHandler MAP_REMOVE_HANDLER = new MapRemoveHandler();
  public static final OperationStepHandler MAP_PUT_HANDLER = new MapPutHandler();

  public static final Set<String> MAP_OPERATION_NAMES =
      new HashSet<>(
          Arrays.asList(
              MAP_CLEAR_DEFINITION.getName(),
              MAP_REMOVE_DEFINITION.getName(),
              MAP_PUT_DEFINITION.getName(),
              MAP_GET_DEFINITION.getName()));

  /** @author Tomaz Cerar (c) 2014 Red Hat Inc. */
  abstract static class AbstractMapHandler extends AbstractCollectionHandler {
    static final SimpleAttributeDefinition KEY =
        new SimpleAttributeDefinition("key", ModelType.STRING, false);

    AbstractMapHandler(AttributeDefinition... attributes) {
      super(attributes);
    }

    AbstractMapHandler(boolean requiredReadWrite, AttributeDefinition... attributes) {
      super(requiredReadWrite, attributes);
    }

    abstract void updateModel(
        final OperationContext context,
        ModelNode model,
        AttributeDefinition attributeDefinition,
        ModelNode attribute)
        throws OperationFailedException;
  }

  /**
   * Empty map, note that is not the same as :undefine(name=name-of-attribute)
   *
   * <p>
   *
   * <pre>map-clear(name=name-of-attribute)</pre>
   *
   * @author Tomaz Cerar (c) 2014 Red Hat Inc.
   */
  public static class MapClearHandler extends AbstractMapHandler {
    private MapClearHandler() {
      super(true);
    }

    void updateModel(
        final OperationContext context,
        ModelNode model,
        AttributeDefinition attributeDefinition,
        ModelNode attribute)
        throws OperationFailedException {
      attribute.setEmptyObject();
    }
  }

  /**
   * Get entry from map:
   *
   * <p>
   *
   * <pre>:map-get(name=name-of-attribute, key=some-key)</pre>
   *
   * @author Tomaz Cerar (c) 2014 Red Hat Inc.
   */
  public static class MapGetHandler extends AbstractMapHandler {
    private MapGetHandler() {
      super(false, KEY);
    }

    void updateModel(
        final OperationContext context,
        ModelNode model,
        AttributeDefinition attributeDefinition,
        ModelNode attribute)
        throws OperationFailedException {
      String key = KEY.resolveModelAttribute(context, model).asString();
      context.getResult().set(attribute.get(key));
    }
  }

  /**
   * put entry to map
   *
   * <p>
   *
   * <pre>:map-put(name=name-of-attribute, key=some-key, value="newvalue")</pre>
   *
   * @author Tomaz Cerar (c) 2014 Red Hat Inc.
   */
  public static class MapPutHandler extends AbstractMapHandler {
    private MapPutHandler() {
      super(KEY, VALUE);
    }

    void updateModel(
        final OperationContext context,
        ModelNode model,
        AttributeDefinition attributeDefinition,
        ModelNode attribute)
        throws OperationFailedException {
      String key = KEY.resolveModelAttribute(context, model).asString();
      ModelNode value = VALUE.resolveModelAttribute(context, model);
      attribute.get(key).set(value);
    }
  }

  /**
   * Remove entry from map
   *
   * <p>
   *
   * <pre>:map-remove(name=name-of-attribute, key=some-key)</pre>
   *
   * @author Tomaz Cerar (c) 2014 Red Hat Inc.
   */
  public static class MapRemoveHandler extends AbstractMapHandler {
    private MapRemoveHandler() {
      super(KEY);
    }

    void updateModel(
        final OperationContext context,
        ModelNode model,
        AttributeDefinition attributeDefinition,
        ModelNode attribute)
        throws OperationFailedException {
      String key = KEY.resolveModelAttribute(context, model).asString();
      attribute.remove(key);
    }
  }
}
Exemple #4
0
 public static ModelNode createOperation(
     final OperationDefinition operationDefinition, final PathAddress address) {
   return getEmptyOperation(operationDefinition.getName(), address.toModelNode());
 }
/** @author Tomaz Cerar (c) 2014 Red Hat Inc. */
public class ListOperations {

  public static final OperationDefinition LIST_ADD_DEFINITION =
      new SimpleOperationDefinitionBuilder("list-add", ControllerResolver.getResolver("global"))
          .setParameters(
              AbstractCollectionHandler.NAME,
              AbstractCollectionHandler.VALUE,
              AbstractListHandler.INDEX)
          .setRuntimeOnly()
          .build();
  public static final OperationDefinition LIST_GET_DEFINITION =
      new SimpleOperationDefinitionBuilder("list-get", ControllerResolver.getResolver("global"))
          .setParameters(AbstractCollectionHandler.NAME, AbstractListHandler.INDEX)
          .setRuntimeOnly()
          .setReadOnly()
          .build();
  public static final OperationDefinition LIST_REMOVE_DEFINITION =
      new SimpleOperationDefinitionBuilder("list-remove", ControllerResolver.getResolver("global"))
          .setParameters(
              AbstractCollectionHandler.NAME,
              AbstractCollectionHandler.VALUE,
              AbstractListHandler.INDEX)
          .setRuntimeOnly()
          .build();
  public static final OperationDefinition LIST_CLEAR_DEFINITION =
      new SimpleOperationDefinitionBuilder("list-clear", ControllerResolver.getResolver("global"))
          .setParameters(AbstractCollectionHandler.NAME)
          .setRuntimeOnly()
          .build();
  public static final OperationStepHandler LIST_ADD_HANDLER = new ListAddHandler();
  public static final OperationStepHandler LIST_REMOVE_HANDLER = new ListRemoveHandler();
  public static final OperationStepHandler LIST_GET_HANDLER = new ListGetHandler();
  public static final OperationStepHandler LIST_CLEAR_HANDLER = new ListClearHandler();

  public static final Set<String> LIST_OPERATION_NAMES =
      new HashSet<>(
          Arrays.asList(
              LIST_CLEAR_DEFINITION.getName(),
              LIST_REMOVE_DEFINITION.getName(),
              LIST_ADD_DEFINITION.getName(),
              LIST_GET_DEFINITION.getName()));

  /** @author Tomaz Cerar (c) 2014 Red Hat Inc. */
  abstract static class AbstractListHandler extends AbstractCollectionHandler {
    static final SimpleAttributeDefinition INDEX =
        new SimpleAttributeDefinition("index", ModelType.INT, true);

    AbstractListHandler(AttributeDefinition... attributes) {
      super(attributes);
    }

    AbstractListHandler(boolean requiredReadWrite, AttributeDefinition... attributes) {
      super(requiredReadWrite, attributes);
    }

    @Override
    public void updateModel(
        OperationContext context,
        ModelNode model,
        AttributeDefinition attributeDefinition,
        ModelNode attribute)
        throws OperationFailedException {
      if (attribute.getType() != ModelType.LIST
          && attributeDefinition.getType() != ModelType.LIST) {
        throw ControllerLogger.MGMT_OP_LOGGER.attributeIsWrongType(
            attributeDefinition.getName(), ModelType.LIST, attributeDefinition.getType());
      }
      updateModel(context, model, attribute);
    }

    abstract void updateModel(
        final OperationContext context, ModelNode model, ModelNode listAttribute)
        throws OperationFailedException;
  }

  /**
   * Add element to list, with optional index where to put it
   *
   * <p>
   *
   * <pre>:list-add(name=list-attribute, value="some value", [index=5])</pre>
   *
   * @author Tomaz Cerar (c) 2014 Red Hat Inc.
   */
  public static class ListAddHandler extends AbstractListHandler {
    private ListAddHandler() {
      super(VALUE, INDEX);
    }

    void updateModel(final OperationContext context, ModelNode model, ModelNode listAttribute)
        throws OperationFailedException {
      String value = VALUE.resolveModelAttribute(context, model).asString();
      ModelNode indexNode = INDEX.resolveModelAttribute(context, model);

      LinkedList<ModelNode> res =
          new LinkedList<>(
              listAttribute.isDefined()
                  ? listAttribute.asList()
                  : Collections.<ModelNode>emptyList());
      if (indexNode.isDefined()) {
        res.add(indexNode.asInt(), new ModelNode(value));
      } else {
        res.add(new ModelNode(value));
      }
      listAttribute.set(res);
    }
  }

  /**
   * Add element to list, with optional index where to put it
   *
   * <p>
   *
   * <pre>:list-remove(name=list-attribute, value="some value")</pre>
   *
   * @author Tomaz Cerar (c) 2014 Red Hat Inc.
   */
  public static class ListRemoveHandler extends AbstractListHandler {
    private ListRemoveHandler() {
      super(VALUE, INDEX);
    }

    void updateModel(final OperationContext context, ModelNode model, ModelNode listAttribute)
        throws OperationFailedException {
      ModelNode value = VALUE.resolveModelAttribute(context, model);
      ModelNode index = INDEX.resolveModelAttribute(context, model);
      List<ModelNode> res = new ArrayList<>(listAttribute.asList());
      if (index.isDefined()) {
        res.remove(index.asInt());
      } else {
        res.remove(value);
      }
      listAttribute.set(res);
    }
  }

  /**
   * Add element to list, with optional index where to put it
   *
   * <p>
   *
   * <pre>:list-remove(name=list-attribute, value="some value")</pre>
   *
   * @author Tomaz Cerar (c) 2014 Red Hat Inc.
   */
  public static class ListGetHandler extends AbstractListHandler {
    private ListGetHandler() {
      super(false, INDEX);
    }

    void updateModel(final OperationContext context, ModelNode model, ModelNode listAttribute)
        throws OperationFailedException {
      int index = INDEX.resolveModelAttribute(context, model).asInt();
      if (listAttribute.hasDefined(index)) {
        context.getResult().set(listAttribute.get(index));
      }
    }
  }

  /**
   * Add element to list, with optional index where to put it
   *
   * <p>
   *
   * <pre>:list-remove(name=list-attribute, value="some value")</pre>
   *
   * @author Tomaz Cerar (c) 2014 Red Hat Inc.
   */
  public static class ListClearHandler extends AbstractListHandler {
    private ListClearHandler() {
      super();
    }

    void updateModel(final OperationContext context, ModelNode model, ModelNode listAttribute)
        throws OperationFailedException {
      listAttribute.setEmptyList();
    }
  }
}