Search in sources :

Example 11 with SoftwareVersion

use of com.emc.storageos.coordinator.client.model.SoftwareVersion in project coprhd-controller by CoprHD.

the class RemoteRepository method finInstallableVersionsFromCatalogRepo.

private List<SoftwareVersion> finInstallableVersionsFromCatalogRepo(String catalogString, List<SoftwareVersion> newVersionList, List<SoftwareVersion> localVersions) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, InvalidSoftwareVersionException, MalformedURLException, RemoteRepositoryException {
    List<SoftwareVersion> validVersions = new ArrayList<SoftwareVersion>();
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(new InputSource(new StringReader(catalogString)));
    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList fileList = (NodeList) xPath.compile("//File").evaluate(doc, XPathConstants.NODESET);
    OUTER: for (int fileItr = 0; fileItr < fileList.getLength(); fileItr++) {
        Node fileNode = fileList.item(fileItr);
        Element element = (Element) fileNode;
        Node nameNode = element.getAttributeNode("Name");
        if (null != nameNode) {
            String fileName = nameNode.getNodeValue();
            if (fileName.endsWith(SOFTWARE_IMAGE_SUFFIX)) {
                String fileVersion = fileName.replace(SOFTWARE_IMAGE_SUFFIX, "");
                SoftwareVersion tempVersion = new SoftwareVersion(fileVersion);
                if (newVersionList.contains(tempVersion)) {
                    Node catalogInfoNode = element.getAttributeNode("CatalogInfo");
                    String catalogInfo = catalogInfoNode.getNodeValue();
                    if (catalogInfo.equals("")) {
                        // Ignore the version that doesn't have the upgradeFromVersion metadata
                        continue;
                    }
                    // key-value pairs are separated by comma
                    String upgradeFromInfoRaw = catalogInfo.split(",")[0];
                    // only need the value
                    String upgradeFromInfo = upgradeFromInfoRaw.split("=")[1];
                    for (String versionStr : upgradeFromInfo.split(";")) {
                        // versions are separated by semicolon
                        for (SoftwareVersion v : localVersions) {
                            if (new SoftwareVersion(versionStr).weakEquals(v)) {
                                // wild card is used in the upgradeFromVersions list,
                                // need use weakEquals
                                validVersions.add(tempVersion);
                                continue OUTER;
                            }
                        }
                    }
                }
            }
        }
    }
    return validVersions;
}
Also used : XPath(javax.xml.xpath.XPath) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) SoftwareVersion(com.emc.storageos.coordinator.client.model.SoftwareVersion) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 12 with SoftwareVersion

use of com.emc.storageos.coordinator.client.model.SoftwareVersion in project coprhd-controller by CoprHD.

the class SyncInfoBuilder method findToRemove.

public static List<SoftwareVersion> findToRemove(final List<SoftwareVersion> localVersions, final SoftwareVersion localCurrent, final SoftwareVersion remoteCurrent, final SoftwareVersion toInstall, final boolean force) throws IOException {
    List<SoftwareVersion> sortedList = new ArrayList<SoftwareVersion>(localVersions);
    Collections.sort(sortedList);
    List<SoftwareVersion> toRemove = new ArrayList<SoftwareVersion>();
    if (force || sortedList.size() > MAX_SOFTWARE_VERSIONS) {
        for (SoftwareVersion v : sortedList) {
            if (!v.equals(localCurrent) && !v.equals(remoteCurrent) && isRemovable(sortedList, v)) {
                toRemove.add(v);
            }
        }
    }
    return toRemove;
}
Also used : SoftwareVersion(com.emc.storageos.coordinator.client.model.SoftwareVersion)

Example 13 with SoftwareVersion

use of com.emc.storageos.coordinator.client.model.SoftwareVersion in project coprhd-controller by CoprHD.

the class SyncInfoBuilder method isRemovable.

// Method to test if a version is removable.
// The first or the last version in the list is removable.
// Version that cannot be upgraded from its previous versions is removable.
// Version that cannot upgrade to its following versions is removable.
// Version a in a list x can upgrade to a list of versions. Those versions form a list y. If any version from list y have at least one
// version other than target
// version to upgrade from, the version a is removable otherwise it's not removable
// Note that the maximum versions allowed is 4.
private static boolean isRemovable(List<SoftwareVersion> versions, SoftwareVersion version) throws IOException {
    int index = versions.indexOf(version);
    int size = versions.size();
    if (index == 0 || index == size - 1) {
        // if the tested version is the first or last version in the list it will not cause further inconsistency for sure
        return true;
    }
    // Get lists of versions that target version can upgrade from and upgrade to
    List<SoftwareVersion> upgradeFromVersionList = new ArrayList<SoftwareVersion>();
    List<SoftwareVersion> upgradeToVersionList = new ArrayList<SoftwareVersion>();
    for (int i = 0; i < index; i++) {
        SoftwareVersion tempVersion = versions.get(i);
        if (tempVersion.isSwitchableTo(version)) {
            upgradeFromVersionList.add(tempVersion);
        }
    }
    for (int i = index + 1; i < size; i++) {
        SoftwareVersion tempVersion = versions.get(i);
        if (version.isSwitchableTo(tempVersion)) {
            upgradeToVersionList.add(tempVersion);
        }
    }
    if (upgradeFromVersionList.isEmpty() || upgradeToVersionList.isEmpty()) {
        // If no version can upgrade to the target version or no version can be upgrade from the target version, target
        return true;
    // version is removable
    }
    OUTLOOP: for (SoftwareVersion v : upgradeToVersionList) {
        int position = versions.indexOf(v);
        for (int i = 0; i < position; i++) {
            if (i != index && versions.get(i).isSwitchableTo(v)) {
                continue OUTLOOP;
            // If this version can be upgrade from a version other than the target continue to test the next version
            }
        }
        // If this version doesn't have any version to upgrade from, false should be returned
        return false;
    }
    // All versions from the upgradeToVersionList have versions other than the target version to upgrade from, true should
    return true;
// be returned
}
Also used : SoftwareVersion(com.emc.storageos.coordinator.client.model.SoftwareVersion)

Example 14 with SoftwareVersion

use of com.emc.storageos.coordinator.client.model.SoftwareVersion in project coprhd-controller by CoprHD.

the class SyncInfoBuilder method getTargetSyncInfo.

/**
 * Select a SoftwareVersion from the leader repository list that can be installed,
 * and a list of versions that have to be removed from the local repository.
 *
 * @return SyncInfo - An immutable Selector object with a SoftwareVersion toInstall and
 *         a list of SoftwareVersions to toRemove. The toInstall might be null.
 *         The toRemove might be empty (but not null).
 * @throws IOException
 * @see SyncInfo
 */
public static SyncInfo getTargetSyncInfo(final RepositoryInfo local, final RepositoryInfo target) {
    final SoftwareVersion localCurrent = (local != null) ? local.getCurrentVersion() : null;
    final List<SoftwareVersion> localVersions = (local != null && local.getVersions() != null) ? local.getVersions() : new ArrayList<SoftwareVersion>();
    final SoftwareVersion targetCurrent = (target != null) ? target.getCurrentVersion() : null;
    final List<SoftwareVersion> targetVersions = (target != null && target.getVersions() != null) ? target.getVersions() : new ArrayList<SoftwareVersion>();
    // Basic validations
    if (localCurrent == null || localVersions == null || localVersions.isEmpty()) {
        log.error("inconsistent local repository state");
        return new SyncInfo();
    }
    if (targetCurrent == null || targetVersions == null || targetVersions.isEmpty()) {
        log.error("inconsistent target repository state");
        return new SyncInfo();
    }
    final String args = MessageFormat.format("local: [{0}/{1}] current={2} versions={3} " + "remote: current {4} versions={5}", localVersions.size(), MAX_SOFTWARE_VERSIONS, localCurrent, Strings.repr(localVersions), targetCurrent, Strings.repr(targetVersions));
    final String prefix = "getTargetSyncInfo(): " + args + " : ";
    // Set force to true as we already did the version check during set_target call
    if (targetCurrent != null && !targetCurrent.equals(localCurrent) && !localVersions.contains(targetCurrent)) {
        try {
            if (!localCurrent.isSwitchableTo(targetCurrent)) {
                // we are not expecting this case - don't change anything, wait for help!
                log.error("{} local current not upgradable to current on leader", prefix);
                return new SyncInfo();
            }
        } catch (IOException e) {
            log.error("Error occured when extracting version metadata from the image file", e);
        }
    }
    // To Install - what version do the target state have that I don't have
    List<SoftwareVersion> toInstallCandidates = new ArrayList<SoftwareVersion>(targetVersions);
    Collections.sort(toInstallCandidates);
    for (SoftwareVersion version : toInstallCandidates) {
        if (localVersions.contains(version)) {
            continue;
        }
        return new SyncInfo(version, new ArrayList<SoftwareVersion>());
    }
    // if we are here, we didn't find anything to install - see if we need to remove any
    // To Remove - what versions do I have that the target does not have
    // Set force to true as we already did the version check during set_target call
    List<SoftwareVersion> toRemoveCandidates = new ArrayList<SoftwareVersion>();
    try {
        toRemoveCandidates = findToRemove(localVersions, localCurrent, null, null, true);
    } catch (IOException e) {
        log.error("Error occured when extracting version metadata from the image file", e);
    }
    List<SoftwareVersion> toRemove = new ArrayList<SoftwareVersion>();
    for (SoftwareVersion version : toRemoveCandidates) {
        if (targetVersions.contains(version)) {
            continue;
        }
        toRemove.add(version);
        return new SyncInfo(toRemove);
    }
    log.info(prefix + " Nothing to do.");
    return new SyncInfo();
}
Also used : SoftwareVersion(com.emc.storageos.coordinator.client.model.SoftwareVersion) IOException(java.io.IOException)

Example 15 with SoftwareVersion

use of com.emc.storageos.coordinator.client.model.SoftwareVersion in project coprhd-controller by CoprHD.

the class UpgradeManager method syncWithRemote.

private boolean syncWithRemote(final RepositoryInfo localInfo, final RepositoryInfo targetInfo, final SyncInfo syncinfo) throws RemoteRepositoryException, LocalRepositoryException {
    // Step1 - if something to install, install
    if (syncinfo.getToInstall() != null && !syncinfo.getToInstall().isEmpty()) {
        final SoftwareVersion toInstall = syncinfo.getToInstall().get(0);
        File image = null;
        if (toInstall != null && (image = getRemoteImage(toInstall)) == null) {
            return false;
        }
        if (image != null) {
            try {
                localRepository.installImage(image);
            } finally {
                image.delete();
            }
        }
    }
    // Step2 - if something to remove, remove
    if (syncinfo.getToRemove() != null && !syncinfo.getToRemove().isEmpty()) {
        for (SoftwareVersion v : syncinfo.getToRemove()) {
            localRepository.removeVersion(v);
        }
    }
    return true;
}
Also used : SoftwareVersion(com.emc.storageos.coordinator.client.model.SoftwareVersion) File(java.io.File)

Aggregations

SoftwareVersion (com.emc.storageos.coordinator.client.model.SoftwareVersion)44 RepositoryInfo (com.emc.storageos.coordinator.client.model.RepositoryInfo)12 APIException (com.emc.storageos.svcs.errorhandling.resources.APIException)10 InvalidSoftwareVersionException (com.emc.storageos.coordinator.exceptions.InvalidSoftwareVersionException)9 RemoteRepositoryException (com.emc.storageos.systemservices.exceptions.RemoteRepositoryException)9 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)8 Path (javax.ws.rs.Path)8 LocalRepositoryException (com.emc.storageos.systemservices.exceptions.LocalRepositoryException)7 IOException (java.io.IOException)7 CheckPermission (com.emc.storageos.security.authorization.CheckPermission)6 ServiceUnavailableException (com.emc.storageos.svcs.errorhandling.resources.ServiceUnavailableException)6 CoordinatorClientException (com.emc.storageos.systemservices.exceptions.CoordinatorClientException)6 ClusterInfo (com.emc.vipr.model.sys.ClusterInfo)5 File (java.io.File)5 Produces (javax.ws.rs.Produces)5 InputStream (java.io.InputStream)4 BadRequestException (com.emc.storageos.svcs.errorhandling.resources.BadRequestException)3 POST (javax.ws.rs.POST)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3