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 {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";
33 export class PoliciesService implements IZoneService {
36 private mapApiDirections = {
37 'RESOURCE': 'resources',
41 constructor(private http: HttpClient, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
42 this.baseUrl = sdcConfig.api.root;
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, {});
49 public addPolicyTarget(topologyTemplateType:string, topologyTemplateId:string, policy:PolicyInstance, targetId:string, targetType:TargetOrMemberType) {
50 let _targets:Array<string>;
51 let _members:Array<string>;
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);
60 let policyTargetRequest:PolicyTargetsRequest = new PolicyTargetsRequest(_members, _targets);
61 return this.updatePolicyTargets(topologyTemplateType, topologyTemplateId, policy.uniqueId, policyTargetRequest);
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);
72 let policyTargetRequest:PolicyTargetsRequest = new PolicyTargetsRequest(_members, _targets);
73 return this.updatePolicyTargets(topologyTemplateType, topologyTemplateId, policy.uniqueId, policyTargetRequest);
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));
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);
86 let groups:Array<string> = _.filter(targets, (target:TargetUiObject)=> {
87 return target.type === TargetOrMemberType.GROUPS;
88 }).map(target => target.uniqueId);
90 let policyTargetRequest:PolicyTargetsRequest = new PolicyTargetsRequest(groups, instances);
91 return this.updatePolicyTargets(topologyTemplateType, topologyTemplateId, policyId, policyTargetRequest);
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)
97 return new PolicyInstance(res);
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});
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);
109 public updateZoneInstanceAssignments(topologyTemplateType:string, topologyTemplateId:string, policyId:string, targets:Array<IZoneInstanceAssignment>):Observable<PolicyInstance>{
110 return this.updateTargets(topologyTemplateType, topologyTemplateId, policyId, targets);
113 public deleteZoneInstance(topologyTemplateType:string, topologyTemplateId:string, policyId:string):Observable<any> {
114 return this.deletePolicy(topologyTemplateType, topologyTemplateId, policyId);