RIC Configuration from UI
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / interceptor.mock.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 Nordix Foundation
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 import {
22   HttpEvent,
23   HttpHandler,
24   HttpInterceptor,
25   HttpRequest,
26   HttpResponse,
27 } from "@angular/common/http";
28 import { Injectable, Injector } from "@angular/core";
29 import { Observable, of } from "rxjs";
30 import * as policyinstance1 from "./mock/policy-instance-1.json";
31 import * as noTypePolicies from "./mock/no-type-policies.json";
32 import * as type0Policies from "./mock/type0-policies.json";
33 import * as type1Policies from "./mock/type1-policies.json";
34 import * as policyinstance2 from "./mock/policy-instance-2.json";
35 import * as noTypePolicyinstance from "./mock/policy-instance-notype.json";
36 import * as policyinstance1Status from "./mock/policy-instance-1-status.json";
37 import * as policyinstance2Status from "./mock/policy-instance-2-status.json";
38 import * as eijobsProd1 from "./mock/ei-jobs-producer1.json";
39 import * as eijobsProd2 from "./mock/ei-jobs-producer2.json";
40 import * as eiProducerIds from "./mock/ei-producerids.json";
41 import * as eiproducer1 from "./mock/ei-producer1.json";
42 import * as eiproducer2 from "./mock/ei-producer2.json";
43 import * as eiproducerstatus1 from "./mock/ei-producer-status1.json";
44 import * as eiproducerstatus2 from "./mock/ei-producer-status2.json";
45 import * as policytypes1 from "./mock/policy-type1.json";
46 import * as policytypes0 from "./mock/policy-type0.json";
47 import * as policyinstanceedit from "./mock/policy-instance-edit.json";
48 import * as ric1 from "./mock/ric1.json";
49 import * as ric2 from "./mock/ric2.json";
50 import * as ricconfig from "./mock/ric-configuration.json";
51 import { delay } from "rxjs/operators";
52
53 const urls = [
54   {
55     url: "/a1-policy/v2/policy-types/1",
56     json: policytypes1,
57   },
58   {
59     url: "/a1-policy/v2/policy-types/0",
60     json: policytypes0,
61   },
62   {
63     url: "/a1-policy/v2/policies?policytype_id=",
64     json: noTypePolicies,
65   },
66   {
67     url: "/a1-policy/v2/policies?policytype_id=0",
68     json: type0Policies,
69   },
70   {
71     url: "/a1-policy/v2/policies?policytype_id=1",
72     json: type1Policies,
73   },
74   {
75     url: "/a1-policy/v2/policies/2001",
76     json: noTypePolicyinstance,
77   },
78   {
79     url: "/a1-policy/v2/policies/2000",
80     json: policyinstance1,
81   },
82   {
83     url: "/a1-policy/v2/policies/2100",
84     json: policyinstance2,
85   },
86   {
87     url: "/a1-policy/v2/policies/2001/status",
88     json: policyinstance1Status,
89   },
90   {
91     url: "/a1-policy/v2/policies/2000/status",
92     json: policyinstance1Status,
93   },
94   {
95     url: "/a1-policy/v2/policies/2100/status",
96     json: policyinstance2Status,
97   },
98   {
99     url: "/a1-policy/v2/policies/2000?type=",
100     json: policyinstanceedit,
101   },
102   {
103     url: "/a1-policy/v2/policies/2100?type=",
104     json: policyinstanceedit,
105   },
106   {
107     url: "/a1-policy/v2/policies/2000?type=1",
108     json: policyinstanceedit,
109   },
110   {
111     url: "/a1-policy/v2/policies/2100?type=1",
112     json: policyinstanceedit,
113   },
114   {
115     url: "/a1-policy/v2/policies/2000?ric=ric1&type=1",
116     json: "",
117   },
118   {
119     url: "/a1-policy/v2/rics?policytype_id=0",
120     json: ric1,
121   },
122   {
123     url: "/a1-policy/v2/rics?policytype_id=1",
124     json: ric1,
125   },
126   {
127     url: "/a1-policy/v2/rics?policytype_id=",
128     json: ric2,
129   },
130   {
131     url: "/ei-producer/v1/eiproducers",
132     json: eiProducerIds,
133   },
134   {
135     url: "/ei-producer/v1/eiproducers/producer1",
136     json: eiproducer1,
137   },
138   {
139     url: "/ei-producer/v1/eiproducers/producer2",
140     json: eiproducer2,
141   },
142   {
143     url: "/ei-producer/v1/eiproducers/producer1/status",
144     json: eiproducerstatus1,
145   },
146   {
147     url: "/ei-producer/v1/eiproducers/producer2/status",
148     json: eiproducerstatus2,
149   },
150   {
151     url: "/ei-producer/v1/eiproducers/producer1/eijobs",
152     json: eijobsProd1,
153   },
154   {
155     url: "/ei-producer/v1/eiproducers/producer2/eijobs",
156     json: eijobsProd2,
157   },
158     {
159         url: '/a1-policy/v2/configuration',
160         json: ricconfig
161     }
162 ];
163
164 @Injectable()
165 export class HttpMockRequestInterceptor implements HttpInterceptor {
166   private toggleTypes = true;
167
168   constructor(private injector: Injector) {}
169
170   intercept(
171     request: HttpRequest<any>,
172     next: HttpHandler
173   ): Observable<HttpEvent<any>> {
174     let result: HttpResponse<any>;
175     if (request.method === "PUT" && request.url.includes("policies")) {
176       result = new HttpResponse({ status: 200 });
177     } else if (request.method === "DELETE") {
178       result = new HttpResponse({ status: 204 });
179     } else if (request.url === "/a1-policy/v2/policy-types") {
180       result = this.getAlternatingNoOfTypes();
181     } else {
182       for (const element of urls) {
183         if (request.url === element.url) {
184           result = new HttpResponse({
185             status: 200,
186             body: (element.json as any).default,
187           });
188         }
189       }
190     }
191
192     if (result) {
193       console.log(
194         "Mock answering http call :" + request.method + " " + request.url,
195         request.method === "PUT" ? request.body : null
196       );
197       console.log("Returning: " + result.status, result.body);
198       // Adding a delay to simulate real server call.
199       return of(result).pipe(delay(10));
200     } else {
201       return next.handle(request);
202     }
203   }
204
205   getAlternatingNoOfTypes(): HttpResponse<any> {
206     let result: HttpResponse<any>;
207     if (this.toggleTypes) {
208       this.toggleTypes = false;
209       result = new HttpResponse({
210         status: 200,
211         body: JSON.parse('{"policytype_ids": ["","1","0"]}'),
212       });
213     } else {
214       this.toggleTypes = true;
215       result = new HttpResponse({
216         status: 200,
217         body: JSON.parse('{"policytype_ids": ["","1"]}'),
218       });
219     }
220     return result;
221   }
222 }