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 ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ============LICENSE_END============================================
26 import {Injectable} from '@angular/core';
27 import {NotificationsService} from 'angular2-notifications';
28 import { saveAs } from 'file-saver';
29 import { appConstants } from '../../../../constants/app-constants';
32 export class UtilityService {
33 clName= "UtilityService";
34 public putAction = appConstants.messages.artifactUploadAction;
35 public getAction = appConstants.messages.artifactgetAction;
36 private retrievalSuccessMessage = appConstants.messages.retrievalSuccessMessage;
37 private retrievalFailureMessage = appConstants.messages.retrievalFailureMessage;
38 private saveSuccessMessage = appConstants.messages.saveSuccessMessage;
39 private saveFailureMessage = appConstants.messages.saveFailureMessage;
40 public connectionErrorMessage = appConstants.errors.connectionError;
42 private successMessage = appConstants.messages.artifactRetrivalsuccessMessage;
43 private failureMessage = appConstants.messages.artifactRetrivalfailureMessage;
45 constructor(private notificationService: NotificationsService) {
48 public setTracelvl( tlvl: number ) {
49 // console.log( this.clName+": setTracelvl: arg="+tlvl );
51 if( tracelvl == null || tracelvl == undefined ) tracelvl= 0;
52 localStorage["Tracelvl"]= tracelvl;
55 public getTracelvl() : number {
56 let tracelvl= localStorage["Tracelvl"];
57 // console.log( this.clName+": getTracelvl: locS: tracelvl="+tracelvl );
58 if( tracelvl == null || tracelvl == undefined ) {
59 tracelvl=0; localStorage["Tracelvl"]= tracelvl;
61 // console.log( this.clName+": getTracelvl: tracelvl="+tracelvl );
66 let x = (new Date().getUTCMilliseconds()) * Math.random();
67 return (x + '').substr(4, 12);
72 public appendSlashes(artifactData) {
73 return artifactData.replace(/"/g, '\\"');
76 public checkResult(result: any) {
78 if (result.output.status.code == appConstants.errorCode["401"]) {
79 this.notificationService.info(appConstants.notifications.titles.information, this.failureMessage);
82 else if (result.output.status.code == appConstants.errorCode["400"]) {
83 this.notificationService.success(appConstants.notifications.titles.success, this.successMessage);
89 public processApiSubscribe(result: any, action, artifactType) {
91 if (result.output.status.code == appConstants.errorCode["401"] && action == this.getAction) {
92 this.notificationService.info(appConstants.notifications.titles.information, this.retrievalFailureMessage);
94 else if (result.output.status.code == appConstants.errorCode["400"] && action == this.getAction) {
95 this.notificationService.success(appConstants.notifications.titles.success, this.retrievalSuccessMessage);
97 if (result.output.status.code == appConstants.errorCode["401"] && action == this.putAction) {
98 this.notificationService.warn(appConstants.notifications.titles.error, this.saveFailureMessage + artifactType);
100 else if (result.output.status.code == appConstants.errorCode["400"] && action == this.putAction) {
101 this.notificationService.success(appConstants.notifications.titles.success, this.saveSuccessMessage + artifactType);
106 public processApiError()
108 this.notificationService.error(appConstants.notifications.titles.error, this.connectionErrorMessage);
112 public checkNotNull(object:any)
115 if (object != undefined || object != null)
117 if(object.length > 0)
124 public createPayLoadForSave(artifactType,vnfType,action,fileName, versionNo, artifactContent)
126 let userId=localStorage['userId'];
127 let apiToken=localStorage['apiToken']
131 case "reference_data":
132 //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 + '"}';
133 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 + '"}';
135 case "template_data":
136 newPayload= JSON.stringify({
140 "artifact-name": fileName,
141 "artifact-type": "APPC-CONFIG",
142 "artifact-version": versionNo,
143 "artifact-contents": artifactContent.replace(/\(([^()]|(R))*\)=\(/g, '').replace(/\)}/g, '}')
147 newPayload=JSON.stringify({
151 "artifact-name": fileName,
152 "artifact-type": "APPC-CONFIG",
153 "artifact-version": versionNo,
154 "artifact-contents": artifactContent
158 newPayload='{"userID": "' + userId + '","vnf-type" : "' + vnfType + '","action" : "' + action + '","artifact-name" : "' + fileName + '","artifact-type" : "APPC-CONFIG","artifact-version" :"'+versionNo+'","artifact-contents" : ' + artifactContent + '}';
160 default : newPayload={};
167 "request-id": apiToken,
168 "action": "uploadArtifact",
169 "payload": newPayload
177 public createPayloadForRetrieve(isReference:boolean,action,vnfType,fileName)
181 payload=JSON.parse(sessionStorage.getItem('updateParams'));
182 payload['userID'] = localStorage['userId'];
183 payload = JSON.stringify(payload);
185 else payload = '{"userID": "' + localStorage['userId'] + '","action": "' + action + '", "vnf-type" : "' + vnfType + '", "artifact-type":"APPC-CONFIG", "artifact-name":"' + fileName + '"}';
189 'request-id': localStorage['apiToken'],
190 'action': 'getArtifact',
198 public downloadArtifactToPc(data,extension, fileName, delay)
200 var blob = new Blob([data], {
201 type: 'text/'+extension
204 saveAs(blob, fileName);