Code coverage report for app/controllers/wizard/step4_controller.js

Statements: 88.55% (147 / 166)      Branches: 71.25% (57 / 80)      Functions: 87.76% (43 / 49)      Lines: 88.55% (147 / 166)      Ignored: none     

All files » app/controllers/wizard/ » step4_controller.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565    1                                     1   1                             2       2               2                             2 2 4               43600 35245                   58                 57 57 45 2               43 43 43                       3     3 2 2 2 1 1                 2                         45 45     45           45 45 45 21 21 21 21   45 45 45 45 45 45 36 36 36   9   45                     185 185 185 15   170 170 170                         101 101 100 80               20 20 20                           138 133 133   5                   39                         5 5 3   5     5                                   133                               133 133                 55 55 55 55 1166 66     55                 55 55 55 55 55 55 11 11 22         11             44 44 44                         61 61 61 61 175   61 61       61 10   61     61 37                             6 20 20 4 4 2                                                     2 2         2 3   2 2                                   23 23 23     23 3 3 3           23                                                     18 18                     1 1     1 1   1     1 1   1                         4 4         1 1     1 1   1     1 1   1          
'use strict';
 
;require.register("controllers/wizard/step4_controller", function (exports, require, module) {
  /**
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
 
  var App = require('app');
 
  App.WizardStep4Controller = Em.ArrayController.extend({
 
    name: 'wizardStep4Controller',
 
    /**
     * List of Services
     * @type {Object[]}
     */
    content: [],
 
    /**
     * Check / Uncheck 'Select All' checkbox with one argument; Check / Uncheck all other checkboxes with more arguments
     * @type {bool}
     */
    isAllChecked: function (key, value) {
      Iif (arguments.length > 1) {
        this.filterProperty('isDisabled', false).filterProperty('isDFS', false).setEach('isSelected', value);
        return value;
      }
      return this.filterProperty('isInstalled', false).filterProperty('isHiddenOnSelectServicePage', false).filterProperty('isDFS', false).everyProperty('isSelected', true);
    }.property('@each.isSelected'),
 
    /**
     * Is Submit button disabled
     * @type {bool}
     */
    isSubmitDisabled: function () {
      return this.filterProperty('isSelected', true).filterProperty('isInstalled', false).length === 0 || App.get('router.btnClickInProgress');
    }.property('@each.isSelected', 'App.router.btnClickInProgress'),
 
    /**
     * List of validation errors. Look to #createError method for information
     * regarding object structure.
     *
     * @type {Object[]}
     */
    errorStack: [],
 
    /**
     * Services which are HDFS compatible
     */
    fileSystems: function () {
      var fileSystems = this.filterProperty('isDFS', true);;
      return fileSystems.map(function (fs) {
        return App.FileSystem.create({ content: fs, services: fileSystems });
      });
    }.property('@each.isDFS'),
 
    /**
     * Drop errorStack content on selected state changes.
     */
    clearErrors: function () {
      if (!this.get('errorStack').someProperty('isAccepted', false)) {
        this.set('errorStack', []);
      }
    }.observes('@each.isSelected'),
 
    /**
     * Check if multiple distributed file systems were selected
     * @return {bool}
     * @method multipleDFSs
     */
    multipleDFSs: function multipleDFSs() {
      return this.filterProperty('isDFS', true).filterProperty('isSelected', true).length > 1;
    },
 
    /**
     * Warn user if he tries to install Spark with HDP 2.2
     * @param {function} callback
     * @method sparkValidation
     */
    sparkValidation: function sparkValidation(callback) {
      var sparkService = this.findProperty('serviceName', 'SPARK');
      if (sparkService && !sparkService.get('isInstalled') && App.get('currentStackName') === 'HDP' && App.get('currentStackVersionNumber') === '2.2') {
        if (sparkService.get('isSelected')) {
          this.addValidationError({
            id: 'sparkWarning',
            type: 'WARNING',
            callback: this.sparkWarningPopup,
            callbackParams: [callback]
          });
        } else {
          //Spark is selected, remove the Spark error from errorObject array
          var sparkError = this.get('errorStack').filterProperty('id', "sparkWarning");
          Eif (sparkError) {
            this.get('errorStack').removeObject(sparkError[0]);
          }
        }
      }
    },
 
    /**
     * Onclick handler for <code>Next</code> button.
     * Disable 'Next' button while it is already under process. (using Router's property 'nextBtnClickInProgress')
     * @method submit
     */
    submit: function submit() {
      Iif (App.get('router.nextBtnClickInProgress')) {
        return;
      }
      if (!this.get('isSubmitDisabled')) {
        this.unSelectServices();
        this.setGroupedServices();
        if (this.validate()) {
          this.set('errorStack', []);
          App.router.send('next');
        }
      }
    },
 
    /**
     * Set isSelected based on property doNotShowAndInstall
     */
    unSelectServices: function unSelectServices() {
      this.filterProperty('isSelected', true).filterProperty('doNotShowAndInstall', true).setEach('isSelected', false);
    },
 
    /**
     * Check if validation passed:
     *  - required file system services selected
     *  - dependencies between services
     *  - monitoring services selected (not required)
     *
     * @return {Boolean}
     * @method validate
     */
    validate: function validate() {
      var result;
      var self = this;
 
      // callback function to reset `isAccepted` needs to be called everytime when a popup from errorStack is dismissed/proceed by user action
      var callback = function callback(id) {
        var check = self.get('errorStack').findProperty('id', id);
        if (check) {
          check.isAccepted = true;
        }
      };
      this.serviceDependencyValidation(callback);
      this.fileSystemServiceValidation(callback);
      if (this.get('wizardController.name') === 'installerController') {
        this.serviceValidation(callback, 'AMBARI_METRICS', 'ambariMetricsCheck');
        this.serviceValidation(callback, 'SMARTSENSE', 'smartSenseCheck');
        this.serviceValidation(callback, 'RANGER', 'rangerCheck');
        this.serviceValidation(callback, 'ATLAS', 'atlasCheck');
      }
      this.dependentServiceValidation('RANGER', 'AMBARI_INFRA_SOLR', 'ambariRangerInfraCheck', callback);
      this.dependentServiceValidation('ATLAS', 'AMBARI_INFRA_SOLR', 'ambariAtlasInfraCheck', callback);
      this.dependentServiceValidation('ATLAS', 'HBASE', 'ambariAtlasHbaseCheck', callback);
      this.dependentServiceValidation('LOGSEARCH', 'AMBARI_INFRA_SOLR', 'ambariLogsearchCheck', callback);
      this.sparkValidation(callback);
      if (!!this.get('errorStack').filterProperty('isShown', false).length) {
        var firstError = this.get('errorStack').findProperty('isShown', false);
        this.showError(firstError);
        result = false;
      } else {
        result = true;
      }
      return result;
    },
 
    /**
     * display validation warning if dependent service not selected
     * @param {string} selectedService
     * @param {string} dependentService
     * @param {string} checkId
     * @param {Function} callback
     */
    dependentServiceValidation: function dependentServiceValidation(selectedService, dependentService, checkId, callback) {
      var selected = this.findProperty('serviceName', selectedService);
      var dependent = this.findProperty('serviceName', dependentService);
      if (selected && selected.get('isSelected') && dependent && !dependent.get('isSelected')) {
        this.serviceValidation(callback, dependentService, checkId);
      } else {
        var unNeededError = this.get('errorStack').filterProperty('id', checkId);
        Eif (unNeededError) {
          this.get('errorStack').removeObject(unNeededError[0]);
        }
      }
    },
 
    /**
     * Check whether user selected service to install and go to next step
     * @param callback {Function}
     * @param serviceName {string}
     * @param id {string}
     * @method serviceValidation
     */
    serviceValidation: function serviceValidation(callback, serviceName, id) {
      var service = this.findProperty('serviceName', serviceName);
      if (service) {
        if (!service.get('isSelected')) {
          this.addValidationError({
            id: id,
            type: 'WARNING',
            callback: this.serviceCheckPopup,
            callbackParams: [callback]
          });
        } else {
          //metrics is selected, remove the metrics error from errorObject array
          var metricsError = this.get('errorStack').filterProperty('id', id);
          Eif (metricsError) {
            this.get('errorStack').removeObject(metricsError[0]);
          }
        }
      }
    },
 
    /**
     * Create error and push it to stack.
     *
     * @param {Object} errorObject - look to #createError
     * @return {Boolean}
     * @method addValidationError
     */
    addValidationError: function addValidationError(errorObject) {
      if (!this.get('errorStack').someProperty('id', errorObject.id)) {
        this.get('errorStack').push(this.createError(errorObject));
        return true;
      }
      return false;
    },
 
    /**
     * Show current error by passed error object.
     *
     * @param {Object} errorObject
     * @method showError
     */
    showError: function showError(errorObject) {
      return errorObject.callback.apply(errorObject.callbackContext, errorObject.callbackParams.concat(errorObject.id));
    },
 
    /**
     * Default primary button("Ok") callback for warning popups.
     *  Change isShown state for last shown error.
     *  Call #submit() method.
     *
     *  @param {function} callback
     *  @param {string} id
     *  @method onPrimaryPopupCallback
     */
    onPrimaryPopupCallback: function onPrimaryPopupCallback(callback, id) {
      var firstError = this.get('errorStack').findProperty('isShown', false);
      if (firstError) {
        firstError.isShown = true;
      }
      Iif (callback) {
        callback(id);
      }
      this.submit();
    },
 
    /**
     * Create error object with passed options.
     * Available options:
     *  id - {String}
     *  type - {String}
     *  isShowed - {Boolean}
     *  callback - {Function}
     *  callbackContext
     *  callbackParams - {Array}
     *
     * @param {Object} opt
     * @return {Object}
     * @method createError
     */
    createError: function createError(opt) {
      var options = {
        // {String} error identifier
        id: '',
        // {String} type of error CRITICAL|WARNING
        type: 'CRITICAL',
        // {Boolean} error was shown
        isShown: false,
        // {Boolean} error was accepted by user
        isAccepted: false,
        // {Function} callback to execute
        callback: null,
        // context which execute from
        callbackContext: this,
        // {Array} params applied to callback
        callbackParams: []
      };
      $.extend(options, opt);
      return options;
    },
 
    /**
     * Checks if a filesystem is present in the Stack
     *
     * @method isDFSStack
     */
    isDFSStack: function isDFSStack() {
      var bDFSStack = false;
      var dfsServices = ['HDFS', 'GLUSTERFS'];
      var availableServices = this.filterProperty('isInstalled', false);
      availableServices.forEach(function (service) {
        if (dfsServices.contains(service.get('serviceName')) || service.get('serviceType') == 'HCFS') {
          bDFSStack = true;
        }
      }, this);
      return bDFSStack;
    },
 
    /**
     * Checks if a filesystem is selected and only one filesystem is selected
     * @param {function} callback
     * @method isFileSystemCheckFailed
     */
    fileSystemServiceValidation: function fileSystemServiceValidation(callback) {
      Eif (this.isDFSStack()) {
        var primaryDFS = this.findProperty('isPrimaryDFS', true);
        Eif (primaryDFS) {
          var primaryDfsDisplayName = primaryDFS.get('displayNameOnSelectServicePage');
          var primaryDfsServiceName = primaryDFS.get('serviceName');
          if (this.multipleDFSs()) {
            var dfsServices = this.filterProperty('isDFS', true).filterProperty('isSelected', true).mapProperty('serviceName');
            var services = dfsServices.map(function (item) {
              return {
                serviceName: item,
                selected: item === primaryDfsServiceName
              };
            });
            this.addValidationError({
              id: 'multipleDFS',
              callback: this.needToAddServicePopup,
              callbackParams: [services, 'multipleDFS', primaryDfsDisplayName, callback]
            });
          } else {
            //if multiple DFS are not selected, remove the related error from the error array
            var fsError = this.get('errorStack').filterProperty('id', "multipleDFS");
            Eif (fsError) {
              this.get('errorStack').removeObject(fsError[0]);
            }
          }
        }
      }
    },
 
    /**
     * Checks if a dependent service is selected without selecting the main service.
     * @param {function} callback
     * @method serviceDependencyValidation
     */
    serviceDependencyValidation: function serviceDependencyValidation(callback) {
      var selectedServices = this.filterProperty('isSelected', true);
      var availableServices = this.get('content');
      var missingDependencies = [];
      selectedServices.forEach(function (service) {
        service.collectMissingDependencies(selectedServices, availableServices, missingDependencies);
      });
      this.cleanExistingServiceCheckErrors();
      this.addServiceCheckErrors(missingDependencies, callback);
    },
 
    cleanExistingServiceCheckErrors: function cleanExistingServiceCheckErrors() {
      var existingServiceCheckErrors = this.get('errorStack').filter(function (error) {
        return error.id.startsWith('serviceCheck_');
      });
      this.get('errorStack').removeObjects(existingServiceCheckErrors);
    },
    addServiceCheckErrors: function addServiceCheckErrors(missingDependencies, callback) {
      for (var i = 0; i < missingDependencies.length; i++) {
        this.addValidationError({
          id: 'serviceCheck_' + missingDependencies[i].get('serviceName'),
          callback: this.needToAddMissingDependency,
          callbackParams: [missingDependencies[i], 'serviceCheck', callback]
        });
      }
    },
 
 
    /**
     * Select co hosted services which not showed on UI.
     *
     * @method setGroupedServices
     */
    setGroupedServices: function setGroupedServices() {
      this.forEach(function (service) {
        var coSelectedServices = service.get('coSelectedServices');
        coSelectedServices.forEach(function (groupedServiceName) {
          var groupedService = this.findProperty('serviceName', groupedServiceName);
          if (groupedService.get('isSelected') !== service.get('isSelected')) {
            groupedService.set('isSelected', service.get('isSelected'));
          }
        }, this);
      }, this);
    },
 
    /**
     * Select/deselect services
     * @param {object[]|object} services array of objects
     *  <code>
     *    [
     *      {
     *        service: 'HDFS',
     *        selected: true
     *      },
     *      ....
     *    ]
     *  </code>
     * @param {string} i18nSuffix
     * @param {string} serviceName
     * @param {function} callback
     * @param {string} id
     * @return {App.ModalPopup}
     * @method needToAddServicePopup
     */
 
    needToAddServicePopup: function needToAddServicePopup(services, i18nSuffix, serviceName, callback, id) {
      var self = this;
      return App.ModalPopup.show({
        'data-qa': 'need-add-service-confirmation-modal',
        header: Em.I18n.t('installer.step4.' + i18nSuffix + '.popup.header').format(serviceName),
        body: Em.I18n.t('installer.step4.' + i18nSuffix + '.popup.body').format(serviceName, serviceName),
        onPrimary: function onPrimary() {
          Em.makeArray(services).forEach(function (service) {
            self.findProperty('serviceName', service.serviceName).set('isSelected', service.selected);
          });
          self.onPrimaryPopupCallback(callback, id);
          this.hide();
        },
        onSecondary: function onSecondary() {
          if (callback) {
            callback(id);
          }
          this._super();
        },
        onClose: function onClose() {
          if (callback) {
            callback(id);
          }
          this._super();
        }
      });
    },
 
    needToAddMissingDependency: function needToAddMissingDependency(missingDependency, i18nSuffix, callback, id) {
      var self = this;
      var displayName = missingDependency.get('displayName');
      Iif (missingDependency.get('hasMultipleOptions')) {
        return this.showDependencyPopup(id, Em.I18n.t('installer.step4.' + i18nSuffix + '.popup.header').format(displayName), Em.I18n.t('installer.step4.' + i18nSuffix + '.popup.body.multiOptions').format(displayName, missingDependency.get('displayOptions')), callback);
      } else {
        return this.showDependencyPopup(id, Em.I18n.t('installer.step4.' + i18nSuffix + '.popup.header').format(displayName), Em.I18n.t('installer.step4.' + i18nSuffix + '.popup.body').format(displayName, missingDependency.get('serviceName')), callback, function () {
          missingDependency.selectFirstCompatible();
          self.onPrimaryPopupCallback(callback, id);
          this.hide();
        });
      }
    },
 
    showDependencyPopup: function showDependencyPopup(id, header, body, callback, primaryAction) {
      return App.ModalPopup.show({
        'data-qa': 'need-add-service-confirmation-modal',
        header: header,
        body: body,
        onPrimary: primaryAction || function () {
          this.onClose();
        },
        onSecondary: function onSecondary() {
          this.onClose();
        },
        onClose: function onClose() {
          if (callback) {
            callback(id);
          }
          this._super();
        }
      });
    },
 
    /**
     * Show popup with info about not selected service
     * @param {function} callback
     * @param {string} id
     * @return {App.ModalPopup}
     * @method serviceCheckPopup
     */
    serviceCheckPopup: function serviceCheckPopup(callback, id) {
      var self = this;
      return App.ModalPopup.show({
        header: Em.I18n.t('installer.step4.limitedFunctionality.popup.header'),
        //body: Em.I18n.t('installer.step4.' + id + '.popup.body'),
        bodyClass: Em.View.extend({
          serviceHeader: Em.I18n.t('installer.step4.' + id + '.popup.header'),
          serviceBody: Em.I18n.t('installer.step4.' + id + '.popup.body'),
          templateName: require('templates/wizard/step4/step4_service_validation_popup')
        }),
        primary: Em.I18n.t('common.proceedAnyway'),
        primaryClass: 'btn-warning',
        onPrimary: function onPrimary() {
          self.onPrimaryPopupCallback(callback);
          this.hide();
        },
        onSecondary: function onSecondary() {
          Eif (callback) {
            callback(id);
          }
          this._super();
        },
        onClose: function onClose() {
          Eif (callback) {
            callback(id);
          }
          this._super();
        }
      });
    },
 
    /**
     * Show popup with Spark installation warning
     * @param {function} callback
     * @param {string} id
     * @return {App.ModalPopup}
     * @method sparkWarningPopup
     */
    sparkWarningPopup: function sparkWarningPopup(callback, id) {
      var self = this;
      return App.ModalPopup.show({
        header: Em.I18n.t('common.warning'),
        body: Em.I18n.t('installer.step4.sparkWarning.popup.body'),
        primary: Em.I18n.t('common.proceed'),
        onPrimary: function onPrimary() {
          self.onPrimaryPopupCallback(callback);
          this.hide();
        },
        onSecondary: function onSecondary() {
          Eif (callback) {
            callback(id);
          }
          this._super();
        },
        onClose: function onClose() {
          Eif (callback) {
            callback(id);
          }
          this._super();
        }
      });
    }
  });
});