2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 import * as _ from "lodash";
22 import { Component, Input, Output, EventEmitter, OnChanges, HostBinding, OnDestroy } from "@angular/core";
23 import { TranslateService } from './../../../../../shared/translator/translate.service';
24 import { Component as TopologyTemplate } from "app/models";
25 import { PoliciesService } from "../../../../../services/policies.service";
26 import { PolicyInstance, PolicyTargetsMap } from './../../../../../../models/graph/zones/policy-instance';
27 import { SimpleChanges } from "@angular/core/src/metadata/lifecycle_hooks";
28 import { SdcUiComponents } from "sdc-ui/lib/angular";
29 import { IModalConfig } from "sdc-ui/lib/angular/modals/models/modal-config";
30 import { AddElementsComponent } from "../../../../../components/ui/modal/add-elements/add-elements.component";
31 import { TargetUiObject } from "../../../../../../models/ui-models/ui-target-object";
32 import { ComponentInstance } from "../../../../../../models/componentsInstances/componentInstance";
33 import { TargetOrMemberType } from "../../../../../../utils/constants";
34 import { GRAPH_EVENTS } from 'app/utils';
35 import { EventListenerService } from 'app/services/event-listener-service';
38 selector: 'policy-targets-tab',
39 templateUrl: './policy-targets-tab.component.html',
40 styleUrls: ['./../base/base-tab.component.less', 'policy-targets-tab.component.less']
43 export class PolicyTargetsTabComponent implements OnChanges, OnDestroy {
45 private targets: Array<TargetUiObject>; // UI object to hold all targets with names.
47 @Input() policy: PolicyInstance;
48 @Input() topologyTemplate: TopologyTemplate;
49 @Input() isViewOnly: boolean;
50 @Output() isLoading: EventEmitter<boolean> = new EventEmitter<boolean>();
51 @HostBinding('class') classes = 'component-details-panel-tab-policy-targets';
53 constructor(private translateService: TranslateService,
54 private policiesService: PoliciesService,
55 private modalService: SdcUiComponents.ModalService,
56 private eventListenerService: EventListenerService
58 this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_POLICY_INSTANCE_UPDATE, this.initTargets)
61 ngOnChanges(changes:SimpleChanges):void {
66 this.eventListenerService.unRegisterObserver(GRAPH_EVENTS.ON_POLICY_INSTANCE_UPDATE);
69 deleteTarget(target: TargetUiObject): void {
70 this.isLoading.emit(true);
71 this.policiesService.deletePolicyTarget(this.topologyTemplate.componentType, this.topologyTemplate.uniqueId, this.policy, target.uniqueId, target.type).subscribe(
72 (policyInstance:PolicyInstance) => {
73 this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_POLICY_INSTANCE_UPDATE, policyInstance);
75 error => console.log("Error deleting target!"),
76 () => this.isLoading.emit(false)
80 private initTargets = (policyInstance?: PolicyInstance) => {
81 this.policy = policyInstance ? policyInstance : this.policy;
82 this.targets = this.policy.getTargetsAsUiObject(this.topologyTemplate.componentInstances, this.topologyTemplate.groupInstances);
85 addTargets = ():void => {
87 var targetsToAdd:Array<TargetUiObject> = this.modalService.getCurrentInstance().innerModalContent.instance.existingElements; //TODO refactor sdc-ui modal in order to return the data
88 if(targetsToAdd.length > 0) {
89 this.modalService.closeModal();
90 this.isLoading.emit(true);
91 var updatedTarget: Array<TargetUiObject> = _.union(this.targets, targetsToAdd);
92 this.policiesService.updateTargets(this.topologyTemplate.componentType, this.topologyTemplate.uniqueId, this.policy.uniqueId, updatedTarget).subscribe(
93 (updatedPolicyInstance:PolicyInstance) => {
94 this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_POLICY_INSTANCE_UPDATE, updatedPolicyInstance);
97 console.log("Error updating targets!");
99 () => this.isLoading.emit(false)
104 getOptionalsTargetsToAdd():Array<TargetUiObject> {
105 let optionalsTargetsToAdd:Array<TargetUiObject> = [];
106 // adding all instances as optional targets to add if not already exist
107 _.forEach(this.topologyTemplate.componentInstances, (instance:ComponentInstance) => {
108 if (!_.some(this.targets, (target:TargetUiObject) => {
109 return target.uniqueId === instance.uniqueId
111 optionalsTargetsToAdd.push(new TargetUiObject(instance.uniqueId, TargetOrMemberType.COMPONENT_INSTANCES, instance.name));
115 // adding all groups as optional targets to add if not already exist
116 _.forEach(this.topologyTemplate.groupInstances, (groupInstance:ComponentInstance) => { // adding all instances as optional targets to add if not already exist
117 if (!_.some(this.targets, (target:TargetUiObject) => {
118 return target.uniqueId === groupInstance.uniqueId
120 optionalsTargetsToAdd.push(new TargetUiObject(groupInstance.uniqueId, TargetOrMemberType.GROUPS, groupInstance.name));
124 return optionalsTargetsToAdd;
127 openAddTargetModal(): void {
128 let addTargetModalConfig: IModalConfig = {
129 title: this.policy.name + " ADD TARGETS",
132 testId: "addTargetsModal",
134 {text: "ADD TARGETS", size: 'xsm', callback: this.addTargets, closeModal: false},
135 {text: 'CANCEL', size: 'sm', type: "secondary", closeModal: true}
138 var optionalTargetsToAdd = this.getOptionalsTargetsToAdd();
139 this.modalService.openCustomModal(addTargetModalConfig, AddElementsComponent, {
140 elementsToAdd: optionalTargetsToAdd,
141 elementName: "target"