Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / services / policies.service.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 import {Injectable, Inject} from "@angular/core";
22 import {Observable} from "rxjs/Observable";
23 import {SdcConfigToken, ISdcConfig} from "../config/sdc-config.config";
24 import {PolicyInstance, PolicyTargetsRequest} from '../../models/graph/zones/policy-instance';
25 import {IZoneInstanceAssignment} from "../../models/graph/zones/zone-instance";
26 import {IZoneService} from "../../models/graph/zones/zone";
27 import {TargetUiObject} from "../../models/ui-models/ui-target-object";
28 import {TargetOrMemberType} from "../../utils/constants";
29 import { HttpClient } from "@angular/common/http";
30
31
32 @Injectable()
33 export class PoliciesService implements IZoneService {
34     protected baseUrl;
35
36     private mapApiDirections = {
37         'RESOURCE': 'resources',
38         'SERVICE': 'services'
39     }
40
41     constructor(private http: HttpClient, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
42         this.baseUrl = sdcConfig.api.root;
43     }
44
45     public createPolicyInstance(topologyTemplateType:string, topologyTemplateId:string, policyType:string): Observable<PolicyInstance> {
46         return this.http.post<PolicyInstance>(this.baseUrl + '/v1/catalog/' + this.mapApiDirections[topologyTemplateType.toUpperCase()] + '/' + topologyTemplateId + '/policies/' + policyType, {});
47     }
48
49     public addPolicyTarget(topologyTemplateType:string, topologyTemplateId:string, policy:PolicyInstance, targetId:string, targetType:TargetOrMemberType) {
50         let _targets:Array<string>;
51         let _members:Array<string>;
52
53         if (targetType === TargetOrMemberType.COMPONENT_INSTANCES) {
54             _targets = angular.copy(policy.targets.COMPONENT_INSTANCES);
55             _targets.push(targetId);
56         } else if (targetType === TargetOrMemberType.GROUPS) {
57             _members = angular.copy(policy.targets.GROUPS);
58             _members.push(targetId);
59         }
60         let policyTargetRequest:PolicyTargetsRequest = new PolicyTargetsRequest(_members, _targets);
61         return this.updatePolicyTargets(topologyTemplateType, topologyTemplateId, policy.uniqueId, policyTargetRequest);
62     }
63
64     public deletePolicyTarget(topologyTemplateType:string, topologyTemplateId:string, policy:PolicyInstance, targetId:string, targetType:TargetOrMemberType): Observable<PolicyInstance> {
65         let _targets:Array<string> = angular.copy(policy.targets.COMPONENT_INSTANCES);
66         let _members:Array<string> = angular.copy(policy.targets.GROUPS);
67         if (targetType === TargetOrMemberType.COMPONENT_INSTANCES) {
68             _targets = _.without(_targets, targetId);
69         } else if (targetType === TargetOrMemberType.GROUPS) {
70             _members = _.without(_members, targetId);
71         }
72         let policyTargetRequest:PolicyTargetsRequest = new PolicyTargetsRequest(_members, _targets);
73         return this.updatePolicyTargets(topologyTemplateType, topologyTemplateId, policy.uniqueId, policyTargetRequest);
74     }
75
76     public updatePolicyTargets(topologyTemplateType:string, topologyTemplateId:string, policyId:string, targets:PolicyTargetsRequest): Observable<PolicyInstance> {
77         return this.http.post<PolicyInstance>(this.baseUrl + '/v1/catalog/' + this.mapApiDirections[topologyTemplateType.toUpperCase()] + '/' + topologyTemplateId + '/policies/' + policyId + '/targets', targets.requestItems)
78             .map(response => new PolicyInstance(response));
79     }
80
81     public updateTargets(topologyTemplateType:string, topologyTemplateId:string, policyId:string, targets:Array<TargetUiObject>):Observable<PolicyInstance> {
82         let instances:Array<string> = _.filter(targets, (target:TargetUiObject)=> {
83             return target.type === TargetOrMemberType.COMPONENT_INSTANCES;
84         }).map(target => target.uniqueId);
85
86         let groups:Array<string> = _.filter(targets, (target:TargetUiObject)=> {
87             return target.type === TargetOrMemberType.GROUPS;
88         }).map(target => target.uniqueId);
89
90         let policyTargetRequest:PolicyTargetsRequest = new PolicyTargetsRequest(groups, instances);
91         return this.updatePolicyTargets(topologyTemplateType, topologyTemplateId, policyId, policyTargetRequest);
92     }
93
94     public getSpecificPolicy(topologyTemplateType:string, topologyTemplateId:string, policyId:string):Observable<PolicyInstance> {
95         return this.http.get<PolicyInstance>(this.baseUrl + '/v1/catalog/' + this.mapApiDirections[topologyTemplateType.toUpperCase()] + '/' + topologyTemplateId + '/policies/' + policyId)
96             .map(res => {
97                 return new PolicyInstance(res);
98             });
99     }
100
101     public updateName(topologyTemplateType:string, topologyTemplateId:string, policyId:string, newName:string):Observable<any> {
102         return this.http.put<PolicyInstance>(this.baseUrl + '/v1/catalog/' + this.mapApiDirections[topologyTemplateType.toUpperCase()] + '/' + topologyTemplateId + '/policies/' + policyId, {name: newName});
103     };
104
105     public deletePolicy(topologyTemplateType:string, topologyTemplateId:string, policyId:string) {
106         return this.http.delete<PolicyInstance>(this.baseUrl + '/v1/catalog/' + this.mapApiDirections[topologyTemplateType.toUpperCase()] + '/' + topologyTemplateId + '/policies/' + policyId);
107     };
108
109     public updateZoneInstanceAssignments(topologyTemplateType:string, topologyTemplateId:string, policyId:string, targets:Array<IZoneInstanceAssignment>):Observable<PolicyInstance>{
110         return this.updateTargets(topologyTemplateType, topologyTemplateId, policyId, targets);
111     };
112
113     public deleteZoneInstance(topologyTemplateType:string, topologyTemplateId:string, policyId:string):Observable<any> {
114         return this.deletePolicy(topologyTemplateType, topologyTemplateId, policyId);
115     };
116
117 }
118