summaryrefslogtreecommitdiffstats
path: root/src/main/resources/META-INF/resources/designer/scripts/CldsModelService.js
blob: f6092b8f2dc4b85cd670d22f90cf5cbfef1e79b3 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2017-2018 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============================================
 * ===================================================================
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */

app.service('cldsModelService', ['alertService', '$http', '$q', function(alertService, $http, $q) {

  function checkIfElementType(name) {
    //This will open the methods located in the app.js
    if (undefined == name) {
      return
    }

    mapping = {
      'tca': TCAWindow,
      'policy': PolicyWindow,
      'vescollector': VesCollectorWindow,
      'holmes': HolmesWindow,
    };

    key = name.split('_')[0].toLowerCase()
    if (key in mapping) {
      mapping[key]();
    }
  }

  this.toggleDeploy = function(uiAction, modelName, controlNamePrefixIn,
    bpmnTextIn, propTextIn, svgXmlIn, templateName, typeID, controlNameUuid, modelEventService, deploymentId) {

    var def = $q.defer();
    var sets = [];
    var action = uiAction.toLowerCase();
    var deployUrl = "/restservices/clds/v1/clds/" + action + "/" + modelName;
    var requestData = {
      name: modelName,
      controlNamePrefix: controlNamePrefixIn,
      bpmnText: bpmnTextIn,
      propText: propTextIn,
      imageText: svgXmlIn,
      templateName: templateName,
      typeId: typeID,
      controlNameUuid: controlNameUuid,
      event: modelEventService,
      deploymentId: deploymentId
    };

    $http.put(deployUrl, requestData)
      .success(function(data) {
        deploymentId = data.deploymentId;
        def.resolve(data);
        alertService.alertMessage("Action Successful:" + uiAction, 1)
      })
      .error(function(data) {
        alertService.alertMessage("Action Failure:" + uiAction, 2)
        def.reject("Save Model not successful");
      });

    return def.promise;
  };

  this.getASDCServices = function() {


    var def = $q.defer();
    var sets = [];

    var svcUrl = "/restservices/clds/v1/clds/sdc/services/";

    $http.get(svcUrl)
      .success(function(data) {
        def.resolve(data);


      })
      .error(function(data) {
        def.reject("sdc Services not found");

      });

    return def.promise;
  };


  this.getASDCService = function(uuid) {


    var def = $q.defer();
    var sets = [];

    var svcUrl = "/restservices/clds/v1/clds/sdc/services/" + uuid;

    $http.get(svcUrl)
      .success(function(data) {
        def.resolve(data);

      })
      .error(function(data) {
        def.reject("SDC service not found");
      });

    return def.promise;
  };
  this.getModel = function(modelName) {


    var def = $q.defer();
    var sets = [];
    var svcUrl = "/restservices/clds/v1/clds/model/" + modelName;

    $http.get(svcUrl)
      .success(function(data) {
        def.resolve(data);

      })
      .error(function(data) {
        def.reject("Open Model not successful");
      });

    return def.promise;
  };
  this.getSavedModel = function() {

    var def = $q.defer();
    var sets = [];

    var svcUrl = "/restservices/clds/v1/clds/model-names";

    $http.get(svcUrl)
      .success(function(data) {
        def.resolve(data);

      })
      .error(function(data) {
        def.reject("Open Model not successful");
      });

    return def.promise;
  };
  this.setModel = function(modelName, controlNamePrefixIn, bpmnTextIn, propTextIn) {


    var def = $q.defer();
    var sets = [];
    var svcUrl = "/restservices/clds/v1/clds/model/" + modelName;
    var svcRequest = {
      name: modelName,
      controlNamePrefix: controlNamePrefixIn,
      bpmnText: bpmnTextIn,
      propText: propTextIn
    };


    $http.put(svcUrl, svcRequest)
      .success(function(data) {
        def.resolve(data);

      })
      .error(function(data) {
        def.reject("Save Model not successful");
      });

    return def.promise;
  };
  this.processAction = function(uiAction, modelName, controlNamePrefixIn, bpmnTextIn, propTextIn, svgXmlIn, templateName, typeID, deploymentId) {


    var def = $q.defer();
    var sets = [];

    //console.log(svgXmlIn);
    var svcUrl = "/restservices/clds/v1/clds/";
    var svcAction = uiAction.toLowerCase();
    if (svcAction == "save" || svcAction == "refresh") {
      svcUrl = svcUrl + "model/" + modelName;
    } else if (svcAction == "test") {
      svcUrl = svcUrl + "action/submit/" + modelName + "?test=true";
    } else {
      svcUrl = svcUrl + "action/" + svcAction + "/" + modelName;
    }

    var svcRequest = {
      name: modelName,
      controlNamePrefix: controlNamePrefixIn,
      bpmnText: bpmnTextIn,
      propText: propTextIn,
      imageText: svgXmlIn,
      templateName: templateName,
      typeId: typeID,
      deploymentId: deploymentId
    };

    $http.put(svcUrl, svcRequest)
      .success(function(data) {
        def.resolve(data);
        alertService.alertMessage("Action Successful:" + uiAction, 1)

      })
      .error(function(data) {
        alertService.alertMessage("Action Failure:" + uiAction, 2);
        //def         alertService.alertMessage("Action Successful:"+uiAction,1);
        def.reject(svcAction + " not successful");
      });

    return def.promise;
  };
  this.manageAction = function(modelName, typeId, typeName) {
    var def = $q.defer();
    var sets = [];

    var config = {
      url: "/restservices/clds/v1/clds/getDispatcherInfo",
      method: "GET",
      params: {
        model: modelName,
        typeId: typeId,
        typeName: typeName
      }
    };
    $http(config)
      .success(function(data) {
        def.resolve(data);

      })
      .error(function(data) {
        def.reject("manage action not successful");
      });

    return def.promise;

  };
  this.checkPermittedActionCd = function(permittedActionCd, menuText, actionCd) {
    if (permittedActionCd.indexOf(actionCd) > -1) {
      document.getElementById(menuText).classList.remove('ThisLink');
    } else {
      document.getElementById(menuText).classList.add('ThisLink');
    }
  };
  this.processActionResponse = function(modelName, pars) {

    // populate control name (prefix and uuid here)
    var controlNamePrefix = pars.controlNamePrefix;
    var controlNameUuid = pars.controlNameUuid;
    deploymentId = pars.deploymentId;
    var modelEventService = pars.modelEventService;
    typeID = pars.typeId;

    var headerText = "Closed Loop Modeler - " + modelName;
    if (controlNameUuid != null) {
      var actionCd = pars.event.actionCd;
      var actionStateCd = pars.event.actionStateCd;
      //headerText = headerText + " [" + controlNamePrefix + controlNameUuid + "] [" + actionCd + ":" + actionStateCd + "]";
      headerText = headerText + " [" + controlNamePrefix + controlNameUuid + "]";
    }

    document.getElementById("modeler_name").textContent = headerText;
    document.getElementById("templa_name").textContent = ("Template Used - " + selected_template);
    setStatus(pars)
    addSVG(pars);
    this.enableDisableMenuOptions(pars);


  };

  this.processRefresh = function(pars) {
    typeID = pars.typeId;
    deploymentId = pars.deploymentId;
    setStatus(pars);
    this.enableDisableMenuOptions(pars);
  }

  function setStatus(pars) {

    var status = pars.status;
    // apply color to status
    var statusColor = 'white';
    if (status.trim() === "DESIGN") {
      statusColor = 'gray'
    } else if (status.trim() === "DISTRIBUTED") {
      statusColor = 'blue'
    } else if (status.trim() === "ACTIVE") {
      statusColor = 'green'
    } else if (status.trim() === "STOPPED") {
      statusColor = 'red'
    } else if (status.trim() === "DELETING") {
      statusColor = 'pink'
    } else if (status.trim() === "ERROR") {
      statusColor = 'orange'
    } else if (status.trim() === "UNKNOWN") {
      statusColor = 'blue'
    } else {
      statusColor = null;
    }


    var statusMsg = '<span style="background-color:' + statusColor + ';-moz-border-radius: 50px;  -webkit-border-radius: 50px;  border-radius: 50px;">&nbsp;&nbsp;&nbsp;' + status + '&nbsp;&nbsp;&nbsp;</span>';
    // display status
    if ($("#status_clds").length >= 1)
      $("#status_clds").remove();
    $("#activity_modeler").append('<span id="status_clds" style="position: absolute;  left: 61%;top: 51px; font-size:20px;">Status: ' + statusMsg + '</span>');


  }

  function addSVG(pars) {

    var svg = pars.imageText.substring(pars.imageText.indexOf("<svg"))
    if ($("#svgContainer").length > 0)
      $("#svgContainer").remove();
    $("#js-canvas").append("<span id=\"svgContainer\">" + svg + "</span>");
    /* added code for height width viewBox */
    $("#svgContainer svg").removeAttr("height");
    $("#svgContainer svg").removeAttr('viewBox');
    $("#svgContainer svg").removeAttr('width');

    $("#svgContainer svg").attr('width', '100%');
    $("#svgContainer svg").attr('height', '100%');

    $("#svgContainer").click(function(event) {
      //console.log($(event.target).parent().html())
      //console.log($($(event.target).parent()).attr("data-element-id"))
      var name = $($(event.target).parent()).attr("data-element-id")
      lastElementSelected = $($(event.target).parent()).attr("data-element-id")
      checkIfElementType(name)

    });
  }
  this.enableDisableMenuOptions = function(pars) {

    var permittedActionCd = pars.permittedActionCd;

    //dropdown options - always true
    document.getElementById('Open CL').classList.remove('ThisLink');

    if (readMOnly) {
      //enable model options
      document.getElementById('Properties CL').classList.remove('ThisLink');
      document.getElementById('Close Model').classList.remove('ThisLink');

      //disable models options
      document.getElementById('Create CL').classList.add('ThisLink');
      document.getElementById('Save CL').classList.add('ThisLink');
      document.getElementById('Revert Model Changes').classList.add('ThisLink');
    } else {
      // enable menu options
      document.getElementById('Create CL').classList.remove('ThisLink');
      document.getElementById('Save CL').classList.remove('ThisLink');
      document.getElementById('Properties CL').classList.remove('ThisLink');
      document.getElementById('Revert Model Changes').classList.remove('ThisLink');
      document.getElementById('Close Model').classList.remove('ThisLink');


      document.getElementById('Validation Test').classList.remove('ThisLink');
      document.getElementById('Refresh Status').classList.remove('ThisLink');
      document.getElementById('Refresh ASDC').classList.remove('ThisLink');
    }

    // enable/disable menu options based on permittedActionCd list
    this.checkPermittedActionCd(permittedActionCd, 'Validation Test', 'TEST');
    this.checkPermittedActionCd(permittedActionCd, 'Submit', 'SUBMIT');
    this.checkPermittedActionCd(permittedActionCd, 'Resubmit', 'RESUBMIT');
    this.checkPermittedActionCd(permittedActionCd, 'Update', 'UPDATE');
    this.checkPermittedActionCd(permittedActionCd, 'Stop', 'STOP');
    this.checkPermittedActionCd(permittedActionCd, 'Restart', 'RESTART');
    this.checkPermittedActionCd(permittedActionCd, 'Delete', 'DELETE');
    this.checkPermittedActionCd(permittedActionCd, 'Deploy', 'DEPLOY');
    this.checkPermittedActionCd(permittedActionCd, 'UnDeploy', 'UNDEPLOY');

  }


  this.getASDCServices().then(function(pars) {

    var services = pars.service;
    asdc_Services = services
  });

}]);