PreUpgradeCheckResourceProvider extends ReadOnlyResourceProvider { public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { final Set<Resource> resources = new HashSet<Resource>(); final Set<String> requestedIds = getRequestPropertyIds(request, predicate); final Set<Map<String, Object>> propertyMaps = getPropertyMaps(predicate); for (Map<String, Object> propertyMap: propertyMaps) { final String clusterName = propertyMap.get(UPGRADE_CHECK_CLUSTER_NAME_PROPERTY_ID).toString(); UpgradeType upgradeType = UpgradeType.ROLLING; if (propertyMap.containsKey(UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID)) { try { upgradeType = UpgradeType.valueOf(propertyMap.get(UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID).toString()); } catch(Exception e){ throw new SystemException(String.format("Property %s has an incorrect value of %s.", UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID, propertyMap.get(UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID))); } } final Cluster cluster; try { cluster = clustersProvider.get().getCluster(clusterName); } catch (AmbariException ambariException) { throw new NoSuchResourceException(ambariException.getMessage()); } String stackName = cluster.getCurrentStackVersion().getStackName(); String sourceStackVersion = cluster.getCurrentStackVersion().getStackVersion(); final PrereqCheckRequest upgradeCheckRequest = new PrereqCheckRequest(clusterName, upgradeType); upgradeCheckRequest.setSourceStackId(cluster.getCurrentStackVersion()); if (propertyMap.containsKey(UPGRADE_CHECK_REPOSITORY_VERSION_PROPERTY_ID)) { String repositoryVersionId = propertyMap.get(UPGRADE_CHECK_REPOSITORY_VERSION_PROPERTY_ID).toString(); RepositoryVersionEntity repositoryVersionEntity = repositoryVersionDAO.findByStackNameAndVersion(stackName, repositoryVersionId); upgradeCheckRequest.setRepositoryVersion(repositoryVersionId); upgradeCheckRequest.setTargetStackId(repositoryVersionEntity.getStackId()); } UpgradePack upgradePack = null; String preferredUpgradePackName = propertyMap.containsKey(UPGRADE_CHECK_UPGRADE_PACK_PROPERTY_ID) ? (String) propertyMap.get(UPGRADE_CHECK_UPGRADE_PACK_PROPERTY_ID) : null; try{ upgradePack = upgradeHelper.get().suggestUpgradePack(clusterName, sourceStackVersion, upgradeCheckRequest.getRepositoryVersion(), Direction.UPGRADE, upgradeType, preferredUpgradePackName); } catch (AmbariException e) { throw new SystemException(e.getMessage(), e); } if (upgradePack == null) { throw new SystemException(String.format("Upgrade pack not found for the target repository version %s", upgradeCheckRequest.getRepositoryVersion())); } List<AbstractCheckDescriptor> upgradeChecksToRun = upgradeCheckRegistry.getFilteredUpgradeChecks(upgradePack); upgradeCheckRequest.setPrerequisiteCheckConfig(upgradePack.getPrerequisiteCheckConfig()); try { Map<String, ServiceInfo> services = getManagementController().getAmbariMetaInfo().getServices(stackName, sourceStackVersion); List<AbstractCheckDescriptor> serviceLevelUpgradeChecksToRun = upgradeCheckRegistry.getServiceLevelUpgradeChecks(upgradePack, services); upgradeChecksToRun.addAll(serviceLevelUpgradeChecksToRun); } catch (ParentObjectNotFoundException parentNotFoundException) { LOG.error("Invalid stack version: " + stackName + "-" + sourceStackVersion, parentNotFoundException); } catch (AmbariException ambariException) { LOG.error("Unable to register all the custom prechecks from the services", ambariException); } catch (Exception e) { LOG.error("Failed to register custom prechecks for the services", e); } for (PrerequisiteCheck prerequisiteCheck : checkHelper.performChecks(upgradeCheckRequest, upgradeChecksToRun)) { final Resource resource = new ResourceImpl(Resource.Type.PreUpgradeCheck); setResourceProperty(resource, UPGRADE_CHECK_ID_PROPERTY_ID, prerequisiteCheck.getId(), requestedIds); setResourceProperty(resource, UPGRADE_CHECK_CHECK_PROPERTY_ID, prerequisiteCheck.getDescription(), requestedIds); setResourceProperty(resource, UPGRADE_CHECK_STATUS_PROPERTY_ID, prerequisiteCheck.getStatus(), requestedIds); setResourceProperty(resource, UPGRADE_CHECK_REASON_PROPERTY_ID, prerequisiteCheck.getFailReason(), requestedIds); setResourceProperty(resource, UPGRADE_CHECK_FAILED_ON_PROPERTY_ID, prerequisiteCheck.getFailedOn(), requestedIds); setResourceProperty(resource, UPGRADE_CHECK_FAILED_DETAIL_PROPERTY_ID,prerequisiteCheck.getFailedDetail(), requestedIds); setResourceProperty(resource, UPGRADE_CHECK_CHECK_TYPE_PROPERTY_ID, prerequisiteCheck.getType(), requestedIds); setResourceProperty(resource, UPGRADE_CHECK_CLUSTER_NAME_PROPERTY_ID, prerequisiteCheck.getClusterName(), requestedIds); setResourceProperty(resource, UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID, upgradeType, requestedIds); if (upgradeCheckRequest.getRepositoryVersion() != null) { setResourceProperty(resource, UPGRADE_CHECK_REPOSITORY_VERSION_PROPERTY_ID, upgradeCheckRequest.getRepositoryVersion(), requestedIds); } resources.add(resource); } } return resources; } PreUpgradeCheckResourceProvider(AmbariManagementController managementController); Set<Resource> getResources(Request request, Predicate predicate); static final String UPGRADE_CHECK_ID_PROPERTY_ID; static final String UPGRADE_CHECK_CHECK_PROPERTY_ID; static final String UPGRADE_CHECK_STATUS_PROPERTY_ID; static final String UPGRADE_CHECK_REASON_PROPERTY_ID; static final String UPGRADE_CHECK_FAILED_ON_PROPERTY_ID; static final String UPGRADE_CHECK_FAILED_DETAIL_PROPERTY_ID; static final String UPGRADE_CHECK_CHECK_TYPE_PROPERTY_ID; static final String UPGRADE_CHECK_CLUSTER_NAME_PROPERTY_ID; static final String UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID; static final String UPGRADE_CHECK_UPGRADE_PACK_PROPERTY_ID; static final String UPGRADE_CHECK_REPOSITORY_VERSION_PROPERTY_ID; static Set<String> propertyIds; @SuppressWarnings("serial")
  static Map<Type, String> keyPropertyIds; }
@Test public void testGetResources() throws Exception{ Injector injector = createInjector(); AmbariManagementController managementController = injector.getInstance(AmbariManagementController.class); Clusters clusters = injector.getInstance(Clusters.class); UpgradeHelper upgradeHelper = injector.getInstance(UpgradeHelper.class); RepositoryVersionDAO repoDao = injector.getInstance(RepositoryVersionDAO.class); RepositoryVersionEntity repo = createNiceMock(RepositoryVersionEntity.class); UpgradePack upgradePack = createNiceMock(UpgradePack.class); PrerequisiteCheckConfig config = createNiceMock(PrerequisiteCheckConfig.class); Cluster cluster = createNiceMock(Cluster.class); Service service = createNiceMock(Service.class); ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class); StackId currentStackId = createNiceMock(StackId.class); StackId targetStackId = createNiceMock(StackId.class); ServiceFactory serviceFactory = createNiceMock(ServiceFactory.class); AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class); Map<String, Service> allServiceMap = new HashMap<String, Service>(); allServiceMap.put("Service100", service); Map<String, ServiceInfo> allServiceInfoMap = new HashMap<String, ServiceInfo>(); allServiceInfoMap.put("Service100", serviceInfo); expect(managementController.getClusters()).andReturn(clusters).anyTimes(); expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes(); expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes(); expect(cluster.getServices()).andReturn(allServiceMap).anyTimes(); expect(cluster.getService("Service100")).andReturn(service).anyTimes(); expect(cluster.getCurrentStackVersion()).andReturn(currentStackId).anyTimes(); expect(currentStackId.getStackName()).andReturn("Stack100").anyTimes(); expect(currentStackId.getStackVersion()).andReturn("1.0").anyTimes(); expect(targetStackId.getStackName()).andReturn("Stack100").anyTimes(); expect(targetStackId.getStackVersion()).andReturn("1.1").anyTimes(); expect(repoDao.findByStackNameAndVersion("Stack100", "Repo100")).andReturn(repo).anyTimes(); expect(repo.getStackId()).andReturn(targetStackId).atLeastOnce(); expect(upgradeHelper.suggestUpgradePack("Cluster100", "1.0", "Repo100", Direction.UPGRADE, UpgradeType.NON_ROLLING, "upgrade_pack11")).andReturn(upgradePack); List<AbstractCheckDescriptor> upgradeChecksToRun = new LinkedList<AbstractCheckDescriptor>(); List<String> prerequisiteChecks = new LinkedList<String>(); prerequisiteChecks.add("org.apache.ambari.server.sample.checks.SampleServiceCheck"); expect(upgradePack.getPrerequisiteCheckConfig()).andReturn(config); expect(upgradePack.getPrerequisiteChecks()).andReturn(prerequisiteChecks).anyTimes(); expect(upgradePack.getTarget()).andReturn("1.1.*.*").anyTimes(); expect(ambariMetaInfo.getServices("Stack100", "1.0")).andReturn(allServiceInfoMap).anyTimes(); String checks = ClassLoader.getSystemClassLoader().getResource("checks").getPath(); expect(serviceInfo.getChecksFolder()).andReturn(new File(checks)); replay(managementController, clusters, cluster, service, serviceInfo, repoDao, repo, upgradeHelper, ambariMetaInfo, upgradePack, config, currentStackId, targetStackId, serviceFactory); ResourceProvider provider = getPreUpgradeCheckResourceProvider(managementController, injector); Request request = PropertyHelper.getReadRequest(new HashSet<String>()); PredicateBuilder builder = new PredicateBuilder(); Predicate predicate = builder.property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_CLUSTER_NAME_PROPERTY_ID).equals("Cluster100").and() .property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_UPGRADE_PACK_PROPERTY_ID).equals("upgrade_pack11").and() .property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID).equals(UpgradeType.NON_ROLLING).and() .property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_REPOSITORY_VERSION_PROPERTY_ID).equals("Repo100").toPredicate(); System.out.println("PreUpgradeCheckResourceProvider - " + provider); Set<Resource> resources = Collections.emptySet(); try { resources = provider.getResources(request, predicate); } catch (Exception e) { e.printStackTrace(); } Assert.assertEquals(1, resources.size()); for (Resource resource : resources) { String id = (String) resource.getPropertyValue(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_ID_PROPERTY_ID); Assert.assertEquals("SAMPLE_SERVICE_CHECK", id); String description = (String) resource.getPropertyValue(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_CHECK_PROPERTY_ID); Assert.assertEquals("Sample service check description.", description); PrereqCheckStatus status = (PrereqCheckStatus) resource.getPropertyValue(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_STATUS_PROPERTY_ID); Assert.assertEquals(PrereqCheckStatus.FAIL, status); String reason = (String) resource.getPropertyValue(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_REASON_PROPERTY_ID); Assert.assertEquals("Sample service check always fails.", reason); PrereqCheckType checkType = (PrereqCheckType) resource.getPropertyValue(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_CHECK_TYPE_PROPERTY_ID); Assert.assertEquals(PrereqCheckType.HOST, checkType); String clusterName = (String) resource.getPropertyValue(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_CLUSTER_NAME_PROPERTY_ID); Assert.assertEquals("Cluster100", clusterName); UpgradeType upgradeType = (UpgradeType) resource.getPropertyValue(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID); Assert.assertEquals(UpgradeType.NON_ROLLING, upgradeType); } verify(managementController, clusters, cluster, service, serviceInfo, repoDao, repo, upgradeHelper, ambariMetaInfo, upgradePack, config, currentStackId, targetStackId, serviceFactory); }