<commit_before>/**
 * 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.MainAdminSecurityDisableController = Em.Controller.extend({

  name: 'mainAdminSecurityDisableController',
  secureMapping: require('data/secure_mapping'),
  configMapping: App.config.get('configMapping'),
  secureProperties: require('data/secure_properties').configProperties.slice(0),
  stages: [],
  configs: [],
  noOfWaitingAjaxCalls: 0,
  secureServices: [],
  serviceConfigTags: [],
  globalProperties: [],
  hasHostPopup: true,
  services: [],
  serviceTimestamp: null,
  isSubmitDisabled: true,
  totalSteps: 3,

  clearStep: function () {
    this.get('stages').clear();
    this.get('secureServices').clear();
    this.get('serviceConfigTags').clear();
  },

  retry: function () {
    var failedStage = this.get('stages').findProperty('isError', true);
    if (failedStage) {
      failedStage.set('isStarted', false);
      failedStage.set('isError', false);
      this.startStage(failedStage);
    }
  },

  loadStep: function () {
    this.clearStep();
    var stages = App.db.getSecurityDeployStages();
    if (stages && stages.length > 0) {
      stages.forEach(function (_stage, index) {
        stages[index] = App.Poll.create(_stage);
      }, this);
      if (stages.someProperty('isError', true)) {
        this.get('stages').pushObjects(stages);
        this.loadSecureServices();
        this.addObserver('stages.@each.isSuccess', this.onCompleteStage);
        return;
      } else if (stages.filterProperty('isStarted', true).someProperty('isCompleted', false)) {
        var runningStage = stages.filterProperty('isStarted', true).findProperty('isCompleted', false);
        runningStage.set('isStarted', false);
        this.get('stages').pushObjects(stages);
      } else {
        this.get('stages').pushObjects(stages);
      }
    } else {
      this.loadStages();
      this.addInfoToStages();
      var runningOperations = App.router.get('backgroundOperationsController.services').filterProperty('isRunning');
      var stopAllOperation = runningOperations.findProperty('name', 'Stop All Services');
      var stopStage = this.get('stages').findProperty('name', 'STOP_SERVICES');
      if (stopStage.get('name') === 'STOP_SERVICES' && stopAllOperation) {
        stopStage.set('requestId', stopAllOperation.get('id'));
      }
    }
    this.loadSecureServices();
    this.addObserver('stages.@each.isSuccess', this.onCompleteStage);
    this.moveToNextStage();
  },


  loadStages: function () {
    this.get('stages').pushObjects([
      App.Poll.create({stage: 'stage2', label: Em.I18n.translations['admin.addSecurity.apply.stage2'], isPolling: true, name: 'STOP_SERVICES'}),
      App.Poll.create({stage: 'stage3', label: Em.I18n.translations['admin.addSecurity.apply.stage3'], isPolling: false, name: 'APPLY_CONFIGURATIONS'}),
      App.Poll.create({stage: 'stage4', label: Em.I18n.translations['admin.addSecurity.apply.stage4'], isPolling: true, name: 'START_SERVICES'})
    ]);
  },

  enableSubmit: function () {
    if (this.get('stages').someProperty('isError', true) || this.get('stages').everyProperty('isSuccess', true)) {
      this.set('isSubmitDisabled', false);
    } else {
      this.set('isSubmitDisabled', true);
    }
  }.observes('stages.@each.isCompleted'),

  startStage: function (currentStage) {
    if (this.get('stages').length === this.totalSteps) {
      if (!currentStage) {
        var startedStages = this.get('stages').filterProperty('isStarted', true);
        currentStage = startedStages.findProperty('isCompleted', false);
      }
      if (currentStage && currentStage.get('isPolling') === true) {
        currentStage.set('isStarted', true);
        currentStage.start();
      } else if (currentStage && currentStage.get('stage') === 'stage3') {
        currentStage.set('isStarted', true);
        if (App.testMode) {
          currentStage.set('isError', false);
          currentStage.set('isCompleted', true);
        } else {
          this.loadClusterConfigs();
        }
      }
    }
  },

  onCompleteStage: function () {
    if (this.get('stages').length === this.totalSteps) {
      var index = this.get('stages').filterProperty('isSuccess', true).length;
      if (index > 0) {
        var lastCompletedStageResult = this.get('stages').objectAt(index - 1).get('isSuccess');
        if (lastCompletedStageResult) {
          var nextStage = this.get('stages').objectAt(index);
          this.moveToNextStage(nextStage);
        }
      }
    }
  },

  moveToNextStage: function (nextStage) {
    if (!nextStage) {
      nextStage = this.get('stages').findProperty('isStarted', false);
    }
    if (nextStage) {
      this.startStage(nextStage);
    }
  },

  updateServices: function () {
    this.services.clear();
    var services = this.get("services");
    this.get("stages").forEach(function (stage) {
      var newService = Ember.Object.create({
        name: stage.label,
        hosts: []
      });
      if (stage && stage.get("polledData")) {
        var hostNames = stage.get("polledData").mapProperty('Tasks.host_name').uniq();
        hostNames.forEach(function (name) {
          newService.hosts.push({
            name: name,
            publicName: name,
            logTasks: stage.polledData.filterProperty("Tasks.host_name", name)
          });
        });
        services.push(newService);
      }
    });
    this.set('serviceTimestamp', new Date().getTime());
  }.observes('stages.@each.polledData'),

  addInfoToStages: function () {
    this.addInfoToStage2();
    this.addInfoToStage4();
  },

  addInfoToStage1: function () {
    var stage1 = this.get('stages').findProperty('stage', 'stage1');
    if (App.testMode) {
      stage1.set('isSucces', true);
      stage1.set('isStarted', true);
      stage1.set('isCompleted', true);
    }
  },

  addInfoToStage2: function () {<commit_msg>AMBARI-2697: Disable security not working in web-ui testMode. (jaimin)<commit_after>/**
 * 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.MainAdminSecurityDisableController = Em.Controller.extend({

  name: 'mainAdminSecurityDisableController',
  secureMapping: require('data/secure_mapping'),
  configMapping: App.config.get('configMapping'),
  secureProperties: require('data/secure_properties').configProperties.slice(0),
  stages: [],
  configs: [],
  noOfWaitingAjaxCalls: 0,
  secureServices: [],
  serviceConfigTags: [],
  globalProperties: [],
  hasHostPopup: true,
  services: [],
  serviceTimestamp: null,
  isSubmitDisabled: true,
  totalSteps: 3,

  clearStep: function () {
    this.get('stages').clear();
    this.get('secureServices').clear();
    this.get('serviceConfigTags').clear();
  },

  retry: function () {
    var failedStage = this.get('stages').findProperty('isError', true);
    if (failedStage) {
      failedStage.set('isStarted', false);
      failedStage.set('isError', false);
      this.startStage(failedStage);
    }
  },

  loadStep: function () {
    this.clearStep();
    var stages = App.db.getSecurityDeployStages();
    if (stages && stages.length > 0) {
      stages.forEach(function (_stage, index) {
        stages[index] = App.Poll.create(_stage);
      }, this);
      if (stages.someProperty('isError', true)) {
        this.get('stages').pushObjects(stages);
        this.loadSecureServices();
        this.addObserver('stages.@each.isSuccess', this.onCompleteStage);
        return;
      } else if (stages.filterProperty('isStarted', true).someProperty('isCompleted', false)) {
        var runningStage = stages.filterProperty('isStarted', true).findProperty('isCompleted', false);
        runningStage.set('isStarted', false);
        this.get('stages').pushObjects(stages);
      } else {
        this.get('stages').pushObjects(stages);
      }
    } else {
      this.loadStages();
      this.addInfoToStages();
      var runningOperations = App.router.get('backgroundOperationsController.services').filterProperty('isRunning');
      var stopAllOperation = runningOperations.findProperty('name', 'Stop All Services');
      var stopStage = this.get('stages').findProperty('name', 'STOP_SERVICES');
      if (stopStage.get('name') === 'STOP_SERVICES' && stopAllOperation) {
        stopStage.set('requestId', stopAllOperation.get('id'));
      }
    }
    this.loadSecureServices();
    this.addObserver('stages.@each.isSuccess', this.onCompleteStage);
    this.moveToNextStage();
  },


  loadStages: function () {
    this.get('stages').pushObjects([
      App.Poll.create({stage: 'stage2', label: Em.I18n.translations['admin.addSecurity.apply.stage2'], isPolling: true, name: 'STOP_SERVICES'}),
      App.Poll.create({stage: 'stage3', label: Em.I18n.translations['admin.addSecurity.apply.stage3'], isPolling: false, name: 'APPLY_CONFIGURATIONS'}),
      App.Poll.create({stage: 'stage4', label: Em.I18n.translations['admin.addSecurity.apply.stage4'], isPolling: true, name: 'START_SERVICES'})
    ]);
  },

  enableSubmit: function () {
    if (this.get('stages').someProperty('isError', true) || this.get('stages').everyProperty('isSuccess', true)) {
      this.set('isSubmitDisabled', false);
    } else {
      this.set('isSubmitDisabled', true);
    }
  }.observes('stages.@each.isCompleted'),

  startStage: function (currentStage) {
    if (this.get('stages').length === this.totalSteps) {
      if (!currentStage) {
        var startedStages = this.get('stages').filterProperty('isStarted', true);
        currentStage = startedStages.findProperty('isCompleted', false);
      }
      if (currentStage && currentStage.get('isPolling') === true) {
        currentStage.set('isStarted', true);
        currentStage.start();
      } else if (currentStage && currentStage.get('stage') === 'stage3') {
        currentStage.set('isStarted', true);
        if (App.testMode) {
          currentStage.set('isError', false);
          currentStage.set('isSuccess', true);
        } else {
          this.loadClusterConfigs();
        }
      }
    }
  },

  onCompleteStage: function () {
    if (this.get('stages').length === this.totalSteps) {
      var index = this.get('stages').filterProperty('isSuccess', true).length;
      if (index > 0) {
        var lastCompletedStageResult = this.get('stages').objectAt(index - 1).get('isSuccess');
        if (lastCompletedStageResult) {
          var nextStage = this.get('stages').objectAt(index);
          this.moveToNextStage(nextStage);
        }
      }
    }
  },

  moveToNextStage: function (nextStage) {
    if (!nextStage) {
      nextStage = this.get('stages').findProperty('isStarted', false);
    }
    if (nextStage) {
      this.startStage(nextStage);
    }
  },

  updateServices: function () {
    this.services.clear();
    var services = this.get("services");
    this.get("stages").forEach(function (stage) {
      var newService = Ember.Object.create({
        name: stage.label,
        hosts: []
      });
      if (stage && stage.get("polledData")) {
        var hostNames = stage.get("polledData").mapProperty('Tasks.host_name').uniq();
        hostNames.forEach(function (name) {
          newService.hosts.push({
            name: name,
            publicName: name,
            logTasks: stage.polledData.filterProperty("Tasks.host_name", name)
          });
        });
        services.push(newService);
      }
    });
    this.set('serviceTimestamp', new Date().getTime());
  }.observes('stages.@each.polledData'),

  addInfoToStages: function () {
    this.addInfoToStage2();
    this.addInfoToStage4();
  },

  addInfoToStage1: function () {
    var stage1 = this.get('stages').findProperty('stage', 'stage1');
    if (App.testMode) {
      stage1.set('isSuccess', true);
      stage1.set('isStarted', true);
      stage1.set('isCompleted', true);
    }
  },

  addInfoToStage2: function () {
{'fraction_non_alphanumeric': 0.14991023339317774, 'fraction_numerical': 0.005086774386594853, 'mean_word_length': 3.3948060486522027, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6034093', 'n_tokens_mistral': 4007, 'n_tokens_neox': 3950, 'n_words': 788}