aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/composition/panel/panel-tabs/policy-targets-tab/policy-targets-tab.component.ts
blob: f11729039712758bcd237386d9d6f64b78155019 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

import * as _ from "lodash";
import { Component, Input, Output, EventEmitter, OnChanges, HostBinding, OnDestroy, OnInit } from "@angular/core";
import { TranslateService } from './../../../../../shared/translator/translate.service';
import { PoliciesService } from "../../../../../services/policies.service";
import { PolicyInstance } from './../../../../../../models/graph/zones/policy-instance';
import { SdcUiComponents, SdcUiCommon, SdcUiServices } from "onap-ui-angular";
import { AddElementsComponent } from "../../../../../components/ui/modal/add-elements/add-elements.component";
import { TargetUiObject } from "../../../../../../models/ui-models/ui-target-object";
import { ComponentInstance } from "../../../../../../models/componentsInstances/componentInstance";
import { TargetOrMemberType } from "../../../../../../utils/constants";
import { GRAPH_EVENTS } from 'app/utils';
import { EventListenerService } from 'app/services/event-listener-service';
import { CompositionService } from "app/ng2/pages/composition/composition.service";
import { WorkspaceService } from "app/ng2/pages/workspace/workspace.service";
import { Store } from "@ngxs/store";
import { Select } from "@ngxs/store";
import { Observable } from "rxjs";
import { tap } from "rxjs/operators";
import {GraphState} from "../../../common/store/graph.state";

@Component({
    selector: 'policy-targets-tab',
    templateUrl: './policy-targets-tab.component.html',
    styleUrls: ['policy-targets-tab.component.less']
})
 
export class PolicyTargetsTabComponent implements OnInit {

    @Input() input:any;


    @Input() isViewOnly: boolean;
    @HostBinding('class') classes = 'component-details-panel-tab-policy-targets';
    @Select(GraphState.getSelectedComponent) policy$: Observable<PolicyInstance>;
    public policy: PolicyInstance;
    private subscription;
    
    private addModalInstance: SdcUiComponents.ModalComponent;
    public targets: Array<TargetUiObject>; // UI object to hold all targets with names.


    constructor(private translateService: TranslateService,
        private policiesService: PoliciesService,
        private modalService: SdcUiServices.ModalService,
        private eventListenerService: EventListenerService,
        private compositionService: CompositionService,
        private workspaceService: WorkspaceService,
        private loaderService: SdcUiServices.LoaderService,
        private store: Store
    ) { }

    ngOnInit() {
        this.subscription = this.policy$.pipe(
            tap((policy) => {
                if(policy instanceof PolicyInstance){
                    this.policy = policy;
                    this.targets = this.policy.getTargetsAsUiObject(<ComponentInstance[]>this.compositionService.componentInstances, this.compositionService.groupInstances);                      
                }
            })).subscribe(); 
    }

    ngOnDestroy () {
        if(this.subscription)
            this.subscription.unsubscribe();
    }

    deleteTarget(target: TargetUiObject): void {
        this.loaderService.activate();
        this.policiesService.deletePolicyTarget(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, this.policy, target.uniqueId, target.type).subscribe(
            (policyInstance:PolicyInstance) => {
                this.targets = this.targets.filter(item => item.uniqueId !== target.uniqueId);
                this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_POLICY_INSTANCE_UPDATE, policyInstance);
                // this.store.dispatch(new UpdateSelectedComponentAction({uniqueId: policyInstance.uniqueId, type:ComponentType.}));
            },
            error => {
                console.log("Error deleting target!");
                this.loaderService.deactivate();
            },
            () => this.loaderService.deactivate()
        );
    }

   
    addTargets = ():void => {
        
        var targetsToAdd:Array<TargetUiObject> = this.addModalInstance.innerModalContent.instance.existingElements; //TODO refactor sdc-ui modal in order to return the data
        if(targetsToAdd.length > 0) {
            this.addModalInstance.closeModal();
            this.loaderService.activate();
            var updatedTargets: Array<TargetUiObject> = _.union(this.targets, targetsToAdd);
            this.policiesService.updateTargets(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, this.policy.uniqueId, updatedTargets).subscribe(
                (updatedPolicyInstance:PolicyInstance) => {
                    this.targets = updatedTargets;
                    this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_POLICY_INSTANCE_UPDATE, updatedPolicyInstance);
                    // this.store.dispatch(new UpdateSelectedComponentAction({component: updatedPolicyInstance}));
                },
                error => {
                    console.log("Error updating targets!");
                    this.loaderService.deactivate();
                },
                () => this.loaderService.deactivate()
            );
        }
    }

    getOptionalsTargetsToAdd():Array<TargetUiObject> {
        let optionalsTargetsToAdd:Array<TargetUiObject> = [];
        // adding all instances as optional targets to add if not already exist
        _.forEach(this.compositionService.componentInstances, (instance:ComponentInstance) => {
            if (!_.some(this.targets, (target:TargetUiObject) => {
                    return target.uniqueId === instance.uniqueId
                })) {
                optionalsTargetsToAdd.push(new TargetUiObject(instance.uniqueId, TargetOrMemberType.COMPONENT_INSTANCES, instance.name));
            }
        });

        // adding all groups as optional targets to add if not already exist
        _.forEach(this.compositionService.groupInstances, (groupInstance:ComponentInstance) => { // adding all instances as optional targets to add if not already exist
            if (!_.some(this.targets, (target:TargetUiObject) => {
                    return target.uniqueId === groupInstance.uniqueId
                })) {
                optionalsTargetsToAdd.push(new TargetUiObject(groupInstance.uniqueId, TargetOrMemberType.GROUPS, groupInstance.name));
            }
        });

        return optionalsTargetsToAdd;
    }

    openAddTargetModal(): void {
        let addTargetModalConfig = {
            title: this.policy.name + " ADD TARGETS",
            size: "md",
            type: SdcUiCommon.ModalType.custom,
            testId: "addTargetsModal",
            buttons: [
                {text: "ADD TARGETS", size: 'xsm', callback: this.addTargets, closeModal: false},
                {text: 'CANCEL', size: 'sm', type: "secondary", closeModal: true}
            ]
        } as SdcUiCommon.IModalConfig;
        var optionalTargetsToAdd = this.getOptionalsTargetsToAdd();
        this.addModalInstance = this.modalService.openCustomModal(addTargetModalConfig, AddElementsComponent, {
            elementsToAdd: optionalTargetsToAdd,
            elementName: "target"
        });
    }
}