dfbbf473493bd1dee57ac166a3702813ec1dcf23
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / ux / onapAai / onapAai-module / src / main / resources / onapAai / onapAai.services.js
1 /*
2  * Copyright (c) 2017 highstreet technologies GmbH and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 define(['app/onapAai/onapAai.module', 'app/mwtnCommons/mwtnCommons.services'], function (onapAaiApp) {
10
11   onapAaiApp.register.factory('$onapAai', function ($q, $http, Base64, $mwtnCommons, $mwtnDatabase, $mwtnLog, Device) {
12
13     var service = {};
14
15     var functionId = "mwtn";
16     var docType = "device";
17     var from = 0;
18     var size = 9999;
19     var sort = undefined;
20     var deviceLookup = {};
21     $mwtnDatabase.getAllData(functionId, docType, from, size, sort).then(
22       function successCallback(response) {
23         response.data.hits.hits.map(function(device){
24           deviceLookup[device._id] = new Device(device._source);
25         });
26       }, function errorCallback(response) {
27         deviceLookup = {};
28       });
29
30     service.checkModules = $mwtnCommons.checkModules;
31     service.getMwtnWebSocketUrl = $mwtnCommons.getMwtnWebSocketUrl;
32     service.gridOptions = $mwtnCommons.gridOptions;
33     service.formatData = $mwtnCommons.formatData;
34     service.formatTimeStamp = $mwtnCommons.formatTimeStamp;
35     service.deleteDocType = $mwtnDatabase.deleteDocType;
36     
37
38     var transactionId = 1;
39     var getHeaders = function () {
40       return {
41         'Accept': 'application/json',
42         'Content-Type': 'application/json',
43         'X-TransactionId': transactionId++
44       }
45     };
46
47     // create or modify a pnf in aai
48     service.createPnf = function (pnfId, doc) {
49       var base = window.location.origin;
50       var getIp = function (extension) {
51         return extension.filter(function (item) {
52           return item['value-name'] === 'neIpAddress';
53         }).map(function (item) {
54           return item.value;
55         })[0];
56       }
57
58       var device = deviceLookup[pnfId];
59       if (!device) device = new Device({"id":pnfId,"name":pnfId,"mediator-ipv4":"127.0.0.1","netconf-port":"830","oam-ipv4":"127.0.0.1","site":"unknown","controller":"this","controller-site":"unknown","vendor":"unknown","type":"unknown","model":"unknown","version":"0.0"});      
60       var data = {
61         "pnf-name": pnfId,
62         "pnf-id": doc.connect.host + ':' + doc.connect.port,
63         "equip-type": device.getType(),
64         "equip-model": device.getModel(),
65         "equip-vendor": device.getVendor(),
66         "ipaddress-v4-oam": getIp(doc['core-model:network-element'].extension) | doc.connect.host,
67         "in-maint": false
68       };
69       console.info('pnf', data);
70       var request = {
71         method: 'PUT',
72         url: base + '/aai/network/pnfs/pnf/' + pnfId, // to es config
73         // withCredentials: true,
74         headers: getHeaders(),
75         data: data
76       };
77       var deferred = $q.defer();
78       $http(request).then(function successCallback(response) {
79         deferred.resolve(response);
80       }, function errorCallback(response) {
81         deferred.reject(response);
82       });
83
84       return deferred.promise;
85     };
86
87     service.deletePnf = function (pnfId) {
88       // curl -X DELETE http://localhost:8282/aai/network/pnfs/pnf/Ericsson-A1 --insecure -v -u AAI:AAI -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'X-FromAppId: SDNR' -H 'X-TransactionId: 9999'
89       var base = window.location.origin;
90       var request = {
91         method: 'DELETE',
92         url: base + '/aai/network/pnfs/pnf/' + pnfId, // to es config
93         // withCredentials: true,
94         headers: getHeaders()
95       };
96       var deferred = $q.defer();
97       $http(request).then(function successCallback(response) {
98         deferred.resolve(response);
99       }, function errorCallback(response) {
100         deferred.reject(response);
101       });
102
103       return deferred.promise;
104     };
105
106     service.getAaiPnfs = function () {
107       // curl https://10.31.1.55:8443/network/pnfs -k -v -u abc:def -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'X-FromAppId: SDNR' -H 'X-TransactionId: 9999'
108
109       var base = window.location.origin;
110       var request = {
111         method: 'GET',
112         url: base + '/aai/network/pnfs', // to es config
113         // withCredentials: true,
114         headers: getHeaders(),
115       };
116
117       var deferred = $q.defer();
118       $http(request).then(function successCallback(response) {
119         deferred.resolve(response);
120       }, function errorCallback(response) {
121         deferred.reject(response);
122       });
123
124       return deferred.promise;
125     };
126
127     return service;
128   });
129
130   // Class Device
131   onapAaiApp.register.factory('Device', function () {
132     var Device = function (data) {
133       if (!data) {
134         data = {id:new Date(), type: 'unknown', name:'unknonw', model: 'unkonwn', vendor:'unknonw', version:'unkonwn'};
135       }
136       this.data = data;
137       this.getData = function () {
138         return this.data;
139       };
140       this.getId = function () {
141         return this.getData().id;
142       };
143       this.getType = function () {
144         return this.getData().type;
145       };
146       this.getName = function () {
147         return this.getData().name;
148       };
149       this.getModel = function () {
150         return this.getData().model;
151       };
152       this.getVendor = function () {
153         return this.getData().vendor;
154       };
155       this.getVersion = function () {
156         return this.getData().version;
157       };
158     };
159     return Device;
160   });
161
162 });