package org.ovirt.engine.core.bll.network.cluster;

import org.ovirt.engine.core.bll.MultiLevelAdministrationHandler;
import org.ovirt.engine.core.bll.PredefinedRoles;
import org.ovirt.engine.core.common.VdcObjectType;
import org.ovirt.engine.core.common.businessentities.permissions;
import org.ovirt.engine.core.common.businessentities.network.Network;
import org.ovirt.engine.core.common.businessentities.network.VnicProfile;
import org.ovirt.engine.core.compat.Guid;

/**
 * Class to hold common static methods that are used in several different places.
 */
public class NetworkHelper {

    /**
     * Grants permissions on the network entity to the given user
     *
     * @param userId
     *            the ID of the user to get the permission
     * @param networkId
     *            the Network ID
     */
    public static void addPermissionsOnNetwork(Guid userId, Guid networkId) {
        addPermission(userId, networkId, PredefinedRoles.NETWORK_ADMIN);
    }

    /**
     * Grants permissions on the vnic profile entity to its creator and usage permission to 'everyone' if publicUse is
     * set to <code>true</code>
     *
     * @param userId
     *            the ID of the user to get the permission
     * @param vnicProfileId
     *            the VNIC Profile
     * @param publicUse
     *            Indicates of the network is intended for a public user
     */
    public static void addPermissionsOnVnicProfile(Guid userId, Guid vnicProfileId, boolean publicUse) {
        addPermission(userId, vnicProfileId, PredefinedRoles.NETWORK_ADMIN);

        // if the profile is for public use, set EVERYONE as a VNICProfileUser on the profile
        if (publicUse) {
            addPermission(MultiLevelAdministrationHandler.EVERYONE_OBJECT_ID,
                    vnicProfileId,
                    PredefinedRoles.VNIC_PROFILE_USER);
        }
    }

    public static VnicProfile createVnicProfile(Network net) {
        VnicProfile profile = new VnicProfile();
        profile.setId(Guid.newGuid());
        profile.setName(net.getName());
        profile.setNetworkId(net.getId());
        profile.setPortMirroring(false);
        return profile;
    }

    private static void addPermission(Guid userId, Guid entityId, PredefinedRoles role) {
        permissions perms = new permissions();
        perms.setad_element_id(userId);
        perms.setObjectType(VdcObjectType.Network);
        perms.setObjectId(entityId);
        perms.setrole_id(role.getId());
        MultiLevelAdministrationHandler.addPermission(perms);
    }
}

@@ -45,16 +45,40 @@
 uster;%0A%0A
+import java.util.List;%0A%0A
 import o
@@ -487,16 +487,68 @@
 at.Guid;
+%0Aimport org.ovirt.engine.core.dal.dbbroker.DbFacade;
 %0A%0A/**%0A *
@@ -2626,11 +2626,849 @@
 );%0A    %7D
+%0A%0A    public static Network getNetworkByVnicProfileId(Guid vnicProfileId) %7B%0A        if (vnicProfileId == null) %7B%0A            return null;%0A        %7D%0A%0A        Network retVal = null;%0A        VnicProfile vnicProfile = DbFacade.getInstance().getVnicProfileDao().get(vnicProfileId);%0A        if (vnicProfile.getNetworkId() != null) %7B%0A            retVal = DbFacade.getInstance().getNetworkDao().get(vnicProfile.getNetworkId());%0A        %7D%0A        return retVal;%0A    %7D%0A%0A    public static boolean isNetworkInCluster(Network network, Guid clusterId) %7B%0A        List%3CNetwork%3E networks = DbFacade.getInstance().getNetworkDao().getAllForCluster(clusterId);%0A        for (Network clusterNetwork : networks) %7B%0A            if (clusterNetwork.getId().equals(network.getId())) %7B%0A                return true;%0A            %7D%0A        %7D%0A%0A        return false;%0A    %7D
 %0A%7D%0A

Add method for checking existence of network in cluster