Search in sources :

Example 6 with GridRestTopologyRequest

use of org.apache.ignite.internal.processors.rest.request.GridRestTopologyRequest in project ignite by apache.

the class GridTcpRestNioListener method createRestRequest.

/**
 * Creates a REST request object from client TCP binary packet.
 *
 * @param ses NIO session.
 * @param msg Request message.
 * @return REST request object.
 */
@Nullable
private GridRestRequest createRestRequest(GridNioSession ses, GridClientMessage msg) {
    GridRestRequest restReq = null;
    if (msg instanceof GridClientAuthenticationRequest) {
        GridClientAuthenticationRequest req = (GridClientAuthenticationRequest) msg;
        restReq = new GridRestAuthenticationRequest();
        restReq.command(NOOP);
        ses.addMeta(CREDS_KEY, req.credentials());
        ses.addMeta(USER_ATTR_KEY, req.userAttributes());
    } else if (msg instanceof GridClientCacheRequest) {
        GridClientCacheRequest req = (GridClientCacheRequest) msg;
        GridRestCacheRequest restCacheReq = new GridRestCacheRequest();
        restCacheReq.cacheName(req.cacheName());
        restCacheReq.cacheFlags(req.cacheFlagsOn());
        restCacheReq.key(req.key());
        restCacheReq.value(req.value());
        restCacheReq.value2(req.value2());
        Map vals = req.values();
        if (vals != null)
            restCacheReq.values(new HashMap<Object, Object>(vals));
        restCacheReq.command(cacheCmdMap.get(req.operation()));
        restReq = restCacheReq;
    } else if (msg instanceof GridClientTaskRequest) {
        GridClientTaskRequest req = (GridClientTaskRequest) msg;
        GridRestTaskRequest restTaskReq = new GridRestTaskRequest();
        restTaskReq.command(EXE);
        restTaskReq.taskName(req.taskName());
        restTaskReq.params(Arrays.asList(req.argument()));
        restReq = restTaskReq;
    } else if (msg instanceof GridClientTopologyRequest) {
        GridClientTopologyRequest req = (GridClientTopologyRequest) msg;
        GridRestTopologyRequest restTopReq = new GridRestTopologyRequest();
        restTopReq.includeMetrics(req.includeMetrics());
        restTopReq.includeAttributes(req.includeAttributes());
        if (req.nodeId() != null) {
            restTopReq.command(NODE);
            restTopReq.nodeId(req.nodeId());
        } else if (req.nodeIp() != null) {
            restTopReq.command(NODE);
            restTopReq.nodeIp(req.nodeIp());
        } else
            restTopReq.command(TOPOLOGY);
        restReq = restTopReq;
    } else if (msg instanceof GridClientStateRequest) {
        GridClientStateRequest req = (GridClientStateRequest) msg;
        GridRestChangeStateRequest restChangeReq = new GridRestChangeStateRequest();
        if (req.isReqCurrentState()) {
            restChangeReq.reqCurrentState();
            restChangeReq.command(CLUSTER_CURRENT_STATE);
        } else {
            restChangeReq.active(req.active());
            restChangeReq.command(req.active() ? CLUSTER_ACTIVATE : CLUSTER_DEACTIVATE);
        }
        restReq = restChangeReq;
    } else if (msg instanceof GridClientClusterStateRequest) {
        GridClientClusterStateRequest req = (GridClientClusterStateRequest) msg;
        boolean forceDeactivation = !(msg instanceof GridClientClusterStateRequestV2) || ((GridClientClusterStateRequestV2) msg).forceDeactivation();
        GridRestClusterStateRequest restChangeReq = new GridRestClusterStateRequest();
        if (req.isReqCurrentState()) {
            restChangeReq.reqCurrentMode();
            restChangeReq.command(CLUSTER_STATE);
        } else {
            restChangeReq.state(req.state());
            restChangeReq.command(CLUSTER_SET_STATE);
            restChangeReq.forceDeactivation(forceDeactivation);
        }
        restReq = restChangeReq;
    } else if (msg instanceof GridClientClusterNameRequest)
        restReq = new GridRestClusterNameRequest();
    else if (msg instanceof GridClientNodeStateBeforeStartRequest) {
        GridClientNodeStateBeforeStartRequest reqClient = (GridClientNodeStateBeforeStartRequest) msg;
        if (reqClient instanceof GridClientWarmUpRequest) {
            GridClientWarmUpRequest warmUpReqClient = (GridClientWarmUpRequest) reqClient;
            restReq = new GridRestWarmUpRequest().stopWarmUp(warmUpReqClient.stopWarmUp());
            restReq.command(WARM_UP);
        } else {
            restReq = new GridRestNodeStateBeforeStartRequest();
            restReq.command(NODE_STATE_BEFORE_START);
        }
    }
    if (restReq != null) {
        restReq.destinationId(msg.destinationId());
        restReq.clientId(msg.clientId());
        restReq.sessionToken(msg.sessionToken());
        restReq.address(ses.remoteAddress());
        restReq.certificates(ses.certificates());
        restReq.credentials(ses.meta(CREDS_KEY));
        restReq.userAttributes(ses.meta(USER_ATTR_KEY));
    }
    return restReq;
}
Also used : GridRestClusterStateRequest(org.apache.ignite.internal.processors.rest.request.GridRestClusterStateRequest) GridClientAuthenticationRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientAuthenticationRequest) GridRestClusterNameRequest(org.apache.ignite.internal.processors.rest.request.GridRestClusterNameRequest) GridClientWarmUpRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientWarmUpRequest) GridRestTopologyRequest(org.apache.ignite.internal.processors.rest.request.GridRestTopologyRequest) GridClientClusterNameRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientClusterNameRequest) GridClientNodeStateBeforeStartRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientNodeStateBeforeStartRequest) GridRestNodeStateBeforeStartRequest(org.apache.ignite.internal.processors.rest.request.GridRestNodeStateBeforeStartRequest) GridRestWarmUpRequest(org.apache.ignite.internal.processors.rest.request.GridRestWarmUpRequest) GridClientClusterStateRequestV2(org.apache.ignite.internal.processors.rest.client.message.GridClientClusterStateRequestV2) GridClientClusterStateRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientClusterStateRequest) GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) GridRestChangeStateRequest(org.apache.ignite.internal.processors.rest.request.GridRestChangeStateRequest) GridRestTaskRequest(org.apache.ignite.internal.processors.rest.request.GridRestTaskRequest) GridClientStateRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientStateRequest) GridRestAuthenticationRequest(org.apache.ignite.internal.processors.rest.request.GridRestAuthenticationRequest) GridClientTopologyRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientTopologyRequest) GridRestRequest(org.apache.ignite.internal.processors.rest.request.GridRestRequest) GridClientCacheRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientCacheRequest) Map(java.util.Map) EnumMap(java.util.EnumMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) GridClientTaskRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientTaskRequest) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with GridRestTopologyRequest

use of org.apache.ignite.internal.processors.rest.request.GridRestTopologyRequest in project ignite by apache.

the class GridTopologyCommandHandler method handleAsync.

/**
 * {@inheritDoc}
 */
@Override
public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest req) {
    assert req instanceof GridRestTopologyRequest : "Invalid command for topology handler: " + req;
    assert SUPPORTED_COMMANDS.contains(req.command());
    if (log.isDebugEnabled())
        log.debug("Handling topology REST request: " + req);
    GridRestTopologyRequest req0 = (GridRestTopologyRequest) req;
    GridRestResponse res = new GridRestResponse();
    boolean mtr = req0.includeMetrics();
    boolean attr = req0.includeAttributes();
    boolean caches = req0.includeCaches();
    switch(req.command()) {
        case TOPOLOGY:
            {
                Collection<ClusterNode> allNodes = F.concat(false, ctx.discovery().allNodes(), ctx.discovery().daemonNodes());
                Collection<GridClientNodeBean> top = new ArrayList<>(allNodes.size());
                for (ClusterNode node : allNodes) top.add(createNodeBean(node, mtr, attr, caches));
                res.setResponse(top);
                break;
            }
        case NODE:
            {
                UUID id = req0.nodeId();
                final String ip = req0.nodeIp();
                if (id == null && ip == null)
                    return new GridFinishedFuture<>(new IgniteCheckedException("Failed to handle request (either id or ip should be specified)."));
                ClusterNode node;
                if (id != null) {
                    // Always refresh topology so client see most up-to-date view.
                    ctx.discovery().alive(id);
                    node = ctx.grid().cluster().node(id);
                    if (ip != null && node != null && !containsIp(node.addresses(), ip))
                        node = null;
                } else
                    node = F.find(ctx.discovery().allNodes(), null, new P1<ClusterNode>() {

                        @Override
                        public boolean apply(ClusterNode n) {
                            return containsIp(n.addresses(), ip);
                        }
                    });
                if (node != null)
                    res.setResponse(createNodeBean(node, mtr, attr, caches));
                else
                    res.setResponse(null);
                break;
            }
        default:
            assert false : "Invalid command for topology handler: " + req;
    }
    if (log.isDebugEnabled())
        log.debug("Handled topology REST request [res=" + res + ", req=" + req + ']');
    return new GridFinishedFuture<>(res);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) P1(org.apache.ignite.internal.util.typedef.P1) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRestTopologyRequest(org.apache.ignite.internal.processors.rest.request.GridRestTopologyRequest) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) Collection(java.util.Collection) UUID(java.util.UUID) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture)

Aggregations

GridRestTopologyRequest (org.apache.ignite.internal.processors.rest.request.GridRestTopologyRequest)7 Map (java.util.Map)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 GridRestCacheRequest (org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest)3 GridRestChangeStateRequest (org.apache.ignite.internal.processors.rest.request.GridRestChangeStateRequest)3 GridRestRequest (org.apache.ignite.internal.processors.rest.request.GridRestRequest)3 GridRestTaskRequest (org.apache.ignite.internal.processors.rest.request.GridRestTaskRequest)3 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)3 Nullable (org.jetbrains.annotations.Nullable)3 Test (org.junit.Test)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2 Iterator (java.util.Iterator)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Ignite (org.apache.ignite.Ignite)2 CacheConfigurationOverride (org.apache.ignite.internal.processors.cache.CacheConfigurationOverride)2 DataStructuresRequest (org.apache.ignite.internal.processors.rest.request.DataStructuresRequest)2 GridRestClusterNameRequest (org.apache.ignite.internal.processors.rest.request.GridRestClusterNameRequest)2