843799df166bc92304beb38c9bcd71a83200cc29
[dcaegen2/services.git] /
1 /*
2     Copyright (C) 2019 CMCC, Inc. and others. All rights reserved.
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
17 /**
18  * @author Chunmeng Guo
19  */
20
21 import {Component, EventEmitter, Input, OnInit, Output, ElementRef, ViewChild} from '@angular/core';
22
23 import { NgbActiveModal } from "@ng-bootstrap/ng-bootstrap";
24 import { RestApiService } from "src/app/core/services/rest-api.service";
25 import { NgxSpinnerService } from "ngx-spinner";
26
27 import { Kafka } from "../../../../core/models/kafka.model";
28
29 @Component({
30   selector: 'app-new-kafka-modal',
31   templateUrl: './new-kafka-modal.component.html',
32   styleUrls: ['./new-kafka-modal.component.css']
33 })
34 export class NewKafkaModalComponent implements OnInit {
35   @Input() kafka: Kafka;
36   @Input() kafkaList_length;
37   @Output() passEntry: EventEmitter<any> = new EventEmitter();
38   kafkaInput: Kafka;
39   exTopicFields: Array<any> = [];
40   exTopicNewField: any = {};
41   protocols: Array<string> = ["SASL_PLAINTEXT"];
42
43   @ViewChild("k_name") k_name: ElementRef;
44   @ViewChild("k_login") k_login: ElementRef;
45   @ViewChild("k_pass") k_pass: ElementRef;
46   @ViewChild("k_enabled") k_enabled: ElementRef;
47   @ViewChild("k_brokerList") k_brokerList: ElementRef;
48   @ViewChild("k_zooKeeper") k_zooKeeper: ElementRef;
49   @ViewChild("k_group") k_group: ElementRef;
50   @ViewChild("k_secure") k_secure: ElementRef;
51   @ViewChild("k_securityProtocol") k_securityProtocol: ElementRef;
52   @ViewChild("k_includedTopic") k_includedTopic: ElementRef;
53   @ViewChild("k_excludedTopic") k_excludedTopic: ElementRef;
54   @ViewChild("k_consumerCount") k_consumerCount: ElementRef;
55   @ViewChild("k_timeout") k_timeout: ElementRef;
56
57   constructor(
58     private activeModal: NgbActiveModal,
59     private kafkaApiService: RestApiService,
60     private spinner: NgxSpinnerService
61   ) { }
62
63   ngOnInit() {
64     // cache for display
65     this.kafkaInput = new Kafka();
66     const feed = {
67       id: null,
68       name: this.kafka.name,
69       enabled: this.kafka.enabled,
70       brokerList: this.kafka.brokerList,
71       zooKeeper: this.kafka.zooKeeper,
72       group: this.kafka.group,
73       secure: this.kafka.secure,
74       login: this.kafka.login,
75       pass: this.kafka.pass,
76       securityProtocol: this.kafka.securityProtocol,
77       includedTopic: this.kafka.includedTopic,
78       excludedTopic: this.kafka.excludedTopic,
79       consumerCount: this.kafka.consumerCount,
80       timeout: this.kafka.timeout
81     };
82     this.kafkaInput = feed;
83     this.exTopicFields = [];
84     if (this.kafkaInput.excludedTopic != null) {
85       var feeds = this.kafkaInput.excludedTopic.split(",");
86       for (var i = 0; i < feeds.length; i++) {
87         var data = { item: feed[i] };
88         this.exTopicFields.push(data);
89       }
90     } else {
91       this.exTopicFields.push([]);
92     }
93   }
94
95   passBack() {
96     this.spinner.show();
97     if (this.kafkaInput.name == '' || this.kafkaInput.name == undefined) {
98       return false;
99     }
100     this.kafka = this.kafkaInput;
101     this.kafka.securityProtocol = this.k_securityProtocol.nativeElement.value;
102     for (var i = 0; i < this.exTopicFields.length; i++) {
103       let item = this.exTopicFields[i].item;
104       if (i == 0) {
105         this.kafka.excludedTopic = item;
106       } else {
107         this.kafka.excludedTopic = this.kafka.excludedTopic + "," + item;
108       }
109     }
110     console.log(this.kafka);
111     this.passEntry.emit(this.kafka);
112
113   }
114
115   addExTopicField() {
116     this.exTopicFields.push(this.exTopicNewField);
117     this.exTopicNewField = {};
118   }
119
120   deleteExTopicField(index: number) {
121     if (this.exTopicFields.length > 1) {
122       this.exTopicFields.splice(index, 1);
123     }
124   }
125 }