Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / mediatorApp / src / services / mediatorService.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 import * as $ from 'jquery';
19
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';
23
24 export const mediatorServerResourcePath = "mediator-server";
25
26 type MediatorServerResponse<TData> = { code: number, data: TData };
27 type IndexableMediatorServer = MediatorServer & { [key: string]: any; };
28
29 /** 
30  * Represents a web api accessor service for all mediator server actions.
31  */
32 class MediatorService {
33   /**
34     * Inserts data into the mediator servers table.
35     */
36   public async insertMediatorServer(server: IndexableMediatorServer): Promise<PostResponse | null> {
37     const path = `/restconf/operations/data-provider:create-mediator-server`;
38
39     const data = {
40       "url": server.url,
41       "name": server.name
42     }
43
44     const result = await requestRest<PostResponse>(path, { method: "POST", body: JSON.stringify({ input: data }) });
45     return result || null;
46   }
47
48   /**
49     * Updates data into the mediator servers table.
50     */
51   public async updateMediatorServer(server: IndexableMediatorServer): Promise<PostResponse | null> {
52     const path = `/restconf/operations/data-provider:update-mediator-server`;
53
54     const data = {
55       "id": server.id,
56       "url": server.url,
57       "name": server.name
58     }
59
60     const result = await requestRest<PostResponse>(path, { method: "POST", body: JSON.stringify({ input: data }) });
61     return result || null;
62   }
63
64   /**
65     * Deletes data from the mediator servers table.
66     */
67   public async deleteMediatorServer(server: MediatorServer): Promise<DeleteResponse | null> {
68     const path = `/restconf/operations/data-provider:delete-mediator-server`;
69
70     const data = {
71       "id": server.id,
72     }
73
74     const result = await requestRest<DeleteResponse>(path, { method: "POST", body: JSON.stringify({ input: data }) });
75     return result || null;
76   }
77
78   public async getMediatorServerById(serverId: string): Promise<MediatorServer | null> {
79     const path = `/restconf/operations/data-provider:read-mediator-server-list`;
80
81     const data = { "filter": [{ "property": "id", "filtervalue": serverId }] }
82
83
84     const result = await requestRest<Result<MediatorServer>>(path, { method: "POST", body: JSON.stringify({ input: data }) });
85
86     if (result && result["data-provider:output"].data[0]) {
87       const firstResult = result["data-provider:output"].data[0];
88
89       return {
90         id: firstResult.id,
91         name: firstResult.name,
92         url: firstResult.url
93       }
94     }
95     else {
96       return null;
97     }
98   }
99
100   // https://cloud-highstreet-technologies.com/wiki/doku.php?id=att:ms:api
101
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",
106       headers: {
107         'Content-Type': 'application/x-www-form-urlencoded'
108       },
109       body: data ? formEncode({
110         ...data,
111         ...{ task: task }
112       }) : null
113     }, true)) || null;
114
115     return result ? JSON.parse(result) as { code: number, data: TData } : null;
116   }
117   /*
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) => {
121       $.ajax({
122         method: data ? 'POST' : 'GET',
123         url: path,
124         data: { ...{ task: task }, ...data },
125         //contentType: 'application/json'
126       }).then((result: any) => {
127         if (typeof result === "string") {
128           resolve(JSON.parse(result));
129         } else {
130           resolve(result);
131         };
132       });
133     });
134   }*/
135
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;
139     return null;
140   }
141
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;
145     return null;
146   }
147
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];
151     return null;
152   }
153
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;
157     return null;
158   }
159
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;
163     return null;
164   }
165
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;
169     return null;
170   }
171
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;
175     return null;
176   }
177
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;
181     return null;
182   }
183
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;
187     return null;
188   }
189
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;
193     return null;
194   }
195
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;
199     return null;
200   }
201 }
202
203 export const mediatorService = new MediatorService;
204 export default mediatorService;