Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / connectApp / src / services / connectService.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 } from '../../../../framework/src/services/restService';
20 import { NetworkElementConnection, ConnectionStatus, UpdateNetworkElement } from '../models/networkElementConnection';
21 import { TlsKeys } from '../models/networkElementConnection';
22 import { convertPropertyNames, replaceUpperCase } from '../../../../framework/src/utilities/yangHelper';
23 import { Result } from '../../../../framework/src/models/elasticSearch';
24
25 import { FeatureTopology, Topology, TopologyNode, Module } from '../models/topologyNetconf';
26 import { guiCutThrough } from '../models/guiCutTrough';
27
28 /**
29 * Represents a web api accessor service for all network element/node actions.
30 */
31 class ConnectService {
32   public getNetworkElementUri = (nodeId: string) => '/rests/data/network-topology:network-topology/topology=topology-netconf/node=' + nodeId;
33
34   public getNetworkElementConnectDataProviderUri = (operation: 'create' | 'update' | 'delete') => `/rests/operations/data-provider:${operation}-network-element-connection`;
35
36   public getAllWebUriExtensionsForNetworkElementListUri = (nodeId: string) => this.getNetworkElementUri(nodeId) + '/yang-ext:mount/core-model:network-element';
37
38   public getNetworkElementYangLibraryFeature = (nodeId: string) => '/rests/data/network-topology:network-topology/topology=topology-netconf/node=' + nodeId + '/yang-ext:mount/ietf-yang-library:yang-library?content=nonconfig';
39
40   /**
41    * Inserts a network element/node.
42    */
43   public async createNetworkElement(element: NetworkElementConnection): Promise<NetworkElementConnection | null> {
44     const path = this.getNetworkElementConnectDataProviderUri('create');
45     const result = await requestRest<NetworkElementConnection>(path, {
46       method: 'POST', body: JSON.stringify(convertPropertyNames({ 'data-provider:input': element }, replaceUpperCase)),
47     });
48     return result || null;
49   }
50
51   /**
52   * Updates a network element/node.
53   */
54   public async updateNetworkElement(element: UpdateNetworkElement): Promise<NetworkElementConnection | null> {
55     const path = this.getNetworkElementConnectDataProviderUri('update');
56     const result = await requestRest<NetworkElementConnection>(path, {
57       method: 'POST', body: JSON.stringify(convertPropertyNames({ 'data-provider:input': element }, replaceUpperCase)),
58     });
59     return result || null;
60   }
61
62   /**
63     * Deletes a network element/node.
64     */
65   public async deleteNetworkElement(element: UpdateNetworkElement): Promise<NetworkElementConnection | null> {
66     const query = {
67       'id': element.id,
68     };
69     const path = this.getNetworkElementConnectDataProviderUri('delete');
70     const result = await requestRest<NetworkElementConnection>(path, {
71       method: 'POST', body: JSON.stringify(convertPropertyNames({ 'data-provider:input': query }, replaceUpperCase)),
72     });
73     return result || null;
74   }
75
76   /** Mounts network element/node */
77   public async mountNetworkElement(networkElement: NetworkElementConnection): Promise<boolean> {
78     const path = this.getNetworkElementUri(networkElement.nodeId);
79     const mountXml = [
80       '<node xmlns="urn:TBD:params:xml:ns:yang:network-topology">',
81       `<node-id>${networkElement.nodeId}</node-id>`,
82       `<host xmlns="urn:opendaylight:netconf-node-topology">${networkElement.host}</host>`,
83       `<port xmlns="urn:opendaylight:netconf-node-topology">${networkElement.port}</port>`,
84       `<username xmlns="urn:opendaylight:netconf-node-topology">${networkElement.username}</username>`,
85       `<password xmlns="urn:opendaylight:netconf-node-topology">${networkElement.password}</password>`,
86       '  <tcp-only xmlns="urn:opendaylight:netconf-node-topology">false</tcp-only>',
87
88       '  <!-- non-mandatory fields with default values, you can safely remove these if you do not wish to override any of these values-->',
89       '  <reconnect-on-changed-schema xmlns="urn:opendaylight:netconf-node-topology">false</reconnect-on-changed-schema>',
90       '  <connection-timeout-millis xmlns="urn:opendaylight:netconf-node-topology">20000</connection-timeout-millis>',
91       '  <max-connection-attempts xmlns="urn:opendaylight:netconf-node-topology">100</max-connection-attempts>',
92       '  <between-attempts-timeout-millis xmlns="urn:opendaylight:netconf-node-topology">2000</between-attempts-timeout-millis>',
93       '  <sleep-factor xmlns="urn:opendaylight:netconf-node-topology">1.5</sleep-factor>',
94
95       '  <!-- keepalive-delay set to 0 turns off keepalives-->',
96       '  <keepalive-delay xmlns="urn:opendaylight:netconf-node-topology">120</keepalive-delay>',
97       '</node>'].join('');
98
99     const tlsXml = [
100       '<node xmlns="urn:TBD:params:xml:ns:yang:network-topology">',
101       `<node-id>${networkElement.nodeId}</node-id>`,
102       '<key-based xmlns="urn:opendaylight:netconf-node-topology">',
103       `<key-id xmlns="urn:opendaylight:netconf-node-topology">${networkElement.tlsKey}</key-id>`,
104       `<username xmlns="urn:opendaylight:netconf-node-topology">${networkElement.username}</username>`,
105       '</key-based>',
106       `<host xmlns="urn:opendaylight:netconf-node-topology">${networkElement.host}</host>`,
107       `<port xmlns="urn:opendaylight:netconf-node-topology">${networkElement.port}</port>`,
108       '<tcp-only xmlns="urn:opendaylight:netconf-node-topology">false</tcp-only>',
109       '<protocol xmlns="urn:opendaylight:netconf-node-topology">',
110       '<name xmlns="urn:opendaylight:netconf-node-topology">TLS</name>',
111       ' </protocol>',
112       '<max-connection-attempts xmlns="urn:opendaylight:netconf-node-topology">2</max-connection-attempts>',
113       '</node>'].join('');
114     let bodyXml;
115     if (networkElement.password) {
116       bodyXml = mountXml;
117     } else {
118       bodyXml = tlsXml;
119     }
120
121     try {
122       const result = await requestRest<string>(path, {
123         method: 'PUT',
124         headers: {
125           'Content-Type': 'application/xml',
126           'Accept': 'application/xml',
127         },
128         body: bodyXml,
129       });
130       // expect an empty answer
131       return result !== null;
132     } catch {
133       return false;
134     }
135   }
136
137   /** Unmounts a network element by its id. */
138   public async unmountNetworkElement(nodeId: string): Promise<boolean> {
139     const path = this.getNetworkElementUri(nodeId);
140
141     try {
142       const result = await requestRest<string>(path, {
143         method: 'DELETE',
144         headers: {
145           'Content-Type': 'application/xml',
146           'Accept': 'application/xml',
147         },
148       });
149       // expect an empty answer
150       return result !== null;
151
152     } catch {
153       return false;
154     }
155   }
156
157   /** Yang capabilities of the selected network element/node */
158   public async infoNetworkElement(nodeId: string): Promise<TopologyNode | null> {
159     const path = this.getNetworkElementUri(nodeId);
160     const topologyRequestPomise = requestRest<Topology>(path, { method: 'GET' });
161
162     return topologyRequestPomise && topologyRequestPomise.then(result => {
163       return result && result['network-topology:node'] && result['network-topology:node'][0] || null;
164     });
165   }
166
167
168   /** Yang features of the selected network element/node module */
169   public async infoNetworkElementFeatures(nodeId: string): Promise<Module[] | null | undefined> {
170     const path = this.getNetworkElementYangLibraryFeature(nodeId);
171     const topologyRequestPomise = requestRest<FeatureTopology>(path, { method: 'GET' });
172
173     return topologyRequestPomise && topologyRequestPomise.then(result => {
174       const resultFinal = result && result['ietf-yang-library:yang-library']
175         && result['ietf-yang-library:yang-library']['module-set'] &&
176         result['ietf-yang-library:yang-library']['module-set'][0] &&
177         result['ietf-yang-library:yang-library']['module-set'][0].module || null;
178       return resultFinal;
179     });
180   }
181
182
183
184   /**
185    * Get the connection state of the network element/ node
186    */
187   public async getNetworkElementConnectionStatus(element: string): Promise<(ConnectionStatus)[] | null> {
188     const path = '/rests/operations/data-provider:read-network-element-connection-list';
189     const query = {
190       'data-provider:input': {
191         'filter': [{
192           'property': 'node-id',
193           'filtervalue': element,
194         }],
195         'pagination': {
196           'size': 20,
197           'page': 1,
198         },
199       },
200     };
201     const result = await requestRest<Result<ConnectionStatus>>(path, { method: 'POST', body: JSON.stringify(query) });
202     return result && result['data-provider:output'] && result['data-provider:output'].data && result['data-provider:output'].data.map(ne => ({
203       status: ne.status,
204     })) || null;
205   }
206
207   /**
208   * Gets all available tlsKeys.
209   */
210
211   public async getTlsKeys(): Promise<(TlsKeys)[] | null> {
212     const path = '/rests/operations/data-provider:read-tls-key-entry';
213     const query = {
214       'data-provider:input': {
215         'filter': [],
216         'sortorder': [],
217         'pagination': {
218           'size': 20,
219           'page': 1,
220         },
221       },
222     };
223
224     const result = await requestRest<Result<string>>(path, { method: 'POST', body: JSON.stringify(query) });
225     return result && result['data-provider:output'] && result['data-provider:output'].data && result['data-provider:output'].data.map(ne => ({
226       key: ne,
227     })) || null;
228   }
229
230   public async getAllWebUriExtensionsForNetworkElementListAsync(neList: string[]): Promise<(guiCutThrough)[]> {
231     const path = '/rests/operations/data-provider:read-gui-cut-through-entry';
232     let webUriList: guiCutThrough[] = [];
233     const query = {
234       'data-provider:input': {
235         'filter': [{
236           'property': 'id',
237           'filtervalues': neList,
238         }],
239         'pagination': {
240           'size': 20,
241           'page': 1,
242         },
243       },
244     };
245
246     const result = await requestRest<Result<guiCutThrough>>(path, { method: 'POST', body: JSON.stringify(query) });
247     const resultData = result && result['data-provider:output'] && result['data-provider:output'].data;
248     neList.forEach(nodeId => {
249       let entryNotFound = true;
250       if (resultData) {
251         try {
252           resultData.forEach(entry => {
253             if (entry.id == nodeId) {
254               entryNotFound = false;
255               if (entry.weburi) {
256                 webUriList.push({ id: nodeId, weburi: entry.weburi });
257               } else {
258                 webUriList.push({ id: nodeId, weburi: undefined });
259               }
260               throw new Error();
261             }
262           });
263         } catch (e) { }
264       }
265       if (entryNotFound)
266         webUriList.push({ id: nodeId, weburi: undefined });
267     });
268     return webUriList;
269   }
270
271   //  public async getAllWebUriExtensionsForNetworkElementListAsync(ne: string[]): Promise<(guiCutThrough)[] | null> {
272
273   //   let promises: any[] = [];
274   //   let webUris: guiCutThrough[] = []
275
276   //   ne.forEach(nodeId => {
277   //     const path = this.getAllWebUriExtensionsForNetworkElementListUri(nodeId);
278
279   // // add search request to array
280   //     promises.push(requestRest<any>(path, { method: "GET" })
281   //       .then(result => {
282   //         if (result != null && result['core-model:network-element'] && result['core-model:network-element'].extension) {
283   //           const webUri = result['core-model:network-element'].extension.find((item: any) => item['value-name'] === "webUri")
284   //           if (webUri) {
285   //             webUris.push({ weburi: webUri.value, id: nodeId });
286   //           } else {
287   //             webUris.push({ weburi: undefined, id: nodeId });
288   //           }
289   //         } else {
290   //           webUris.push({ weburi: undefined, id: nodeId });
291   //         }
292   //       })
293   //       .catch(error => {
294   //         webUris.push({ weburi: undefined, id: nodeId });
295   //       }))
296   //   })
297   //   // wait until all promises are done and return weburis
298   //   return Promise.all(promises).then(result => { return webUris });
299   // }
300
301 }
302
303
304
305 export const connectService = new ConnectService();