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, requestRestExt } from '../../../../framework/src/services/restService';
20 import { convertPropertyNames, replaceHyphen } from '../../../../framework/src/utilities/yangHelper';
22 import { NetworkElementConnection } from '../models/networkElementConnection';
24 type ImportOnlyResponse = {
25 'ietf-yang-library:yang-library': {
27 'import-only-module': {
36 type CapabilityResponse = {
37 'network-topology:node': {
39 'netconf-node-topology:available-capabilities': {
40 'available-capability': {
41 'capability-origin': string;
45 'netconf-node-topology:unavailable-capabilities': {
46 'unavailable-capability': {
48 'failure-reason': string;
54 type CapabilityAnswer = {
55 availableCapabilities: {
56 capabilityOrigin: string;
60 unavailableCapabilities: {
61 failureReason: string;
71 const capParser = /^\(.*\?revision=(\d{4}-\d{2}-\d{2})\)(\S+)$/i;
74 public getNetworkElementUri = (nodeId: string) => '/rests/data/network-topology:network-topology/topology=topology-netconf/node=' + nodeId;
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']
82 return importOnlyModules;
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)) || [])
93 const capMatch = cap && capParser.exec(cap.capability);
95 capabilityOrigin: cap.capabilityOrigin,
96 capability: capMatch && capMatch[2] || '',
97 version: capMatch && capMatch[1] || '',
99 }).filter((cap) => cap != null) || [] as any;
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)) || [])
106 const capMatch = cap && capParser.exec(cap.capability);
108 failureReason: cap.failureReason,
109 capability: capMatch && capMatch[2] || '',
110 version: capMatch && capMatch[1] || '',
112 }).filter((cap) => cap != null) || [] as any;
114 const importOnlyModules = availableCapabilities && availableCapabilities.findIndex((ac: { capability: string }) => ac.capability && ac.capability.toLowerCase() === 'ietf-yang-library') > -1
115 ? await this.getImportOnlyModules(nodeId)
118 return { availableCapabilities, unavailableCapabilities, importOnlyModules };
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;
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;
133 /** Reads the config data by restconf path.
134 * @param path The restconf path to be used for read.
137 public getConfigData(path: string) {
138 return requestRestExt<{ [key: string]: any }>(path, { method: 'GET' });
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.
146 public setConfigData(path: string, data: any) {
147 return requestRestExt<{ [key: string]: any }>(path, { method: 'PUT', body: JSON.stringify(data) });
150 public executeRpc(path: string, data: any) {
151 return requestRestExt<{ [key: string]: any }>(path, { method: 'POST', body: JSON.stringify(data) });
154 /** Removes the element by restconf path.
155 * @param path The restconf path to identify the note to update.
156 * @returns The restconf result.
158 public removeConfigElement(path: string) {
159 return requestRestExt<{ [key: string]: any }>(path, { method: 'DELETE' });
163 export const restService = new RestService();
164 export default restService;