[VID-3] Setting docker image tag
[vid.git] / vid / src / main / webapp / app / vid / scripts / controller / creationDialogController.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 "use strict";
22
23 var creationDialogController = function( COMPONENT, FIELD, $scope, $http, $timeout, $log,
24                 CreationService, UtilityService) {
25
26         $scope.isDialogVisible = false;
27         $scope.summaryControl = {};
28         $scope.userProvidedControl = {};
29
30         var callbackFunction = undefined;
31         var componentId = undefined;
32
33         $scope.$on("createComponent", function(event, request) {
34
35                 $scope.isSpinnerVisible = true;
36                 $scope.isErrorVisible = false;
37                 $scope.isDataVisible = false;
38                 $scope.isConfirmEnabled = false;
39                 $scope.isDialogVisible = true;
40                 $scope.popup.isVisible = true;
41
42                 callbackFunction = request.callbackFunction;
43                 componentId = request.componentId;
44                 CreationService.initializeComponent(request.componentId);
45
46                 CreationService.setHttpErrorHandler(function(response) {
47                         showError("System failure", UtilityService
48                                         .getHttpErrorMessage(response));
49                 });
50
51                 $scope.componentName = CreationService.getComponentDisplayName();
52
53                 CreationService.getParameters(handleGetParametersResponse);
54
55         });
56
57         var handleGetParametersResponse = function(parameters) {
58                 $scope.summaryControl.setList(parameters.summaryList);
59                 $scope.userProvidedControl.setList(parameters.userProvidedList);
60
61                 $scope.isSpinnerVisible = false;
62                 $scope.isDataVisible = true;
63                 $scope.isConfirmEnabled = true;
64         };
65         
66         var validateInstanceName = function(iname) {
67                 var patt1 = /^([a-z])+([0-9a-z\-_]*)$/i;
68                 
69                 if ( iname == null ){
70                         return false;
71                 }
72                 if ( !iname.match(patt1) ) {
73                         return false;
74                 }
75                 return true;
76         }
77
78         $scope.userParameterChanged = function(id) {
79                 CreationService.updateUserParameterList(id, $scope.userProvidedControl);
80         }
81
82         $scope.confirm = function() {
83
84                 var requiredFields = $scope.userProvidedControl.getRequiredFields();
85                 if (requiredFields !== "") {
86                         showError("Missing data", requiredFields);
87                         return;
88                 }
89                 // validate the instance names for volumeGroup, vfModule, network
90                 
91                 if  ( componentId != COMPONENT.SERVICE ) {
92                         var paramList = $scope.userProvidedControl.getList();
93                         var instanceName = "";
94                         
95                         if ( paramList != null ) {
96                                 for (var i = 0; i < paramList.length; i++) {
97                                         if (paramList[i].id === FIELD.ID.INSTANCE_NAME) {
98                                                 instanceName = paramList[i].value;
99                                                 break;
100                                         }
101                                 }
102                         }
103                         var isValid = validateInstanceName (instanceName);
104                         if ( isValid ) {
105                                 $scope.isErrorVisible = false;
106                         } else {
107                                 showError("Invalid instance name: " + instanceName, 
108                                                 "The instance name must contain only alphanumeric or \"_-.\" characters, and must start with an alphabetic character");
109                                 return;
110                         }
111                 }
112                 var requestDetails = CreationService
113                                 .getMsoRequestDetails($scope.userProvidedControl.getList());
114
115                 $scope.isDialogVisible = false;
116
117                 $scope.$broadcast("createInstance", {
118                         url : CreationService.getMsoUrl(),
119                         requestDetails : requestDetails,
120                         callbackFunction : function(response) {
121                                 if (response.isSuccessful) {
122                                         $scope.popup.isVisible = false;
123                                         runCallback(response);
124                                 } else {
125                                         $scope.isDialogVisible = true;
126                                 }
127                         }
128                 });
129         }
130
131         $scope.cancel = function() {
132                 $scope.isDialogVisible = false;
133                 $scope.popup.isVisible = false;
134                 runCallback(false);
135         }
136
137         var runCallback = function(response) {
138                 if (angular.isFunction(callbackFunction)) {
139                         callbackFunction({
140                                 isSuccessful : response.isSuccessful,
141                                 control : $scope.userProvidedControl.getList(),
142                                 instanceId : response.instanceId
143                         });
144                 }
145         }
146
147         var showError = function(summary, details) {
148                 var message = summary;
149                 if (UtilityService.hasContents(details)) {
150                         message += " (" + details + ")";
151                 }
152                 $scope.isSpinnerVisible = false;
153                 $scope.isErrorVisible = true;
154                 $scope.error = message;
155         }
156         
157 }
158
159 app
160                 .controller("creationDialogController", [ "COMPONENT", "FIELD", "$scope", "$http",
161                                 "$timeout", "$log", "CreationService", "UtilityService",
162                                 creationDialogController ]);