re base code
[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 {HttpService} from "./http.service";
24 import {SdcConfigToken, ISdcConfig} from "../config/sdc-config.config";
25 import {PolicyInstance, PolicyTargetsRequest} from '../../models/graph/zones/policy-instance';
26 import {IZoneInstanceAssignment} from "../../models/graph/zones/zone-instance";
27 import {IZoneService} from "../../models/graph/zones/zone";
28 import {TargetUiObject} from "../../models/ui-models/ui-target-object";
29 import {TargetOrMemberType} from "../../utils/constants";
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:HttpService, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
42         this.baseUrl = sdcConfig.api.root;
43     }
44
45     public createPolicyInstance(topologyTemplateType:string, topologyTemplateId:string, policyType:string) {
46         return this.http.post(this.baseUrl + '/v1/catalog/' + this.mapApiDirections[topologyTemplateType.toUpperCase()] + '/' + topologyTemplateId + '/policies/' + policyType, {}).map(resp => {
47             return resp.json();
48         });
49     }
50
51     public addPolicyTarget(topologyTemplateType:string, topologyTemplateId:string, policy:PolicyInstance, targetId:string, targetType:TargetOrMemberType) {
52         let _targets:Array<string>;
53         let _members:Array<string>;
54
55         if (targetType === TargetOrMemberType.COMPONENT_INSTANCES) {
56             _targets = angular.copy(policy.targets.COMPONENT_INSTANCES);
57             _targets.push(targetId);
58         } else if (targetType === TargetOrMemberType.GROUPS) {
59             _members = angular.copy(policy.targets.GROUPS);
60             _members.push(targetId);
61         }
62         let policyTargetRequest:PolicyTargetsRequest = new PolicyTargetsRequest(_members, _targets);
63         return this.updatePolicyTargets(topologyTemplateType, topologyTemplateId, policy.uniqueId, policyTargetRequest);
64     }
65
66     public deletePolicyTarget(topologyTemplateType:string, topologyTemplateId:string, policy:PolicyInstance, targetId:string, targetType:TargetOrMemberType): Observable<PolicyInstance> {
67         let _targets:Array<string> = angular.copy(policy.targets.COMPONENT_INSTANCES);
68         let _members:Array<string> = angular.copy(policy.targets.GROUPS);
69         if (targetType === TargetOrMemberType.COMPONENT_INSTANCES) {
70             _targets = _.without(_targets, targetId);
71         } else if (targetType === TargetOrMemberType.GROUPS) {
72             _members = _.without(_members, targetId);
73         }
74         let policyTargetRequest:PolicyTargetsRequest = new PolicyTargetsRequest(_members, _targets);
75         return this.updatePolicyTargets(topologyTemplateType, topologyTemplateId, policy.uniqueId, policyTargetRequest);
76     }
77
78     public updatePolicyTargets(topologyTemplateType:string, topologyTemplateId:string, policyId:string, targets:PolicyTargetsRequest): Observable<PolicyInstance> {
79         return this.http.post(this.baseUrl + '/v1/catalog/' + this.mapApiDirections[topologyTemplateType.toUpperCase()] + '/' + topologyTemplateId + '/policies/' + policyId + '/targets', targets.requestItems)
80             .map(response => new PolicyInstance(response.json()));
81     }
82
83     public updateTargets(topologyTemplateType:string, topologyTemplateId:string, policyId:string, targets:Array<TargetUiObject>):Observable<PolicyInstance> {
84         let instances:Array<string> = _.filter(targets, (target:TargetUiObject)=> {
85             return target.type === TargetOrMemberType.COMPONENT_INSTANCES;
86         }).map(target => target.uniqueId);
87
88         let groups:Array<string> = _.filter(targets, (target:TargetUiObject)=> {
89             return target.type === TargetOrMemberType.GROUPS;
90         }).map(target => target.uniqueId);
91
92         let policyTargetRequest:PolicyTargetsRequest = new PolicyTargetsRequest(groups, instances);
93         return this.updatePolicyTargets(topologyTemplateType, topologyTemplateId, policyId, policyTargetRequest);
94     }
95
96     public getSpecificPolicy(topologyTemplateType:string, topologyTemplateId:string, policyId:string):Observable<PolicyInstance> {
97         return this.http.get(this.baseUrl + '/v1/catalog/' + this.mapApiDirections[topologyTemplateType.toUpperCase()] + '/' + topologyTemplateId + '/policies/' + policyId)
98             .map(res => {
99                 return new PolicyInstance(res.json());
100             });
101     }
102
103     public updateName(topologyTemplateType:string, topologyTemplateId:string, policyId:string, newName:string):Observable<any> {
104         return this.http.put(this.baseUrl + '/v1/catalog/' + this.mapApiDirections[topologyTemplateType.toUpperCase()] + '/' + topologyTemplateId + '/policies/' + policyId, {name: newName}).map(res => {
105             return res.json();
106         });
107     };
108
109     public deletePolicy(topologyTemplateType:string, topologyTemplateId:string, policyId:string) {
110         return this.http.delete(this.baseUrl + '/v1/catalog/' + this.mapApiDirections[topologyTemplateType.toUpperCase()] + '/' + topologyTemplateId + '/policies/' + policyId).map(resp => {
111             return resp.json();
112         });
113     };
114
115     public updateZoneInstanceAssignments(topologyTemplateType:string, topologyTemplateId:string, policyId:string, targets:Array<IZoneInstanceAssignment>):Observable<PolicyInstance>{
116         return this.updateTargets(topologyTemplateType, topologyTemplateId, policyId, targets);
117     };
118
119     public deleteZoneInstance(topologyTemplateType:string, topologyTemplateId:string, policyId:string):Observable<any> {
120         return this.deletePolicy(topologyTemplateType, topologyTemplateId, policyId);
121     };
122
123 }
124