summaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/storeUtil/utils/vnfGroup/vnfGroup.reducers.ts
blob: 2923c09b20e52f06db9fc03e1e3274272007f592 (plain)
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
import {Action} from "redux";
import {VnfGroupInstance} from "../../../models/vnfGroupInstance";
import * as _ from "lodash";
import {ServiceInstance} from "../../../models/serviceInstance";
import {ServiceState} from "../main.reducer";
import {
  CreateVnfGroupInstanceAction,
  DeleteActionVnfGroupInstanceAction, SetOptionalMembersVnfGroupInstanceAction,
  UpdateVnfGroupInstanceAction,
  VnfGroupActions
} from "./vnfGroup.actions";
import {ServiceInstanceActions} from "../../../models/serviceInstanceActions";

export function vnfGroupReducer(state: ServiceState, action: Action): ServiceState {
  switch (action.type) {
    case VnfGroupActions.CREATE_VNF_GROUP_INSTANCE: {
      const updateVnfGroupInstanceAction = <CreateVnfGroupInstanceAction>action;
      const serviceUuid = updateVnfGroupInstanceAction.serviceUuid;
      let vnfGroupModelName = updateVnfGroupInstanceAction.vnfGroupModelName;
      let newState = _.cloneDeep(state);

      updateVnfGroupInstanceAction.vnfGroupInstance.originalName = vnfGroupModelName;
      updateVnfGroupInstanceAction.vnfGroupModelName = calculateNextUniqueModelName(vnfGroupModelName, serviceUuid, newState, 'vnfGroups');

      let vnfGroupInstance: VnfGroupInstance = newState.serviceInstance[serviceUuid].vnfGroups[vnfGroupModelName];
      vnfGroupInstance = new VnfGroupInstance();
      updateVnfGroupInstanceAction.vnfGroupInstance.vnfGroupStoreKey = updateVnfGroupInstanceAction.vnfGroupModelName;
      updateVnfGroupInstanceAction.vnfGroupInstance.originalName = vnfGroupModelName;
      vnfGroupInstance.originalName = updateVnfGroupInstanceAction.vnfGroupInstance.originalName;
      vnfGroupInstance.vnfGroupStoreKey = updateVnfGroupInstanceAction.vnfGroupInstance.vnfGroupStoreKey;
      updateServiceValidationCounter(newState, vnfGroupInstance['isMissingData'], updateVnfGroupInstanceAction.vnfGroupInstance['isMissingData'], serviceUuid);

      newState.serviceInstance[serviceUuid].vnfGroups[updateVnfGroupInstanceAction.vnfGroupModelName] = Object.assign(vnfGroupInstance, updateVnfGroupInstanceAction.vnfGroupInstance);
      return newState;
    }
    case VnfGroupActions.UPDATE_VNF_GROUP_INSTANCE: {
      const updateVnfInstanceAction = <UpdateVnfGroupInstanceAction>action;
      const serviceUuid = updateVnfInstanceAction.serviceUuid;
      let vnfGroupStoreKey = updateVnfInstanceAction.vnfGroupStoreKey;


      let newState = _.cloneDeep(state);
      let vnfGroupInstance: VnfGroupInstance = newState.serviceInstance[serviceUuid].vnfGroups[vnfGroupStoreKey];
      updateUniqueNames(vnfGroupInstance ? vnfGroupInstance.instanceName : null, updateVnfInstanceAction.vnfGroupInstance.instanceName, newState.serviceInstance[serviceUuid]);

      vnfGroupInstance = vnfGroupInstance || new VnfGroupInstance();
      updateServiceValidationCounter(newState, vnfGroupInstance['isMissingData'], updateVnfInstanceAction.vnfGroupInstance['isMissingData'], serviceUuid);

      newState.serviceInstance[serviceUuid].vnfGroups[vnfGroupStoreKey] = Object.assign(vnfGroupInstance, updateVnfInstanceAction.vnfGroupInstance);
      return newState;
    }
    case VnfGroupActions.DELETE_ACTION_VNF_GROUP_INSTANCE : {
      let newState = _.cloneDeep(state);
      let oldAction = newState.serviceInstance[(<DeleteActionVnfGroupInstanceAction>action).serviceId].vnfGroups[(<DeleteActionVnfGroupInstanceAction>action).vnfGroupStoreKey].action;
      if(oldAction === ServiceInstanceActions.None_Delete || oldAction === ServiceInstanceActions.Update_Delete) return newState;
      newState.serviceInstance[(<DeleteActionVnfGroupInstanceAction>action).serviceId].vnfGroups[(<DeleteActionVnfGroupInstanceAction>action).vnfGroupStoreKey].action = (oldAction + '_Delete') as ServiceInstanceActions;
      return newState;
    }
    case VnfGroupActions.UNDO_DELETE_ACTION_VNF_GROUP_INSTANCE : {
      let newState = _.cloneDeep(state);
      let oldState = newState.serviceInstance[(<DeleteActionVnfGroupInstanceAction>action).serviceId].vnfGroups[(<DeleteActionVnfGroupInstanceAction>action).vnfGroupStoreKey].action;
      newState.serviceInstance[(<DeleteActionVnfGroupInstanceAction>action).serviceId].vnfGroups[(<DeleteActionVnfGroupInstanceAction>action).vnfGroupStoreKey].action = (oldState.split('_')[0]) as ServiceInstanceActions;
      return newState;
    }
    case VnfGroupActions.SET_OPTIONAL_MEMBERS_VNF_GROUP_INSTANCE:{
      let newState = _.cloneDeep(state);
      newState.serviceInstance[(<SetOptionalMembersVnfGroupInstanceAction>action).serviceId].optionalGroupMembersMap[(<SetOptionalMembersVnfGroupInstanceAction>action).path] = (<SetOptionalMembersVnfGroupInstanceAction>action).vnfMembers;
      return newState;
    }
  }
}

const updateServiceValidationCounter = (newState: any, oldValidationState: boolean, newValidationState: boolean, serviceUuid: string) => {
  if (oldValidationState && !newValidationState) {
    newState.serviceInstance[serviceUuid].validationCounter--;
  } else if (!oldValidationState && newValidationState) {
    newState.serviceInstance[serviceUuid].validationCounter++;
  }
};


const updateUniqueNames = (oldName: string, newName: string, serviceInstance: ServiceInstance): void => {
  let existingNames = serviceInstance.existingNames;
  if (!_.isNil(oldName) && oldName.toLowerCase() in existingNames) {
    delete existingNames[oldName.toLowerCase()];
  }
  if (!_.isNil(newName)) {
    existingNames[newName.toLowerCase()] = "";
  }
};


export const calculateNextUniqueModelName = (vnfGroupModelName: string, serviceId: string, state: any, levelName: string): string => {
  let counter: number = null;
  while (true) {
    let pattern = !_.isNil(counter) ? ("_" + counter) : "";
    if (!_.isNil(state.serviceInstance[serviceId][levelName][vnfGroupModelName + pattern])) {
      counter = counter ? (counter + 1) : 1;
    } else {
      return vnfGroupModelName + pattern;
    }
  }
};