summaryrefslogtreecommitdiffstats
path: root/mod/designtool/designtool-web/src/main/webapp/js/jquery/dcae-mod.js
blob: 48c5d8e3eaae570390938dc97cb31d3c5332aa76 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
============LICENSE_START=======================================================
Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
================================================================================
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
============LICENSE_END=========================================================
*/

console.log("loading dcae-mod");

    var dt_id;

    /**
    * @desc: on load of page, makes submit button disabled. Also makes an api call to get the host IP of the current instance
    */
    $(document).ready(function (){
        if(dt_id == null){   $('#operate-submit-btn').prop('disabled', true);   }

        getDistributionTargets();
    });

   /**
    * common function to reuse : invokes api to get new updates list environments.
    * @desc: Makes the select dropdown empty first. Then manually add Placeholder as first/default Option.
    *        And then dynamically add list of Environments as Options.
    */
    function getDistributionTargets(){
        var select = document.getElementById("environmentType");
         if(select && select.options && select.options.length > 0){
             select.options.length=0;
             var element= document.createElement("option");
             element.textContent= "Select Environment";
             element.selected=true;
             element.disabled=true;
             element.className="combo-option-text";
             select.appendChild(element);
          }else{  select=[];   }

           $.ajax({
                   type: 'GET',
                   url:  '/distributor/distribution-targets',
                   dataType: 'json',
                   contentType: 'application/json',
                   success: function(data){
                    if(data){
                           for(var i=0; i < data.distributionTargets.length; i++){
                             var opt= data.distributionTargets[i];
                             var element= document.createElement("option");
                              element.textContent= opt.name;
                              element.value= opt.id;
                              element.className="combo-option-text";
                              select.appendChild(element);
                           }
                    }
                  }
           })
    }

    /**
    * @desc: submit button functionality to distribute/POST process group to the environment.
    */
     var distributeGraph = function(){
        var selected_id = $('#operation-context-id').text();
        // process group id (nifi api) != flow id (nifi registry api)
        // so must first fetch the flow id from nifi api
        $.ajax({
          type: 'GET',
          url: '../nifi-api/process-groups/'+selected_id,
          contentType: 'application/json',
          success: function(data) {
            const flow_id = data["component"]["versionControlInformation"]["flowId"];
            const request = {"processGroupId": flow_id}

            $.ajax({
                    type: 'POST',
                    data: JSON.stringify(request),
                    url:  '/distributor/distribution-targets/'+dt_id+'/process-groups',
                    dataType: 'json',
                    contentType: 'application/json',
                    success: function(data){
                         alert("Success, Your flow have been distributed successfully");
                    },
                    error: function(err) {
                        alert("Issue with distribution:\n\n" + JSON.stringify(err, null, 2));
                    }
            });
          }
        })
     };


   /**
   * @desc: selection of distribution target environment to post the process group
   */
   var onEnvironmentSelect = function(){
     dt_id = $('#environmentType').val();
     console.log(dt_id);
     if(dt_id == null){   $('#operate-submit-btn').prop('disabled', true);   }
     else{  $('#operate-submit-btn').prop('disabled', false);      }
    };


    /**
    * @desc: event handler for Refresh icon in Operate panel :  invokes api to refresh the list of Envs
    */
    var refreshEnvironments= function(){    getDistributionTargets();    };


    /**
    * @desc: event handler for Close icon of Setting/ Distribution Env CRUD dialog :  invokes api to refresh the list of Envs
    */
    var onCloseSettings= function(){  getDistributionTargets();  };


function uecvalue(n) {
  return encodeURIComponent("" + $(n).val());
}

function esc(s) {
  return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

function onBoard() {
  $("#onboarding-in-progress").show();
  var url = "/acumos-adapter/onboard.js?acumos=" + uecvalue("#furl");
  if ($("#cat-menu").val() != "*") {
    url += "&catalogId=" + uecvalue("#cat-menu");
    if ($("#sol-menu").val() != "*") {
      url += "&solutionId=" + uecvalue("#sol-menu");
      if ($("#rev-menu").val() != "*") {
        url += "&revisionId=" + uecvalue("#rev-menu");
      }
    }
  }
  var xhr = new XMLHttpRequest();
  xhr.onerror = function() {
    $("#onboarding-in-progress").hide();
    alert("Onboarding failed");
  }
  xhr.onload = function() {
    if (this.status < 400) {
      $("#onboarding-in-progress").hide();
      alert("Onboarding successful");
    } else {
      alert("Onboarding error: " + this.statusText);
    }
  }
  xhr.open("POST", url);
  xhr.send();
}

function chooseSolution() {
  if ($("#sol-menu").val() == "*") {
    _updatevis();
  } else {
    lookupItem("#ac-revs", "#rev-menu", "/acumos-adapter/listRevisions.js?acumos=" + uecvalue("#furl") + "&solutionId=" + uecvalue("#sol-menu"));
  }
}

function chooseCatalog() {
  if ($("#cat-menu").val() == "*") {
    _updatevis();
  } else {
    lookupItem("#ac-sols", "#sol-menu", "/acumos-adapter/listSolutions.js?acumos=" + uecvalue("#furl") + "&catalogId=" + uecvalue("#cat-menu"));
  }
}

function lookupCatalogs() {
  $("#onboard").show();
  lookupItem("#c-acumos", "#cat-menu", "/acumos-adapter/listCatalogs.js?acumos=" + uecvalue("#furl"));
}

function lookupItem(dblock, smenu, url) {
  var xhr = new XMLHttpRequest();
  var xmenu = $(smenu);
  xmenu[0].options.length = 1;
  xmenu.val("*");
  xhr.onerror = function() {
    alert("Error querying remote Acumos system");
    $(dblock).hide();
  }
  xhr.onload = function() {
    var xresp = JSON.parse(this.response);
    var i;
    for (i = 0; i < xresp.length; i++) {
      var option = document.createElement("option");
      option.text = esc(xresp[i].name);
      option.value = xresp[i].id;
      xmenu[0].add(option);
    }
    if (xresp.length == 0) {
      $(dblock).hide();
    } else {
      $(dblock).show();
    }
    _updatevis();
  };
  xhr.open("GET", url);
  xhr.send();
}

function setModelType() {
  if ($("#model-type").val() == "mtAcumos") {
    $("#furl").val("");
    $("#mt-acumos").show();
  }
  _updatevis();
}

function _updatevis() {
  if ($("#model-type").val() != "mtAcumos") {
    $("#mt-acumos").hide();
    $("#furl").val("");
  }
  if ($("#furl").val() == "") {
    $("#c-acumos").hide();
    $("#onboard").hide();
    $("#cat-menu").val("*");
  }
  if ($("#cat-menu").val() == "*") {
    $("#ac-sols").hide();
    $("#sol-menu").val("");
  }
  if ($("#sol-menu").val() == "*") {
    $("#ac-revs").hide();
    $("#rev-menu").val("");
  }
}

function onBoardComponent() {
  _onBoardFile("#cspec", "/onboarding/components");
}

function onBoardDataFormat() {
  _onBoardFile("#dfspec", "/onboarding/dataformats");
}

function _onBoardFile(source, url) {
  reader = new FileReader();
  reader.onerror = function() {
    alert("Error reading file");
  }
  reader.onload = function(evt) {
    xhr = new XMLHttpRequest();
    xhr.onload = function() {
      if (this.status >= 400) {
        alert("File upload failed " + this.statusText);
      } else {
        alert("File upload complete");
      }
    }
    xhr.onerror = function() {
      alert("File upload failed");
    }
    xhr.open("POST", url);
    xhr.overrideMimeType("application/json");
    xhr.send(evt.target.result);
  };
  reader.readAsBinaryString($(source)[0].files[0]);
}