aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/odlux/apps/connectApp/src/services/connectService.ts
blob: 427acd3ec3634e4751721c6ef25bfcf0285d9119 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
 * ============LICENSE_START========================================================================
 * ONAP : ccsdk feature sdnr wt odlux
 * =================================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
 * =================================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 * ============LICENSE_END==========================================================================
 */

import { requestRest } from '../../../../framework/src/services/restService';
import { NetworkElementConnection, ConnectionStatus, UpdateNetworkElement } from '../models/networkElementConnection';
import { TlsKeys } from '../models/networkElementConnection'
import { convertPropertyNames, replaceUpperCase } from '../../../../framework/src/utilities/yangHelper';
import { Result } from '../../../../framework/src/models/elasticSearch';

import { FeatureTopology, Topology, TopologyNode, Module } from '../models/topologyNetconf';
import { guiCutThrough } from '../models/guiCutTrough';

/**
* Represents a web api accessor service for all Network Elements actions.
*/
class ConnectService {
  public getNetworkElementUri = (nodeId: string) => '/rests/data/network-topology:network-topology/topology=topology-netconf/node=' + nodeId;
  public getNetworkElementConnectDataProviderUri = (operation: "create" | "update" | "delete") => `/rests/operations/data-provider:${operation}-network-element-connection`;
  public getAllWebUriExtensionsForNetworkElementListUri = (nodeId: string) => this.getNetworkElementUri(nodeId) + '/yang-ext:mount/core-model:network-element';
  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'

  /**
   * Inserts a network elements.
   */
  public async createNetworkElement(element: NetworkElementConnection): Promise<NetworkElementConnection | null> {
    const path = this.getNetworkElementConnectDataProviderUri("create");
    const result = await requestRest<NetworkElementConnection>(path, {
      method: "POST", body: JSON.stringify(convertPropertyNames({ "data-provider:input": element }, replaceUpperCase))
    });
    return result || null;
  }

  /**
  * Updates a network element.
  */
  public async updateNetworkElement(element: UpdateNetworkElement): Promise<NetworkElementConnection | null> {
    const path = this.getNetworkElementConnectDataProviderUri("update");
    const result = await requestRest<NetworkElementConnection>(path, {
      method: "POST", body: JSON.stringify(convertPropertyNames({ "data-provider:input": element }, replaceUpperCase))
    });
    return result || null;
  }

  /**
    * Deletes a network element.
    */
  public async deleteNetworkElement(element: UpdateNetworkElement): Promise<NetworkElementConnection | null> {
    const query = {
      "id": element.id
    };
    const path = this.getNetworkElementConnectDataProviderUri("delete");
    const result = await requestRest<NetworkElementConnection>(path, {
      method: "POST", body: JSON.stringify(convertPropertyNames({ "data-provider:input": query }, replaceUpperCase))
    });
    return result || null;
  }

  /** Mounts network element. */
  public async mountNetworkElement(networkElement: NetworkElementConnection): Promise<boolean> {
    const path = this.getNetworkElementUri(networkElement.nodeId);
    const mountXml = [
      '<node xmlns="urn:TBD:params:xml:ns:yang:network-topology">',
      `<node-id>${networkElement.nodeId}</node-id>`,
      `<host xmlns="urn:opendaylight:netconf-node-topology">${networkElement.host}</host>`,
      `<port xmlns="urn:opendaylight:netconf-node-topology">${networkElement.port}</port>`,
      `<username xmlns="urn:opendaylight:netconf-node-topology">${networkElement.username}</username>`,
      `<password xmlns="urn:opendaylight:netconf-node-topology">${networkElement.password}</password>`,
      '  <tcp-only xmlns="urn:opendaylight:netconf-node-topology">false</tcp-only>',

      '  <!-- non-mandatory fields with default values, you can safely remove these if you do not wish to override any of these values-->',
      '  <reconnect-on-changed-schema xmlns="urn:opendaylight:netconf-node-topology">false</reconnect-on-changed-schema>',
      '  <connection-timeout-millis xmlns="urn:opendaylight:netconf-node-topology">20000</connection-timeout-millis>',
      '  <max-connection-attempts xmlns="urn:opendaylight:netconf-node-topology">100</max-connection-attempts>',
      '  <between-attempts-timeout-millis xmlns="urn:opendaylight:netconf-node-topology">2000</between-attempts-timeout-millis>',
      '  <sleep-factor xmlns="urn:opendaylight:netconf-node-topology">1.5</sleep-factor>',

      '  <!-- keepalive-delay set to 0 turns off keepalives-->',
      '  <keepalive-delay xmlns="urn:opendaylight:netconf-node-topology">120</keepalive-delay>',
      '</node>'].join('');

    const tlsXml = [
      '<node xmlns="urn:TBD:params:xml:ns:yang:network-topology">',
      `<node-id>${networkElement.nodeId}</node-id>`,
      '<key-based xmlns="urn:opendaylight:netconf-node-topology">',
      `<key-id xmlns="urn:opendaylight:netconf-node-topology">${networkElement.tlsKey}</key-id>`,
      `<username xmlns="urn:opendaylight:netconf-node-topology">${networkElement.username}</username>`,
      '</key-based>',
      `<host xmlns="urn:opendaylight:netconf-node-topology">${networkElement.host}</host>`,
      `<port xmlns="urn:opendaylight:netconf-node-topology">${networkElement.port}</port>`,
      '<tcp-only xmlns="urn:opendaylight:netconf-node-topology">false</tcp-only>',
      '<protocol xmlns="urn:opendaylight:netconf-node-topology">',
      '<name xmlns="urn:opendaylight:netconf-node-topology">TLS</name>',
      ' </protocol>',
      '<max-connection-attempts xmlns="urn:opendaylight:netconf-node-topology">2</max-connection-attempts>',
      '</node>'].join('')
    let bodyXml;
    if (networkElement.password) {
      bodyXml = mountXml
    }
    else {
      bodyXml = tlsXml
    }

    try {
      const result = await requestRest<string>(path, {
        method: 'PUT',
        headers: {
          'Content-Type': 'application/xml',
          'Accept': 'application/xml'
        },
        body: bodyXml
      });
      // expect an empty answer
      return result !== null;
    } catch {
      return false;
    }
  };

  /** Unmounts a network element by its id. */
  public async unmountNetworkElement(nodeId: string): Promise<boolean> {
    const path = this.getNetworkElementUri(nodeId);

    try {
      const result = await requestRest<string>(path, {
        method: 'DELETE',
        headers: {
          'Content-Type': 'application/xml',
          'Accept': 'application/xml'
        },
      });
      // expect an empty answer
      return result !== null;

    } catch {
      return false;
    }
  };

  /** Yang capabilities of the selected network elements. */
  public async infoNetworkElement(nodeId: string): Promise<TopologyNode | null> {
    const path = this.getNetworkElementUri(nodeId);
    const topologyRequestPomise = requestRest<Topology>(path, { method: "GET" });

    return topologyRequestPomise && topologyRequestPomise.then(result => {
      return result && result["network-topology:node"] && result["network-topology:node"][0] || null;
    });
  }


  /** Yang features of the selected network element module. */
  public async infoNetworkElementFeatures(nodeId: string): Promise<Module[] | null | undefined> {
    const path = this.getNetworkElementYangLibraryFeature(nodeId);
    const topologyRequestPomise = requestRest<FeatureTopology>(path, { method: "GET" });

    return topologyRequestPomise && topologyRequestPomise.then(result => {
      const resultFinal = result && result['ietf-yang-library:yang-library']
        && result["ietf-yang-library:yang-library"]["module-set"] &&
        result["ietf-yang-library:yang-library"]["module-set"][0] &&
        result["ietf-yang-library:yang-library"]["module-set"][0]['module'] || null;
      return resultFinal;
    });
  }



  /**
   * Get the connection state of the network element.
   */
  public async getNetworkElementConnectionStatus(element: string): Promise<(ConnectionStatus)[] | null> {
    const path = `/rests/operations/data-provider:read-network-element-connection-list`;
    const query = {
      "data-provider:input": {
        "filter": [{
          "property": "node-id",
          "filtervalue": element
        }],
        "pagination": {
          "size": 20,
          "page": 1
        }
      }
    }
    const result = await requestRest<Result<ConnectionStatus>>(path, { method: "POST", body: JSON.stringify(query) });
    return result && result["data-provider:output"] && result["data-provider:output"].data && result["data-provider:output"].data.map(ne => ({
      status: ne.status
    })) || null;
  }

  /**
  * Gets all available tlsKeys.
  */

  public async getTlsKeys(): Promise<(TlsKeys)[] | null> {
    const path = '/rests/operations/data-provider:read-tls-key-entry';
    const query = {
      "data-provider:input": {
        "filter": [],
        "sortorder": [],
        "pagination": {
          "size": 20,
          "page": 1
        }
      }
    };

    const result = await requestRest<Result<string>>(path, { method: "POST", body: JSON.stringify(query) });
    return result && result["data-provider:output"] && result["data-provider:output"].data && result["data-provider:output"].data.map(ne => ({
      key: ne
    })) || null;
  }

  public async getAllWebUriExtensionsForNetworkElementListAsync(neList: string[]): Promise<(guiCutThrough)[]> {
    const path = `/rests/operations/data-provider:read-gui-cut-through-entry`;
    let webUriList: guiCutThrough[] = []
    const query = {
      "data-provider:input": {
        "filter": [{
          "property": "id",
          "filtervalues": neList
        }],
        "pagination": {
          "size": 20,
          "page": 1
        }
      }
    }

    const result = await requestRest<Result<guiCutThrough>>(path, { method: "POST", body: JSON.stringify(query) });
    const resultData = result && result["data-provider:output"] && result["data-provider:output"].data;
    neList.forEach(nodeId => {
      let entryNotFound = true;
      if (resultData) {
        const BreakException = {};
        try {
          resultData.forEach(entry => {
            if (entry.id == nodeId) {
              entryNotFound = false;
              if (entry.weburi) {
                webUriList.push({ id: nodeId, weburi: entry.weburi });
              } else {
                webUriList.push({ id: nodeId, weburi: undefined });
              }
              throw BreakException;
            }
          });
        } catch (e) { }
      }
      if (entryNotFound)
        webUriList.push({ id: nodeId, weburi: undefined });
    });
    return webUriList;
  }

  //  public async getAllWebUriExtensionsForNetworkElementListAsync(ne: string[]): Promise<(guiCutThrough)[] | null> {

  //   let promises: any[] = [];
  //   let webUris: guiCutThrough[] = []

  //   ne.forEach(nodeId => {
  //     const path = this.getAllWebUriExtensionsForNetworkElementListUri(nodeId);

  // // add search request to array
  //     promises.push(requestRest<any>(path, { method: "GET" })
  //       .then(result => {
  //         if (result != null && result['core-model:network-element'] && result['core-model:network-element'].extension) {
  //           const webUri = result['core-model:network-element'].extension.find((item: any) => item['value-name'] === "webUri")
  //           if (webUri) {
  //             webUris.push({ weburi: webUri.value, id: nodeId });
  //           } else {
  //             webUris.push({ weburi: undefined, id: nodeId });
  //           }
  //         } else {
  //           webUris.push({ weburi: undefined, id: nodeId });
  //         }
  //       })
  //       .catch(error => {
  //         webUris.push({ weburi: undefined, id: nodeId });
  //       }))
  //   })
  //   // wait until all promises are done and return weburis
  //   return Promise.all(promises).then(result => { return webUris });
  // }

}



export const connectService = new ConnectService();