[UI] Ignore malformed results
[validation.git] / ui / src / main / webapp / app / BluvalUI / ModifyBlueprintInstance / ModifyBlueprintInstanceController.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('ModifyBlueprintInstance');
18 app
19         .controller(
20                 'ModifyBlueprintInstanceController',
21                 function($scope, restAPISvc) {
22
23                     initialize();
24
25                     function initialize() {
26                         $scope.loadingBlueprintInstances = true;
27                         $scope.loadingLayers = true;
28                         $scope.blueprintInstanceInfos = [];
29                         $scope.declerativeInsts = [];
30                         $scope.selectedBlueprintInstance = '';
31                         $scope.layers = [];
32                         $scope.layerInfos = [];
33                         $scope.selectedLayer = '';
34                         $scope.configuredLayers = [];
35                         $scope.oldLayers = [];
36                         $scope.newLayers = [];
37                         restAPISvc
38                                 .getRestAPI(
39                                         "/api/v1/blueprintinstance/",
40                                         function(data) {
41                                             if (data) {
42                                                 $scope.blueprintInstanceInfos = data;
43                                                 angular
44                                                         .forEach(
45                                                                 data,
46                                                                 function(
47                                                                         blueprintInstance) {
48                                                                     var temp = "id: "
49                                                                             + blueprintInstance.blueprintInstanceId
50                                                                             + " name: "
51                                                                             + blueprintInstance["blueprint"]["blueprintName"]
52                                                                             + " version: "
53                                                                             + blueprintInstance["version"];
54                                                                     $scope.declerativeInsts
55                                                                             .push(temp);
56                                                                     restAPISvc
57                                                                             .getRestAPI(
58                                                                                     "/api/v1/layer/",
59                                                                                     function(
60                                                                                             data2) {
61                                                                                         if (data2) {
62                                                                                             $scope.layerInfos = data2;
63                                                                                             angular
64                                                                                                     .forEach(
65                                                                                                             data2,
66                                                                                                             function(
67                                                                                                                     layer) {
68                                                                                                                 if ($scope.layers
69                                                                                                                         .indexOf(layer.layer) === -1) {
70                                                                                                                     $scope.layers
71                                                                                                                             .push(layer.layer);
72                                                                                                                 }
73                                                                                                             });
74                                                                                         } else {
75                                                                                             confirm("No layers found");
76                                                                                         }
77                                                                                     });
78                                                                 });
79                                             } else {
80                                                 confirm("No blueprint instances found");
81                                             }
82                                             $scope.loadingBlueprintInstances = false;
83                                             $scope.loadingLayers = false;
84                                         });
85                     }
86
87                     $scope.selectedBlueprintInstanceChange = function() {
88                         $scope.oldLayers = [];
89                         $scope.newLayers = [];
90                         var finalBlueprintInstanceInfo = '';
91                         var id = $scope.selectedBlueprintInstance
92                                 .substring($scope.selectedBlueprintInstance
93                                         .indexOf("id:") + 4,
94                                         $scope.selectedBlueprintInstance
95                                                 .indexOf("name") - 1);
96                         angular
97                                 .forEach(
98                                         $scope.blueprintInstanceInfos,
99                                         function(blueprintInstanceInfo) {
100                                             if (blueprintInstanceInfo.blueprintInstanceId
101                                                     .toString().trim() === id
102                                                     .toString().trim()) {
103                                                 finalBlueprintInstanceInfo = blueprintInstanceInfo;
104                                             }
105                                         });
106                         if (!finalBlueprintInstanceInfo) {
107                             confirm("Error in blueprint instance data");
108                             return;
109                         }
110                         angular.forEach(
111                                 finalBlueprintInstanceInfo.blueprintLayers,
112                                 function(layer) {
113                                     $scope.oldLayers.push(layer.layer);
114                                 });
115                     }
116
117                     $scope.addConfiguredLayer = function(configuredLayer) {
118                         if ($scope.configuredLayers.indexOf(configuredLayer
119                                 .trim()) === -1) {
120                             $scope.configuredLayers.push(configuredLayer);
121                         }
122                     }
123
124                     $scope.deleteConfiguredLayer = function(index) {
125                         $scope.configuredLayers.splice(index, 1);
126                     }
127
128                     $scope.modify = function() {
129                         if (!$scope.selectedBlueprintInstance
130                                 || !$scope.configuredLayers
131                                 || $scope.configuredLayers.length === 0) {
132                             confirm("You must specify all the data");
133                             return;
134                         }
135                         var finalBlueprintInstanceInfo = '';
136                         var id = $scope.selectedBlueprintInstance
137                                 .substring($scope.selectedBlueprintInstance
138                                         .indexOf("id:") + 4,
139                                         $scope.selectedBlueprintInstance
140                                                 .indexOf("name") - 1);
141                         angular
142                                 .forEach(
143                                         $scope.blueprintInstanceInfos,
144                                         function(blueprintInstanceInfo) {
145                                             if (blueprintInstanceInfo.blueprintInstanceId
146                                                     .toString().trim() === id
147                                                     .toString().trim()) {
148                                                 finalBlueprintInstanceInfo = blueprintInstanceInfo;
149                                             }
150                                         });
151                         if (!finalBlueprintInstanceInfo) {
152                             confirm("Error in blueprint instance data");
153                             return;
154                         }
155
156                         var blueprintLayers = [];
157                         angular.forEach($scope.layerInfos, function(layerInfo) {
158                             if ($scope.configuredLayers
159                                     .indexOf(layerInfo.layer) !== -1) {
160                                 blueprintLayers.push(layerInfo);
161                             }
162                         });
163                         if (!blueprintLayers || blueprintLayers.length === 0) {
164                             confirm("Error in blueprint layers data");
165                             return;
166                         }
167                         finalBlueprintInstanceInfo.blueprintLayers = blueprintLayers;
168                         restAPISvc
169                                 .postRestAPI(
170                                         "/api/v1/blueprintinstance/",
171                                         finalBlueprintInstanceInfo,
172                                         function(data) {
173                                             if (data) {
174                                                 var confirmText = "The blueprint instance has been modified successfully.";
175                                                 confirm(confirmText);
176                                             } else {
177                                                 confirm("Error when modifying the blueprint instance");
178                                             }
179                                             initialize();
180                                         });
181                     }
182                 });