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 { Component, HostBinding, Input, OnInit } from '@angular/core';
22 import { Select, Store } from '@ngxs/store';
23 import { CompositionService } from 'app/ng2/pages/composition/composition.service';
24 import { WorkspaceService } from 'app/ng2/pages/workspace/workspace.service';
25 import { EventListenerService } from 'app/services/event-listener-service';
26 import { GRAPH_EVENTS } from 'app/utils';
27 import * as _ from 'lodash';
28 import { SdcUiCommon, SdcUiComponents, SdcUiServices } from 'onap-ui-angular';
29 import { Observable } from 'rxjs';
30 import { tap } from 'rxjs/operators';
31 import { ComponentInstance } from '../../../../../../models/componentsInstances/componentInstance';
32 import { PolicyInstance } from '../../../../../../models/graph/zones/policy-instance';
33 import { TargetUiObject } from '../../../../../../models/ui-models/ui-target-object';
34 import { TargetOrMemberType } from '../../../../../../utils/constants';
35 import { AddElementsComponent } from '../../../../../components/ui/modal/add-elements/add-elements.component';
36 import { PoliciesService } from '../../../../../services/policies.service';
37 import { TranslateService } from '../../../../../shared/translator/translate.service';
38 import { GraphState } from '../../../common/store/graph.state';
41 selector: 'policy-targets-tab',
42 templateUrl: './policy-targets-tab.component.html',
43 styleUrls: ['policy-targets-tab.component.less']
46 export class PolicyTargetsTabComponent implements OnInit {
50 @Input() isViewOnly: boolean;
51 @HostBinding('class') classes = 'component-details-panel-tab-policy-targets';
52 @Select(GraphState.getSelectedComponent) policy$: Observable<PolicyInstance>;
53 public policy: PolicyInstance;
54 public targets: TargetUiObject[]; // UI object to hold all targets with names.
57 private addModalInstance: SdcUiComponents.ModalComponent;
59 constructor(private translateService: TranslateService,
60 private policiesService: PoliciesService,
61 private modalService: SdcUiServices.ModalService,
62 private eventListenerService: EventListenerService,
63 private compositionService: CompositionService,
64 private workspaceService: WorkspaceService,
65 private loaderService: SdcUiServices.LoaderService,
70 this.subscription = this.policy$.pipe(
72 if (policy instanceof PolicyInstance) {
74 this.targets = this.policy.getTargetsAsUiObject(this.compositionService.componentInstances as ComponentInstance[], this.compositionService.groupInstances);
80 if (this.subscription) {
81 this.subscription.unsubscribe();
85 deleteTarget(target: TargetUiObject): void {
86 this.loaderService.activate();
87 this.policiesService.deletePolicyTarget(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, this.policy, target.uniqueId, target.type).subscribe(
88 (policyInstance: PolicyInstance) => {
89 this.targets = this.targets.filter((item) => item.uniqueId !== target.uniqueId);
90 this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_POLICY_INSTANCE_UPDATE, policyInstance);
91 // this.store.dispatch(new UpdateSelectedComponentAction({uniqueId: policyInstance.uniqueId, type:ComponentType.}));
94 console.log('Error deleting target!');
95 this.loaderService.deactivate();
97 () => this.loaderService.deactivate()
101 addTargets = (): void => {
103 const targetsToAdd: TargetUiObject[] = this.addModalInstance.innerModalContent.instance.existingElements; // TODO refactor sdc-ui modal in order to return the data
104 if (targetsToAdd.length > 0) {
105 this.addModalInstance.closeModal();
106 this.loaderService.activate();
107 const updatedTargets: TargetUiObject[] = _.union(this.targets, targetsToAdd);
108 this.policiesService.updateTargets(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, this.policy.uniqueId, updatedTargets).subscribe(
109 (updatedPolicyInstance: PolicyInstance) => {
110 this.targets = updatedTargets;
111 this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_POLICY_INSTANCE_UPDATE, updatedPolicyInstance);
112 // this.store.dispatch(new UpdateSelectedComponentAction({component: updatedPolicyInstance}));
115 console.log('Error updating targets!');
116 this.loaderService.deactivate();
118 () => this.loaderService.deactivate()
123 getOptionalsTargetsToAdd(): TargetUiObject[] {
124 const optionalsTargetsToAdd: TargetUiObject[] = [];
125 // adding all instances as optional targets to add if not already exist
126 _.forEach(this.compositionService.componentInstances, (instance: ComponentInstance) => {
127 if (!_.some(this.targets, (target: TargetUiObject) => {
128 return target.uniqueId === instance.uniqueId;
130 optionalsTargetsToAdd.push(new TargetUiObject(instance.uniqueId, TargetOrMemberType.COMPONENT_INSTANCES, instance.name));
134 // adding all groups as optional targets to add if not already exist
135 _.forEach(this.compositionService.groupInstances, (groupInstance: ComponentInstance) => { // adding all instances as optional targets to add if not already exist
136 if (!_.some(this.targets, (target: TargetUiObject) => {
137 return target.uniqueId === groupInstance.uniqueId;
139 optionalsTargetsToAdd.push(new TargetUiObject(groupInstance.uniqueId, TargetOrMemberType.GROUPS, groupInstance.name));
143 // Sort targets alphabetically
144 optionalsTargetsToAdd.sort((a, b) => (a.name < b.name ? -1 : 1));
146 return optionalsTargetsToAdd;
149 openAddTargetModal(): void {
150 const addTargetModalConfig = {
151 title: this.policy.name + ' ADD TARGETS',
153 type: SdcUiCommon.ModalType.custom,
154 testId: 'addTargetsModal',
156 {text: 'ADD TARGETS', size: 'xsm', callback: this.addTargets, closeModal: false},
157 {text: 'CANCEL', size: 'sm', type: 'secondary', closeModal: true}
159 } as SdcUiCommon.IModalConfig;
160 const optionalTargetsToAdd = this.getOptionalsTargetsToAdd();
161 this.addModalInstance = this.modalService.openCustomModal(addTargetModalConfig, AddElementsComponent, {
162 elementsToAdd: optionalTargetsToAdd,
163 elementName: 'target'