Create new VSP, onboard from TOSCA file - UI
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / SoftwareProduct.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 import {connect} from 'react-redux';
17
18 import i18n from 'nfvo-utils/i18n/i18n.js';
19 import VersionControllerUtils from 'nfvo-components/panel/versionController/VersionControllerUtils.js';
20 import TabulatedEditor from 'src/nfvo-components/editor/TabulatedEditor.jsx';
21
22 import {enums} from 'sdc-app/onboarding/OnboardingConstants.js';
23 import OnboardingActionHelper from 'sdc-app/onboarding/OnboardingActionHelper.js';
24
25 import {navigationItems, mapScreenToNavigationItem, onboardingMethod as onboardingMethodTypes, onboardingOriginTypes} from './SoftwareProductConstants.js';
26 import SoftwareProductActionHelper from './SoftwareProductActionHelper.js';
27 import SoftwareProductComponentsActionHelper from './components/SoftwareProductComponentsActionHelper.js';
28 import SoftwareProductDependenciesActionHelper from './dependencies/SoftwareProductDependenciesActionHelper.js';
29
30 import HeatSetupActionHelper from './attachments/setup/HeatSetupActionHelper.js';
31 import { actionsEnum as versionControllerActions } from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
32
33 function getActiveNavigationId(screen, componentId) {
34         let activeItemId = componentId ? mapScreenToNavigationItem[screen] + '|' + componentId : mapScreenToNavigationItem[screen];
35         return activeItemId;
36 }
37
38 const buildComponentNavigationBarGroups = ({componentId, meta}) => {
39         const groups = ([
40                 {
41                         id: navigationItems.GENERAL + '|' + componentId,
42                         name: i18n('General'),
43                         disabled: false,
44                         meta
45                 }, {
46                         id: navigationItems.COMPUTE + '|' + componentId,
47                         name: i18n('Compute'),
48                         disabled: false,
49                         meta
50                 }, {
51                         id: navigationItems.LOAD_BALANCING + '|' + componentId,
52                         name: i18n('High Availability & Load Balancing'),
53                         disabled: false,
54                         meta
55                 }, {
56                         id: navigationItems.NETWORKS + '|' + componentId,
57                         name: i18n('Networks'),
58                         disabled: false,
59                         meta
60                 }, {
61                         id: navigationItems.STORAGE + '|' + componentId,
62                         name: i18n('Storage'),
63                         disabled: false,
64                         meta
65                 }, {
66                         id: navigationItems.IMAGES + '|' + componentId,
67                         name: i18n('Images'),
68                         disabled: false,
69                         meta
70                 }, {
71                         id: navigationItems.PROCESS_DETAILS + '|' + componentId,
72                         name: i18n('Process Details'),
73                         disabled: false,
74                         meta
75                 }, {
76                         id: navigationItems.MONITORING + '|' + componentId,
77                         name: i18n('Monitoring'),
78                         disabled: false,
79                         meta
80                 }
81         ]);
82
83         return groups;
84 };
85
86 const buildNavigationBarProps = ({softwareProduct, meta, screen, componentId, componentsList, mapOfExpandedIds}) => {
87         const {softwareProductEditor: {data: currentSoftwareProduct = {}}} = softwareProduct;
88         const {id, name, onboardingMethod, onboardingOrigin} = currentSoftwareProduct;
89         const groups = [{
90                 id: id,
91                 name: name,
92                 items: [
93                         {
94                                 id: navigationItems.VENDOR_SOFTWARE_PRODUCT,
95                                 name: i18n('Overview'),
96                                 disabled: false,
97                                 meta
98                         }, {
99                                 id: navigationItems.GENERAL,
100                                 name: i18n('General'),
101                                 disabled: false,
102                                 meta
103                         },
104                         {
105                                 id: navigationItems.DEPLOYMENT_FLAVORS,
106                                 name: i18n('Deployment Flavors'),
107                                 disabled: false,
108                                 hidden: onboardingMethod !== onboardingMethodTypes.MANUAL,
109                                 meta
110                         }, {
111                                 id: navigationItems.PROCESS_DETAILS,
112                                 name: i18n('Process Details'),
113                                 disabled: false,
114                                 meta
115                         }, {
116                                 id: navigationItems.NETWORKS,
117                                 name: i18n('Networks'),
118                                 disabled: false,
119                                 meta
120                         }, {
121                                 id: navigationItems.ATTACHMENTS,
122                                 name: i18n('Attachments'),
123                                 disabled: false,
124                                 hidden: onboardingOrigin === onboardingOriginTypes.NONE,
125                                 meta
126                         }, {
127                                 id: navigationItems.ACTIVITY_LOG,
128                                 name: i18n('Activity Log'),
129                                 disabled: false,
130                                 meta
131                         }, {
132                                 id: navigationItems.DEPENDENCIES,
133                                 name: i18n('Component Dependencies'),
134                                 hidden: componentsList.length <= 1,
135                                 disabled: false,
136                                 meta
137                         }, {
138                                 id: navigationItems.COMPONENTS,
139                                 name: i18n('Components'),
140                                 hidden: componentsList.length <= 0,
141                                 meta,
142                                 expanded: mapOfExpandedIds[navigationItems.COMPONENTS] === true && screen !== enums.SCREEN.SOFTWARE_PRODUCT_LANDING_PAGE,
143                                 items: [
144                                         ...componentsList.map(({id, displayName}) => ({
145                                                 id: navigationItems.COMPONENTS + '|' + id,
146                                                 name: displayName,
147                                                 meta,
148                                                 expanded: mapOfExpandedIds[navigationItems.COMPONENTS + '|' + id] === true  && screen !== enums.SCREEN.SOFTWARE_PRODUCT_LANDING_PAGE,
149                                                 items: buildComponentNavigationBarGroups({componentId: id, meta})
150                                         }))
151                                 ]
152                         }
153                 ]
154         }];
155         let activeItemId = getActiveNavigationId(screen, componentId);
156         return {
157                 activeItemId, groups
158         };
159 };
160
161 const buildVersionControllerProps = (softwareProduct) => {
162         const {softwareProductEditor} = softwareProduct;
163         const {data: currentSoftwareProduct = {}, isValidityData = true} = softwareProductEditor;
164
165         const {version, viewableVersions, status: currentStatus, lockingUser} = currentSoftwareProduct;
166         const {status, isCheckedOut} = VersionControllerUtils.getCheckOutStatusKindByUserID(currentStatus, lockingUser);
167
168         return {
169                 status, isCheckedOut, version, viewableVersions,
170                 isFormDataValid: isValidityData
171         };
172 };
173
174 function buildMeta({softwareProduct, componentId, softwareProductDependencies}) {
175         const {softwareProductEditor, softwareProductComponents, softwareProductQuestionnaire, softwareProductAttachments} = softwareProduct;
176         const {data: currentSoftwareProduct = {}} = softwareProductEditor;
177         const {version, onboardingOrigin} = currentSoftwareProduct;
178         const isReadOnlyMode = VersionControllerUtils.isReadOnly(currentSoftwareProduct);
179         const {qdata} = softwareProductQuestionnaire;
180         const {heatSetup, heatSetupCache} = softwareProductAttachments;
181         let currentComponentMeta = {};
182         if(componentId) {
183                 const {componentEditor: {data: componentData = {} , qdata: componentQdata}} = softwareProductComponents;
184                 currentComponentMeta = {componentData, componentQdata};
185         }
186         const meta = {softwareProduct: currentSoftwareProduct, qdata, version, onboardingOrigin, heatSetup, heatSetupCache, isReadOnlyMode, currentComponentMeta, softwareProductDependencies};
187         return meta;
188 }
189
190 const mapStateToProps = ({softwareProduct}, {currentScreen: {screen, props: {componentId}}}) => {
191         const {softwareProductEditor, softwareProductComponents, softwareProductDependencies} = softwareProduct;
192         const {mapOfExpandedIds = []} = softwareProductEditor;
193         const {componentsList = []} = softwareProductComponents;
194
195         const meta = buildMeta({softwareProduct, componentId, softwareProductDependencies});
196         return {
197                 versionControllerProps: buildVersionControllerProps(softwareProduct),
198                 navigationBarProps: buildNavigationBarProps({softwareProduct, meta, screen, componentId, componentsList, mapOfExpandedIds}),
199                 meta
200         };
201 };
202
203 const autoSaveBeforeNavigate = ({dispatch, screen, softwareProductId, componentId,
204                 meta: {isReadOnlyMode, softwareProduct, version, qdata, softwareProductDependencies,
205                 currentComponentMeta: {componentData, componentQdata}}}) => {
206         let promise;
207         if (isReadOnlyMode) {
208                 promise = Promise.resolve();
209         } else {
210                 switch(screen) {
211                         case enums.SCREEN.SOFTWARE_PRODUCT_DEPENDENCIES:
212                                 promise = SoftwareProductDependenciesActionHelper.saveDependencies(dispatch,{softwareProductId, version, dependenciesList: softwareProductDependencies});
213                         case enums.SCREEN.SOFTWARE_PRODUCT_DETAILS:
214                                 promise = SoftwareProductActionHelper.updateSoftwareProduct(dispatch, {softwareProduct, qdata});
215                                 break;
216                         case enums.SCREEN.SOFTWARE_PRODUCT_COMPONENT_GENERAL:
217                                 promise = SoftwareProductComponentsActionHelper.updateSoftwareProductComponent(dispatch,
218                                         {softwareProductId, version, vspComponentId: componentId, componentData, qdata: componentQdata});
219                                 break;
220                         case enums.SCREEN.SOFTWARE_PRODUCT_COMPONENT_COMPUTE:
221                         case enums.SCREEN.SOFTWARE_PRODUCT_COMPONENT_STORAGE:
222                         case enums.SCREEN.SOFTWARE_PRODUCT_COMPONENT_NETWORK:
223                         case enums.SCREEN.SOFTWARE_PRODUCT_COMPONENT_IMAGES:
224                         case enums.SCREEN.SOFTWARE_PRODUCT_COMPONENT_LOAD_BALANCING:
225                                 promise = SoftwareProductComponentsActionHelper.updateSoftwareProductComponentQuestionnaire(dispatch, {softwareProductId, version, vspComponentId: componentId, qdata: componentQdata});
226                                 break;
227                         default:
228                                 promise = Promise.resolve();
229                                 break;
230                 }
231         }
232         return promise;
233 };
234
235
236 const onComponentNavigate = (dispatch, {id, softwareProductId, version, currentComponentId}) => {
237         const [nextScreen, nextComponentId] = id.split('|');
238         switch(nextScreen) {
239                 case navigationItems.COMPONENTS:
240                         if(nextComponentId === currentComponentId) {
241                                 OnboardingActionHelper.navigateToSoftwareProductComponents(dispatch, {softwareProductId});
242                         } else {
243                                 OnboardingActionHelper.navigateToSoftwareProductComponentGeneral(dispatch, {softwareProductId, componentId: nextComponentId, version});
244                         }
245                         break;
246                 case navigationItems.GENERAL:
247                         OnboardingActionHelper.navigateToSoftwareProductComponentGeneral(dispatch, {softwareProductId, componentId: nextComponentId, version});
248                         break;
249                 case navigationItems.COMPUTE:
250                         OnboardingActionHelper.navigateToComponentCompute(dispatch, {softwareProductId, componentId: nextComponentId, version});
251                         break;
252                 case navigationItems.LOAD_BALANCING:
253                         OnboardingActionHelper.navigateToComponentLoadBalancing(dispatch, {softwareProductId, componentId: nextComponentId, version});
254                         break;
255                 case navigationItems.NETWORKS:
256                         OnboardingActionHelper.navigateToComponentNetwork(dispatch, {softwareProductId, componentId: nextComponentId, version});
257                         break;
258                 case navigationItems.IMAGES:
259                         OnboardingActionHelper.navigateToComponentImages(dispatch, {softwareProductId, componentId: nextComponentId, version});
260                         break;
261                 case navigationItems.STORAGE:
262                         OnboardingActionHelper.navigateToComponentStorage(dispatch, {softwareProductId, componentId: nextComponentId, version});
263                         break;
264                 case navigationItems.PROCESS_DETAILS:
265                         OnboardingActionHelper.navigateToSoftwareProductComponentProcesses(dispatch, {softwareProductId, componentId: nextComponentId, version});
266                         break;
267                 case navigationItems.MONITORING:
268                         OnboardingActionHelper.navigateToSoftwareProductComponentMonitoring(dispatch, {softwareProductId, componentId: nextComponentId, version});
269                         break;
270         }
271 };
272
273 const mapActionsToProps = (dispatch, {currentScreen: {screen, props: {softwareProductId, componentId: currentComponentId}}}) => {
274
275         const props = {
276                 onVersionSwitching: (version, meta) => {
277                         SoftwareProductActionHelper.fetchSoftwareProduct(dispatch, {softwareProductId, version});
278                         props.onNavigate({id: getActiveNavigationId(screen, currentComponentId), meta, version});
279                 },
280                 onToggle: (groups, itemIdToExpand) => groups.map(({items}) => SoftwareProductActionHelper.toggleNavigationItems(dispatch, {items, itemIdToExpand})),
281                 onNavigate: ({id, meta, version}) => {
282                         let {onboardingOrigin, heatSetup, heatSetupCache} = meta;
283                         let heatSetupPopupPromise = screen === enums.SCREEN.SOFTWARE_PRODUCT_ATTACHMENTS ?
284                                                                 HeatSetupActionHelper.heatSetupLeaveConfirmation(dispatch, {softwareProductId, heatSetup, heatSetupCache}) :
285                                                                 Promise.resolve();
286                         let preNavigate = meta ? autoSaveBeforeNavigate({dispatch, screen, meta, softwareProductId, componentId: currentComponentId}) : Promise.resolve();
287                         version = version || (meta ? meta.version : undefined);
288                         Promise.all([preNavigate, heatSetupPopupPromise]).then(() => {
289                                 switch(id) {
290                                         case navigationItems.VENDOR_SOFTWARE_PRODUCT:
291                                                 OnboardingActionHelper.navigateToSoftwareProductLandingPage(dispatch, {softwareProductId, version});
292                                                 break;
293                                         case navigationItems.GENERAL:
294                                                 OnboardingActionHelper.navigateToSoftwareProductDetails(dispatch, {softwareProductId, version});
295                                                 break;
296                                         case navigationItems.DEPLOYMENT_FLAVORS:
297                                                 OnboardingActionHelper.navigateToSoftwareProductDeployment(dispatch, {softwareProductId, version});
298                                                 break;
299                                         case navigationItems.PROCESS_DETAILS:
300                                                 OnboardingActionHelper.navigateToSoftwareProductProcesses(dispatch, {softwareProductId, version});
301                                                 break;
302                                         case navigationItems.NETWORKS:
303                                                 OnboardingActionHelper.navigateToSoftwareProductNetworks(dispatch, {softwareProductId, version});
304                                                 break;
305                                         case navigationItems.DEPENDENCIES:
306                                                 OnboardingActionHelper.navigateToSoftwareProductDependencies(dispatch, {softwareProductId, version});
307                                                 break;
308                                         case navigationItems.ATTACHMENTS:
309                                                 if(onboardingOrigin === onboardingOriginTypes.ZIP) {
310                                                         OnboardingActionHelper.navigateToSoftwareProductAttachmentsSetupTab(dispatch, {softwareProductId, version});
311                                                 }
312                                                 else if(onboardingOrigin === onboardingOriginTypes.CSAR) {
313                                                         OnboardingActionHelper.navigateToSoftwareProductAttachmentsValidationTab(dispatch, {softwareProductId, version});
314                                                 }
315                                                 break;
316                                         case navigationItems.COMPONENTS:
317                                                 OnboardingActionHelper.navigateToSoftwareProductComponents(dispatch, {softwareProductId, version});
318                                                 break;
319                                         case navigationItems.ACTIVITY_LOG:
320                                                 OnboardingActionHelper.navigateToSoftwareProductActivityLog(dispatch, {softwareProductId, version});
321                                                 break;
322                                         default:
323                                                 onComponentNavigate(dispatch, {id, softwareProductId, version, screen, currentComponentId});
324                                                 break;
325                                 }
326                         }).catch((e) => {console.error(e);});
327                 }
328         };
329
330         switch (screen) {
331                 case enums.SCREEN.SOFTWARE_PRODUCT_LANDING_PAGE:
332                 case enums.SCREEN.SOFTWARE_PRODUCT_ATTACHMENTS:
333                 case enums.SCREEN.SOFTWARE_PRODUCT_PROCESSES:
334                 case enums.SCREEN.SOFTWARE_PRODUCT_NETWORKS:
335                 case enums.SCREEN.SOFTWARE_PRODUCT_DEPENDENCIES:
336                 case enums.SCREEN.SOFTWARE_PRODUCT_ACTIVITY_LOG:
337                 case enums.SCREEN.SOFTWARE_PRODUCT_COMPONENTS:
338                 case enums.SCREEN.SOFTWARE_PRODUCT_DEPLOYMENT:
339                 case enums.SCREEN.SOFTWARE_PRODUCT_COMPONENT_PROCESSES:
340                 case enums.SCREEN.SOFTWARE_PRODUCT_COMPONENT_MONITORING:
341                         props.onSave = () => Promise.resolve();
342                         break;
343                 default:
344                         props.onSave = ({softwareProduct, qdata}) => SoftwareProductActionHelper.updateSoftwareProduct(dispatch, {softwareProduct, qdata});
345                         break;
346         }
347
348
349         props.onVersionControllerAction = (action, version, meta) => {
350                 let {heatSetup, heatSetupCache} = meta;
351                 let heatSetupPopupPromise = screen === enums.SCREEN.SOFTWARE_PRODUCT_ATTACHMENTS && action === versionControllerActions.CHECK_IN ?
352                                                                 HeatSetupActionHelper.heatSetupLeaveConfirmation(dispatch, {softwareProductId, heatSetup, heatSetupCache}) :
353                                                                 Promise.resolve();
354                 heatSetupPopupPromise.then(() => {
355                         return SoftwareProductActionHelper.performVCAction(dispatch, {softwareProductId, action, version}).then(({newVersion}) => {
356                                 //props.onNavigate({id: getActiveNavigationId(screen, currentComponentId), version});
357                                 if(screen === enums.SCREEN.SOFTWARE_PRODUCT_ACTIVITY_LOG) {
358                                         OnboardingActionHelper.navigateToSoftwareProductActivityLog(dispatch, {softwareProductId, version: newVersion});
359                                 }
360                         });
361                 }).catch((e) => {console.error(e);});
362         };
363         return props;
364 };
365
366 export default connect(mapStateToProps, mapActionsToProps)(TabulatedEditor);