Name

global.PatternApplicationFactory

Description

No description available

Script

var PatternApplicationFactory = Class.create();
PatternApplicationFactory.prototype = {
  HORIZONTAL_PROBE_NAME : "HorizontalDiscoveryProbe-Horizontal Patt",
  HORIZONTAL_PROBE_ID : "4f64c6389f230200fe2ab0aec32e7068",
  EXPLORATION_PHASE : "exploration",
  ACTIVE : "1",
  APPLICATION : "1",
  SUCCESS : "Success",//Should not translate this is checked and expected in the UI JS validation
  RUNS_ON_SYS_ID:"60bc4e22c0a8010e01f074cbe6bd73c3",



  initialize: function() {
  },

  /***
   * update the cluster process group with the new classifier
   * @param classifierGR @NotNull  - glide record that is process classifier
   * @param processGroupGR - glide record from the clustered processes
   */
  updateProcessGroupWithClassifier: function(classifierGR,processGroupGR){
  	gs.info("PatternApplicationFactory : added classifier "+classifierGR.sys_id+ " to "+processGroupGR.suggested_name);
  	processGroupGR.classifier = classifierGR.sys_id;
  	processGroupGR.update();
  },


  createDiscoveryForApplication : function(applicationName,discoveredCMDBCIType,classifierRuleCondition,relationSysID,processGroupGR){

  	gs.info("Creating Discovery for Application "+applicationName);

  	var classifierName = applicationName +" classifier";

  	var defaultIdentificationSectionName = applicationName+" identification";

  	//create pattern
  	var patternID = this.createPattern(applicationName,discoveredCMDBCIType, defaultIdentificationSectionName);
  	if (patternID.indexOf('Error:') != -1) {
  		return patternID;
  	}
  	else if (gs.nil(patternID)) {
  		return gs.getMessage('Error: Failed to create pattern');
  	}


  	//create classifier
  	var classifierGR = this.createClassifier(classifierName, discoveredCMDBCIType, classifierRuleCondition, relationSysID);



  	if (!gs.nil(classifierGR)) {
  		this.updateProcessGroupWithClassifier(classifierGR,processGroupGR);

  		gs.info("Creating horizontal probe for pattern "+patternID+" and classifier "+classifierGR.sys_id);
  		this.createHorizontalProbe(patternID, classifierGR.sys_id);
  	}else{
  		gs.error("Failed to create classifier "+classifierName);
  	}

  	return this.SUCCESS;
  },


  /***
   * create a Classifier that will invoke the discovery pattern based on the condtion rules
   * @param name
   * @param table
   * @param condition
   * @param rel_type
   * @returns {*}
   */
  createClassifier : function(name, table, condition, rel_type) {

  	var errMessage;

  	var classificationGr = new GlideRecord('discovery_classy_proc');
  	classificationGr.addQuery('name', name);
  	classificationGr.query();
  	if (classificationGr.next()) {
  		errMessage = gs.getMessage('Error: Duplicate process classifier name: {0}', [name]);
  		this.errorMsg(errMessage);
  		return null;
  	}

  	if (gs.nil(name)) {
  		errMessage = gs.getMessage('Error: The classifier name must not be empty');
  		this.errorMsg(errMessage);
  		return null;
  	}

  	if (gs.nil(table)) {
  		errMessage = gs.getMessage('Error: The table must not be not empty');
  		this.errorMsg(errMessage);
  		return null;
  	}

  	var tableGr = new GlideRecord(table);
  	if (!tableGr.isValid()) {
  		errMessage = gs.getMessage('Error: There is no table {0}', [table]);
  		this.errorMsg(errMessage);
  		return null;
  	}

  	if (gs.nil(condition)) {
  		errMessage = gs.getMessage('Error: The condition must not be empty');
  		this.errorMsg(errMessage);
  		return null;
  	}

  	var relGr = new GlideRecord('cmdb_rel_type');
  	relGr.addQuery('sys_id', rel_type);
  	relGr.query();
  	if (!relGr.next()) {
  		errMessage = gs.getMessage('Error: There is no relation type with sys ID: {0}', [rel_type]);
  		this.errorMsg(errMessage);
  		return null;
  	}

  	classificationGr.initialize();
  	classificationGr.setValue('name',name);
  	classificationGr.setValue('table', table);
  	classificationGr.setValue('condition', condition.trim());
  	classificationGr.setValue('relation_type', rel_type);
  	classificationGr.setValue('active', this.ACTIVE);
  	classificationGr.setValue('order',10000);
  	classificationGr.update();
  	return classificationGr;

  },

  createHorizontalProbe : function(patternID, classificationSysID) {
  	var probeGr = new GlideRecord('discovery_classifier_probe');
  	probeGr.setValue('name', this.HORIZONTAL_PROBE_NAME);
  	probeGr.setValue('child',this.HORIZONTAL_PROBE_ID);
  	probeGr.setValue('classy', classificationSysID);
  	probeGr.setValue('phase', this.EXPLORATION_PHASE);
  	probeGr.setValue('active', this.ACTIVE);
  	probeGr.setValue('pattern',patternID);
  	probeGr.update();
  	return this.SUCCESS;
  },

  createPattern : function(name, ciType, defaultIdentificationSectionName) {
  	var patternGr = new GlideRecord('sa_pattern');
  	var ndl = this.createEmptyNDL(name,ciType, defaultIdentificationSectionName,patternGr);
  	if (ndl.indexOf('Error:') != -1) {
  		gs.error("Failed to create pattern with name "+name);
  		return ndl;
  	}
  	patternGr.setValue('ndl',ndl);
  	patternGr.setValue('name', name);
  	patternGr.setValue('ci_type', ciType);
  	patternGr.setValue('cpattern_type', this.APPLICATION);
  	patternGr.setValue('enforce_proc_classy', 'true');
  	return patternGr.update();
  	
  },

  createEmptyNDL : function(name, ciType, defaultIdentificationSectionName, patternGr) {
  	var errMessage = gs.getMessage('Error: ');
  	if (gs.nil(defaultIdentificationSectionName)) {
  		errMessage += gs.getMessage('The default identification section cannot be empty');
  		this.errorMsg(errMessage);
  		return errMessage;
  	}

  	var gr = new GlideRecord(ciType);
  	if (gr.isValid()) {
  		var patternLibrary = new SNC.GlidePatternLibrary();
  		var ndl = patternLibrary.createEmptyNdlForGenericApplication(name, ciType, defaultIdentificationSectionName,  patternGr, true);
  		return ndl;
  	}
  	errMessage += gs.getMessage('There is no CI Type table: {0}', [ciType]);
  	this.errorMsg(errMessage);
  	return errMessage;
  },
  errorMsg : function(msg) {
  	gs.error(msg);
  },


  type: 'PatternApplicationFactory'
};

Sys ID

fb50e4c3732233005ad2db37aef6a7ea

Offical Documentation

Official Docs: