org.opendaylight.openflowplugin.openflow.md.util.RoleUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.opendaylight.openflowplugin.openflow.md.util.RoleUtil.java

Source

/**
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.openflowplugin.openflow.md.util;

import com.google.common.base.Function;
import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.JdkFutureAdapters;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.SettableFuture;
import java.math.BigInteger;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
import org.opendaylight.openflowplugin.openflow.md.core.session.OFRoleManager;
import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
import org.opendaylight.openflowplugin.openflow.md.core.session.RolePushException;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ControllerRole;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInputBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.common.config.impl.rev140326.OfpRole;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 */
public final class RoleUtil {

    private static final Logger LOG = LoggerFactory.getLogger(RoleUtil.class);
    private static final Function<Exception, RolePushException> exceptionFunction = new Function<Exception, RolePushException>() {
        @Override
        public RolePushException apply(Exception input) {
            RolePushException output = null;
            if (input instanceof ExecutionException) {
                if (input.getCause() instanceof RolePushException) {
                    output = (RolePushException) input.getCause();
                }
            }

            if (output == null) {
                output = new RolePushException(input.getMessage(), input);
            }

            return output;
        }
    };

    private RoleUtil() {
        throw new UnsupportedOperationException("RoleUtil is not expected to be instantiated.");
    }

    /**
     * @param role openflow role for controller
     * @return protocol role
     */
    public static ControllerRole toOFJavaRole(OfpRole role) {
        ControllerRole ofJavaRole = null;
        switch (role) {
        case BECOMEMASTER:
            ofJavaRole = ControllerRole.OFPCRROLEMASTER;
            break;
        case BECOMESLAVE:
            ofJavaRole = ControllerRole.OFPCRROLESLAVE;
            break;
        case BECOMEEQUAL:
            ofJavaRole = ControllerRole.OFPCRROLEEQUAL;
            break;
        case NOCHANGE:
            ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
            break;
        default:
            // no intention
            LOG.warn("given role is not supported by protocol roles: {}", role);
            break;
        }
        return ofJavaRole;
    }

    /**
     * @param session switch session context
     * @param role  controller openflow role
     * @param generationId generate id for role negotiation
     * @return input builder
     */
    public static RoleRequestInputBuilder createRoleRequestInput(final SessionContext session, OfpRole role,
            BigInteger generationId) {

        ControllerRole ofJavaRole = RoleUtil.toOFJavaRole(role);

        return new RoleRequestInputBuilder().setGenerationId(generationId).setRole(ofJavaRole)
                .setVersion(session.getFeatures().getVersion()).setXid(session.getNextXid());
    }

    /**
     * @param sessionContext switch session context
     * @param ofpRole controller openflow role
     * @param generationId generate id for role negotiation
     * @return roleRequest future result
     */
    public static Future<RpcResult<RoleRequestOutput>> sendRoleChangeRequest(SessionContext sessionContext,
            OfpRole ofpRole, BigInteger generationId) {
        RoleRequestInputBuilder ruleRequestInputBld = RoleUtil.createRoleRequestInput(sessionContext, ofpRole,
                generationId);
        Future<RpcResult<RoleRequestOutput>> roleReply = sessionContext.getPrimaryConductor().getConnectionAdapter()
                .roleRequest(ruleRequestInputBld.build());
        return roleReply;
    }

    /**
     * @param sessionContext switch session context
     * @return generationId from future RpcResult
     */
    public static Future<BigInteger> readGenerationIdFromDevice(SessionContext sessionContext) {
        Future<RpcResult<RoleRequestOutput>> roleReply = sendRoleChangeRequest(sessionContext, OfpRole.NOCHANGE,
                BigInteger.ZERO);
        final SettableFuture<BigInteger> result = SettableFuture.create();

        Futures.addCallback(JdkFutureAdapters.listenInPoolThread(roleReply),
                new FutureCallback<RpcResult<RoleRequestOutput>>() {
                    @Override
                    public void onSuccess(RpcResult<RoleRequestOutput> input) {
                        if (input != null && input.getResult() != null) {
                            result.set(input.getResult().getGenerationId());
                        }
                    }

                    @Override
                    public void onFailure(Throwable t) {
                        //TODO
                    }
                });
        return result;
    }

    /**
     * @param generationId generate id for role negotiation
     * @return next (incremented value)
     */
    public static BigInteger getNextGenerationId(BigInteger generationId) {
        BigInteger nextGenerationId = null;
        if (generationId.compareTo(OFRoleManager.MAX_GENERATION_ID) < 0) {
            nextGenerationId = generationId.add(BigInteger.ONE);
        } else {
            nextGenerationId = BigInteger.ZERO;
        }

        return nextGenerationId;
    }

    /**
     * @param rolePushResult result of role push request
     * @return future which throws {@link RolePushException}
     */
    public static CheckedFuture<Boolean, RolePushException> makeCheckedRuleRequestFxResult(
            ListenableFuture<Boolean> rolePushResult) {
        return Futures.makeChecked(rolePushResult, exceptionFunction);
    }

    public static void fireRoleChange(int role, List<String> datapathIds) {
        LOG.info("Starting fire role change...");
        try (OFRoleManager roleManager = new OFRoleManager(OFSessionUtil.getSessionManager())) {
            roleManager.manageRoleChange(OfpRole.forValue(role), datapathIds);
        } catch (Exception e) {
            LOG.error("Failed to close role manager after sending role change request", e);
        }
    }

    public static Map<String, String> getSwitchesRoles() {
        //
        //TODO check OpenFlow 1.3> protocol version
        //
        //
        Map<String, String> swsRoles = null;
        try (OFRoleManager roleManager = new OFRoleManager(OFSessionUtil.getSessionManager())) {
            swsRoles = roleManager.getSwitchesRoles();
        } catch (Exception e) {
            LOG.error("Failed to close role manager after sending role change request", e);
        }
        if (swsRoles == null) {
            LOG.error("Failed while getting the switches' roles");
        }
        return swsRoles;
    }

    public static ControllerRole readRoleFromDevice(SessionContext sessionContext) {
        Future<ControllerRole> roleFuture = null;
        Future<RpcResult<RoleRequestOutput>> roleReply = sendRoleChangeRequest(sessionContext, OfpRole.NOCHANGE,
                BigInteger.ZERO);
        roleFuture = Futures.transform(JdkFutureAdapters.listenInPoolThread(roleReply),
                new Function<RpcResult<RoleRequestOutput>, ControllerRole>() {
                    @Override
                    public ControllerRole apply(RpcResult<RoleRequestOutput> input) {
                        return input.getResult().getRole();
                    }
                });
        ControllerRole role = null;
        try {
            role = roleFuture.get();
        } catch (ExecutionException | InterruptedException e) {
            LOG.error(e.getMessage());
        }
        return role;
    }

}