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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
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';
25 import { FeatureTopology, Topology, TopologyNode, Module } from '../models/topologyNetconf';
26 import { guiCutThrough } from '../models/guiCutTrough';
29 * Represents a web api accessor service for all network element/node actions.
31 class ConnectService {
32 public getNetworkElementUri = (nodeId: string) => '/rests/data/network-topology:network-topology/topology=topology-netconf/node=' + nodeId;
34 public getNetworkElementConnectDataProviderUri = (operation: 'create' | 'update' | 'delete') => `/rests/operations/data-provider:${operation}-network-element-connection`;
36 public getAllWebUriExtensionsForNetworkElementListUri = (nodeId: string) => this.getNetworkElementUri(nodeId) + '/yang-ext:mount/core-model:network-element';
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';
41 * Inserts a network element/node.
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)),
48 return result || null;
52 * Updates a network element/node.
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)),
59 return result || null;
63 * Deletes a network element/node.
65 public async deleteNetworkElement(element: UpdateNetworkElement): Promise<NetworkElementConnection | null> {
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)),
73 return result || null;
76 /** Mounts network element/node */
77 public async mountNetworkElement(networkElement: NetworkElementConnection): Promise<boolean> {
78 const path = this.getNetworkElementUri(networkElement.nodeId);
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>',
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>',
95 ' <!-- keepalive-delay set to 0 turns off keepalives-->',
96 ' <keepalive-delay xmlns="urn:opendaylight:netconf-node-topology">120</keepalive-delay>',
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>`,
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>',
112 '<max-connection-attempts xmlns="urn:opendaylight:netconf-node-topology">2</max-connection-attempts>',
115 if (networkElement.password) {
122 const result = await requestRest<string>(path, {
125 'Content-Type': 'application/xml',
126 'Accept': 'application/xml',
130 // expect an empty answer
131 return result !== null;
137 /** Unmounts a network element by its id. */
138 public async unmountNetworkElement(nodeId: string): Promise<boolean> {
139 const path = this.getNetworkElementUri(nodeId);
142 const result = await requestRest<string>(path, {
145 'Content-Type': 'application/xml',
146 'Accept': 'application/xml',
149 // expect an empty answer
150 return result !== null;
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' });
162 return topologyRequestPomise && topologyRequestPomise.then(result => {
163 return result && result['network-topology:node'] && result['network-topology:node'][0] || null;
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' });
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;
185 * Get the connection state of the network element/ node
187 public async getNetworkElementConnectionStatus(element: string): Promise<(ConnectionStatus)[] | null> {
188 const path = '/rests/operations/data-provider:read-network-element-connection-list';
190 'data-provider:input': {
192 'property': 'node-id',
193 'filtervalue': element,
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 => ({
208 * Gets all available tlsKeys.
211 public async getTlsKeys(): Promise<(TlsKeys)[] | null> {
212 const path = '/rests/operations/data-provider:read-tls-key-entry';
214 'data-provider:input': {
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 => ({
230 public async getAllWebUriExtensionsForNetworkElementListAsync(neList: string[]): Promise<(guiCutThrough)[]> {
231 const path = '/rests/operations/data-provider:read-gui-cut-through-entry';
232 let webUriList: guiCutThrough[] = [];
234 'data-provider:input': {
237 'filtervalues': neList,
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;
252 resultData.forEach(entry => {
253 if (entry.id == nodeId) {
254 entryNotFound = false;
256 webUriList.push({ id: nodeId, weburi: entry.weburi });
258 webUriList.push({ id: nodeId, weburi: undefined });
266 webUriList.push({ id: nodeId, weburi: undefined });
271 // public async getAllWebUriExtensionsForNetworkElementListAsync(ne: string[]): Promise<(guiCutThrough)[] | null> {
273 // let promises: any[] = [];
274 // let webUris: guiCutThrough[] = []
276 // ne.forEach(nodeId => {
277 // const path = this.getAllWebUriExtensionsForNetworkElementListUri(nodeId);
279 // // add search request to array
280 // promises.push(requestRest<any>(path, { method: "GET" })
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")
285 // webUris.push({ weburi: webUri.value, id: nodeId });
287 // webUris.push({ weburi: undefined, id: nodeId });
290 // webUris.push({ weburi: undefined, id: nodeId });
294 // webUris.push({ weburi: undefined, id: nodeId });
297 // // wait until all promises are done and return weburis
298 // return Promise.all(promises).then(result => { return webUris });
305 export const connectService = new ConnectService();