APPC CDT to Support Multiple Templates for VNFCs
[appc/cdt.git] / src / app / vnfs / build-artifacts / reference-dataform / reference-dataform.util.ts
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
5 ===================================================================
6 Copyright (C) 2018 IBM.
7 ===================================================================
8 Unless otherwise specified, all software contained herein is licensed
9 under the Apache License, Version 2.0 (the License);
10 you may not use this software except in compliance with the License.
11 You may obtain a copy of the License at
12
13     http://www.apache.org/licenses/LICENSE-2.0
14
15 Unless required by applicable law or agreed to in writing, software
16 distributed under the License is distributed on an "AS IS" BASIS,
17 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 See the License for the specific language governing permissions and
19 limitations under the License.
20 ============LICENSE_END============================================
21 */
22
23 import { Injectable } from '@angular/core';
24 import { NotificationsService } from 'angular2-notifications';
25 import {Observable} from 'rxjs';
26 import {UtilityService} from '../../../shared/services/utilityService/utility.service';
27 import { environment } from '../../../../environments/environment';
28 import { HttpUtilService } from '../../../shared/services/httpUtil/http-util.service';
29 //import {APIService} from "../../../shared/services/cdt.apicall";
30
31 @Injectable()
32 export class ReferenceDataFormUtil {
33
34     private successMessage = 'Retrieved artifact successfully';
35     private failureMessage = 'There is no artifact saved in APPC for the selected action!';
36     private response: Observable<Object>;
37
38     constructor(
39       private notificationService: NotificationsService, private utilityService:UtilityService, private httpUtils: HttpUtilService
40     ) {
41     }
42
43     checkResult(result: any) {
44
45         if (result.output.status.code == '401') {
46             this.notificationService.info('Information', this.failureMessage);
47             return false;
48         }
49         else if (result.output.status.code == '400') {
50             this.notificationService.success('Success', this.successMessage);
51             return true;
52         }
53
54     }
55
56     // utility to add the slashes to the payload
57     appendSlashes(artifactData) {
58         return artifactData.replace(/"/g, '\\"');
59     }
60
61     public nullCheckForVnfcType(object: any) {
62         if (object == undefined || object == 'null' || object == false) {
63             return true;
64         }
65
66     }
67
68     public nullCheckForVnfcTypeList(object: any) {
69         if (object == undefined || object == null || object.length == 0) {
70             return true;
71         }
72
73     }
74
75     //used for forming the file exension
76     decideExtension(obj) {
77         //marking the extension based on the device-protocol selected by the user 
78         let extension = '.json';
79         switch (obj['device-protocol']) {
80             case 'ANSIBLE':
81             case 'CHEF':
82             case 'CLI':
83                 extension = '.json';
84                 break;
85             case 'NETCONF-XML':
86             case 'REST':
87                 extension = '.xml';
88                 break;
89         }
90         return extension;
91     }
92
93
94
95     createArtifactName(action, vnf, type, extension) {
96         if (type)
97             return action + '_' + vnf.replace(/ /g, '').replace(new RegExp('/', 'g'), '_').replace(/ /g, '') + '_' + type + '_' + '0.0.1V' + extension;
98         else
99             return action + '_' + vnf.replace(/ /g, '').replace(new RegExp('/', 'g'), '_').replace(/ /g, '') + '_' + '0.0.1V' + extension;
100     }
101
102     createArtifactNameForIdentifiers(action, vnf, type, extension)
103     {
104         return action + '_' + vnf.replace(/ /g, '').replace(new RegExp('/', 'g'), '_').replace(/ /g, '') + '_' + '0.0.1V_' + type + extension;
105     }
106
107
108     createConfigTemplate(config_template_fileName) {
109         var template= {
110             'artifact-name': 'template_' + config_template_fileName,
111             'artifact-type': 'config_template'
112         };
113
114         return template;
115     }
116
117     createConfigTemplateForPushReplaceData(config_template_fileName) {
118         var template= {
119             'template_artifact': 'template_' + config_template_fileName,
120         };
121
122         return template;
123     }
124
125     createPdTemplate(pd_fileName) {
126         var pd= {
127             'artifact-name': 'pd_' + pd_fileName,
128             'artifact-type': 'parameter_definitions'
129         };
130
131         return pd;
132     }
133
134     createPdTemplateForPushReplaceData(pd_fileName) {
135         var pd= {
136             'pd_artifact': 'pd_' + pd_fileName,
137         };
138
139         return pd;
140     }
141
142     createParamValue(param_fileName) {
143         var paramValue= {
144             'artifact-name': 'param_' + param_fileName,
145             'artifact-type': 'param_values'
146         };
147         return paramValue;
148     }
149
150     createParamValueForPushReplaceData(param_fileName) {
151         var paramValue= {
152             'param_artifact': 'param_' + param_fileName,
153         };
154         return paramValue;
155     }
156
157     handleApiData(payloadData,artifactType)
158     {
159         this.response =
160         this.httpUtils.post({
161             url: environment.getDesigns,
162             data: payloadData });
163         this.response.subscribe( response => {
164           this.utilityService.processApiSubscribe(response, this.utilityService.putAction, artifactType)
165         },
166         error => this.utilityService.processApiError());
167     }
168
169 }