Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 1 | import { Action } from 'redux'; |
| 2 | import { |
| 3 | CREATE_VF_MODULE, |
| 4 | CreateVFModuleInstanceAction, |
| 5 | DELETE_SERVICE_INSTANCE, |
| 6 | DELETE_VNF_INSTANCE, |
| 7 | DELETE_VNF_MODULE_INSTANCE, |
| 8 | DeleteServiceInstanceAction, |
| 9 | DeleteVfModuleInstanceAction, |
| 10 | DeleteVnfInstanceAction, |
| 11 | UPDATE_AIC_ZONES, |
| 12 | UPDATE_CATEGORY_PARAMETERS, |
| 13 | UPDATE_LCP_REGIONS_AND_TENANTS, |
| 14 | UPDATE_MODEL, |
| 15 | UPDATE_NETWORK_FUNCTION, |
| 16 | UPDATE_PRODUCT_FAMILIES, |
| 17 | UPDATE_SERVICE_INSTANCE, |
| 18 | UPDATE_SERVICE_TYPES, |
| 19 | UPDATE_SUBSCRIBERS, |
| 20 | UPDATE_USER_ID, |
| 21 | UPDATE_VF_MODULE, |
| 22 | UPDATE_VNF_INSTANCE, |
| 23 | UpdateAicZonesAction, |
| 24 | UpdateCategoryParametersAction, |
| 25 | UpdateLcpRegionsAndTenantsAction, |
| 26 | UpdateNetworkCollectionFunction, |
| 27 | UpdateProductFamiliesAction, |
| 28 | UpdateServiceInstanceAction, |
| 29 | UpdateServiceModelAction, |
| 30 | UpdateServiceTypesAction, |
| 31 | UpdateSubscribersAction, |
| 32 | UpdateUserIdAction, |
| 33 | UpdateVFModuleInstanceAction, |
| 34 | UpdateVnfInstanceAction, |
| 35 | } from './service.actions'; |
| 36 | import { LcpRegionsAndTenants } from './shared/models/lcpRegionsAndTenants'; |
| 37 | import * as _ from 'lodash'; |
| 38 | import { ServiceInstance } from './shared/models/serviceInstance'; |
| 39 | import { CategoryParams } from './shared/models/categoryParams'; |
| 40 | import { SelectOptionInterface } from './shared/models/selectOption'; |
| 41 | import { ServiceType } from './shared/models/serviceType'; |
| 42 | import { VnfInstance } from './shared/models/vnfInstance'; |
| 43 | import { VfModuleMap } from './shared/models/vfModulesMap'; |
| 44 | |
| 45 | export interface ServiceState { |
| 46 | serviceHierarchy: any; |
| 47 | serviceInstance: { [uuid: string]: ServiceInstance; }; |
| 48 | lcpRegionsAndTenants: LcpRegionsAndTenants; |
| 49 | subscribers: SelectOptionInterface[]; |
| 50 | productFamilies: any; |
| 51 | serviceTypes: { [subscriberId: string]: ServiceType[]; }; |
| 52 | aicZones: SelectOptionInterface[]; |
| 53 | categoryParameters: CategoryParams; |
| 54 | } |
| 55 | |
| 56 | const initialState: ServiceState = { |
| 57 | serviceHierarchy: {}, |
| 58 | serviceInstance: {}, |
| 59 | lcpRegionsAndTenants: new LcpRegionsAndTenants(), |
| 60 | subscribers: null, |
| 61 | productFamilies: null, |
| 62 | serviceTypes: {}, |
| 63 | aicZones: null, |
| 64 | categoryParameters: new CategoryParams() |
| 65 | }; |
| 66 | |
| 67 | export const ServiceReducer = |
| 68 | function (state: ServiceState = initialState, action: Action): ServiceState { |
| 69 | switch (action.type) { |
| 70 | case UPDATE_MODEL: { |
| 71 | let uuid = (<UpdateServiceModelAction>action).serviceHierarchy.service.uuid; |
| 72 | state.serviceHierarchy[uuid] = _.cloneDeep((<UpdateServiceModelAction>action).serviceHierarchy); |
| 73 | return Object.assign({}, state); |
| 74 | } |
| 75 | case UPDATE_SERVICE_INSTANCE: { |
| 76 | const updateServiceInstanceAction = <UpdateServiceInstanceAction>action; |
| 77 | const uuid = updateServiceInstanceAction.serviceUuid; |
| 78 | const newState = _.cloneDeep(state); |
| 79 | |
| 80 | const serviceInstance: ServiceInstance = newState.serviceInstance[uuid] || new ServiceInstance(); |
| 81 | |
| 82 | newState.serviceInstance[uuid] = Object.assign(serviceInstance, updateServiceInstanceAction.serviceInstance); |
| 83 | return newState; |
| 84 | } |
| 85 | case UPDATE_VNF_INSTANCE: { |
| 86 | const updateVnfInstanceAction = <UpdateVnfInstanceAction>action; |
| 87 | const serviceUuid = updateVnfInstanceAction.serviceUuid; |
| 88 | const vnfModelName = updateVnfInstanceAction.vnfModelName; |
| 89 | |
| 90 | const newState = _.cloneDeep(state); |
| 91 | const vnfInstance: VnfInstance = newState.serviceInstance[serviceUuid].vnfs[vnfModelName] || new VnfInstance(); |
| 92 | |
| 93 | newState.serviceInstance[serviceUuid].vnfs[vnfModelName] = Object.assign(vnfInstance, updateVnfInstanceAction.vnfInstance); |
| 94 | return newState; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | case UPDATE_USER_ID: { |
| 99 | const updateUserId : UpdateUserIdAction = <UpdateUserIdAction>action; |
| 100 | // var newState2 = {...state,'userId':updateUserId.userId} |
| 101 | var newState = _.cloneDeep(state); |
| 102 | newState['userId'] = updateUserId.userId; |
| 103 | return newState; |
| 104 | |
| 105 | // state = (... {userId:action["userId"]},state]} |
| 106 | } |
| 107 | |
| 108 | case UPDATE_VF_MODULE: { |
| 109 | const updateVFModuleInstanceAction = <UpdateVFModuleInstanceAction>action; |
| 110 | const vfInstance = updateVFModuleInstanceAction.vfInstance; |
| 111 | const serviceUuid = updateVFModuleInstanceAction.serviceUuid; |
| 112 | const vfModuleId = updateVFModuleInstanceAction.vfId; |
| 113 | const newState = _.cloneDeep(state); |
| 114 | const vnfs = newState.serviceHierarchy[serviceUuid].vnfs; |
| 115 | const vnfId = getVfModuleParentVnfId(vnfs, vfModuleId); |
| 116 | let vfModulesMap = newState.serviceInstance[serviceUuid].vnfs[vnfId].vfModules[vfModuleId] || new VfModuleMap(); |
| 117 | vfModulesMap[vfModuleId] = vfInstance; |
| 118 | newState.serviceInstance[serviceUuid].vnfs[vnfId].vfModules[vfModuleId] = vfModulesMap; |
| 119 | return newState; |
| 120 | } |
| 121 | |
| 122 | case CREATE_VF_MODULE: { |
| 123 | const updateVFModuleInstanceAction = <CreateVFModuleInstanceAction>action; |
| 124 | const vfInstance = updateVFModuleInstanceAction.vfInstance; |
| 125 | const serviceUuid = updateVFModuleInstanceAction.serviceUuid; |
| 126 | const vfModuleId = updateVFModuleInstanceAction.vfId; |
| 127 | const index = updateVFModuleInstanceAction.index; |
| 128 | let newState = Object.assign({}, state); |
| 129 | const vnfs = newState.serviceHierarchy[serviceUuid].vnfs; |
| 130 | const vnfId = getVfModuleParentVnfId(vnfs, vfModuleId); |
| 131 | let vfModulesMap = newState.serviceInstance[serviceUuid].vnfs[vnfId].vfModules[vfModuleId] || new VfModuleMap(); |
| 132 | let randomId = generateId(); |
| 133 | vfModulesMap[vfModuleId + randomId] = vfInstance; |
| 134 | |
| 135 | newState.serviceInstance[serviceUuid].vnfs[vnfId].vfModules[vfModuleId] = vfModulesMap; |
| 136 | return newState; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | case UPDATE_LCP_REGIONS_AND_TENANTS: { |
| 141 | Object.assign(state, (<UpdateLcpRegionsAndTenantsAction>action)); |
| 142 | return Object.assign({}, state); |
| 143 | } |
| 144 | case UPDATE_SUBSCRIBERS: { |
| 145 | Object.assign(state, (<UpdateSubscribersAction>action)); |
| 146 | return Object.assign({}, state); |
| 147 | } |
| 148 | case UPDATE_AIC_ZONES: { |
| 149 | Object.assign(state, (<UpdateAicZonesAction>action)); |
| 150 | return Object.assign({}, state); |
| 151 | } |
| 152 | case UPDATE_PRODUCT_FAMILIES: { |
| 153 | Object.assign(state, (<UpdateProductFamiliesAction>action)); |
| 154 | return Object.assign({}, state); |
| 155 | } |
| 156 | case UPDATE_NETWORK_FUNCTION: { |
| 157 | let networkFunctionReduxObj = state["networkFunctions"] == undefined ? {} : state["networkFunctions"]; |
| 158 | networkFunctionReduxObj[(<UpdateNetworkCollectionFunction>action).network_function] = (<UpdateNetworkCollectionFunction>action).networksAccordingToNetworkCollection; |
| 159 | Object.assign(state, {"networkFunctions":networkFunctionReduxObj}); |
| 160 | return Object.assign({}, state); |
| 161 | } |
| 162 | case UPDATE_SERVICE_TYPES: { |
| 163 | let subscriberId = (<UpdateServiceTypesAction>action).subscriberId; |
| 164 | let serviceTypes = (<UpdateServiceTypesAction>action).serviceTypes; |
| 165 | state.serviceTypes[subscriberId] = serviceTypes; |
| 166 | return Object.assign({}, state); |
| 167 | } |
| 168 | case UPDATE_CATEGORY_PARAMETERS: { |
| 169 | Object.assign(state, (<UpdateCategoryParametersAction>action)); |
| 170 | return Object.assign({}, state); |
| 171 | } |
| 172 | case DELETE_SERVICE_INSTANCE: { |
| 173 | const uuid = (<DeleteServiceInstanceAction>action).serviceUuid; |
| 174 | if (state.serviceHierarchy[uuid]) { |
| 175 | let newState = _.omit(state, ['serviceInstance[' + uuid + ']']); |
| 176 | return Object.assign({}, state, newState); |
| 177 | } |
| 178 | return Object.assign({}, state); |
| 179 | } |
| 180 | case DELETE_VNF_INSTANCE: { |
| 181 | const actionData =(<DeleteVnfInstanceAction>action); |
| 182 | if(state.serviceInstance[actionData.serviceModelId]){ |
| 183 | delete state.serviceInstance[actionData.serviceModelId].vnfs[actionData.modelName]; |
| 184 | } |
| 185 | return Object.assign({}, state); |
| 186 | } |
| 187 | |
| 188 | case DELETE_VNF_MODULE_INSTANCE: { |
| 189 | const actionData =(<DeleteVfModuleInstanceAction>action); |
| 190 | if(state.serviceInstance[actionData.serviceModelId]){ |
| 191 | delete state.serviceInstance[actionData.serviceModelId].vnfs[actionData.vfName].vfModules[actionData.modelName]; |
| 192 | } |
| 193 | return Object.assign({}, state); |
| 194 | } |
| 195 | default: |
| 196 | return Object.assign({}, state); |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | const generateId = () => { |
| 201 | return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5); |
| 202 | }; |
| 203 | |
| 204 | |
| 205 | const getVfModuleParentVnfId = (vnfs: object, vfModuleId: string) => { |
| 206 | let vnfId = undefined; |
| 207 | _.forOwn(vnfs, (value, key) => { |
| 208 | if (vnfs[key].vfModules && vnfs[key].vfModules[vfModuleId]) { |
| 209 | vnfId = vnfs[key].modelCustomizationName; |
| 210 | return false; |
| 211 | } |
| 212 | }); |
| 213 | return vnfId; |
| 214 | }; |