Search in sources :

Example 6 with NetworkInfo

use of org.onap.so.openstack.beans.NetworkInfo in project so by onap.

the class MsoNetworkAdapterImpl method updateNetwork.

/**
 * This is the "Update Network" web service implementation. It will update an existing Network of the requested type
 * in the specified cloud and tenant. The typical use will be to replace the VLANs with the supplied list (to add or
 * remove a VLAN), but other properties may be updated as well.
 *
 * There will be a pre-defined set of network types defined in the MSO Catalog. All such networks will have a
 * similar configuration, based on the allowable Openstack networking definitions. This includes basic networks,
 * provider networks (with a single VLAN), and multi-provider networks (one or more VLANs).
 *
 * Initially, all provider networks must currently be "vlan" type, and multi-provider networks must be multiple
 * VLANs on the same physical network.
 *
 * This service supports two modes of Network update: - via Heat Templates - via Neutron API The network
 * orchestration mode for each network type is declared in its catalog definition. All Heat-based templates must
 * support some subset of the same input parameters: network_name, physical_network, vlan, segments.
 *
 * The method returns a NetworkRollback object. This object can be passed as-is to the rollbackNetwork operation to
 * undo everything that was updated. This is useful if a network is successfully updated but orchestration fails on
 * a subsequent operation.
 */
public void updateNetwork(String cloudSiteId, String tenantId, String networkType, String modelCustomizationUuid, String networkId, String networkName, String physicalNetworkName, List<Integer> vlans, List<RouteTarget> routeTargets, String shared, String external, List<Subnet> subnets, List<String> policyFqdns, List<String> routeTableFqdns, MsoRequest msoRequest, Holder<String> stackId) throws NetworkException {
    logger.debug("***UPDATE Network adapter with Network: {} of type {} in {}/{}", networkName, networkType, cloudSiteId, tenantId);
    // Will capture execution time for metrics
    long startTime = System.currentTimeMillis();
    Optional<CloudSite> cloudSiteOpt = cloudConfig.getCloudSite(cloudSiteId);
    if (!cloudSiteOpt.isPresent()) {
        String error = String.format("UpdateNetwork: Configuration Error. Stack %s in %s/%s: CloudSite does not exist in MSO Configuration", networkName, cloudSiteId, tenantId);
        logger.error(LoggingAnchor.THREE, MessageEnum.RA_CONFIG_EXC, ErrorCode.DataError.getValue(), error);
        // Set the detailed error as the Exception 'message'
        throw new NetworkException(error, MsoExceptionCategory.USERDATA);
    }
    NetworkResource networkResource = networkCheck(startTime, networkType, modelCustomizationUuid, networkName, physicalNetworkName, vlans, routeTargets, cloudSiteId, cloudSiteOpt.get());
    String mode = networkResource.getOrchestrationMode();
    NetworkType neutronNetworkType = NetworkType.valueOf(networkResource.getNeutronNetworkType());
    if (NEUTRON_MODE.equals(mode)) {
        // Verify that the Network exists
        // For Neutron-based orchestration, the networkId is the Neutron Network UUID.
        NetworkInfo netInfo = null;
        try {
            netInfo = neutron.queryNetwork(networkId, tenantId, cloudSiteId);
        } catch (MsoException me) {
            me.addContext(UPDATE_NETWORK_CONTEXT);
            logger.error("{} {} Exception - queryNetwork query {} in {}/{} ", MessageEnum.RA_QUERY_NETWORK_EXC, ErrorCode.BusinessProcessError.getValue(), networkId, cloudSiteId, tenantId, me);
            throw new NetworkException(me);
        }
        if (netInfo == null) {
            String error = String.format("Update Nework: Network %s does not exist in %s/%s", networkId, cloudSiteId, tenantId);
            logger.error(LoggingAnchor.THREE, MessageEnum.RA_NETWORK_NOT_FOUND, ErrorCode.BusinessProcessError.getValue(), error);
            // Does not exist. Throw an exception (can't update a non-existent network)
            throw new NetworkException(error, MsoExceptionCategory.USERDATA);
        }
        try {
            netInfo = neutron.updateNetwork(cloudSiteId, tenantId, networkId, neutronNetworkType, physicalNetworkName, vlans);
        } catch (MsoException me) {
            me.addContext(UPDATE_NETWORK_CONTEXT);
            logger.error("{} {} Exception - updateNetwork {} in {}/{} ", MessageEnum.RA_UPDATE_NETWORK_ERR, ErrorCode.DataError.getValue(), networkId, cloudSiteId, tenantId, me);
            throw new NetworkException(me);
        }
        logger.debug("Network {} updated, id = {}", networkId, netInfo.getId());
    } else if ("HEAT".equals(mode)) {
        // First, look up to see that the Network already exists.
        // For Heat-based orchestration, the networkId is the network Stack ID.
        StackInfo heatStack = null;
        try {
            heatStack = heat.queryStack(cloudSiteId, CLOUD_OWNER, tenantId, networkName);
        } catch (MsoException me) {
            me.addContext(UPDATE_NETWORK_CONTEXT);
            logger.error("{} {} Exception - QueryStack query {} in {}/{} ", MessageEnum.RA_QUERY_NETWORK_EXC, ErrorCode.DataError.getValue(), networkId, cloudSiteId, tenantId, me);
            throw new NetworkException(me);
        }
        if (heatStack == null || (heatStack.getStatus() == HeatStatus.NOTFOUND)) {
            String error = String.format("UpdateNetwork: Stack %s does not exist in %s/%s", networkName, cloudSiteId, tenantId);
            logger.error(LoggingAnchor.THREE, MessageEnum.RA_NETWORK_NOT_FOUND, ErrorCode.DataError.getValue(), error);
            // Network stack does not exist. Return an error
            throw new NetworkException(error, MsoExceptionCategory.USERDATA);
        }
        // Get the previous parameters for rollback
        Map<String, Object> heatParams = heatStack.getParameters();
        List<Integer> previousVlans = new ArrayList<>();
        String vlansParam = (String) heatParams.get(VLANS);
        if (vlansParam != null) {
            for (String vlan : vlansParam.split(",")) {
                try {
                    previousVlans.add(Integer.parseInt(vlan));
                } catch (NumberFormatException e) {
                    logger.warn("{} {} Exception - VLAN parse for params {} ", MessageEnum.RA_VLAN_PARSE, ErrorCode.DataError.getValue(), vlansParam, e);
                }
            }
        }
        logger.debug("Update Stack:  Previous VLANS: {}", previousVlans);
        // Ready to deploy the updated Network via Heat
        HeatTemplate heatTemplate = networkResource.getHeatTemplate();
        if (heatTemplate == null) {
            String error = "Network error - undefined Heat Template. Network Type=" + networkType;
            logger.error(LoggingAnchor.THREE, MessageEnum.RA_PARAM_NOT_FOUND, ErrorCode.DataError.getValue(), error);
            throw new NetworkException(error, MsoExceptionCategory.INTERNAL);
        }
        logger.debug("Got HEAT Template from DB: {}", heatTemplate);
        // "Fix" the template if it has CR/LF (getting this from Oracle)
        String template = heatTemplate.getHeatTemplate();
        template = template.replaceAll("\r\n", "\n");
        boolean os3template = false;
        String os3nw = OS3_NW;
        os3nw = environment.getProperty(OS3_NW_PROPERTY, OS3_NW);
        if (template.contains(os3nw))
            os3template = true;
        // Build the common set of HEAT template parameters
        Map<String, Object> stackParams = populateNetworkParams(neutronNetworkType, networkName, physicalNetworkName, vlans, routeTargets, shared, external, os3template);
        // Shouldn't happen unless DB config is wrong, since all networks use same inputs
        try {
            stackParams = heat.validateStackParams(stackParams, heatTemplate);
        } catch (IllegalArgumentException e) {
            String error = "UpdateNetwork: Configuration Error: Network Type=" + networkType;
            logger.error(LoggingAnchor.THREE, MessageEnum.RA_CONFIG_EXC, ErrorCode.DataError.getValue(), error);
            throw new NetworkException(error, MsoExceptionCategory.INTERNAL, e);
        }
        if (subnets != null) {
            try {
                if (os3template) {
                    template = mergeSubnetsAIC3(template, subnets, stackParams);
                } else {
                    template = mergeSubnets(template, subnets);
                }
            } catch (MsoException me) {
                me.addContext(UPDATE_NETWORK_CONTEXT);
                logger.error("{} {} Exception - UpdateNetwork mergeSubnets for network type {} in {}/{} ", MessageEnum.RA_UPDATE_NETWORK_ERR, ErrorCode.DataError.getValue(), neutronNetworkType.toString(), cloudSiteId, tenantId, me);
                throw new NetworkException(me);
            }
        }
        if (policyFqdns != null && os3template) {
            try {
                mergePolicyRefs(policyFqdns, stackParams);
            } catch (MsoException me) {
                me.addContext(UPDATE_NETWORK_CONTEXT);
                logger.error("{} {} Exception - UpdateNetwork mergePolicyRefs type {} in {}/{} ", MessageEnum.RA_UPDATE_NETWORK_ERR, ErrorCode.DataError.getValue(), neutronNetworkType.toString(), cloudSiteId, tenantId, me);
                throw new NetworkException(me);
            }
        }
        if (routeTableFqdns != null && !routeTableFqdns.isEmpty() && os3template) {
            try {
                mergeRouteTableRefs(routeTableFqdns, stackParams);
            } catch (MsoException me) {
                me.addContext(UPDATE_NETWORK_CONTEXT);
                logger.error("{} {} Exception - UpdateNetwork mergeRouteTableRefs type {} in {}/{} ", MessageEnum.RA_UPDATE_NETWORK_ERR, ErrorCode.DataError.getValue(), neutronNetworkType.toString(), cloudSiteId, tenantId, me);
                throw new NetworkException(me);
            }
        }
        // Ignore MsoStackNotFound exception because we already checked.
        try {
            heatStack = heatWithUpdate.updateStack(cloudSiteId, CLOUD_OWNER, tenantId, networkId, template, stackParams, false, heatTemplate.getTimeoutMinutes());
        } catch (MsoException me) {
            me.addContext(UPDATE_NETWORK_CONTEXT);
            logger.error("{} {} Exception - update network {} in {}/{} ", MessageEnum.RA_UPDATE_NETWORK_ERR, ErrorCode.DataError.getValue(), networkId, cloudSiteId, tenantId, me);
            throw new NetworkException(me);
        }
        stackId.value = heatStack.getCanonicalName();
        logger.debug("Network {} successfully updated via HEAT", networkId);
    }
    return;
}
Also used : MsoException(org.onap.so.openstack.exceptions.MsoException) NetworkInfo(org.onap.so.openstack.beans.NetworkInfo) NetworkResource(org.onap.so.db.catalog.beans.NetworkResource) NetworkType(org.onap.so.openstack.utils.MsoNeutronUtils.NetworkType) HeatTemplate(org.onap.so.db.catalog.beans.HeatTemplate) CloudSite(org.onap.so.db.catalog.beans.CloudSite) List(java.util.List) ArrayList(java.util.ArrayList) NetworkException(org.onap.so.adapters.network.exceptions.NetworkException) StackInfo(org.onap.so.openstack.beans.StackInfo) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with NetworkInfo

use of org.onap.so.openstack.beans.NetworkInfo in project so by onap.

the class NetworkInfoMapperTest method checkLocateVlanInformationNoSegments.

@Test
public void checkLocateVlanInformationNoSegments() {
    Network network = new Network();
    network.setProviderPhysicalNetwork("test-physical-network");
    network.setProviderNetworkType("vlan");
    network.setProviderSegmentationId(2);
    NetworkInfoMapper mapper = new NetworkInfoMapper(network);
    NetworkInfo result = mapper.map();
    assertEquals("test-physical-network", result.getProvider());
    assertEquals(1, result.getVlans().size());
    assertEquals(2, result.getVlans().get(0).intValue());
}
Also used : NetworkInfo(org.onap.so.openstack.beans.NetworkInfo) Network(com.woorea.openstack.quantum.model.Network) Test(org.junit.Test)

Example 8 with NetworkInfo

use of org.onap.so.openstack.beans.NetworkInfo in project so by onap.

the class NetworkInfoMapperTest method checkLocateVlanInformationSegmentsAndPhysical.

@Test
public void checkLocateVlanInformationSegmentsAndPhysical() {
    Network network = new Network();
    addSegments(network);
    network.setProviderPhysicalNetwork("test-physical-network");
    network.setProviderNetworkType("vlan");
    network.setProviderSegmentationId(2);
    NetworkInfoMapper mapper = new NetworkInfoMapper(network);
    NetworkInfo result = mapper.map();
    assertEquals("test-physical-network", result.getProvider());
    assertEquals(1, result.getVlans().size());
    assertEquals(2, result.getVlans().get(0).intValue());
}
Also used : NetworkInfo(org.onap.so.openstack.beans.NetworkInfo) Network(com.woorea.openstack.quantum.model.Network) Test(org.junit.Test)

Example 9 with NetworkInfo

use of org.onap.so.openstack.beans.NetworkInfo in project so by onap.

the class NetworkInfoMapperTest method checkLocateVlanInformationSegments.

@Test
public void checkLocateVlanInformationSegments() {
    Network network = new Network();
    addSegments(network);
    NetworkInfoMapper mapper = new NetworkInfoMapper(network);
    NetworkInfo result = mapper.map();
    assertEquals("type1", result.getProvider());
    assertEquals(2, result.getVlans().size());
    assertEquals(Arrays.asList(1, 2).toString(), result.getVlans().toString());
}
Also used : NetworkInfo(org.onap.so.openstack.beans.NetworkInfo) Network(com.woorea.openstack.quantum.model.Network) Test(org.junit.Test)

Example 10 with NetworkInfo

use of org.onap.so.openstack.beans.NetworkInfo in project so by onap.

the class MsoNeutronUtilsTest method queryNetworkTest.

@Test
public void queryNetworkTest() throws Exception {
    StubOpenStack.mockOpenStackGetNeutronNetwork(wireMockServer, "GetNeutronNetwork.json", "43173f6a-d699-414b-888f-ab243dda6dfe");
    NetworkInfo networkInfo = msoNeutronUtils.queryNetwork("43173f6a-d699-414b-888f-ab243dda6dfe", "tenantId", "MTN13");
    Assert.assertEquals("net1", networkInfo.getName());
}
Also used : NetworkInfo(org.onap.so.openstack.beans.NetworkInfo) Test(org.junit.Test) BaseTest(org.onap.so.BaseTest)

Aggregations

NetworkInfo (org.onap.so.openstack.beans.NetworkInfo)12 Test (org.junit.Test)10 BaseTest (org.onap.so.BaseTest)6 Network (com.woorea.openstack.quantum.model.Network)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 NetworkException (org.onap.so.adapters.network.exceptions.NetworkException)1 CloudSite (org.onap.so.db.catalog.beans.CloudSite)1 HeatTemplate (org.onap.so.db.catalog.beans.HeatTemplate)1 NetworkResource (org.onap.so.db.catalog.beans.NetworkResource)1 StackInfo (org.onap.so.openstack.beans.StackInfo)1 MsoException (org.onap.so.openstack.exceptions.MsoException)1 NetworkType (org.onap.so.openstack.utils.MsoNeutronUtils.NetworkType)1