1 import {Action} from "redux";
3 ChangeInstanceCounterAction, RemoveInstanceAction, DuplicateBulkInstancesAction,
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";
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);
22 case GeneralActions.UPDATE_SUBSCRIBERS: {
23 Object.assign(state, (<UpdateSubscribersAction>action));
24 return Object.assign({}, state);
26 case GeneralActions.UPDATE_AIC_ZONES: {
27 Object.assign(state, (<UpdateAicZonesAction>action));
28 return Object.assign({}, state);
30 case GeneralActions.UPDATE_PRODUCT_FAMILIES: {
31 Object.assign(state, (<UpdateProductFamiliesAction>action));
32 return Object.assign({}, state);
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);
40 case GeneralActions.UPDATE_SERVICE_TYPES: {
41 state.serviceTypes[(<UpdateServiceTypesAction>action).subscriberId] = (<UpdateServiceTypesAction>action).serviceTypes;
42 return Object.assign({}, state);
44 case GeneralActions.UPDATE_CATEGORY_PARAMETERS: {
45 Object.assign(state, (<UpdateCategoryParametersAction>action));
46 return Object.assign({}, state);
48 case GeneralActions.UPDATE_USER_ID: {
49 const updateUserId: UpdateUserIdAction = <UpdateUserIdAction>action;
50 let newState = _.cloneDeep(state);
51 newState['userId'] = updateUserId.userId;
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]);
67 delete state.serviceInstance[actionData.serviceModelId][typeNodeInformation.hierarchyName][actionData.storeKey];
69 return Object.assign({}, state);
72 case GeneralActions.CHANGE_INSTANCE_COUNTER : {
73 const changeInstanceCounterAction = <ChangeInstanceCounterAction>action;
74 const typeNodeInformation : TypeNodeInformation = new TypeNodeInformation(changeInstanceCounterAction.node);
75 let newState = _.cloneDeep(state);
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;
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;
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);
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];
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];
109 _.forOwn(vnf.vfModules, (vfModules, vfModulesKey) => {
110 updateIsMissingDataOnDeleteVFModule(state, serviceModelId, storeKey, vfModulesKey);
115 let isMissingData: boolean = state.serviceInstance[serviceModelId][typeNodeInformation.hierarchyName][storeKey].isMissingData;
116 updateServiceValidationCounter(state, isMissingData, false, serviceModelId);
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()];
124 if(!_.isNil(newName)) {
125 existingNames[newName.toLowerCase()] = "";
129 const updateIsMissingDataOnDeleteVFModule = (state: any, serviceModelId: string, vnfStoreKey: string, vfModuleName): void => {
130 const vfModules = state.serviceInstance[serviceModelId].vnfs[vnfStoreKey].vfModules[vfModuleName];
132 _.forOwn(vfModules, (vfModuleInstance) => {
133 let isMissingData: boolean = vfModuleInstance.isMissingData;
134 updateServiceValidationCounter(state, isMissingData, false, serviceModelId);
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++;