Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / maintenanceApp / src / services / maintenenceService.ts
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 import { DeleteResponse, PostResponse } from '../../../../framework/src/models/elasticSearch';
19 import { requestRest } from '../../../../framework/src/services/restService';
20 import { convertPropertyNames, replaceUpperCase } from '../../../../framework/src/utilities/yangHelper';
21 import { MaintenanceEntry } from '../models/maintenanceEntryType';
22
23 import { convertToISODateString } from '../utils/timeUtils';
24
25
26 export const maintenenceEntryDatabasePath = 'mwtn/maintenancemode';
27
28 /**
29  * Represents a web api accessor service for all maintenence entries related actions.
30  */
31 class MaintenenceService {
32
33   /**
34   * Adds or updates one maintenence entry to the backend.
35   */
36   public async writeMaintenenceEntry(maintenenceEntry: MaintenanceEntry): Promise<PostResponse | null> {
37     const path = '/rests/operations/data-provider:create-maintenance';
38
39     const query = {
40       'id': maintenenceEntry.mId,
41       'node-id': maintenenceEntry.nodeId,
42       'active': maintenenceEntry.active,
43       'description': maintenenceEntry.description,
44       'end': convertToISODateString(maintenenceEntry.end),
45       'start': convertToISODateString(maintenenceEntry.start),
46     };
47
48     const result = await requestRest<PostResponse>(path, { method: 'POST', body: JSON.stringify(convertPropertyNames({ 'data-provider:input': query }, replaceUpperCase)) });
49     return result || null;
50   }
51
52   /**
53   * Deletes one maintenence entry by its mountId from the backend.
54   */
55   public async deleteMaintenenceEntry(maintenenceEntry: MaintenanceEntry): Promise<(DeleteResponse) | null> {
56     const path = '/rests/operations/data-provider:delete-maintenance';
57
58     const query = {
59       'id': maintenenceEntry.mId,
60       'node-id': maintenenceEntry.nodeId,
61       'active': maintenenceEntry.active,
62       'description': maintenenceEntry.description,
63       'end': convertToISODateString(maintenenceEntry.end),
64       'start': convertToISODateString(maintenenceEntry.start),
65     };
66     const result = await requestRest<DeleteResponse>(path, { method: 'POST', body: JSON.stringify(convertPropertyNames({ 'data-provider:input': query }, replaceUpperCase)) });
67     return result || null;
68   }
69 }
70
71 export const maintenenceService = new MaintenenceService();
72 export default maintenenceService;