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
13 http://www.apache.org/licenses/LICENSE-2.0
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.
21 ============LICENSE_END============================================
25 import {Injectable} from '@angular/core';
26 import {NotificationsService} from 'angular2-notifications';
27 import { saveAs } from 'file-saver';
28 import { appConstants } from '../../../../constants/app-constants';
31 export class UtilityService {
32 clName= "UtilityService";
33 public putAction = appConstants.messages.artifactUploadAction;
34 public getAction = appConstants.messages.artifactgetAction;
35 private retrievalSuccessMessage = appConstants.messages.retrievalSuccessMessage;
36 private retrievalFailureMessage = appConstants.messages.retrievalFailureMessage;
37 private saveSuccessMessage = appConstants.messages.saveSuccessMessage;
38 private saveFailureMessage = appConstants.messages.saveFailureMessage;
39 public connectionErrorMessage = appConstants.errors.connectionError;
41 private successMessage = appConstants.messages.artifactRetrivalsuccessMessage;
42 private failureMessage = appConstants.messages.artifactRetrivalfailureMessage;
44 constructor(private notificationService: NotificationsService) {
47 public setTracelvl( tlvl: number ) {
48 // console.log( this.clName+": setTracelvl: arg="+tlvl );
50 if( tracelvl == null || tracelvl == undefined ) tracelvl= 0;
51 localStorage["Tracelvl"]= tracelvl;
54 public getTracelvl() : number {
55 let tracelvl= localStorage["Tracelvl"];
56 // console.log( this.clName+": getTracelvl: locS: tracelvl="+tracelvl );
57 if( tracelvl == null || tracelvl == undefined ) {
58 tracelvl=0; localStorage["Tracelvl"]= tracelvl;
60 // console.log( this.clName+": getTracelvl: tracelvl="+tracelvl );
65 let x = (new Date().getUTCMilliseconds()) * Math.random();
66 return (x + '').substr(4, 12);
71 public appendSlashes(artifactData) {
72 return artifactData.replace(/"/g, '\\"');
75 // get current date in "mm/dd/yyyy"
77 let toDay = new Date();
78 return (toDay.getMonth()+1) +"/"+ toDay.getDate() +"/"+ toDay.getFullYear();
84 public checkResult(result: any) {
86 if (result.output.status.code == appConstants.errorCode["401"]) {
87 this.notificationService.info(appConstants.notifications.titles.information, this.failureMessage);
90 else if (result.output.status.code == appConstants.errorCode["400"]) {
91 this.notificationService.success(appConstants.notifications.titles.success, this.successMessage);
97 public processApiSubscribe(result: any, action, artifactType) {
99 if (result.output.status.code == appConstants.errorCode["401"] && action == this.getAction) {
100 this.notificationService.info(appConstants.notifications.titles.information, this.retrievalFailureMessage);
102 else if (result.output.status.code == appConstants.errorCode["400"] && action == this.getAction) {
103 this.notificationService.success(appConstants.notifications.titles.success, this.retrievalSuccessMessage);
105 if (result.output.status.code == appConstants.errorCode["401"] && action == this.putAction) {
106 this.notificationService.warn(appConstants.notifications.titles.error, this.saveFailureMessage + artifactType);
108 else if (result.output.status.code == appConstants.errorCode["400"] && action == this.putAction) {
109 this.notificationService.success(appConstants.notifications.titles.success, this.saveSuccessMessage + artifactType);
114 public processApiError()
116 this.notificationService.error(appConstants.notifications.titles.error, this.connectionErrorMessage);
120 public checkNotNull(object:any)
123 if (object != undefined || object != null)
125 if(object.length > 0)
132 public createPayLoadForSave(artifactType,vnfType,action,fileName, versionNo, artifactContent)
134 let userId=localStorage['userId'];
135 let apiToken=localStorage['apiToken']
139 case "reference_data":
140 //newPayload='{"userID": "' + userId + '","vnf-type" : "' + vnfType + '","action: "'+action+'","artifact-name" : "' + fileName.replace(/ /g, '').replace(new RegExp('/', 'g'), '_').replace(/ /g, '') + '","artifact-type" : "APPC-CONFIG","artifact-version" : "0.1","artifact-contents" :" ' + artifactContent + '"}';
141 newPayload='{"userID": "' + userId + '","vnf-type" : "' + vnfType + '","action" : "AllAction","artifact-name" : "' + fileName.replace(/ /g, '').replace(new RegExp('/', 'g'), '_').replace(/ /g, '') + '","artifact-type" : "APPC-CONFIG","artifact-version" : "0.1","artifact-contents" :" ' + artifactContent + '"}';
143 case "template_data":
144 newPayload= JSON.stringify({
148 "artifact-name": fileName,
149 "artifact-type": "APPC-CONFIG",
150 "artifact-version": versionNo,
151 "artifact-contents": artifactContent.replace(/\(([^()]|(R))*\)=\(/g, '').replace(/\)}/g, '}')
155 newPayload=JSON.stringify({
159 "artifact-name": fileName,
160 "artifact-type": "APPC-CONFIG",
161 "artifact-version": versionNo,
162 "artifact-contents": artifactContent
166 newPayload='{"userID": "' + userId + '","vnf-type" : "' + vnfType + '","action" : "' + action + '","artifact-name" : "' + fileName + '","artifact-type" : "APPC-CONFIG","artifact-version" :"'+versionNo+'","artifact-contents" : ' + artifactContent + '}';
168 default : newPayload={};
175 "request-id": apiToken,
176 "action": "uploadArtifact",
177 "payload": newPayload
185 public createPayloadForRetrieve(isReference:boolean,action,vnfType,fileName)
189 payload=JSON.parse(sessionStorage.getItem('updateParams'));
190 payload['userID'] = localStorage['userId'];
191 payload = JSON.stringify(payload);
193 else payload = '{"userID": "' + localStorage['userId'] + '","action": "' + action + '", "vnf-type" : "' + vnfType + '", "artifact-type":"APPC-CONFIG", "artifact-name":"' + fileName + '"}';
197 'request-id': localStorage['apiToken'],
198 'action': 'getArtifact',
206 public downloadArtifactToPc(data,extension, fileName, delay)
208 var blob = new Blob([data], {
209 type: 'text/'+extension
212 saveAs(blob, fileName);