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 {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";
33 export class PoliciesService implements IZoneService {
36 private mapApiDirections = {
37 'RESOURCE': 'resources',
41 constructor(private http:HttpService, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
42 this.baseUrl = sdcConfig.api.root;
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 => {
51 public addPolicyTarget(topologyTemplateType:string, topologyTemplateId:string, policy:PolicyInstance, targetId:string, targetType:TargetOrMemberType) {
52 let _targets:Array<string>;
53 let _members:Array<string>;
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);
62 let policyTargetRequest:PolicyTargetsRequest = new PolicyTargetsRequest(_members, _targets);
63 return this.updatePolicyTargets(topologyTemplateType, topologyTemplateId, policy.uniqueId, policyTargetRequest);
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);
74 let policyTargetRequest:PolicyTargetsRequest = new PolicyTargetsRequest(_members, _targets);
75 return this.updatePolicyTargets(topologyTemplateType, topologyTemplateId, policy.uniqueId, policyTargetRequest);
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()));
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);
88 let groups:Array<string> = _.filter(targets, (target:TargetUiObject)=> {
89 return target.type === TargetOrMemberType.GROUPS;
90 }).map(target => target.uniqueId);
92 let policyTargetRequest:PolicyTargetsRequest = new PolicyTargetsRequest(groups, instances);
93 return this.updatePolicyTargets(topologyTemplateType, topologyTemplateId, policyId, policyTargetRequest);
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)
99 return new PolicyInstance(res.json());
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 => {
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 => {
115 public updateZoneInstanceAssignments(topologyTemplateType:string, topologyTemplateId:string, policyId:string, targets:Array<IZoneInstanceAssignment>):Observable<PolicyInstance>{
116 return this.updateTargets(topologyTemplateType, topologyTemplateId, policyId, targets);
119 public deleteZoneInstance(topologyTemplateType:string, topologyTemplateId:string, policyId:string):Observable<any> {
120 return this.deletePolicy(topologyTemplateType, topologyTemplateId, policyId);