Search in sources :

Example 1 with PolicyMap

use of com.cloud.utils.cisco.n1kv.vsm.PolicyMap in project CloudStack-archive by CloudStack-extras.

the class HypervisorHostHelper method updatePortProfile.

public static void updatePortProfile(VmwareContext context, String ethPortProfileName, String vethPortProfileName, Integer vlanId, Integer networkRateMbps, long peakBandwidth, long burstRate) throws Exception {
    NetconfHelper netconfClient = null;
    Map<String, String> vsmCredentials = getValidatedVsmCredentials(context);
    String vsmIp = vsmCredentials.get("vsmip");
    String vsmUserName = vsmCredentials.get("vsmusername");
    String vsmPassword = vsmCredentials.get("vsmpassword");
    String msg;
    try {
        netconfClient = new NetconfHelper(vsmIp, vsmUserName, vsmPassword);
    } catch (CloudRuntimeException e) {
        msg = "Failed to connect to Nexus 1000v " + vsmIp + " with credentials of user " + vsmUserName + ". Exception: " + e.toString();
        s_logger.error(msg);
        throw new CloudRuntimeException(msg);
    }
    PortProfile portProfile = netconfClient.getPortProfileByName(vethPortProfileName);
    int averageBandwidth = 0;
    String policyName = s_policyNamePrefix;
    if (networkRateMbps != null) {
        averageBandwidth = networkRateMbps.intValue();
        policyName += averageBandwidth;
    }
    if (averageBandwidth > 0) {
        PolicyMap policyMap = netconfClient.getPolicyMapByName(portProfile.inputPolicyMap);
        if (policyMap.committedRate == averageBandwidth && policyMap.peakRate == peakBandwidth && policyMap.burstRate == burstRate) {
            s_logger.debug("Detected that policy map is already applied to port profile " + vethPortProfileName);
            if (netconfClient != null) {
                netconfClient.disconnect();
                s_logger.debug("Disconnected Nexus 1000v session.");
            }
            return;
        } else {
            try {
                // TODO(sateesh): Change the type of peakBandwidth &
                // burstRate in PolicyMap to long.
                s_logger.info("Adding policy map " + policyName);
                netconfClient.addPolicyMap(policyName, averageBandwidth, (int) peakBandwidth, (int) burstRate);
            } catch (CloudRuntimeException e) {
                msg = "Failed to add policy map of " + policyName + " with parameters " + "committed rate = " + averageBandwidth + "peak bandwidth = " + peakBandwidth + "burst size = " + burstRate + ". Exception: " + e.toString();
                s_logger.error(msg);
                if (netconfClient != null) {
                    netconfClient.disconnect();
                    s_logger.debug("Disconnected Nexus 1000v session.");
                }
                throw new CloudRuntimeException(msg);
            }
            try {
                s_logger.info("Associating policy map " + policyName + " with port profile " + vethPortProfileName + ".");
                netconfClient.attachServicePolicy(policyName, vethPortProfileName);
            } catch (CloudRuntimeException e) {
                msg = "Failed to associate policy map " + policyName + " with port profile " + vethPortProfileName + ". Exception: " + e.toString();
                s_logger.error(msg);
                if (netconfClient != null) {
                    netconfClient.disconnect();
                    s_logger.debug("Disconnected Nexus 1000v session.");
                }
                throw new CloudRuntimeException(msg);
            }
        }
    }
    if (vlanId == null) {
        s_logger.info("Skipping update operation over ethernet port profile " + ethPortProfileName + " for untagged VLAN.");
        if (netconfClient != null) {
            netconfClient.disconnect();
            s_logger.debug("Disconnected Nexus 1000v session.");
        }
        return;
    }
    String currentVlan = portProfile.vlan;
    String newVlan = Integer.toString(vlanId.intValue());
    if (currentVlan.equalsIgnoreCase(newVlan)) {
        if (netconfClient != null) {
            netconfClient.disconnect();
            s_logger.debug("Disconnected Nexus 1000v session.");
        }
        return;
    }
    List<Pair<OperationType, String>> params = new ArrayList<Pair<OperationType, String>>();
    params.add(new Pair<OperationType, String>(OperationType.addvlanid, newVlan));
    try {
        s_logger.info("Updating vEthernet port profile with VLAN " + vlanId.toString());
        netconfClient.updatePortProfile(ethPortProfileName, SwitchPortMode.trunk, params);
    } catch (CloudRuntimeException e) {
        msg = "Failed to update ethernet port profile " + ethPortProfileName + " with parameters " + params.toString() + ". Exception: " + e.toString();
        s_logger.error(msg);
        if (netconfClient != null) {
            netconfClient.disconnect();
            s_logger.debug("Disconnected Nexus 1000v session.");
        }
        throw new CloudRuntimeException(msg);
    }
    try {
        netconfClient.updatePortProfile(vethPortProfileName, SwitchPortMode.access, params);
    } catch (CloudRuntimeException e) {
        msg = "Failed to update vEthernet port profile " + vethPortProfileName + " with parameters " + params.toString() + ". Exception: " + e.toString();
        s_logger.error(msg);
        if (netconfClient != null) {
            netconfClient.disconnect();
            s_logger.debug("Disconnected Nexus 1000v session.");
        }
        throw new CloudRuntimeException(msg);
    }
}
Also used : PortProfile(com.cloud.utils.cisco.n1kv.vsm.PortProfile) PolicyMap(com.cloud.utils.cisco.n1kv.vsm.PolicyMap) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) OperationType(com.cloud.utils.cisco.n1kv.vsm.VsmCommand.OperationType) NetconfHelper(com.cloud.utils.cisco.n1kv.vsm.NetconfHelper) Pair(com.cloud.utils.Pair)

Example 2 with PolicyMap

use of com.cloud.utils.cisco.n1kv.vsm.PolicyMap in project cloudstack by apache.

the class HypervisorHostHelper method updatePortProfile.

public static void updatePortProfile(VmwareContext context, String ethPortProfileName, String vethPortProfileName, Integer vlanId, Integer networkRateMbps, long peakBandwidth, long burstRate) throws Exception {
    NetconfHelper netconfClient = null;
    Map<String, String> vsmCredentials = getValidatedVsmCredentials(context);
    String vsmIp = vsmCredentials.get("vsmip");
    String vsmUserName = vsmCredentials.get("vsmusername");
    String vsmPassword = vsmCredentials.get("vsmpassword");
    String msg;
    try {
        netconfClient = new NetconfHelper(vsmIp, vsmUserName, vsmPassword);
    } catch (CloudRuntimeException e) {
        msg = "Failed to connect to Nexus 1000v " + vsmIp + " with credentials of user " + vsmUserName + ". Exception: " + e.toString();
        s_logger.error(msg);
        throw new CloudRuntimeException(msg);
    }
    PortProfile portProfile = netconfClient.getPortProfileByName(vethPortProfileName);
    int averageBandwidth = 0;
    String policyName = s_policyNamePrefix;
    if (networkRateMbps != null) {
        averageBandwidth = networkRateMbps.intValue();
        policyName += averageBandwidth;
    }
    if (averageBandwidth > 0) {
        PolicyMap policyMap = netconfClient.getPolicyMapByName(portProfile.inputPolicyMap);
        if (policyMap.committedRate == averageBandwidth && policyMap.peakRate == peakBandwidth && policyMap.burstRate == burstRate) {
            s_logger.debug("Detected that policy map is already applied to port profile " + vethPortProfileName);
            if (netconfClient != null) {
                netconfClient.disconnect();
                s_logger.debug("Disconnected Nexus 1000v session.");
            }
            return;
        } else {
            try {
                // TODO(sateesh): Change the type of peakBandwidth &
                // burstRate in PolicyMap to long.
                s_logger.info("Adding policy map " + policyName);
                netconfClient.addPolicyMap(policyName, averageBandwidth, (int) peakBandwidth, (int) burstRate);
            } catch (CloudRuntimeException e) {
                msg = "Failed to add policy map of " + policyName + " with parameters " + "committed rate = " + averageBandwidth + "peak bandwidth = " + peakBandwidth + "burst size = " + burstRate + ". Exception: " + e.toString();
                s_logger.error(msg);
                if (netconfClient != null) {
                    netconfClient.disconnect();
                    s_logger.debug("Disconnected Nexus 1000v session.");
                }
                throw new CloudRuntimeException(msg);
            }
            try {
                s_logger.info("Associating policy map " + policyName + " with port profile " + vethPortProfileName + ".");
                netconfClient.attachServicePolicy(policyName, vethPortProfileName);
            } catch (CloudRuntimeException e) {
                msg = "Failed to associate policy map " + policyName + " with port profile " + vethPortProfileName + ". Exception: " + e.toString();
                s_logger.error(msg);
                if (netconfClient != null) {
                    netconfClient.disconnect();
                    s_logger.debug("Disconnected Nexus 1000v session.");
                }
                throw new CloudRuntimeException(msg);
            }
        }
    }
    if (vlanId == null) {
        s_logger.info("Skipping update operation over ethernet port profile " + ethPortProfileName + " for untagged VLAN.");
        if (netconfClient != null) {
            netconfClient.disconnect();
            s_logger.debug("Disconnected Nexus 1000v session.");
        }
        return;
    }
    String currentVlan = portProfile.vlan;
    String newVlan = Integer.toString(vlanId.intValue());
    if (currentVlan.equalsIgnoreCase(newVlan)) {
        if (netconfClient != null) {
            netconfClient.disconnect();
            s_logger.debug("Disconnected Nexus 1000v session.");
        }
        return;
    }
    List<Pair<OperationType, String>> params = new ArrayList<Pair<OperationType, String>>();
    params.add(new Pair<OperationType, String>(OperationType.addvlanid, newVlan));
    try {
        s_logger.info("Updating vEthernet port profile with VLAN " + vlanId.toString());
        netconfClient.updatePortProfile(ethPortProfileName, SwitchPortMode.trunk, params);
    } catch (CloudRuntimeException e) {
        msg = "Failed to update ethernet port profile " + ethPortProfileName + " with parameters " + params.toString() + ". Exception: " + e.toString();
        s_logger.error(msg);
        if (netconfClient != null) {
            netconfClient.disconnect();
            s_logger.debug("Disconnected Nexus 1000v session.");
        }
        throw new CloudRuntimeException(msg);
    }
    try {
        netconfClient.updatePortProfile(vethPortProfileName, SwitchPortMode.access, params);
    } catch (CloudRuntimeException e) {
        msg = "Failed to update vEthernet port profile " + vethPortProfileName + " with parameters " + params.toString() + ". Exception: " + e.toString();
        s_logger.error(msg);
        if (netconfClient != null) {
            netconfClient.disconnect();
            s_logger.debug("Disconnected Nexus 1000v session.");
        }
        throw new CloudRuntimeException(msg);
    }
}
Also used : PortProfile(com.cloud.utils.cisco.n1kv.vsm.PortProfile) PolicyMap(com.cloud.utils.cisco.n1kv.vsm.PolicyMap) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) OperationType(com.cloud.utils.cisco.n1kv.vsm.VsmCommand.OperationType) NetconfHelper(com.cloud.utils.cisco.n1kv.vsm.NetconfHelper) Pair(com.cloud.utils.Pair)

Aggregations

Pair (com.cloud.utils.Pair)2 NetconfHelper (com.cloud.utils.cisco.n1kv.vsm.NetconfHelper)2 PolicyMap (com.cloud.utils.cisco.n1kv.vsm.PolicyMap)2 PortProfile (com.cloud.utils.cisco.n1kv.vsm.PortProfile)2 OperationType (com.cloud.utils.cisco.n1kv.vsm.VsmCommand.OperationType)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 ArrayList (java.util.ArrayList)2