[UI] Support data registration
[validation.git] / ui / src / main / webapp / app / BluvalUI / RegisterBlueprintInstance / RegisterBlueprintInstanceController.js
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 var app = angular.module('RegisterBlueprintInstance');
18 app
19         .controller(
20                 'RegisterBlueprintInstanceController',
21                 function($scope, restAPISvc) {
22
23                     initialize();
24
25                     function initialize() {
26                         $scope.loadingBlueprints = true;
27                         $scope.loadingLayers = true;
28                         $scope.blueprints = [];
29                         $scope.blueprintInfos = [];
30                         $scope.layers = [];
31                         $scope.layerInfos = [];
32                         $scope.selectedLayer = '';
33                         $scope.configuredLayers = [];
34                         restAPISvc
35                                 .getRestAPI(
36                                         "/api/v1/blueprint/",
37                                         function(data) {
38                                             if (data) {
39                                                 $scope.blueprintInfos = data;
40                                                 angular
41                                                         .forEach(
42                                                                 data,
43                                                                 function(
44                                                                         blueprint) {
45                                                                     $scope.blueprints
46                                                                             .push(blueprint.blueprintName);
47                                                                     restAPISvc
48                                                                             .getRestAPI(
49                                                                                     "/api/v1/layer/",
50                                                                                     function(
51                                                                                             data2) {
52                                                                                         if (data2) {
53                                                                                             $scope.layerInfos = data2;
54                                                                                             angular
55                                                                                                     .forEach(
56                                                                                                             data2,
57                                                                                                             function(
58                                                                                                                     layer) {
59                                                                                                                 if ($scope.layers
60                                                                                                                         .indexOf(layer.layer) === -1) {
61                                                                                                                     $scope.layers
62                                                                                                                             .push(layer.layer);
63                                                                                                                 }
64                                                                                                             });
65                                                                                         } else {
66                                                                                             confirm("No layers found");
67                                                                                         }
68                                                                                     });
69                                                                 });
70                                             } else {
71                                                 confirm("No blueprints found");
72                                             }
73                                             $scope.loadingBlueprints = false;
74                                             $scope.loadingLayers = false;
75                                         });
76                     }
77
78                     $scope.addConfiguredLayer = function(configuredLayer) {
79                         if ($scope.configuredLayers.indexOf(configuredLayer
80                                 .trim()) === -1) {
81                             $scope.configuredLayers.push(configuredLayer);
82                         }
83                     }
84
85                     $scope.deleteConfiguredLayer = function(index) {
86                         $scope.configuredLayers.splice(index, 1);
87                     }
88
89                     $scope.register = function() {
90                         if (!$scope.selectedBlueprint || !$scope.definedVersion
91                                 || !$scope.configuredLayers
92                                 || $scope.configuredLayers.length === 0) {
93                             confirm("You must specify all the fields");
94                             return;
95                         }
96                         var blueprint = '';
97                         angular
98                                 .forEach(
99                                         $scope.blueprintInfos,
100                                         function(blueprintInfo) {
101                                             if (blueprintInfo.blueprintName
102                                                     .toString().trim() === $scope.selectedBlueprint
103                                                     .toString().trim()) {
104                                                 blueprint = blueprintInfo;
105                                             }
106                                         });
107                         if (!blueprint) {
108                             confirm("Error in blueprint data");
109                             return;
110                         }
111                         var blueprintLayers = [];
112                         angular.forEach($scope.layerInfos, function(layerInfo) {
113                             if ($scope.configuredLayers
114                                     .indexOf(layerInfo.layer) !== -1) {
115                                 blueprintLayers.push(layerInfo);
116                             }
117                         });
118                         if (!blueprintLayers || blueprintLayers.length === 0) {
119                             confirm("Error in blueprint layers data");
120                             return;
121                         }
122                         var blueprintInstance = {
123                             "blueprint" : blueprint,
124                             "version" : $scope.definedVersion,
125                             "blueprintLayers" : blueprintLayers
126                         };
127                         restAPISvc
128                                 .postRestAPI(
129                                         "/api/v1/blueprintinstance/",
130                                         blueprintInstance,
131                                         function(data) {
132                                             if (data) {
133                                                 var confirmText = "The blueprint instance has been registered successfully. Blueprint instance id:"
134                                                         + data.blueprintInstanceId;
135                                                 confirm(confirmText);
136                                             } else {
137                                                 confirm("Error when registering the blueprint instance");
138                                             }
139                                             initialize();
140                                         });
141                     }
142                 });