Merge changes from topics "VID-14", "VID-13", "VID-12"
[vid.git] / vid-webpack-master / src / app / shared / storeUtil / utils / service / service.reducers.ts
1 import {Action} from "redux";
2 import {
3   AddServiceAction,
4   ChangeServiceDirty,
5   CreateServiceInstanceAction,
6   ServiceActions,
7   UndoUpgradeServiceAction,
8   UpdateServiceInstanceAction,
9   UpdateServiceModelAction,
10   UpgradeServiceAction
11 } from "./service.actions";
12 import {ServiceInstance} from "../../../models/serviceInstance";
13 import {ServiceState} from "../main.reducer";
14 import {ServiceInstanceActions} from "../../../models/serviceInstanceActions";
15 import * as _ from "lodash";
16
17 export function serviceReducer(state: ServiceState, action: Action) : ServiceState{
18
19   switch (action.type) {
20     case ServiceActions.UPDATE_SERVICE_INSTANCE : {
21       let newState = _.cloneDeep(state);
22       const updateServiceInstanceAction = <UpdateServiceInstanceAction>action;
23       const uuid = updateServiceInstanceAction.serviceUuid;
24       const serviceInstance = updateServiceInstanceAction.serviceInstance;
25
26       updateUniqueNames(serviceInstance.instanceName, updateServiceInstanceAction.serviceInstance.instanceName, newState.serviceInstance[uuid]);
27
28       newState.serviceInstance[uuid] = _.merge(newState.serviceInstance[uuid], serviceInstance);
29       return newState;
30     }
31     case ServiceActions.CREATE_SERVICE_INSTANCE : {
32       const updateServiceInstanceAction = <CreateServiceInstanceAction>action;
33       const uuid = updateServiceInstanceAction.serviceUuid;
34       let newState = _.cloneDeep(state);
35
36       const serviceInstance: ServiceInstance =  new ServiceInstance();
37       const currentInstaceName = state.serviceInstance[uuid] ? serviceInstance.instanceName : null;
38
39       newState.serviceInstance[uuid] = Object.assign(serviceInstance, updateServiceInstanceAction.serviceInstance);
40       newState.serviceInstance[uuid].vidNotions = _.get(state,`serviceHierarchy[${uuid}].service.vidNotions`);
41       if (!_.isNil(updateServiceInstanceAction.serviceInstance)) {
42         updateUniqueNames(currentInstaceName, updateServiceInstanceAction.serviceInstance.instanceName, newState.serviceInstance[uuid]);
43       }
44       return newState;
45     }
46     case ServiceActions.DELETE_ALL_SERVICE_INSTANCES: {
47       if (state.serviceInstance) {
48         let newState = _.cloneDeep(state);
49         newState.serviceInstance = {};
50         return Object.assign({}, state, newState);
51       }
52       return Object.assign({}, state);
53     }
54     case ServiceActions.UPDATE_MODEL: {
55       let uuid = (<UpdateServiceModelAction>action).serviceHierarchy.service.uuid;
56       state.serviceHierarchy[uuid] = _.cloneDeep((<UpdateServiceModelAction>action).serviceHierarchy);
57       return Object.assign({}, state);
58     }
59     case ServiceActions.ADD_SERVICE_ACTION: {
60       const uuid: string = (<AddServiceAction>action).serviceUuid;
61       const actionToAdd: ServiceInstanceActions =  (<AddServiceAction>action).action;
62       state.serviceInstance[uuid].action =  actionToAdd;
63       return Object.assign({}, state);
64     }
65     case ServiceActions.CHANGE_SERVICE_IS_DIRTY : {
66       let newState = _.cloneDeep(state);
67       let serviceInstanceAction: ServiceInstanceActions = newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].action;
68
69       if(serviceInstanceAction !== ServiceInstanceActions.None){
70         newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = true;
71         return newState;
72       }
73
74       const nodes =  (<ChangeServiceDirty>action).nodes;
75       for(let node of nodes){
76         const dirty = isDirty(node);
77         if(dirty) {
78           newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = true;
79           return newState;
80         }
81       }
82       newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = false;
83       return newState;
84     }
85     case ServiceActions.UPGRADE_SERVICE_ACTION: {
86       let clonedState = _.cloneDeep(state);
87       let oldServiceAction: string = ServiceInstanceActions.None;
88       const castingAction = <UpgradeServiceAction>action;
89       const uuid: string = castingAction.serviceUuid;
90       return upgradeServiceInstance(clonedState, uuid, oldServiceAction);
91     }
92
93     case ServiceActions.UNDO_UPGRADE_SERVICE_ACTION: {
94       let clonedState = _.cloneDeep(state);
95       const castingAction = <UndoUpgradeServiceAction>action;
96       const uuid: string = castingAction.serviceUuid;
97       if(!_.isNil(clonedState.serviceInstance[uuid].action) && clonedState.serviceInstance[uuid].action.includes("Upgrade")) {
98         return undoUpgradeServiceInstance(clonedState, uuid);
99       }
100     }
101   }
102 }
103
104 const isDirty = (node) : boolean => {
105   if(node.action !== ServiceInstanceActions.None) return true;
106   if(!_.isNil(node.children) && node.children.length > 0){
107     for(let child of node.children){
108       const dirty: boolean = isDirty(child);
109       if(dirty) return true;
110     }
111   }
112   return false;
113 };
114
115 const updateUniqueNames = (oldName : string, newName : string, serviceInstance : ServiceInstance) : void => {
116   let existingNames = serviceInstance.existingNames;
117   if (!_.isNil(oldName) && oldName.toLowerCase() in existingNames) {
118     delete existingNames[oldName.toLowerCase()];
119   }
120   if(!_.isNil(newName)) {
121     existingNames[newName.toLowerCase()] = "";
122   }
123 };
124
125 function upgradeServiceInstance(clonedState, uuid: string, oldServiceAction: string) {
126   if(!clonedState.serviceInstance[uuid].action.includes("Upgrade")){
127     clonedState.serviceInstance[uuid].action = (`${oldServiceAction}_Upgrade`) as ServiceInstanceActions;
128   }
129   clonedState.serviceInstance[uuid].isUpgraded = true;
130   clonedState.serviceInstance[uuid].upgradedVFMSonsCounter++;
131   return clonedState;
132 }
133
134 function undoUpgradeServiceInstance(clonedState, uuid: string) {
135   clonedState.serviceInstance[uuid].upgradedVFMSonsCounter--;
136   if(clonedState.serviceInstance[uuid].upgradedVFMSonsCounter == 0){
137     clonedState.serviceInstance[uuid].action = ServiceInstanceActions.None;
138     clonedState.serviceInstance[uuid].isUpgraded = false;
139   }
140   return clonedState;
141 }
142
143
144