fix oauth code
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / configurationApp / src / services / restServices.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
19 import { requestRest, requestRestExt } from '../../../../framework/src/services/restService';
20 import { convertPropertyNames, replaceHyphen } from '../../../../framework/src/utilities/yangHelper';
21
22 import { NetworkElementConnection } from '../models/networkElementConnection';
23
24 type ImportOnlyResponse = {
25   'ietf-yang-library:yang-library': {
26     'module-set': {
27       'import-only-module': {
28         'name': string;
29         'revision': string;
30       }[];
31     }[];
32   };
33 };
34
35
36 type CapabilityResponse = {
37   'network-topology:node': {
38     'node-id': string;
39     'netconf-node-topology:available-capabilities': {
40       'available-capability': {
41         'capability-origin': string;
42         'capability': string;
43       }[];
44     };
45     'netconf-node-topology:unavailable-capabilities': {
46       'unavailable-capability': {
47         'capability': string;
48         'failure-reason': string;
49       }[];
50     };
51   }[];
52 };
53
54 type CapabilityAnswer = {
55   availableCapabilities: {
56     capabilityOrigin: string;
57     capability: string;
58     version: string;
59   }[] | null;
60   unavailableCapabilities: {
61     failureReason: string;
62     capability: string;
63     version: string;
64   }[] | null;
65   importOnlyModules: {
66     name: string;
67     revision: string;
68   }[] | null;
69 };
70
71 const capParser = /^\(.*\?revision=(\d{4}-\d{2}-\d{2})\)(\S+)$/i;
72
73 class RestService {
74   public getNetworkElementUri = (nodeId: string) => '/rests/data/network-topology:network-topology/topology=topology-netconf/node=' + nodeId;
75
76   public async getImportOnlyModules(nodeId: string): Promise<{ name: string; revision: string }[]> {
77     const path = `${this.getNetworkElementUri(nodeId)}/yang-ext:mount/ietf-yang-library:yang-library?content=nonconfig&fields=module-set(import-only-module(name;revision))`;
78     const importOnlyResult = await requestRest<ImportOnlyResponse>(path, { method: 'GET' });
79     const importOnlyModules = importOnlyResult
80       ? importOnlyResult['ietf-yang-library:yang-library']['module-set'][0]['import-only-module']
81       : [];
82     return importOnlyModules;
83   }
84
85   public async getCapabilitiesByMountId(nodeId: string): Promise<CapabilityAnswer> {
86     const path = this.getNetworkElementUri(nodeId);
87     const capabilitiesResult = await requestRest<CapabilityResponse>(path, { method: 'GET' });
88     const availableCapabilities = capabilitiesResult && capabilitiesResult['network-topology:node'] && capabilitiesResult['network-topology:node'].length > 0 &&
89       (capabilitiesResult['network-topology:node'][0]['netconf-node-topology:available-capabilities'] &&
90         capabilitiesResult['network-topology:node'][0]['netconf-node-topology:available-capabilities']['available-capability'] &&
91         capabilitiesResult['network-topology:node'][0]['netconf-node-topology:available-capabilities']['available-capability'].map<any>(obj => convertPropertyNames(obj, replaceHyphen)) || [])
92         .map(cap => {
93           const capMatch = cap && capParser.exec(cap.capability);
94           return capMatch ? {
95             capabilityOrigin: cap.capabilityOrigin,
96             capability: capMatch && capMatch[2] || '',
97             version: capMatch && capMatch[1] || '',
98           } : null ;
99         }).filter((cap) => cap != null) || [] as any;
100
101     const unavailableCapabilities = capabilitiesResult && capabilitiesResult['network-topology:node'] && capabilitiesResult['network-topology:node'].length > 0 &&
102       (capabilitiesResult['network-topology:node'][0]['netconf-node-topology:unavailable-capabilities'] &&
103       capabilitiesResult['network-topology:node'][0]['netconf-node-topology:unavailable-capabilities']['unavailable-capability'] &&
104       capabilitiesResult['network-topology:node'][0]['netconf-node-topology:unavailable-capabilities']['unavailable-capability'].map<any>(obj => convertPropertyNames(obj, replaceHyphen)) || [])
105         .map(cap => {
106           const capMatch = cap && capParser.exec(cap.capability);
107           return capMatch ? {
108             failureReason: cap.failureReason,
109             capability: capMatch && capMatch[2] || '',
110             version: capMatch && capMatch[1] || '',
111           } : null ;
112         }).filter((cap) => cap != null) || [] as any;
113
114     const importOnlyModules = availableCapabilities && availableCapabilities.findIndex((ac: { capability: string }) => ac.capability && ac.capability.toLowerCase() === 'ietf-yang-library') > -1
115       ? await this.getImportOnlyModules(nodeId)
116       : null;
117
118     return { availableCapabilities, unavailableCapabilities, importOnlyModules };
119   }
120
121   public async getMountedNetworkElementByMountId(nodeId: string): Promise<NetworkElementConnection | null> {
122     // const path = 'restconf/operational/network-topology:network-topology/topology/topology-netconf/node/' + nodeId;
123     // const connectedNetworkElement = await requestRest<NetworkElementConnection>(path, { method: "GET" });
124     // return connectedNetworkElement || null;
125
126     const path = '/rests/operations/data-provider:read-network-element-connection-list';
127     const body = { 'data-provider:input': { 'filter': [{ 'property': 'node-id', 'filtervalue': nodeId }], 'sortorder': [], 'pagination': { 'size': 1, 'page': 1 } } };
128     const networkElementResult = await requestRest<{ 'data-provider:output': { data: NetworkElementConnection[] } }>(path, { method: 'POST', body: JSON.stringify(body) });
129     return networkElementResult && networkElementResult['data-provider:output'] && networkElementResult['data-provider:output'].data &&
130       networkElementResult['data-provider:output'].data.map(obj => convertPropertyNames(obj, replaceHyphen))[0] || null;
131   }
132
133   /** Reads the config data by restconf path.
134   * @param path The restconf path to be used for read.
135   * @returns The data.
136   */
137   public getConfigData(path: string) {
138     return requestRestExt<{ [key: string]: any }>(path, { method: 'GET' });
139   }
140
141   /** Updates or creates the config data by restconf path using data.
142    * @param path The restconf path to identify the note to update.
143    * @param data The data to be updated.
144    * @returns The written data.
145    */
146   public setConfigData(path: string, data: any) {
147     return requestRestExt<{ [key: string]: any }>(path, { method: 'PUT', body: JSON.stringify(data) });
148   }
149
150   public executeRpc(path: string, data: any) {
151     return requestRestExt<{ [key: string]: any }>(path, { method: 'POST', body: JSON.stringify(data) });
152   }
153
154   /** Removes the element by restconf path.
155   * @param path The restconf path to identify the note to update.
156   * @returns The restconf result.
157   */
158   public removeConfigElement(path: string) {
159     return requestRestExt<{ [key: string]: any }>(path, { method: 'DELETE' });
160   }
161 }
162
163 export const restService = new RestService();
164 export default restService;