8ee47331fc265140601312c1be3a68a47986f9ed
[vid.git] / vid-webpack-master / src / app / shared / storeUtil / utils / general / general.reducers.ts
1 import {Action} from "redux";
2 import {
3   ChangeInstanceCounterAction, RemoveInstanceAction, DuplicateBulkInstancesAction,
4   GeneralActions,
5   UpdateAicZonesAction, UpdateCategoryParametersAction,
6   UpdateLcpRegionsAndTenantsAction, UpdateNetworkCollectionFunction,
7   UpdateProductFamiliesAction, UpdateServiceTypesAction,
8   UpdateSubscribersAction, UpdateUserIdAction
9 } from "./general.actions";
10 import {TypeNodeInformation} from "../../../../drawingBoard/service-planning/typeNodeInformation.model";
11 import * as _ from "lodash";
12 import {ITreeNode} from "angular-tree-component/dist/defs/api";
13 import {ServiceInstance} from "../../../models/serviceInstance";
14 import {ServiceState} from "../main.reducer";
15
16 export function generalReducer(state: ServiceState, action: Action) : ServiceState {
17   switch (action.type) {
18     case GeneralActions.UPDATE_LCP_REGIONS_AND_TENANTS: {
19       Object.assign(state, (<UpdateLcpRegionsAndTenantsAction>action));
20       return Object.assign({}, state);
21     }
22     case GeneralActions.UPDATE_SUBSCRIBERS: {
23       Object.assign(state, (<UpdateSubscribersAction>action));
24       return Object.assign({}, state);
25     }
26     case GeneralActions.UPDATE_AIC_ZONES: {
27       Object.assign(state, (<UpdateAicZonesAction>action));
28       return Object.assign({}, state);
29     }
30     case GeneralActions.UPDATE_PRODUCT_FAMILIES: {
31       Object.assign(state, (<UpdateProductFamiliesAction>action));
32       return Object.assign({}, state);
33     }
34     case GeneralActions.UPDATE_NETWORK_FUNCTION: {
35       const networkFunctionReduxObj = state['networkFunctions'] == undefined ? {} : state['networkFunctions'];
36       networkFunctionReduxObj[(<UpdateNetworkCollectionFunction>action).network_function] = (<UpdateNetworkCollectionFunction>action).networksAccordingToNetworkCollection;
37       Object.assign(state, {'networkFunctions': networkFunctionReduxObj});
38       return Object.assign({}, state);
39     }
40     case GeneralActions.UPDATE_SERVICE_TYPES: {
41       state.serviceTypes[(<UpdateServiceTypesAction>action).subscriberId] = (<UpdateServiceTypesAction>action).serviceTypes;
42       return Object.assign({}, state);
43     }
44     case GeneralActions.UPDATE_CATEGORY_PARAMETERS: {
45       Object.assign(state, (<UpdateCategoryParametersAction>action));
46       return Object.assign({}, state);
47     }
48     case GeneralActions.UPDATE_USER_ID: {
49       const updateUserId: UpdateUserIdAction = <UpdateUserIdAction>action;
50       let newState = _.cloneDeep(state);
51       newState['userId'] = updateUserId.userId;
52       return newState;
53     }
54     case GeneralActions.REMOVE_INSTANCE: {
55       const actionData = (<RemoveInstanceAction>action);
56       if (state.serviceInstance[actionData.serviceModelId]) {
57         const typeNodeInformation : TypeNodeInformation = new TypeNodeInformation(actionData.node);
58         updateIsMissingDataOnDelete(state, actionData.serviceModelId, actionData.storeKey, actionData.node);
59         updateUniqueNames(state.serviceInstance[actionData.serviceModelId][typeNodeInformation.hierarchyName][actionData.storeKey].instanceName, null, state.serviceInstance[actionData.serviceModelId]);
60         if(actionData.node.data.type === 'VF'){
61           _.forOwn(state.serviceInstance[actionData.serviceModelId][typeNodeInformation.hierarchyName][actionData.storeKey].vfModules, (vfModuleMap) => {
62             _.forOwn(vfModuleMap, (vfModuleInstance) => {
63               updateUniqueNames(vfModuleInstance.instanceName, null, state.serviceInstance[actionData.serviceModelId]);
64             })
65           });
66         }
67         delete state.serviceInstance[actionData.serviceModelId][typeNodeInformation.hierarchyName][actionData.storeKey];
68       }
69       return Object.assign({}, state);
70     }
71
72     case GeneralActions.CHANGE_INSTANCE_COUNTER : {
73       const changeInstanceCounterAction = <ChangeInstanceCounterAction>action;
74       const typeNodeInformation : TypeNodeInformation =  new TypeNodeInformation(changeInstanceCounterAction.node);
75       let newState = _.cloneDeep(state);
76
77       let existing: number = getExistingCounter(newState, changeInstanceCounterAction.serviceUUID, changeInstanceCounterAction.UUID, typeNodeInformation);
78       newState.serviceInstance[changeInstanceCounterAction.serviceUUID][typeNodeInformation.existingMappingCounterName][changeInstanceCounterAction.UUID] = existing ? existing + changeInstanceCounterAction.changeBy : changeInstanceCounterAction.changeBy;
79       return newState;
80     }
81
82     case GeneralActions.DUPLICATE_BULK_INSTANCES : {
83       const createInstanceAction = <DuplicateBulkInstancesAction>action;
84       const typeNodeInformation : TypeNodeInformation =  new TypeNodeInformation(createInstanceAction.node);
85       const serviceId = createInstanceAction.serviceId;
86       const objects = createInstanceAction.objects;
87
88       let newState = _.cloneDeep(state);
89       newState.serviceInstance[serviceId].existingNames = createInstanceAction.existingNames;
90       newState.serviceInstance[serviceId][typeNodeInformation.hierarchyName] = Object.assign({}, newState.serviceInstance[serviceId][typeNodeInformation.hierarchyName], objects);
91       return newState;
92     }
93   }
94 }
95
96 const getExistingCounter = (state: any, serviceModelId: string, modelId: string, typeNodeInformation : TypeNodeInformation ) : number => {
97   const serviceExistingCounterMap = state.serviceInstance[serviceModelId][typeNodeInformation.existingMappingCounterName];
98   if(serviceExistingCounterMap && !_.isNil(serviceExistingCounterMap[modelId])){
99     return serviceExistingCounterMap[modelId];
100   }else {
101     return null;
102   }
103 };
104
105 const updateIsMissingDataOnDelete = (state: any, serviceModelId: string, storeKey: string, node: ITreeNode): void => {
106   const typeNodeInformation : TypeNodeInformation = new TypeNodeInformation(node);
107   let vnf = state.serviceInstance[serviceModelId][typeNodeInformation.hierarchyName][storeKey];
108   if(node.children){
109     _.forOwn(vnf.vfModules, (vfModules, vfModulesKey) => {
110       updateIsMissingDataOnDeleteVFModule(state, serviceModelId, storeKey, vfModulesKey);
111     });
112   }
113
114
115   let isMissingData: boolean = state.serviceInstance[serviceModelId][typeNodeInformation.hierarchyName][storeKey].isMissingData;
116   updateServiceValidationCounter(state, isMissingData, false, serviceModelId);
117 };
118
119 const updateUniqueNames = (oldName : string, newName : string, serviceInstance : ServiceInstance) : void => {
120   let existingNames = serviceInstance.existingNames;
121   if (!_.isNil(oldName) && oldName.toLowerCase() in existingNames) {
122     delete existingNames[oldName.toLowerCase()];
123   }
124   if(!_.isNil(newName)) {
125     existingNames[newName.toLowerCase()] = "";
126   }
127 };
128
129 const updateIsMissingDataOnDeleteVFModule = (state: any, serviceModelId: string, vnfStoreKey: string, vfModuleName): void => {
130   const vfModules = state.serviceInstance[serviceModelId].vnfs[vnfStoreKey].vfModules[vfModuleName];
131
132   _.forOwn(vfModules, (vfModuleInstance) => {
133     let isMissingData: boolean = vfModuleInstance.isMissingData;
134     updateServiceValidationCounter(state, isMissingData, false, serviceModelId);
135   });
136 };
137
138 const updateServiceValidationCounter = (newState: any, oldValidationState: boolean, newValidationState: boolean, serviceUuid: string) => {
139   if (oldValidationState && !newValidationState) {
140     newState.serviceInstance[serviceUuid].validationCounter--;
141   } else if (!oldValidationState && newValidationState) {
142     newState.serviceInstance[serviceUuid].validationCounter++;
143   }
144 };
145
146
147