summaryrefslogtreecommitdiffstats
path: root/sdnr/wireless-transport/code-Carbon-SR1/ux/onapAai/onapAai-module/src/main/resources/onapAai/onapAai.services.js
blob: dfbbf473493bd1dee57ac166a3702813ec1dcf23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (c) 2017 highstreet technologies GmbH and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

define(['app/onapAai/onapAai.module', 'app/mwtnCommons/mwtnCommons.services'], function (onapAaiApp) {

  onapAaiApp.register.factory('$onapAai', function ($q, $http, Base64, $mwtnCommons, $mwtnDatabase, $mwtnLog, Device) {

    var service = {};

    var functionId = "mwtn";
    var docType = "device";
    var from = 0;
    var size = 9999;
    var sort = undefined;
    var deviceLookup = {};
    $mwtnDatabase.getAllData(functionId, docType, from, size, sort).then(
      function successCallback(response) {
        response.data.hits.hits.map(function(device){
          deviceLookup[device._id] = new Device(device._source);
        });
      }, function errorCallback(response) {
        deviceLookup = {};
      });

    service.checkModules = $mwtnCommons.checkModules;
    service.getMwtnWebSocketUrl = $mwtnCommons.getMwtnWebSocketUrl;
    service.gridOptions = $mwtnCommons.gridOptions;
    service.formatData = $mwtnCommons.formatData;
    service.formatTimeStamp = $mwtnCommons.formatTimeStamp;
    service.deleteDocType = $mwtnDatabase.deleteDocType;
    

    var transactionId = 1;
    var getHeaders = function () {
      return {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'X-TransactionId': transactionId++
      }
    };

    // create or modify a pnf in aai
    service.createPnf = function (pnfId, doc) {
      var base = window.location.origin;
      var getIp = function (extension) {
        return extension.filter(function (item) {
          return item['value-name'] === 'neIpAddress';
        }).map(function (item) {
          return item.value;
        })[0];
      }

      var device = deviceLookup[pnfId];
      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"});      
      var data = {
        "pnf-name": pnfId,
        "pnf-id": doc.connect.host + ':' + doc.connect.port,
        "equip-type": device.getType(),
        "equip-model": device.getModel(),
        "equip-vendor": device.getVendor(),
        "ipaddress-v4-oam": getIp(doc['core-model:network-element'].extension) | doc.connect.host,
        "in-maint": false
      };
      console.info('pnf', data);
      var request = {
        method: 'PUT',
        url: base + '/aai/network/pnfs/pnf/' + pnfId, // to es config
        // withCredentials: true,
        headers: getHeaders(),
        data: data
      };
      var deferred = $q.defer();
      $http(request).then(function successCallback(response) {
        deferred.resolve(response);
      }, function errorCallback(response) {
        deferred.reject(response);
      });

      return deferred.promise;
    };

    service.deletePnf = function (pnfId) {
      // 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'
      var base = window.location.origin;
      var request = {
        method: 'DELETE',
        url: base + '/aai/network/pnfs/pnf/' + pnfId, // to es config
        // withCredentials: true,
        headers: getHeaders()
      };
      var deferred = $q.defer();
      $http(request).then(function successCallback(response) {
        deferred.resolve(response);
      }, function errorCallback(response) {
        deferred.reject(response);
      });

      return deferred.promise;
    };

    service.getAaiPnfs = function () {
      // 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'

      var base = window.location.origin;
      var request = {
        method: 'GET',
        url: base + '/aai/network/pnfs', // to es config
        // withCredentials: true,
        headers: getHeaders(),
      };

      var deferred = $q.defer();
      $http(request).then(function successCallback(response) {
        deferred.resolve(response);
      }, function errorCallback(response) {
        deferred.reject(response);
      });

      return deferred.promise;
    };

    return service;
  });

  // Class Device
  onapAaiApp.register.factory('Device', function () {
    var Device = function (data) {
      if (!data) {
        data = {id:new Date(), type: 'unknown', name:'unknonw', model: 'unkonwn', vendor:'unknonw', version:'unkonwn'};
      }
      this.data = data;
      this.getData = function () {
        return this.data;
      };
      this.getId = function () {
        return this.getData().id;
      };
      this.getType = function () {
        return this.getData().type;
      };
      this.getName = function () {
        return this.getData().name;
      };
      this.getModel = function () {
        return this.getData().model;
      };
      this.getVendor = function () {
        return this.getData().vendor;
      };
      this.getVersion = function () {
        return this.getData().version;
      };
    };
    return Device;
  });

});