Search in sources :

Example 16 with CloudSite

use of org.onap.so.db.catalog.beans.CloudSite in project so by onap.

the class BaseTest method init.

@Before
public void init() throws IOException {
    CloudIdentity identity = getCloudIdentity();
    CloudSite cloudSite = getCloudSite(identity);
    mockCloud(identity, cloudSite);
}
Also used : CloudIdentity(org.onap.so.db.catalog.beans.CloudIdentity) CloudSite(org.onap.so.db.catalog.beans.CloudSite) Before(org.junit.Before)

Example 17 with CloudSite

use of org.onap.so.db.catalog.beans.CloudSite in project so by onap.

the class MsoMulticloudUtilsTest method createStackMulticloudClientIsNull.

@Ignore
@Test
public void createStackMulticloudClientIsNull() {
    try {
        multicloudUtilsMock.cloudConfig = cloudConfigMock;
        CloudSite cloudSite = new CloudSite();
        cloudSite.setIdentityService(new CloudIdentity());
        when(cloudConfigMock.getCloudSite("MTN13")).thenReturn(Optional.of(cloudSite));
        multicloudUtilsMock.createStack("MNT14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(), "TEST-heat", new HashMap<>(), false, 200, "TEST-env", new HashMap<>(), new HashMap<>(), false, false);
    } catch (MsoException e) {
        assertEquals("0 : Multicloud client could not be initialized", e.toString());
        return;
    }
    fail("MsoOpenstackException expected!");
}
Also used : MsoException(org.onap.so.openstack.exceptions.MsoException) CloudSite(org.onap.so.db.catalog.beans.CloudSite) CloudIdentity(org.onap.so.db.catalog.beans.CloudIdentity) VduModelInfo(org.onap.so.adapters.vdu.VduModelInfo) Ignore(org.junit.Ignore) Test(org.junit.Test) BaseTest(org.onap.so.BaseTest)

Example 18 with CloudSite

use of org.onap.so.db.catalog.beans.CloudSite in project so by onap.

the class R__CloudConfigMigration method migrateCloudSite.

private void migrateCloudSite(Collection<CloudSite> entities, Connection connection) throws SQLException {
    logger.debug("Starting migration for CloudConfig-->CloudSite");
    String insert = "INSERT INTO `cloud_sites` (`ID`, `REGION_ID`, `IDENTITY_SERVICE_ID`, `CLOUD_VERSION`, `CLLI`, `CLOUDIFY_ID`, `PLATFORM`, `ORCHESTRATOR`, `LAST_UPDATED_BY`) " + "VALUES (?,?,?,?,?,?,?,?,?);";
    try (Statement stmt = connection.createStatement();
        PreparedStatement ps = connection.prepareStatement(insert)) {
        for (CloudSite cloudSite : entities) {
            try (ResultSet rows = stmt.executeQuery("Select count(1) from cloud_sites where id='" + cloudSite.getId() + "'")) {
                int count = 0;
                while (rows.next()) {
                    count = rows.getInt(1);
                }
                if (count == 0) {
                    ps.setString(1, cloudSite.getId());
                    ps.setString(2, cloudSite.getRegionId());
                    ps.setString(3, cloudSite.getIdentityServiceId());
                    ps.setString(4, cloudSite.getCloudVersion());
                    ps.setString(5, cloudSite.getClli());
                    ps.setString(6, cloudSite.getCloudifyId());
                    ps.setString(7, cloudSite.getPlatform());
                    ps.setString(8, cloudSite.getOrchestrator());
                    ps.setString(9, FLYWAY);
                    ps.executeUpdate();
                }
            }
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CloudSite(org.onap.so.db.catalog.beans.CloudSite) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 19 with CloudSite

use of org.onap.so.db.catalog.beans.CloudSite in project so by onap.

the class MsoVnfAdapterImpl method updateVfModule.

public void updateVfModule(String cloudSiteId, String cloudOwner, String tenantId, String vnfType, String vnfVersion, String vnfName, String requestType, String volumeGroupHeatStackId, String baseVfHeatStackId, String vfModuleStackId, String modelCustomizationUuid, Map<String, Object> inputs, MsoRequest msoRequest, Holder<Map<String, String>> outputs, Holder<VnfRollback> rollback) throws VnfException {
    String vfModuleName = vnfName;
    String vfModuleType = vnfType;
    StringBuilder sbInit = new StringBuilder();
    sbInit.append("updateVfModule: \n");
    sbInit.append("cloudOwner=" + cloudOwner + "\n");
    sbInit.append("cloudSiteId=" + cloudSiteId + "\n");
    sbInit.append("tenantId=" + tenantId + "\n");
    sbInit.append("vnfType=" + vnfType + "\n");
    sbInit.append("vnfVersion=" + vnfVersion + "\n");
    sbInit.append("vnfName=" + vnfName + "\n");
    sbInit.append("requestType=" + requestType + "\n");
    sbInit.append("volumeGroupHeatStackId=" + volumeGroupHeatStackId + "\n");
    sbInit.append("baseVfHeatStackId=" + baseVfHeatStackId + "\n");
    sbInit.append("vfModuleStackId=" + vfModuleStackId + "\n");
    sbInit.append("modelCustomizationUuid=" + modelCustomizationUuid + "\n");
    logger.debug(sbInit.toString());
    String mcu = modelCustomizationUuid;
    boolean useMCUuid = false;
    if (mcu != null && !mcu.isEmpty()) {
        if ("null".equalsIgnoreCase(mcu)) {
            logger.debug("modelCustomizationUuid: passed in as the string 'null' - will ignore: {}", modelCustomizationUuid);
            useMCUuid = false;
            mcu = "";
        } else {
            logger.debug("Found modelCustomizationUuid! Will use that: {}", mcu);
            useMCUuid = true;
        }
    }
    String requestTypeString = "";
    if (requestType != null && !"".equals(requestType)) {
        requestTypeString = requestType;
    }
    String nestedStackId = null;
    if (volumeGroupHeatStackId != null && !"".equals(volumeGroupHeatStackId) && !"null".equalsIgnoreCase(volumeGroupHeatStackId)) {
        nestedStackId = volumeGroupHeatStackId;
    }
    String nestedBaseStackId = null;
    if (baseVfHeatStackId != null && !"".equals(baseVfHeatStackId) && !"null".equalsIgnoreCase(baseVfHeatStackId)) {
        nestedBaseStackId = baseVfHeatStackId;
    }
    if (inputs == null) {
        // Create an empty set of inputs
        inputs = new HashMap<>();
        logger.debug("inputs == null - setting to empty");
    }
    boolean isBaseRequest = false;
    boolean isVolumeRequest = false;
    if (requestTypeString.startsWith("VOLUME")) {
        isVolumeRequest = true;
    }
    if ((vfModuleName == null || "".equals(vfModuleName.trim())) && vfModuleStackId != null) {
        vfModuleName = this.getVfModuleNameFromModuleStackId(vfModuleStackId);
    }
    logger.debug("Updating VFModule: " + vfModuleName + " of type " + vfModuleType + "in " + cloudOwner + "/" + cloudSiteId + "/" + tenantId);
    logger.debug("requestTypeString = " + requestTypeString + ", nestedVolumeStackId = " + nestedStackId + ", nestedBaseStackId = " + nestedBaseStackId);
    // Build a default rollback object (no actions performed)
    VnfRollback vfRollback = new VnfRollback();
    vfRollback.setCloudSiteId(cloudSiteId);
    vfRollback.setCloudOwner(cloudOwner);
    vfRollback.setTenantId(tenantId);
    vfRollback.setMsoRequest(msoRequest);
    vfRollback.setRequestType(requestTypeString);
    vfRollback.setVolumeGroupHeatStackId(volumeGroupHeatStackId);
    vfRollback.setBaseGroupHeatStackId(baseVfHeatStackId);
    vfRollback.setIsBase(isBaseRequest);
    vfRollback.setVfModuleStackId(vfModuleStackId);
    vfRollback.setModelCustomizationUuid(mcu);
    StackInfo heatStack;
    logger.debug("UpdateVfModule - querying for {}", vfModuleName);
    try {
        heatStack = msoHeatUtils.queryStack(cloudSiteId, cloudOwner, tenantId, vfModuleName);
    } catch (MsoException me) {
        // Failed to query the Stack due to an openstack exception.
        // Convert to a generic VnfException
        me.addContext("UpdateVFModule");
        String error = "Update VFModule: Query " + vfModuleName + " in " + cloudOwner + "/" + cloudSiteId + "/" + tenantId + ": " + me;
        logger.error(LoggingAnchor.NINE, MessageEnum.RA_QUERY_VNF_ERR.toString(), vfModuleName, cloudOwner, cloudSiteId, tenantId, OPENSTACK, QUERY_STACK, ErrorCode.DataError.getValue(), "Exception - " + QUERY_STACK, me);
        logger.debug(error);
        throw new VnfException(me);
    }
    // TODO - do we need to check for the other status possibilities?
    if (heatStack == null || heatStack.getStatus() == HeatStatus.NOTFOUND) {
        // Not Found
        String error = "Update VF: Stack " + vfModuleName + " does not exist in " + cloudOwner + "/" + cloudSiteId + "/" + tenantId;
        logger.error(LoggingAnchor.NINE, MessageEnum.RA_VNF_NOT_EXIST.toString(), vfModuleName, cloudOwner, cloudSiteId, tenantId, OPENSTACK, QUERY_STACK, ErrorCode.DataError.getValue(), error);
        throw new VnfNotFound(cloudSiteId, cloudOwner, tenantId, vfModuleName);
    } else {
        logger.debug("Found Existing stack, status={}", heatStack.getStatus());
        // Populate the outputs from the existing stack.
        outputs.value = copyStringOutputs(heatStack.getOutputs());
        // Default rollback - no updates performed
        rollback.value = vfRollback;
    }
    // 1604 Cinder Volume support - handle a nestedStackId if sent (volumeGroupHeatStackId):
    StackInfo nestedHeatStack = null;
    if (nestedStackId != null) {
        try {
            logger.debug("Querying for nestedStackId = {}", nestedStackId);
            nestedHeatStack = msoHeatUtils.queryStack(cloudSiteId, cloudOwner, tenantId, nestedStackId);
        } catch (MsoException me) {
            // Failed to query the Stack due to an openstack exception.
            // Convert to a generic VnfException
            me.addContext("UpdateVFModule");
            String error = "Update VF: Attached heatStack ID Query " + nestedStackId + " in " + cloudOwner + "/" + cloudSiteId + "/" + tenantId + ": " + me;
            logger.error(LoggingAnchor.NINE, MessageEnum.RA_QUERY_VNF_ERR.toString(), vnfName, cloudOwner, cloudSiteId, tenantId, OPENSTACK, QUERY_STACK, ErrorCode.DataError.getValue(), "Exception - " + error, me);
            logger.debug("ERROR trying to query nested stack= {}", error);
            throw new VnfException(me);
        }
        if (nestedHeatStack == null || nestedHeatStack.getStatus() == HeatStatus.NOTFOUND) {
            String error = "Update VFModule: Attached volume heatStack ID DOES NOT EXIST " + nestedStackId + " in " + cloudOwner + "/" + cloudSiteId + "/" + tenantId + " " + USER_ERROR;
            logger.error(LoggingAnchor.TEN, MessageEnum.RA_QUERY_VNF_ERR.toString(), vnfName, cloudOwner, cloudSiteId, tenantId, error, OPENSTACK, QUERY_STACK, ErrorCode.DataError.getValue(), error);
            logger.debug(error);
            throw new VnfException(error, MsoExceptionCategory.USERDATA);
        } else {
            logger.debug("Found nested heat stack - copying values to inputs *later*");
            msoHeatUtils.copyStringOutputsToInputs(inputs, nestedHeatStack.getOutputs(), false);
        }
    }
    // handle a nestedBaseStackId if sent - this is the stack ID of the base.
    StackInfo nestedBaseHeatStack = null;
    if (nestedBaseStackId != null) {
        try {
            logger.debug("Querying for nestedBaseStackId = {}", nestedBaseStackId);
            nestedBaseHeatStack = msoHeatUtils.queryStack(cloudSiteId, cloudOwner, tenantId, nestedBaseStackId);
        } catch (MsoException me) {
            // Failed to query the Stack due to an openstack exception.
            // Convert to a generic VnfException
            me.addContext("UpdateVfModule");
            String error = "Update VFModule: Attached baseHeatStack ID Query " + nestedBaseStackId + " in " + cloudOwner + "/" + cloudSiteId + "/" + tenantId + ": " + me;
            logger.error(LoggingAnchor.NINE, MessageEnum.RA_QUERY_VNF_ERR.toString(), vfModuleName, cloudOwner, cloudSiteId, tenantId, OPENSTACK, QUERY_STACK, ErrorCode.DataError.getValue(), "Exception - " + error, me);
            logger.debug("ERROR trying to query nested base stack= {}", error);
            throw new VnfException(me);
        }
        if (nestedBaseHeatStack == null || nestedBaseHeatStack.getStatus() == HeatStatus.NOTFOUND) {
            String error = "Update VFModule: Attached base heatStack ID DOES NOT EXIST " + nestedBaseStackId + " in " + cloudOwner + "/" + cloudSiteId + "/" + tenantId + " " + USER_ERROR;
            logger.error(LoggingAnchor.TEN, MessageEnum.RA_QUERY_VNF_ERR.toString(), vfModuleName, cloudOwner, cloudSiteId, tenantId, error, OPENSTACK, QUERY_STACK, ErrorCode.DataError.getValue(), error);
            logger.debug(error);
            throw new VnfException(error, MsoExceptionCategory.USERDATA);
        } else {
            logger.debug("Found nested base heat stack - copying values to inputs *later*");
            msoHeatUtils.copyStringOutputsToInputs(inputs, nestedBaseHeatStack.getOutputs(), false);
        }
    }
    // Retrieve the VF definition
    VnfResource vnfResource = null;
    VfModule vf = null;
    VfModuleCustomization vfmc = null;
    if (useMCUuid) {
        vfmc = vfModuleCustomRepo.findFirstByModelCustomizationUUIDOrderByCreatedDesc(modelCustomizationUuid);
        vf = vfmc != null ? vfmc.getVfModule() : null;
        if (vf == null) {
            logger.debug("Unable to find a vfModule matching modelCustomizationUuid={}", mcu);
        }
    } else {
        logger.debug("1707 and later - MUST PROVIDE Model Customization UUID!");
    }
    if (vf == null) {
        String error = "Update VfModule: unable to find vfModule with modelCustomizationUuid=" + mcu;
        logger.error(LoggingAnchor.SIX, MessageEnum.RA_VNF_UNKNOWN_PARAM.toString(), "VF Module Type", vfModuleType, OPENSTACK, ErrorCode.DataError.getValue(), error);
        throw new VnfException(error, MsoExceptionCategory.USERDATA);
    }
    logger.debug("Got VF module definition from Catalog: {}", vf.toString());
    if (vf.getIsBase()) {
        isBaseRequest = true;
        logger.debug("This a BASE update request");
    } else {
        logger.debug("This is *not* a BASE VF update request");
        if (!isVolumeRequest && nestedBaseStackId == null) {
            logger.debug("DANGER WILL ROBINSON! This is unexpected - no nestedBaseStackId with this non-base request");
        }
    }
    // if we have a vf Module - then we have to query to get the VnfResource record.
    if (vf.getModelUUID() != null) {
        String vnfResourceModelUuid = vf.getModelUUID();
        vnfResource = vf.getVnfResources();
        if (vnfResource == null) {
            logger.debug("Unable to find vnfResource at ? will not error for now...", vnfResourceModelUuid);
        }
    }
    String minVersionVnf = null;
    String maxVersionVnf = null;
    if (vnfResource != null) {
        try {
            minVersionVnf = vnfResource.getAicVersionMin();
            maxVersionVnf = vnfResource.getAicVersionMax();
        } catch (Exception e) {
            logger.debug("Unable to pull min/max version for this VNF Resource entry", e);
            minVersionVnf = null;
            maxVersionVnf = null;
        }
        if (minVersionVnf != null && "".equals(minVersionVnf)) {
            minVersionVnf = null;
        }
        if (maxVersionVnf != null && "".equals(maxVersionVnf)) {
            maxVersionVnf = null;
        }
    }
    if (minVersionVnf != null && maxVersionVnf != null) {
        MavenLikeVersioning aicV = new MavenLikeVersioning();
        // double check
        if (this.cloudConfig != null) {
            Optional<CloudSite> cloudSiteOpt = this.cloudConfig.getCloudSite(cloudSiteId);
            if (cloudSiteOpt.isPresent()) {
                aicV.setVersion(cloudSiteOpt.get().getCloudVersion());
                boolean moreThanMin = true;
                boolean equalToMin = true;
                boolean moreThanMax = true;
                boolean equalToMax = true;
                boolean doNotTest = false;
                try {
                    moreThanMin = aicV.isMoreRecentThan(minVersionVnf);
                    equalToMin = aicV.isTheSameVersion(minVersionVnf);
                    moreThanMax = aicV.isMoreRecentThan(maxVersionVnf);
                    equalToMax = aicV.isTheSameVersion(maxVersionVnf);
                } catch (Exception e) {
                    logger.debug("An exception occured while trying to test AIC Version {} - will default to not check", e.getMessage(), e);
                    doNotTest = true;
                }
                if (!doNotTest) {
                    if (// aic >= min
                    (moreThanMin || equalToMin) && ((equalToMax) || !(moreThanMax))) {
                        // aic <= max
                        logger.debug("VNF Resource " + vnfResource.getModelName() + " " + VERSION_MIN + " =" + minVersionVnf + " " + VERSION_MAX + " :" + maxVersionVnf + " supported on Cloud: " + cloudSiteId + " with AIC_Version:" + aicV);
                    } else {
                        // ERROR
                        String error = "VNF Resource type: " + vnfResource.getModelName() + " " + VERSION_MIN + " =" + minVersionVnf + " " + VERSION_MAX + " :" + maxVersionVnf + " NOT supported on Cloud: " + cloudSiteId + " with AIC_Version:" + aicV;
                        logger.error(LoggingAnchor.FIVE, MessageEnum.RA_CONFIG_EXC.toString(), error, OPENSTACK, ErrorCode.BusinessProcessError.getValue(), "Exception - setVersion");
                        logger.debug(error);
                        throw new VnfException(error, MsoExceptionCategory.USERDATA);
                    }
                } else {
                    logger.debug("bypassing testing AIC version...");
                }
            }
        // let this error out downstream to avoid introducing uncertainty at this stage
        } else {
            logger.debug("cloudConfig is NULL - cannot check cloud site version");
        }
    } else {
        logger.debug("AIC Version not set in VNF_Resource - do not error for now - not checked.");
    }
    // End Version check 1607
    HeatTemplate heatTemplate = null;
    HeatEnvironment heatEnvironment = null;
    if (isVolumeRequest) {
        heatTemplate = vf.getVolumeHeatTemplate();
        heatEnvironment = vfmc.getVolumeHeatEnv();
    } else {
        heatTemplate = vf.getModuleHeatTemplate();
        heatEnvironment = vfmc.getHeatEnvironment();
    }
    if (heatTemplate == null) {
        String error = "UpdateVF: No Heat Template ID defined in catalog database for " + vfModuleType + ", modelCustomizationUuid=" + mcu + ", vfModuleUuid=" + vf.getModelUUID() + ", reqType=" + requestTypeString;
        logger.error(LoggingAnchor.SIX, MessageEnum.RA_VNF_UNKNOWN_PARAM.toString(), "Heat Template ID", vfModuleType, OPENSTACK, ErrorCode.DataError.getValue(), error);
        throw new VnfException(error, MsoExceptionCategory.INTERNAL);
    } else {
        logger.debug("Got HEAT Template from DB: {}", heatTemplate.getHeatTemplate());
    }
    if (heatEnvironment == null) {
        String error = "Update VNF: undefined Heat Environment. VF=" + vfModuleType + ", modelCustomizationUuid=" + mcu + ", vfModuleUuid=" + vf.getModelUUID() + ", reqType=" + requestTypeString;
        logger.error(LoggingAnchor.FIVE, MessageEnum.RA_VNF_UNKNOWN_PARAM.toString(), "Heat Environment ID", OPENSTACK, ErrorCode.DataError.getValue(), error);
        throw new VnfException(error, MsoExceptionCategory.INTERNAL);
    } else {
        logger.debug("Got Heat Environment from DB: {}", heatEnvironment.getEnvironment());
    }
    logger.debug("In MsoVnfAdapterImpl, about to call db.getNestedTemplates avec templateId={}", heatTemplate.getArtifactUuid());
    List<HeatTemplate> nestedTemplates = heatTemplate.getChildTemplates();
    Map<String, Object> nestedTemplatesChecked = new HashMap<>();
    if (nestedTemplates != null && !nestedTemplates.isEmpty()) {
        // for debugging print them out
        logger.debug("Contents of nestedTemplates - to be added to files: on stack:");
        for (HeatTemplate entry : nestedTemplates) {
            nestedTemplatesChecked.put(entry.getTemplateName(), entry.getTemplateBody());
            logger.debug(entry.getTemplateName() + " -> " + entry.getTemplateBody());
        }
    } else {
        logger.debug("No nested templates found - nothing to do here");
        nestedTemplatesChecked = null;
    }
    // Also add the files: for any get_files associated with this VfModule
    // *if* there are any
    logger.debug("In MsoVnfAdapterImpl.updateVfModule, about to call db.getHeatFiles avec vfModuleId={}", vf.getModelUUID());
    List<HeatFiles> heatFiles = null;
    Map<String, Object> heatFilesObjects = new HashMap<>();
    // Add ability to turn on adding get_files with volume requests (by property).
    boolean addGetFilesOnVolumeReq = false;
    try {
        String propertyString = this.environment.getProperty(MsoVnfAdapterImpl.ADD_GET_FILES_ON_VOLUME_REQ);
        if ("true".equalsIgnoreCase(propertyString) || "y".equalsIgnoreCase(propertyString)) {
            addGetFilesOnVolumeReq = true;
            logger.debug("AddGetFilesOnVolumeReq - setting to true! {}", propertyString);
        }
    } catch (Exception e) {
        logger.debug("An error occured trying to get property {} - default to false", MsoVnfAdapterImpl.ADD_GET_FILES_ON_VOLUME_REQ, e);
    }
    if (!isVolumeRequest || addGetFilesOnVolumeReq) {
        logger.debug("In MsoVnfAdapterImpl updateVfModule, about to call db.getHeatFilesForVfModule avec " + "vfModuleId={}", vf.getModelUUID());
        heatFiles = vf.getHeatFiles();
        if (heatFiles != null && !heatFiles.isEmpty()) {
            // add these to stack - to be done in createStack
            // here, we will map them to Map<String, Object> from Map<String, HeatFiles>
            // this will match the nested templates format
            logger.debug("Contents of heatFiles - to be added to files: on stack:");
            for (HeatFiles heatfile : heatFiles) {
                logger.debug(heatfile.getFileName() + " -> " + heatfile.getFileBody());
                heatFilesObjects.put(heatfile.getFileName(), heatfile.getFileBody());
            }
        } else {
            logger.debug("No heat files found -nothing to do here");
            heatFilesObjects = null;
        }
    }
    // Check that required parameters have been supplied
    String missingParams = null;
    List<String> paramList = new ArrayList<>();
    // New for 1510 - consult the PARAM_ALIAS field to see if we've been
    // supplied an alias. Only check if we don't find it initially.
    // Also new in 1510 - don't flag missing parameters if there's an environment - because they might be there.
    // And also new - add parameter to turn off checking all together if we find we're blocking orders we
    // shouldn't
    boolean checkRequiredParameters = true;
    try {
        String propertyString = this.environment.getProperty(MsoVnfAdapterImpl.CHECK_REQD_PARAMS);
        if ("false".equalsIgnoreCase(propertyString) || "n".equalsIgnoreCase(propertyString)) {
            checkRequiredParameters = false;
            logger.debug("CheckRequiredParameters is FALSE. Will still check but then skip blocking...", MsoVnfAdapterImpl.CHECK_REQD_PARAMS);
        }
    } catch (Exception e) {
        // No problem - default is true
        logger.debug("An exception occured trying to get property {}", MsoVnfAdapterImpl.CHECK_REQD_PARAMS, e);
    }
    // 1604 - Add enhanced environment & parameter checking
    // Part 1: parse envt entries to see if reqd parameter is there (before used a simple grep
    // Part 2: only submit to openstack the parameters in the envt that are in the heat template
    // Note this also removes any comments
    MsoHeatEnvironmentEntry mhee = null;
    if (heatEnvironment != null && heatEnvironment.getEnvironment().toLowerCase().contains("parameters:")) {
        logger.debug("Enhanced environment checking enabled - 1604");
        StringBuilder sb = new StringBuilder(heatEnvironment.getEnvironment());
        mhee = new MsoHeatEnvironmentEntry(sb);
        StringBuilder sb2 = new StringBuilder("\nHeat Template Parameters:\n");
        for (HeatTemplateParam parm : heatTemplate.getParameters()) {
            sb2.append("\t" + parm.getParamName() + ", required=" + parm.isRequired());
        }
        if (!mhee.isValid()) {
            sb2.append("Environment says it's not valid! " + mhee.getErrorString());
        } else {
            sb2.append("\nEnvironment:");
            sb2.append(mhee.toFullString());
        }
        logger.debug(sb2.toString());
    } else {
        logger.debug("NO ENVIRONMENT for this entry");
    }
    // New for 1607 - support params of json type
    HashMap<String, JsonNode> jsonParams = new HashMap<>();
    boolean hasJson = false;
    for (HeatTemplateParam parm : heatTemplate.getParameters()) {
        logger.debug("Parameter:'" + parm.getParamName() + "', isRequired=" + parm.isRequired() + ", alias=" + parm.getParamAlias());
        // handle json
        String parameterType = parm.getParamType();
        if (parameterType == null || "".equals(parameterType.trim())) {
            parameterType = "String";
        }
        JsonNode jsonNode = null;
        if ("json".equalsIgnoreCase(parameterType) && inputs != null) {
            if (inputs.containsKey(parm.getParamName())) {
                hasJson = true;
                String jsonString = null;
                try {
                    jsonString = JSON_MAPPER.writeValueAsString(inputs.get(parm.getParamName()));
                    jsonNode = JSON_MAPPER.readTree(jsonString);
                } catch (JsonParseException jpe) {
                    // TODO - what to do here?
                    // for now - send the error to debug
                    logger.debug("Json Error Converting {} - {}", parm.getParamName(), jpe.getMessage(), jpe);
                    hasJson = false;
                    jsonNode = null;
                } catch (Exception e) {
                    // or here?
                    logger.debug("Json Error Converting {} {}", parm.getParamName(), e.getMessage(), e);
                    hasJson = false;
                    jsonNode = null;
                }
                if (jsonNode != null) {
                    jsonParams.put(parm.getParamName(), jsonNode);
                }
            } else if (inputs.containsKey(parm.getParamAlias())) {
                hasJson = true;
                String jsonString = null;
                try {
                    jsonString = (String) inputs.get(parm.getParamAlias());
                    jsonNode = JSON_MAPPER.readTree(jsonString);
                } catch (JsonParseException jpe) {
                    // TODO - what to do here?
                    // for now - send the error to debug, but just leave it as a String
                    String errorMessage = jpe.getMessage();
                    logger.debug("Json Error Converting " + parm.getParamName() + " - " + errorMessage, jpe);
                    hasJson = false;
                    jsonNode = null;
                } catch (Exception e) {
                    // or here?
                    logger.debug("Json Error Converting " + parm.getParamName() + " " + e.getMessage(), e);
                    hasJson = false;
                    jsonNode = null;
                }
                if (jsonNode != null) {
                    // Notice here - we add it to the jsonParams hashMap with the actual name -
                    // then manipulate the inputs so when we check for aliases below - it will not
                    // get flagged.
                    jsonParams.put(parm.getParamName(), jsonNode);
                    inputs.remove(parm.getParamAlias());
                    inputs.put(parm.getParamName(), jsonString);
                }
            }
        // TODO add a check for the parameter in the env file
        }
        if (parm.isRequired() && (inputs == null || !inputs.containsKey(parm.getParamName()))) {
            if (inputs.containsKey(parm.getParamAlias())) {
                // They've submitted using an alias name. Remove that from inputs, and add back using real name.
                String realParamName = parm.getParamName();
                String alias = parm.getParamAlias();
                Object value = inputs.get(alias);
                logger.debug("*Found an Alias: paramName=" + realParamName + ",alias=" + alias + ",value=" + value);
                inputs.remove(alias);
                inputs.put(realParamName, value);
                logger.debug("{} entry removed from inputs, added back using {}", alias, realParamName);
            } else // enhanced - check if it's in the Environment (note: that method
            if (mhee != null && mhee.containsParameter(parm.getParamName())) {
                logger.debug("Required parameter {} appears to be in environment - do not count as missing", parm.getParamName());
            } else {
                logger.debug("adding to missing parameters list: {}", parm.getParamName());
                if (missingParams == null) {
                    missingParams = parm.getParamName();
                } else {
                    missingParams += "," + parm.getParamName();
                }
            }
        }
        paramList.add(parm.getParamName());
    }
    if (missingParams != null) {
        // Problem - missing one or more required parameters
        if (checkRequiredParameters) {
            String error = "Update VNF: Missing Required inputs: " + missingParams;
            logger.error(LoggingAnchor.FIVE, MessageEnum.RA_MISSING_PARAM.toString(), missingParams, OPENSTACK, ErrorCode.DataError.getValue(), error);
            throw new VnfException(error, MsoExceptionCategory.USERDATA);
        } else {
            logger.debug("found missing parameters - but checkRequiredParameters is false - will not block");
        }
    }
    // Just submit the envt entry as received from the database
    String newEnvironmentString = null;
    if (mhee != null) {
        newEnvironmentString = mhee.getRawEntry().toString();
    }
    // Remove any extraneous parameters (don't throw an error)
    if (inputs != null) {
        List<String> extraParams = new ArrayList<>();
        extraParams.addAll(inputs.keySet());
        // This is not a valid parameter for this template
        extraParams.removeAll(paramList);
        if (!extraParams.isEmpty()) {
            logger.warn(LoggingAnchor.SIX, MessageEnum.RA_VNF_EXTRA_PARAM.toString(), vnfType, extraParams.toString(), OPENSTACK, ErrorCode.DataError.getValue(), "Extra params");
            inputs.keySet().removeAll(extraParams);
        }
    }
    Map<String, Object> goldenInputs = copyStringInputs(inputs);
    // 1607 - when we get here - we have clean inputs. Create inputsTwo in case we have json
    Map<String, Object> inputsTwo = null;
    if (hasJson && jsonParams.size() > 0) {
        inputsTwo = new HashMap<>();
        for (Map.Entry<String, Object> entry : inputs.entrySet()) {
            String keyParamName = entry.getKey();
            Object value = entry.getValue();
            if (jsonParams.containsKey(keyParamName)) {
                inputsTwo.put(keyParamName, jsonParams.get(keyParamName));
            } else {
                inputsTwo.put(keyParamName, value);
            }
        }
        goldenInputs = inputsTwo;
    }
    // "Fix" the template if it has CR/LF (getting this from Oracle)
    String template = heatTemplate.getHeatTemplate();
    template = template.replaceAll("\r\n", "\n");
    // because we already checked for those.
    try {
        heatStack = heatU.updateStack(cloudSiteId, cloudOwner, tenantId, vfModuleName, template, goldenInputs, true, heatTemplate.getTimeoutMinutes(), newEnvironmentString, // heatEnvironmentString,
        nestedTemplatesChecked, heatFilesObjects);
    } catch (MsoException me) {
        me.addContext("UpdateVFModule");
        String error = "Update VFModule " + vfModuleType + " in " + cloudOwner + "/" + cloudSiteId + "/" + tenantId + ": " + me;
        logger.error(LoggingAnchor.EIGHT, MessageEnum.RA_UPDATE_VNF_ERR.toString(), vfModuleType, cloudOwner, cloudSiteId, tenantId, OPENSTACK, ErrorCode.DataError.getValue(), "Exception - " + error, me);
        throw new VnfException(me);
    }
    // Reach this point if updateStack is successful.
    // Populate remaining rollback info and response parameters.
    vfRollback.setVnfId(heatStack.getCanonicalName());
    vfRollback.setVnfCreated(true);
    outputs.value = copyStringOutputs(heatStack.getOutputs());
    rollback.value = vfRollback;
}
Also used : HashMap(java.util.HashMap) HeatEnvironment(org.onap.so.db.catalog.beans.HeatEnvironment) ArrayList(java.util.ArrayList) VnfNotFound(org.onap.so.adapters.vnf.exceptions.VnfNotFound) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonParseException(com.fasterxml.jackson.core.JsonParseException) VfModule(org.onap.so.db.catalog.beans.VfModule) VnfResource(org.onap.so.db.catalog.beans.VnfResource) VnfRollback(org.onap.so.openstack.beans.VnfRollback) VnfException(org.onap.so.adapters.vnf.exceptions.VnfException) HeatFiles(org.onap.so.db.catalog.beans.HeatFiles) StackInfo(org.onap.so.openstack.beans.StackInfo) MavenLikeVersioning(org.onap.so.db.catalog.utils.MavenLikeVersioning) MsoException(org.onap.so.openstack.exceptions.MsoException) MsoHeatNotFoundException(org.onap.so.openstack.exceptions.MsoHeatNotFoundException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) VnfException(org.onap.so.adapters.vnf.exceptions.VnfException) MsoException(org.onap.so.openstack.exceptions.MsoException) MsoHeatEnvironmentEntry(org.onap.so.openstack.utils.MsoHeatEnvironmentEntry) HeatTemplate(org.onap.so.db.catalog.beans.HeatTemplate) CloudSite(org.onap.so.db.catalog.beans.CloudSite) VfModuleCustomization(org.onap.so.db.catalog.beans.VfModuleCustomization) HashMap(java.util.HashMap) Map(java.util.Map) HeatTemplateParam(org.onap.so.db.catalog.beans.HeatTemplateParam)

Example 20 with CloudSite

use of org.onap.so.db.catalog.beans.CloudSite 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)

Aggregations

CloudSite (org.onap.so.db.catalog.beans.CloudSite)54 Test (org.junit.Test)25 MsoCloudSiteNotFound (org.onap.so.openstack.exceptions.MsoCloudSiteNotFound)13 CloudIdentity (org.onap.so.db.catalog.beans.CloudIdentity)12 MsoException (org.onap.so.openstack.exceptions.MsoException)12 OpenStackBaseException (com.woorea.openstack.base.client.OpenStackBaseException)9 StackInfo (org.onap.so.openstack.beans.StackInfo)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 BaseTest (org.onap.so.BaseTest)7 CatalogDbAdapterBaseTest (org.onap.so.adapters.catalogdb.CatalogDbAdapterBaseTest)6 Keystone (com.woorea.openstack.keystone.Keystone)5 Quantum (com.woorea.openstack.quantum.Quantum)5 HeatTemplate (org.onap.so.db.catalog.beans.HeatTemplate)5 OpenStackRequest (com.woorea.openstack.base.client.OpenStackRequest)4 Heat (com.woorea.openstack.heat.Heat)4 Tenant (com.woorea.openstack.keystone.model.Tenant)4 Network (com.woorea.openstack.quantum.model.Network)4 MsoTenant (org.onap.so.openstack.beans.MsoTenant)4 JsonParseException (com.fasterxml.jackson.core.JsonParseException)3