1 import {Action} from "redux";
5 CreateServiceInstanceAction,
7 UndoUpgradeServiceAction,
8 UpdateServiceInstanceAction,
9 UpdateServiceModelAction,
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";
17 export function serviceReducer(state: ServiceState, action: Action) : ServiceState{
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;
26 updateUniqueNames(serviceInstance.instanceName, updateServiceInstanceAction.serviceInstance.instanceName, newState.serviceInstance[uuid]);
28 newState.serviceInstance[uuid] = _.merge(newState.serviceInstance[uuid], serviceInstance);
31 case ServiceActions.CREATE_SERVICE_INSTANCE : {
32 const updateServiceInstanceAction = <CreateServiceInstanceAction>action;
33 const uuid = updateServiceInstanceAction.serviceUuid;
34 let newState = _.cloneDeep(state);
36 const serviceInstance: ServiceInstance = new ServiceInstance();
37 const currentInstaceName = state.serviceInstance[uuid] ? serviceInstance.instanceName : null;
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]);
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);
52 return Object.assign({}, state);
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);
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);
65 case ServiceActions.CHANGE_SERVICE_IS_DIRTY : {
66 let newState = _.cloneDeep(state);
67 let serviceInstanceAction: ServiceInstanceActions = newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].action;
69 if(serviceInstanceAction !== ServiceInstanceActions.None){
70 newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = true;
74 const nodes = (<ChangeServiceDirty>action).nodes;
75 for(let node of nodes){
76 const dirty = isDirty(node);
78 newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = true;
82 newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = false;
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);
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);
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;
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()];
120 if(!_.isNil(newName)) {
121 existingNames[newName.toLowerCase()] = "";
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;
129 clonedState.serviceInstance[uuid].isUpgraded = true;
130 clonedState.serviceInstance[uuid].upgradedVFMSonsCounter++;
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;