summaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/storeUtil/utils/vnf/vnf.reducers.ts
blob: cc24d8dc334eb91cf37e604b47eab9cf1befe2cd (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import {Action} from "redux";
import {VnfInstance} from "../../../models/vnfInstance";
import {
  CreateVnfInstanceAction,
  DeleteActionVnfInstanceAction, RemoveVnfInstanceAction, UndoDeleteActionVnfInstanceAction,
  UpdateVnfInstanceAction, UpdateVnfPosition,
  VNFActions
} from "./vnf.actions";
import * as _ from "lodash";
import {ServiceInstance} from "../../../models/serviceInstance";
import {ServiceState} from "../main.reducer";
import {ServiceInstanceActions} from "../../../models/serviceInstanceActions";

export function vnfReducer(state: ServiceState, action: Action): ServiceState {
  switch (action.type) {
    case VNFActions.CREATE_VNF_INSTANCE: {
      const updateVnfInstanceAction = <CreateVnfInstanceAction>action;
      const serviceUuid = updateVnfInstanceAction.serviceUuid;
      let vnfModelName = updateVnfInstanceAction.vnfModelName;
      let newState = _.cloneDeep(state);

      updateVnfInstanceAction.vnfInstance.originalName = vnfModelName;
      updateVnfInstanceAction.vnfModelName = calculateNextUniqueModelName(vnfModelName, serviceUuid, newState, 'vnfs');

      let vnfInstance: VnfInstance = newState.serviceInstance[serviceUuid].vnfs[vnfModelName];
      vnfInstance = new VnfInstance();
      updateVnfInstanceAction.vnfInstance.vnfStoreKey = updateVnfInstanceAction.vnfModelName;
      updateVnfInstanceAction.vnfInstance.originalName = vnfModelName;
      vnfInstance.originalName = updateVnfInstanceAction.vnfInstance.originalName;
      vnfInstance.vnfStoreKey = updateVnfInstanceAction.vnfInstance.vnfStoreKey;
      updateServiceValidationCounter(newState, vnfInstance['isMissingData'], updateVnfInstanceAction.vnfInstance['isMissingData'], serviceUuid);

      newState.serviceInstance[serviceUuid].vnfs[updateVnfInstanceAction.vnfModelName] = Object.assign(vnfInstance, updateVnfInstanceAction.vnfInstance);
      return newState;
    }

    case VNFActions.UPDATE_VNF_INSTANCE: {
      const updateVnfInstanceAction = <UpdateVnfInstanceAction>action;
      const serviceUuid = updateVnfInstanceAction.serviceUuid;
      let vnfStoreKey = updateVnfInstanceAction.vnfStoreKey;


      let newState = _.cloneDeep(state);
      let vnfInstance: VnfInstance = newState.serviceInstance[serviceUuid].vnfs[vnfStoreKey];
      updateUniqueNames(vnfInstance ? vnfInstance.instanceName : null, updateVnfInstanceAction.vnfInstance.instanceName, newState.serviceInstance[serviceUuid]);

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

      newState.serviceInstance[serviceUuid].vnfs[vnfStoreKey] = Object.assign(vnfInstance, updateVnfInstanceAction.vnfInstance);
      return newState;
    }

    case VNFActions.DELETE_ACTION_VNF_INSTANCE : {
      let newState = _.cloneDeep(state);
      let vnf = newState.serviceInstance[(<DeleteActionVnfInstanceAction>action).serviceId].vnfs[(<DeleteActionVnfInstanceAction>action).vnfStoreKey];
      let oldAction = vnf.action;
      if(oldAction === ServiceInstanceActions.None_Delete || oldAction === ServiceInstanceActions.Update_Delete) return newState;
      newState.serviceInstance[(<DeleteActionVnfInstanceAction>action).serviceId].vnfs[(<DeleteActionVnfInstanceAction>action).vnfStoreKey].action = (oldAction + '_Delete') as ServiceInstanceActions;
      updateServiceValidationCounter(newState, vnf['isMissingData'], false,  (<RemoveVnfInstanceAction>action).serviceId);
      return newState;
    }

    case VNFActions.UNDO_DELETE_ACTION_VNF_INSTANCE : {
      let newState = _.cloneDeep(state);
      let vnf = newState.serviceInstance[(<UndoDeleteActionVnfInstanceAction>action).serviceId].vnfs[(<UndoDeleteActionVnfInstanceAction>action).vnfStoreKey];
      let oldState = vnf.action;
      newState.serviceInstance[(<UndoDeleteActionVnfInstanceAction>action).serviceId].vnfs[(<UndoDeleteActionVnfInstanceAction>action).vnfStoreKey].action = (oldState.split('_')[0]) as ServiceInstanceActions;
      updateServiceValidationCounter(newState, vnf['isMissingData'], false,  (<UndoDeleteActionVnfInstanceAction>action).serviceId);
      return newState;
    }

    case VNFActions.REMOVE_VNF_INSTANCE : {
      let newState = _.cloneDeep(state);
      let vnfInstance = newState.serviceInstance[(<RemoveVnfInstanceAction>action).serviceId].vnfs[(<RemoveVnfInstanceAction>action).vnfStoreKey];
      updateServiceValidationCounter(newState, vnfInstance['isMissingData'], false,  (<RemoveVnfInstanceAction>action).serviceId);
      delete newState.serviceInstance[(<RemoveVnfInstanceAction>action).serviceId].vnfs[(<RemoveVnfInstanceAction>action).vnfStoreKey];
      return newState;
    }

    case VNFActions.UPDATE_VNF_POSITION : {
      let newState = _.cloneDeep(state);
      newState.serviceInstance[(<UpdateVnfPosition>action).instanceId].vnfs[(<UpdateVnfPosition>action).vnfStoreKey].position = (<UpdateVnfPosition>action).node.position;
      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 = (vnfModelName: 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][vnfModelName + pattern])) {
      counter = counter ? (counter + 1) : 1;
    } else {
      return vnfModelName + pattern;
    }
  }
};