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==========================================================================
18 import * as $ from 'jquery';
20 import { requestRest, formEncode } from '../../../../framework/src/services/restService';
21 import { MediatorServer, MediatorServerVersionInfo, MediatorConfig, MediatorServerDevice, MediatorConfigResponse } from '../models/mediatorServer';
22 import { PostResponse, DeleteResponse, Result } from '../../../../framework/src/models';
24 export const mediatorServerResourcePath = "mediator-server";
26 type MediatorServerResponse<TData> = { code: number, data: TData };
27 type IndexableMediatorServer = MediatorServer & { [key: string]: any; };
30 * Represents a web api accessor service for all mediator server actions.
32 class MediatorService {
34 * Inserts data into the mediator servers table.
36 public async insertMediatorServer(server: IndexableMediatorServer): Promise<PostResponse | null> {
37 const path = `/restconf/operations/data-provider:create-mediator-server`;
44 const result = await requestRest<PostResponse>(path, { method: "POST", body: JSON.stringify({ input: data }) });
45 return result || null;
49 * Updates data into the mediator servers table.
51 public async updateMediatorServer(server: IndexableMediatorServer): Promise<PostResponse | null> {
52 const path = `/restconf/operations/data-provider:update-mediator-server`;
60 const result = await requestRest<PostResponse>(path, { method: "POST", body: JSON.stringify({ input: data }) });
61 return result || null;
65 * Deletes data from the mediator servers table.
67 public async deleteMediatorServer(server: MediatorServer): Promise<DeleteResponse | null> {
68 const path = `/restconf/operations/data-provider:delete-mediator-server`;
74 const result = await requestRest<DeleteResponse>(path, { method: "POST", body: JSON.stringify({ input: data }) });
75 return result || null;
78 public async getMediatorServerById(serverId: string): Promise<MediatorServer | null> {
79 const path = `/restconf/operations/data-provider:read-mediator-server-list`;
81 const data = { "filter": [{ "property": "id", "filtervalue": serverId }] }
84 const result = await requestRest<Result<MediatorServer>>(path, { method: "POST", body: JSON.stringify({ input: data }) });
86 if (result && result["data-provider:output"].data[0]) {
87 const firstResult = result["data-provider:output"].data[0];
91 name: firstResult.name,
100 // https://cloud-highstreet-technologies.com/wiki/doku.php?id=att:ms:api
102 private async accassMediatorServer<TData = {}>(mediatorServerId: string, task: string, data?: {}): Promise<MediatorServerResponse<TData> | null> {
103 const path = `ms/${mediatorServerId}/api/'?task=${task}`;
104 const result = (await requestRest<string>(path, {
105 method: data ? "POST" : "GET",
107 'Content-Type': 'application/x-www-form-urlencoded'
109 body: data ? formEncode({
115 return result ? JSON.parse(result) as { code: number, data: TData } : null;
118 private accassMediatorServer<TData = {}>(mediatorServerId: string, task: string, data?: {}): Promise<MediatorServerResponse<TData> | null> {
119 const path = `ms/${mediatorServerId}/api/?task=${task}`;
120 return new Promise<{ code: number, data: TData }>((resolve, reject) => {
122 method: data ? 'POST' : 'GET',
124 data: { ...{ task: task }, ...data },
125 //contentType: 'application/json'
126 }).then((result: any) => {
127 if (typeof result === "string") {
128 resolve(JSON.parse(result));
136 public async getMediatorServerVersion(mediatorServerId: string): Promise<MediatorServerVersionInfo | null> {
137 const result = await this.accassMediatorServer<MediatorServerVersionInfo>(mediatorServerId, 'version');
138 if (result && result.code === 1) return result.data;
142 public async getMediatorServerAllConfigs(mediatorServerId: string): Promise<MediatorConfigResponse[] | null> {
143 const result = await this.accassMediatorServer<MediatorConfigResponse[]>(mediatorServerId, 'getconfig');
144 if (result && result.code === 1) return result.data;
148 public async getMediatorServerConfigByName(mediatorServerId: string, name: string): Promise<MediatorConfigResponse | null> {
149 const result = await this.accassMediatorServer<MediatorConfigResponse[]>(mediatorServerId, `getconfig&name=${name}`);
150 if (result && result.code === 1 && result.data && result.data.length === 1) return result.data[0];
154 public async getMediatorServerSupportedDevices(mediatorServerId: string): Promise<MediatorServerDevice[] | null> {
155 const result = await this.accassMediatorServer<MediatorServerDevice[]>(mediatorServerId, 'getdevices');
156 if (result && result.code === 1) return result.data;
160 public async startMediatorByName(mediatorServerId: string, name: string): Promise<string | null> {
161 const result = await this.accassMediatorServer<string>(mediatorServerId, `start&name=${name}`);
162 if (result && result.code === 1) return result.data;
166 public async stopMediatorByName(mediatorServerId: string, name: string): Promise<string | null> {
167 const result = await this.accassMediatorServer<string>(mediatorServerId, `stop&name=${name}`);
168 if (result && result.code === 1) return result.data;
172 public async createMediatorConfig(mediatorServerId: string, config: MediatorConfig): Promise<string | null> {
173 const result = await this.accassMediatorServer<string>(mediatorServerId, 'create', { config: JSON.stringify(config) });
174 if (result && result.code === 1) return result.data;
178 public async updateMediatorConfigByName(mediatorServerId: string, config: MediatorConfig): Promise<string | null> {
179 const result = await this.accassMediatorServer<string>(mediatorServerId, 'update', { config: JSON.stringify(config) });
180 if (result && result.code === 1) return result.data;
184 public async deleteMediatorConfigByName(mediatorServerId: string, name: string): Promise<string | null> {
185 const result = await this.accassMediatorServer<string>(mediatorServerId, `delete&name=${name}`);
186 if (result && result.code === 1) return result.data;
190 public async getMediatorServerFreeNcPorts(mediatorServerId: string, limit?: number): Promise<number[] | null> {
191 const result = await this.accassMediatorServer<number[]>(mediatorServerId, 'getncports', { limit });
192 if (result && result.code === 1) return result.data;
196 public async getMediatorServerFreeSnmpPorts(mediatorServerId: string, limit?: number): Promise<number[] | null> {
197 const result = await this.accassMediatorServer<number[]>(mediatorServerId, 'getsnmpports', { limit });
198 if (result && result.code === 1) return result.data;
203 export const mediatorService = new MediatorService;
204 export default mediatorService;