summaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/storeUtil/utils/vnf/vnf.reducers.ts
blob: c5cd88aa7d684ad67d845aa1a6e883e9121617eb (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {Action} from "redux";
import {VnfInstance} from "../../../models/vnfInstance";
import {
  CreateVnfInstanceAction,
  RemoveVnfInstanceAction,
  UpdateVnfInstanceAction, UpdateVnfPosition, UpgradeVnfAction,
  VNFActions
} from "./vnf.actions";
import * as _ from "lodash";
import {ServiceInstance} from "../../../models/serviceInstance";
import {ServiceState} from "../main.reducer";
import {ServiceInstanceActions} from "../../../models/serviceInstanceActions";
import {deleteFirstLevel, updateServiceValidationCounter} from "../reducersHelper";
import {ActionOnFirstLevel} from "../firstLevel/firstLevel.actions";

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 : {
      return deleteFirstLevel(state, <ActionOnFirstLevel>action,true);
    }

    case VNFActions.UNDO_DELETE_ACTION_VNF_INSTANCE : {
      let newState = _.cloneDeep(state);
      let vnf = newState.serviceInstance[(<ActionOnFirstLevel>action).serviceId].vnfs[(<ActionOnFirstLevel>action).storeKey];
      let oldState = vnf.action;
      newState.serviceInstance[(<ActionOnFirstLevel>action).serviceId].vnfs[(<ActionOnFirstLevel>action).storeKey].action = (oldState.split('_')[0]) as ServiceInstanceActions;
      updateServiceValidationCounter(newState, vnf['isMissingData'], false,  (<ActionOnFirstLevel>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;
    }

    case VNFActions.UPGRADE_VNF_ACTION: {
      let clonedState = _.cloneDeep(state);
      const castingAction = <UpgradeVnfAction>action;
      let oldAction = clonedState
        .serviceInstance[castingAction.serviceUuid]
        .vnfs[castingAction.vnfStoreKey].action;
      if(!oldAction.includes("Upgrade")) {
        clonedState.serviceInstance[castingAction.serviceUuid]
          .vnfs[castingAction.vnfStoreKey]
          .action = (`${oldAction}_Upgrade`) as ServiceInstanceActions;
      }

      if(_.isNil(clonedState.serviceInstance[castingAction.serviceUuid]
        .vnfs[castingAction.vnfStoreKey].upgradedVFMSonsCounter)) {
        clonedState.serviceInstance[castingAction.serviceUuid]
          .vnfs[castingAction.vnfStoreKey].upgradedVFMSonsCounter = 1;
        return clonedState;
      }
      clonedState.serviceInstance[castingAction.serviceUuid]
        .vnfs[castingAction.vnfStoreKey].upgradedVFMSonsCounter++;
      return clonedState;
    }

    case VNFActions.UNDO_UPGRADE_VNF_ACTION: {
      let clonedState = _.cloneDeep(state);
      const castingAction = <UpgradeVnfAction>action;
      if(clonedState.serviceInstance[castingAction.serviceUuid]
        .vnfs[castingAction.vnfStoreKey]
        .action.includes("Upgrade")) {
        clonedState
          .serviceInstance[castingAction.serviceUuid]
          .vnfs[castingAction.vnfStoreKey].upgradedVFMSonsCounter--;
        if(clonedState.serviceInstance[castingAction.serviceUuid].vnfs[castingAction.vnfStoreKey]
          .upgradedVFMSonsCounter === 0){
          clonedState.serviceInstance[castingAction.serviceUuid]
            .vnfs[castingAction.vnfStoreKey]
            .action = ServiceInstanceActions.None;
        }
      }
      return clonedState;
    }

  }
}


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;
    }
  }
};