added ansible server functionality
[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(private notificationService: NotificationsService, private utilityService:UtilityService, private apiService: APIService) {
39     }
40
41     checkResult(result: any) {
42
43         if (result.output.status.code == '401') {
44             this.notificationService.info('Information', this.failureMessage);
45             return false;
46         }
47         else if (result.output.status.code == '400') {
48             this.notificationService.success('Success', this.successMessage);
49             return true;
50         }
51
52     }
53
54     // utility to add the slashes to the payload
55     appendSlashes(artifactData) {
56         return artifactData.replace(/"/g, '\\"');
57     }
58
59     public nullCheckForVnfcType(object: any) {
60         if (object == undefined || object == 'null' || object == false) {
61             return true;
62         }
63
64     }
65
66     public nullCheckForVnfcTypeList(object: any) {
67         if (object == undefined || object == null || object.length == 0) {
68             return true;
69         }
70
71     }
72
73     //used for forming the file exension
74     decideExtension(obj) {
75         //marking the extension based on the device-protocol selected by the user 
76         let extension = '.json';
77         switch (obj['device-protocol']) {
78             case 'ANSIBLE':
79             case 'CHEF':
80             case 'CLI':
81                 extension = '.json';
82                 break;
83             case 'NETCONF-XML':
84             case 'REST':
85                 extension = '.xml';
86                 break;
87         }
88         return extension;
89     }
90
91
92
93     createArtifactName(action, vnf, type, extension) {
94         if (type)
95             return action + '_' + vnf.replace(/ /g, '').replace(new RegExp('/', 'g'), '_').replace(/ /g, '') + '_' + type + '_' + '0.0.1V' + extension;
96         else
97             return action + '_' + vnf.replace(/ /g, '').replace(new RegExp('/', 'g'), '_').replace(/ /g, '') + '_' + '0.0.1V' + extension;
98     }
99
100     createArtifactNameForIdentifiers(action, vnf, type, extension)
101     {
102         return action + '_' + vnf.replace(/ /g, '').replace(new RegExp('/', 'g'), '_').replace(/ /g, '') + '_' + '0.0.1V_' + type + extension;
103     }
104
105
106     createConfigTemplate(config_template_fileName) {
107         var template= {
108             'artifact-name': 'template_' + config_template_fileName,
109             'artifact-type': 'config_template'
110         };
111
112         return template;
113     }
114
115     createConfigTemplateForPushReplaceData(config_template_fileName) {
116         var template= {
117             'template_artifact': 'template_' + config_template_fileName,
118         };
119
120         return template;
121     }
122
123     createPdTemplate(pd_fileName) {
124         var pd= {
125             'artifact-name': 'pd_' + pd_fileName,
126             'artifact-type': 'parameter_definitions'
127         };
128
129         return pd;
130     }
131
132     createPdTemplateForPushReplaceData(pd_fileName) {
133         var pd= {
134             'pd_artifact': 'pd_' + pd_fileName,
135         };
136
137         return pd;
138     }
139
140     createParamValue(param_fileName) {
141         var paramValue= {
142             'artifact-name': 'param_' + param_fileName,
143             'artifact-type': 'param_values'
144         };
145         return paramValue;
146     }
147
148     createParamValueForPushReplaceData(param_fileName) {
149         var paramValue= {
150             'param_artifact': 'param_' + param_fileName,
151         };
152         return paramValue;
153     }
154
155     handleApiData(data,artifactType)
156     {
157         this.response = this.apiService.callGetArtifactsApi(data);
158         this.response.subscribe(response => {
159           this.utilityService.processApiSubscribe(response, this.utilityService.putAction, artifactType)
160         },
161         error => this.utilityService.processApiError());
162     }
163
164 }