Commit 104d05f0 authored by Simon Pastor's avatar Simon Pastor
Browse files

moving to scenario utils

parent a1b8cd36
Loading
Loading
Loading
Loading
+6 −129
Original line number Diff line number Diff line
@@ -31,14 +31,6 @@ import ExecPageContainer from './exec/exec-page-container';
import SettingsPageContainer from './settings/settings-page-container';
import MonitorPageContainer from './monitor/monitor-page-container';

import {
  FIELD_PARENT,
  FIELD_NAME,
  getElemFieldVal,
  setElemFieldVal,
  createUniqueName
} from '../util/elem-utils';

import {
  TYPE_CFG,
  TYPE_EXEC,
@@ -49,9 +41,7 @@ import {
  PAGE_CONFIGURE,
  PAGE_EXECUTE,
  PAGE_MONITOR,
  PAGE_SETTINGS,
  COMMON_ZONE_TYPE_STR,
  DEFAULT_NL_TYPE_STR
  PAGE_SETTINGS
} from '../meep-constants';

import {
@@ -59,8 +49,8 @@ import {
  createNewScenario,
  addElementToScenario,
  updateElementInScenario,
  removeElementFromScenario,
  getElementFromScenario
  cloneElementInScenario,
  removeElementFromScenario
} from '../util/scenario-utils';

import {
@@ -446,122 +436,9 @@ class MeepContainer extends Component {

  // Clone element in scenario
  cloneScenarioElem(element) {

    var inDomainCloneBranch = false, inZoneCloneBranch = false, inNlCloneBranch = false, inPlCloneBranch = false;
    var newZoneRootParentName = '';
    var newNlRootParentName = '';
    var newPlRootParentName = '';
    var newProcessRootParentName = '';
    var elementFromScenario;

    var scenario = this.props.cfg.scenario;
    // Domains
    for (var i in scenario.deployment.domains) {
      var domain = scenario.deployment.domains[i];

      // Add domain to graph and table (ignore public domain)
      if (domain.id === element.id) {
        newZoneRootParentName = this.cloneElement(element, getElemFieldVal(element, FIELD_PARENT), true);
        inDomainCloneBranch = true;
      } else {
        inDomainCloneBranch = false;
      }

      // Zones
      for (var j in domain.zones) {
        var zone = domain.zones[j];

        if (inDomainCloneBranch) {
          if (zone.name.indexOf(COMMON_ZONE_TYPE_STR) !== -1) {
            newNlRootParentName = newZoneRootParentName + COMMON_ZONE_TYPE_STR;
          } else {
            elementFromScenario = getElementFromScenario(scenario, zone.id);
            newNlRootParentName = this.cloneElement(elementFromScenario, newZoneRootParentName, false);
          }
        } else {
          if (zone.id === element.id) {
            newNlRootParentName = this.cloneElement(element, getElemFieldVal(element, FIELD_PARENT), true);
            inZoneCloneBranch = true;
          } else {
            inZoneCloneBranch = false;
          }
        }

        // Network Locations
        for (var k in zone.networkLocations) {
          var nl = zone.networkLocations[k];

          if (inDomainCloneBranch || inZoneCloneBranch) {
            if (nl.name.indexOf(DEFAULT_NL_TYPE_STR) !== -1) {
              newPlRootParentName = newNlRootParentName;
            } else {
              elementFromScenario = getElementFromScenario(scenario, nl.id);
              newPlRootParentName = this.cloneElement(elementFromScenario, newNlRootParentName, false);
            }
          } else {
            if (nl.id === element.id) {
              newPlRootParentName = this.cloneElement(element, getElemFieldVal(element, FIELD_PARENT, true));
              inNlCloneBranch = true;
            } else {
              inNlCloneBranch = false;
            }
          }

          // Physical Locations
          for (var l in nl.physicalLocations) {
            var pl = nl.physicalLocations[l];

            if (inDomainCloneBranch || inZoneCloneBranch || inNlCloneBranch) {
              elementFromScenario = getElementFromScenario(scenario, pl.id);
              newProcessRootParentName = this.cloneElement(elementFromScenario, newPlRootParentName, false);
            } else {
              if (pl.id === element.id) {
                newProcessRootParentName = this.cloneElement(element, getElemFieldVal(element, FIELD_PARENT, true));
                inPlCloneBranch = true;
              } else {
                inPlCloneBranch = false;
              }
            }

            // Processes
            for (var m in pl.processes) {
              var proc = pl.processes[m];

              if (inDomainCloneBranch || inZoneCloneBranch || inNlCloneBranch || inPlCloneBranch) {
                elementFromScenario = getElementFromScenario(scenario, proc.id);
                this.cloneElement(elementFromScenario, newProcessRootParentName, false);
              } else {
                if (proc.id === element.id) {
                  this.cloneElement(element, getElemFieldVal(element, FIELD_PARENT, true));
                }
              }
            }
          }
        }
      }

      if(inDomainCloneBranch || inZoneCloneBranch || inNlCloneBranch || inPlCloneBranch) {
        break;
      }
    }
    this.changeScenario(TYPE_CFG, scenario);
  }

  // CLONE ELEMENT, return new element name
  cloneElement(element, newParentName, isRoot) {
    let newElement = deepCopy(element);

    var name = getElemFieldVal(element, FIELD_NAME);
    if (isRoot === false) {
      name = createUniqueName(this.props.cfg.table.entries, name + '-copy');
      setElemFieldVal(newElement, FIELD_NAME, name);
    }
    setElemFieldVal(newElement, FIELD_PARENT, newParentName);

    // add new element to scenario
    // new id and label will be created as part of the addNewElementToScenario called by newScenarioElem
    this.newScenarioElem(TYPE_CFG, newElement, false);
    return name;
    var updatedScenario = updateObject({}, this.props.cfg.scenario);
    cloneElementInScenario(updatedScenario, element, this.props.cfg.table);
    this.changeScenario(TYPE_CFG, updatedScenario);
  }

  renderPage() {
+120 −2
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@

import _ from 'lodash';
import * as vis from 'vis';
import { updateObject } from './object-util';
import { updateObject, deepCopy } from './object-util';
import uuid from 'uuid';

import {
@@ -68,7 +68,8 @@ import {
  FIELD_APP_PKT_LOSS,
  createElem,
  getElemFieldVal,
  setElemFieldVal
  setElemFieldVal,
  createUniqueName
} from './elem-utils';

import {
@@ -598,6 +599,123 @@ export function updateElementInScenario(scenario, element) {
  }
}

// Clone network element in scenario
export function cloneElementInScenario(scenario, element, table) {
  var inDomainCloneBranch = false, inZoneCloneBranch = false, inNlCloneBranch = false, inPlCloneBranch = false;
  var newZoneRootParentName = '';
  var newNlRootParentName = '';
  var newPlRootParentName = '';
  var newProcessRootParentName = '';
  var elementFromScenario;

  // Domains
  for (var i in scenario.deployment.domains) {
    var domain = scenario.deployment.domains[i];

    // Add domain to graph and table (ignore public domain)
    if (domain.id === element.id) {
      newZoneRootParentName = cloneElement(scenario, element, getElemFieldVal(element, FIELD_PARENT), true, table);
      inDomainCloneBranch = true;
    } else {
      inDomainCloneBranch = false;
    }

    // Zones
    for (var j in domain.zones) {
      var zone = domain.zones[j];

      if (inDomainCloneBranch) {
        if (zone.name.indexOf(COMMON_ZONE_TYPE_STR) !== -1) {
          newNlRootParentName = newZoneRootParentName + COMMON_ZONE_TYPE_STR;
        } else {
          elementFromScenario = getElementFromScenario(scenario, zone.id);
          newNlRootParentName = cloneElement(scenario, elementFromScenario, newZoneRootParentName, false, table);
        }
      } else {
        if (zone.id === element.id) {
          newNlRootParentName = cloneElement(scenario, element, getElemFieldVal(element, FIELD_PARENT), true, table);
          inZoneCloneBranch = true;
        } else {
          inZoneCloneBranch = false;
        }
      }

      // Network Locations
      for (var k in zone.networkLocations) {
        var nl = zone.networkLocations[k];

        if (inDomainCloneBranch || inZoneCloneBranch) {
          if (nl.name.indexOf(DEFAULT_NL_TYPE_STR) !== -1) {
            newPlRootParentName = newNlRootParentName;
          } else {
            elementFromScenario = getElementFromScenario(scenario, nl.id);
            newPlRootParentName = cloneElement(scenario, elementFromScenario, newNlRootParentName, false, table);
          }
        } else {
          if (nl.id === element.id) {
            newPlRootParentName = cloneElement(scenario, element, getElemFieldVal(element, FIELD_PARENT, true, table));
            inNlCloneBranch = true;
          } else {
            inNlCloneBranch = false;
          }
        }

        // Physical Locations
        for (var l in nl.physicalLocations) {
          var pl = nl.physicalLocations[l];

          if (inDomainCloneBranch || inZoneCloneBranch || inNlCloneBranch) {
            elementFromScenario = getElementFromScenario(scenario, pl.id);
            newProcessRootParentName = cloneElement(scenario, elementFromScenario, newPlRootParentName, false, table);
          } else {
            if (pl.id === element.id) {
              newProcessRootParentName = cloneElement(scenario, element, getElemFieldVal(element, FIELD_PARENT, true, table));
              inPlCloneBranch = true;
            } else {
              inPlCloneBranch = false;
            }
          }

          // Processes
          for (var m in pl.processes) {
            var proc = pl.processes[m];

            if (inDomainCloneBranch || inZoneCloneBranch || inNlCloneBranch || inPlCloneBranch) {
              elementFromScenario = getElementFromScenario(scenario, proc.id);
              cloneElement(scenario, elementFromScenario, newProcessRootParentName, false, table);
            } else {
              if (proc.id === element.id) {
                cloneElement(scenario, element, getElemFieldVal(element, FIELD_PARENT, true, table));
              }
            }
          }
        }
      }
    }

    if(inDomainCloneBranch || inZoneCloneBranch || inNlCloneBranch || inPlCloneBranch) {
      break;
    }
  }
}

// CLONE ELEMENT, return new element name
function cloneElement(scenario, element, newParentName, isRoot, table) {
  let newElement = deepCopy(element);

  var name = getElemFieldVal(element, FIELD_NAME);
  if (isRoot === false) {
    name = createUniqueName(table.entries, name + '-copy');
    setElemFieldVal(newElement, FIELD_NAME, name);
  }
  setElemFieldVal(newElement, FIELD_PARENT, newParentName);

  // add new element to scenario
  // new id and label will be created as part of the addNewElementToScenario called by newScenarioElem
  addElementToScenario(scenario, newElement);
  return name;
}

// Remove the specific element and its children from the scenario
export function removeElementFromScenario(scenario, element) {
  var name = getElemFieldVal(element, FIELD_NAME);