aboutsummaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/service.reducer.ts
blob: a11f31e805094685b8103b7e9984f1e8d460f7e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { Action } from 'redux';
import {
  CREATE_VF_MODULE,
  CreateVFModuleInstanceAction,
  DELETE_SERVICE_INSTANCE,
  DELETE_VNF_INSTANCE,
  DELETE_VNF_MODULE_INSTANCE,
  DeleteServiceInstanceAction,
  DeleteVfModuleInstanceAction,
  DeleteVnfInstanceAction,
  UPDATE_AIC_ZONES,
  UPDATE_CATEGORY_PARAMETERS,
  UPDATE_LCP_REGIONS_AND_TENANTS,
  UPDATE_MODEL,
  UPDATE_NETWORK_FUNCTION,
  UPDATE_PRODUCT_FAMILIES,
  UPDATE_SERVICE_INSTANCE,
  UPDATE_SERVICE_TYPES,
  UPDATE_SUBSCRIBERS,
  UPDATE_USER_ID,
  UPDATE_VF_MODULE,
  UPDATE_VNF_INSTANCE,
  UpdateAicZonesAction,
  UpdateCategoryParametersAction,
  UpdateLcpRegionsAndTenantsAction,
  UpdateNetworkCollectionFunction,
  UpdateProductFamiliesAction,
  UpdateServiceInstanceAction,
  UpdateServiceModelAction,
  UpdateServiceTypesAction,
  UpdateSubscribersAction,
  UpdateUserIdAction,
  UpdateVFModuleInstanceAction,
  UpdateVnfInstanceAction,
} from './service.actions';
import { LcpRegionsAndTenants } from './shared/models/lcpRegionsAndTenants';
import * as _ from 'lodash';
import { ServiceInstance } from './shared/models/serviceInstance';
import { CategoryParams } from './shared/models/categoryParams';
import { SelectOptionInterface } from './shared/models/selectOption';
import { ServiceType } from './shared/models/serviceType';
import { VnfInstance } from './shared/models/vnfInstance';
import { VfModuleMap } from './shared/models/vfModulesMap';

export interface ServiceState {
  serviceHierarchy: any;
  serviceInstance: { [uuid: string]: ServiceInstance; };
  lcpRegionsAndTenants: LcpRegionsAndTenants;
  subscribers: SelectOptionInterface[];
  productFamilies: any;
  serviceTypes: { [subscriberId: string]: ServiceType[]; };
  aicZones: SelectOptionInterface[];
  categoryParameters: CategoryParams;
}

const initialState: ServiceState = {
  serviceHierarchy: {},
  serviceInstance: {},
  lcpRegionsAndTenants: new LcpRegionsAndTenants(),
  subscribers: null,
  productFamilies: null,
  serviceTypes: {},
  aicZones: null,
  categoryParameters: new CategoryParams()
};

export const ServiceReducer =
  function (state: ServiceState = initialState, action: Action): ServiceState {
    switch (action.type) {
      case UPDATE_MODEL: {
        let uuid = (<UpdateServiceModelAction>action).serviceHierarchy.service.uuid;
        state.serviceHierarchy[uuid] = _.cloneDeep((<UpdateServiceModelAction>action).serviceHierarchy);
        return Object.assign({}, state);
      }
      case UPDATE_SERVICE_INSTANCE: {
        const updateServiceInstanceAction = <UpdateServiceInstanceAction>action;
        const uuid = updateServiceInstanceAction.serviceUuid;
        const newState = _.cloneDeep(state);

        const serviceInstance: ServiceInstance = newState.serviceInstance[uuid] || new ServiceInstance();

        newState.serviceInstance[uuid] = Object.assign(serviceInstance, updateServiceInstanceAction.serviceInstance);
        return newState;
      }
      case UPDATE_VNF_INSTANCE: {
        const updateVnfInstanceAction = <UpdateVnfInstanceAction>action;
        const serviceUuid = updateVnfInstanceAction.serviceUuid;
        const vnfModelName = updateVnfInstanceAction.vnfModelName;

        const newState = _.cloneDeep(state);
        const vnfInstance: VnfInstance = newState.serviceInstance[serviceUuid].vnfs[vnfModelName] || new VnfInstance();

        newState.serviceInstance[serviceUuid].vnfs[vnfModelName] = Object.assign(vnfInstance, updateVnfInstanceAction.vnfInstance);
        return newState;
      }


      case UPDATE_USER_ID: {
        const updateUserId : UpdateUserIdAction = <UpdateUserIdAction>action;
        // var newState2 = {...state,'userId':updateUserId.userId}
        var newState = _.cloneDeep(state);
        newState['userId'] = updateUserId.userId;
        return newState;

        // state = (...  {userId:action["userId"]},state]}
      }

      case UPDATE_VF_MODULE: {
        const updateVFModuleInstanceAction = <UpdateVFModuleInstanceAction>action;
        const vfInstance = updateVFModuleInstanceAction.vfInstance;
        const serviceUuid = updateVFModuleInstanceAction.serviceUuid;
        const vfModuleId = updateVFModuleInstanceAction.vfId;
        const newState = _.cloneDeep(state);
        const vnfs = newState.serviceHierarchy[serviceUuid].vnfs;
        const vnfId = getVfModuleParentVnfId(vnfs, vfModuleId);
        let vfModulesMap = newState.serviceInstance[serviceUuid].vnfs[vnfId].vfModules[vfModuleId] || new VfModuleMap();
        vfModulesMap[vfModuleId] = vfInstance;
        newState.serviceInstance[serviceUuid].vnfs[vnfId].vfModules[vfModuleId] = vfModulesMap;
        return newState;
      }

      case CREATE_VF_MODULE: {
        const updateVFModuleInstanceAction = <CreateVFModuleInstanceAction>action;
        const vfInstance = updateVFModuleInstanceAction.vfInstance;
        const serviceUuid = updateVFModuleInstanceAction.serviceUuid;
        const vfModuleId = updateVFModuleInstanceAction.vfId;
        const index = updateVFModuleInstanceAction.index;
        let newState = Object.assign({}, state);
        const vnfs = newState.serviceHierarchy[serviceUuid].vnfs;
        const vnfId = getVfModuleParentVnfId(vnfs, vfModuleId);
        let vfModulesMap = newState.serviceInstance[serviceUuid].vnfs[vnfId].vfModules[vfModuleId] || new VfModuleMap();
        let randomId  = generateId();
        vfModulesMap[vfModuleId + randomId] = vfInstance;

        newState.serviceInstance[serviceUuid].vnfs[vnfId].vfModules[vfModuleId] = vfModulesMap;
        return newState;
      }


      case UPDATE_LCP_REGIONS_AND_TENANTS: {
        Object.assign(state, (<UpdateLcpRegionsAndTenantsAction>action));
        return Object.assign({}, state);
      }
      case UPDATE_SUBSCRIBERS: {
        Object.assign(state, (<UpdateSubscribersAction>action));
        return Object.assign({}, state);
      }
      case UPDATE_AIC_ZONES: {
        Object.assign(state, (<UpdateAicZonesAction>action));
        return Object.assign({}, state);
      }
      case UPDATE_PRODUCT_FAMILIES: {
        Object.assign(state, (<UpdateProductFamiliesAction>action));
        return Object.assign({}, state);
      }
      case UPDATE_NETWORK_FUNCTION: {
        let networkFunctionReduxObj = state["networkFunctions"] == undefined ? {} : state["networkFunctions"];
        networkFunctionReduxObj[(<UpdateNetworkCollectionFunction>action).network_function] = (<UpdateNetworkCollectionFunction>action).networksAccordingToNetworkCollection;
        Object.assign(state, {"networkFunctions":networkFunctionReduxObj});
        return Object.assign({}, state);
      }
      case UPDATE_SERVICE_TYPES: {
        let subscriberId = (<UpdateServiceTypesAction>action).subscriberId;
        let serviceTypes = (<UpdateServiceTypesAction>action).serviceTypes;
        state.serviceTypes[subscriberId] = serviceTypes;
        return Object.assign({}, state);
      }
      case UPDATE_CATEGORY_PARAMETERS: {
        Object.assign(state, (<UpdateCategoryParametersAction>action));
        return Object.assign({}, state);
      }
      case DELETE_SERVICE_INSTANCE: {
        const uuid = (<DeleteServiceInstanceAction>action).serviceUuid;
        if (state.serviceHierarchy[uuid]) {
          let newState = _.omit(state, ['serviceInstance[' + uuid + ']']);
          return Object.assign({}, state, newState);
        }
        return Object.assign({}, state);
      }
      case DELETE_VNF_INSTANCE: {
        const actionData =(<DeleteVnfInstanceAction>action);
        if(state.serviceInstance[actionData.serviceModelId]){
          delete state.serviceInstance[actionData.serviceModelId].vnfs[actionData.modelName];
        }
        return Object.assign({}, state);
      }

      case DELETE_VNF_MODULE_INSTANCE: {
        const actionData =(<DeleteVfModuleInstanceAction>action);
        if(state.serviceInstance[actionData.serviceModelId]){
          delete state.serviceInstance[actionData.serviceModelId].vnfs[actionData.vfName].vfModules[actionData.modelName];
        }
        return Object.assign({}, state);
      }
      default:
        return Object.assign({}, state);
    }
  };

const generateId = () => {
  return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);
};


const getVfModuleParentVnfId = (vnfs: object, vfModuleId: string) => {
  let vnfId = undefined;
  _.forOwn(vnfs, (value, key) => {
    if (vnfs[key].vfModules && vnfs[key].vfModules[vfModuleId]) {
      vnfId = vnfs[key].modelCustomizationName;
      return false;
    }
  });
  return vnfId;
};