Add DCAE MOD design tool project
[dcaegen2/platform.git] / mod / designtool / designtool-web / src / main / webapp / js / jquery / dcae-mod.js
1 /*
2 ============LICENSE_START=======================================================
3 Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
4 ================================================================================
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9      http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ============LICENSE_END=========================================================
17 */
18
19 console.log("loading dcae-mod");
20
21     var dt_id;
22     var hostname;
23
24     /**
25     * @desc: on load of page, makes submit button disabled. Also makes an api call to get the host IP of the current instance
26     */
27     $(document).ready(function (){
28         if(dt_id == null){   $('#operate-submit-btn').prop('disabled', true);   }
29
30         //get hostname
31         $.ajax({
32                type: 'GET',
33                url:   '../nifi-api/flow/config',
34                dataType: 'json',
35                contentType: 'application/json',
36                success: function(data){
37                     hostname= data.flowConfiguration.dcaeDistributorApiHostname;
38
39                    //function call: invokes api to refresh the list of Envs
40                     if(hostname){    getDistributionTargets();   }
41                   }
42           });
43     });
44
45    /**
46     * common function to reuse : invokes api to get new updates list environments.
47     * @desc: Makes the select dropdown empty first. Then manually add Placeholder as first/default Option.
48     *        And then dynamically add list of Environments as Options.
49     */
50     function getDistributionTargets(){
51         var select = document.getElementById("environmentType");
52          if(select && select.options && select.options.length > 0){
53              select.options.length=0;
54              var element= document.createElement("option");
55              element.textContent= "Select Environment";
56              element.selected=true;
57              element.disabled=true;
58              element.className="combo-option-text";
59              select.appendChild(element);
60           }else{  select=[];   }
61
62            $.ajax({
63                    type: 'GET',
64                    url:  hostname+'/distribution-targets',
65                    dataType: 'json',
66                    contentType: 'application/json',
67                    success: function(data){
68                     if(data){
69                            for(var i=0; i < data.distributionTargets.length; i++){
70                              var opt= data.distributionTargets[i];
71                              var element= document.createElement("option");
72                               element.textContent= opt.name;
73                               element.value= opt.id;
74                               element.className="combo-option-text";
75                               select.appendChild(element);
76                            }
77                     }
78                   }
79            })
80     }
81
82     /**
83     * @desc: submit button functionality to distribute/POST process group to the environment.
84     */
85      var distributeGraph = function(){
86         var selected_id = $('#operation-context-id').text();
87         // process group id (nifi api) != flow id (nifi registry api)
88         // so must first fetch the flow id from nifi api
89         $.ajax({
90           type: 'GET',
91           url: '../nifi-api/process-groups/'+selected_id,
92           contentType: 'application/json',
93           success: function(data) {
94             const flow_id = data["component"]["versionControlInformation"]["flowId"];
95             const request = {"processGroupId": flow_id}
96
97             $.ajax({
98                     type: 'POST',
99                     data: JSON.stringify(request),
100                     url:  hostname+'/distribution-targets/'+dt_id+'/process-groups',
101                     dataType: 'json',
102                     contentType: 'application/json',
103                     success: function(data){
104                          alert("Success, Your flow have been distributed successfully");
105                     },
106                     error: function(err) {
107                         alert("Issue with distribution:\n\n" + JSON.stringify(err, null, 2));
108                     }
109             });
110           }
111         })
112      };
113
114
115    /**
116    * @desc: selection of distribution target environment to post the process group
117    */
118    var onEnvironmentSelect = function(){
119      dt_id = $('#environmentType').val();
120      console.log(dt_id);
121      if(dt_id == null){   $('#operate-submit-btn').prop('disabled', true);   }
122      else{  $('#operate-submit-btn').prop('disabled', false);      }
123     };
124
125
126     /**
127     * @desc: event handler for Refresh icon in Operate panel :  invokes api to refresh the list of Envs
128     */
129     var refreshEnvironments= function(){    getDistributionTargets();    };
130
131
132     /**
133     * @desc: event handler for Close icon of Setting/ Distribution Env CRUD dialog :  invokes api to refresh the list of Envs
134     */
135     var onCloseSettings= function(){  getDistributionTargets();  };