1f0ee3ee06532b26144237a6b5e348a508cf169c
[holmes/rule-management.git] / rulemgt-frontend / src / app / correlation-ruleList / alarmRule.service.ts
1 /*
2  Copyright 2018 ZTE Corporation.
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8      http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 import { Injectable } from '@angular/core';
17 import { Http, Response, Headers } from '@angular/http';
18
19
20 import { RuleModel } from './alarmRule';
21 import { RuleRequest } from './ruleRequest';
22 import { Router } from '@angular/router';
23 import { ModalService } from '../correlation-modal/modal.service';
24
25 @Injectable()
26 export class AlarmRuleService {
27     private ruleUrl = "/api/holmes-rule-mgmt/v1/rule";
28     private headers = new Headers({ 'Content-Type': 'application/json' });
29     constructor(private http: Http, private modalService: ModalService, private router: Router) { }
30
31     getRules(): Promise<any> {
32         return this.http.get(this.ruleUrl)
33             .toPromise()
34             .then(res => res.json())
35             .catch(this.handleError);
36     }
37
38     private handleError(error: any): Promise<any> {
39         console.error('An error occurred', error);
40         return Promise.reject(error._body || error);
41     }
42
43     search(ruleId: string): Promise<RuleModel> {
44         if (typeof (ruleId) == "string") {
45             let rule = [{
46                 ruleId: null,
47                 ruleName: null,
48                 description: null,
49                 content: null,
50                 createtime: null,
51                 creator: null,
52                 updatetime: null,
53                 modifier: null,
54                 enabled: 0,
55             }]
56         }
57         let data = { 'ruleId': ruleId };
58         var queryrequest = JSON.stringify(data);
59         const url = `${this.ruleUrl}?queryrequest=${queryrequest}`;
60         return this.http.get(url, {headers:this.headers})
61             .toPromise()
62             .then(res => res.json().correlationRules as RuleModel[])
63             .catch(this.handleError);
64     }
65
66     searchrules(rule: RuleRequest): Promise<RuleModel[]> {
67         let data = { ruleName: rule.ruleName, enabled: rule.enabled }
68         console.log(JSON.stringify(data));
69         const url = `${this.ruleUrl}?queryrequest=${JSON.stringify(data)}`
70         return this.http.get(url, { body: data, headers: this.headers })
71             .toPromise()
72             .then(res => res.json().correlationRules as RuleModel[])
73             .catch(this.handleError);
74     }
75
76     checkContent(ruleContent: string): Promise<any> {
77         const url = "/api/holmes-engine-mgmt/v1/rule";
78         let data = { content: ruleContent };
79         return this.http
80             .post(url, JSON.stringify(data), { headers: this.headers })
81             .toPromise()
82             .then(res => res)
83             .catch(error => error);
84     }
85
86     updateRule(rule: RuleModel): Promise<any> {
87         let rules = {
88             "ruleId": rule.ruleId,
89             "description": rule.description,
90             "content": rule.content,
91             "enabled": rule.enabled,
92             "loopControlName": rule.loopControlName
93         }
94         const url = `${this.ruleUrl}`
95         return this.http
96             .post(url, JSON.stringify(rules), { headers: this.headers })
97             .toPromise()
98             .then(res => res)
99             .catch(error => error)
100     }
101
102     save(rule: RuleModel): Promise<any> {
103         let ruledata = {
104             "description": rule.description,
105             "content": rule.content,
106             "enabled": rule.enabled,
107             "ruleName": rule.ruleName,
108             "loopControlName": rule.loopControlName
109         }
110         return this.http.put(this.ruleUrl, JSON.stringify(ruledata), { headers: this.headers })
111             .toPromise()
112             .then(res => res)
113             .catch(error => error);
114     }
115
116     public delete(ruleId: string): Promise<void> {
117         const url = `${this.ruleUrl}` + '/' + ruleId;
118         return this.http.delete(url, { headers: this.headers })
119             .toPromise()
120             .then(res => {
121                
122             })
123             .catch(this.handleError);
124     }
125 }