summaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/storeUtil/utils/general/general.reducers.ts
blob: 8ee47331fc265140601312c1be3a68a47986f9ed (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
import {Action} from "redux";
import {
  ChangeInstanceCounterAction, RemoveInstanceAction, DuplicateBulkInstancesAction,
  GeneralActions,
  UpdateAicZonesAction, UpdateCategoryParametersAction,
  UpdateLcpRegionsAndTenantsAction, UpdateNetworkCollectionFunction,
  UpdateProductFamiliesAction, UpdateServiceTypesAction,
  UpdateSubscribersAction, UpdateUserIdAction
} from "./general.actions";
import {TypeNodeInformation} from "../../../../drawingBoard/service-planning/typeNodeInformation.model";
import * as _ from "lodash";
import {ITreeNode} from "angular-tree-component/dist/defs/api";
import {ServiceInstance} from "../../../models/serviceInstance";
import {ServiceState} from "../main.reducer";

export function generalReducer(state: ServiceState, action: Action) : ServiceState {
  switch (action.type) {
    case GeneralActions.UPDATE_LCP_REGIONS_AND_TENANTS: {
      Object.assign(state, (<UpdateLcpRegionsAndTenantsAction>action));
      return Object.assign({}, state);
    }
    case GeneralActions.UPDATE_SUBSCRIBERS: {
      Object.assign(state, (<UpdateSubscribersAction>action));
      return Object.assign({}, state);
    }
    case GeneralActions.UPDATE_AIC_ZONES: {
      Object.assign(state, (<UpdateAicZonesAction>action));
      return Object.assign({}, state);
    }
    case GeneralActions.UPDATE_PRODUCT_FAMILIES: {
      Object.assign(state, (<UpdateProductFamiliesAction>action));
      return Object.assign({}, state);
    }
    case GeneralActions.UPDATE_NETWORK_FUNCTION: {
      const networkFunctionReduxObj = state['networkFunctions'] == undefined ? {} : state['networkFunctions'];
      networkFunctionReduxObj[(<UpdateNetworkCollectionFunction>action).network_function] = (<UpdateNetworkCollectionFunction>action).networksAccordingToNetworkCollection;
      Object.assign(state, {'networkFunctions': networkFunctionReduxObj});
      return Object.assign({}, state);
    }
    case GeneralActions.UPDATE_SERVICE_TYPES: {
      state.serviceTypes[(<UpdateServiceTypesAction>action).subscriberId] = (<UpdateServiceTypesAction>action).serviceTypes;
      return Object.assign({}, state);
    }
    case GeneralActions.UPDATE_CATEGORY_PARAMETERS: {
      Object.assign(state, (<UpdateCategoryParametersAction>action));
      return Object.assign({}, state);
    }
    case GeneralActions.UPDATE_USER_ID: {
      const updateUserId: UpdateUserIdAction = <UpdateUserIdAction>action;
      let newState = _.cloneDeep(state);
      newState['userId'] = updateUserId.userId;
      return newState;
    }
    case GeneralActions.REMOVE_INSTANCE: {
      const actionData = (<RemoveInstanceAction>action);
      if (state.serviceInstance[actionData.serviceModelId]) {
        const typeNodeInformation : TypeNodeInformation = new TypeNodeInformation(actionData.node);
        updateIsMissingDataOnDelete(state, actionData.serviceModelId, actionData.storeKey, actionData.node);
        updateUniqueNames(state.serviceInstance[actionData.serviceModelId][typeNodeInformation.hierarchyName][actionData.storeKey].instanceName, null, state.serviceInstance[actionData.serviceModelId]);
        if(actionData.node.data.type === 'VF'){
          _.forOwn(state.serviceInstance[actionData.serviceModelId][typeNodeInformation.hierarchyName][actionData.storeKey].vfModules, (vfModuleMap) => {
            _.forOwn(vfModuleMap, (vfModuleInstance) => {
              updateUniqueNames(vfModuleInstance.instanceName, null, state.serviceInstance[actionData.serviceModelId]);
            })
          });
        }
        delete state.serviceInstance[actionData.serviceModelId][typeNodeInformation.hierarchyName][actionData.storeKey];
      }
      return Object.assign({}, state);
    }

    case GeneralActions.CHANGE_INSTANCE_COUNTER : {
      const changeInstanceCounterAction = <ChangeInstanceCounterAction>action;
      const typeNodeInformation : TypeNodeInformation =  new TypeNodeInformation(changeInstanceCounterAction.node);
      let newState = _.cloneDeep(state);

      let existing: number = getExistingCounter(newState, changeInstanceCounterAction.serviceUUID, changeInstanceCounterAction.UUID, typeNodeInformation);
      newState.serviceInstance[changeInstanceCounterAction.serviceUUID][typeNodeInformation.existingMappingCounterName][changeInstanceCounterAction.UUID] = existing ? existing + changeInstanceCounterAction.changeBy : changeInstanceCounterAction.changeBy;
      return newState;
    }

    case GeneralActions.DUPLICATE_BULK_INSTANCES : {
      const createInstanceAction = <DuplicateBulkInstancesAction>action;
      const typeNodeInformation : TypeNodeInformation =  new TypeNodeInformation(createInstanceAction.node);
      const serviceId = createInstanceAction.serviceId;
      const objects = createInstanceAction.objects;

      let newState = _.cloneDeep(state);
      newState.serviceInstance[serviceId].existingNames = createInstanceAction.existingNames;
      newState.serviceInstance[serviceId][typeNodeInformation.hierarchyName] = Object.assign({}, newState.serviceInstance[serviceId][typeNodeInformation.hierarchyName], objects);
      return newState;
    }
  }
}

const getExistingCounter = (state: any, serviceModelId: string, modelId: string, typeNodeInformation : TypeNodeInformation ) : number => {
  const serviceExistingCounterMap = state.serviceInstance[serviceModelId][typeNodeInformation.existingMappingCounterName];
  if(serviceExistingCounterMap && !_.isNil(serviceExistingCounterMap[modelId])){
    return serviceExistingCounterMap[modelId];
  }else {
    return null;
  }
};

const updateIsMissingDataOnDelete = (state: any, serviceModelId: string, storeKey: string, node: ITreeNode): void => {
  const typeNodeInformation : TypeNodeInformation = new TypeNodeInformation(node);
  let vnf = state.serviceInstance[serviceModelId][typeNodeInformation.hierarchyName][storeKey];
  if(node.children){
    _.forOwn(vnf.vfModules, (vfModules, vfModulesKey) => {
      updateIsMissingDataOnDeleteVFModule(state, serviceModelId, storeKey, vfModulesKey);
    });
  }


  let isMissingData: boolean = state.serviceInstance[serviceModelId][typeNodeInformation.hierarchyName][storeKey].isMissingData;
  updateServiceValidationCounter(state, isMissingData, false, serviceModelId);
};

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()] = "";
  }
};

const updateIsMissingDataOnDeleteVFModule = (state: any, serviceModelId: string, vnfStoreKey: string, vfModuleName): void => {
  const vfModules = state.serviceInstance[serviceModelId].vnfs[vnfStoreKey].vfModules[vfModuleName];

  _.forOwn(vfModules, (vfModuleInstance) => {
    let isMissingData: boolean = vfModuleInstance.isMissingData;
    updateServiceValidationCounter(state, isMissingData, false, serviceModelId);
  });
};

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