Fix test identities
[groupbasedpolicy.git] / groupbasedpolicy-ui / module / src / main / resources / gbp / common / gbp.controller.js
1 define([
2     'app/gbp/common/gbp.service',
3     'app/gbp/resolved-policy/resolved-policy-list.service',
4     'app/gbp/endpoints/sxp-mapping-list.service'
5 ], function () {
6     'use strict';
7
8     angular.module('app.gbp').controller('RootGbpCtrl', RootGbpCtrl);
9
10     RootGbpCtrl.$inject = ['$mdDialog', '$rootScope', '$scope', '$state',
11         'EndpointsListService', 'NextTopologyService', 'ResolvedPolicyListService', 'RootGbpService',
12         'TenantListService', 'SxpMappingListService'];
13
14     function RootGbpCtrl($mdDialog, $rootScope, $scope, $state,
15         EndpointsListService, NextTopologyService, ResolvedPolicyListService, RootGbpService,
16         TenantListService, SxpMappingListService) {
17
18         /* properties */
19         $scope.apiType = 'operational';
20         $scope.activeObject = null;
21         $scope.endpoints = EndpointsListService.createList();
22         $scope.endpointSgtList = SxpMappingListService.createList();
23         $scope.innerObj = {};
24         $scope.rootTenant = null;
25         $scope.rootTenants = TenantListService.createList();
26         $scope.resolvedPolicy = {};
27         $scope.sidePanelObject = null;
28         $scope.sidePanelPage = false;
29         $scope.sidePanelPageEndpoint = false;
30         $scope.stateUrl = null;
31         $scope.topologyData = { nodes: [], links: [] };
32         $scope.viewPath = 'src/app/gbp/';
33
34         var resolvedPolicies = ResolvedPolicyListService.createList();
35         getResolvedPolicies();
36
37         /* methods */
38         $scope.broadcastFromRoot = broadcastFromRoot;
39         $scope.closeSidePanel = closeSidePanel;
40         $scope.openSfcDialog = openSfcDialog;
41         $scope.openSidePanel = openSidePanel;
42         $scope.setRootTenant = setRootTenant;
43         $scope.fillTopologyData = fillTopologyData;
44         $scope.highlightNode = highlightNode;
45         $scope.highlightLink = highlightLink;
46         $scope.fadeAll = fadeAll;
47         $scope.rootOpenEndpointDialog = rootOpenEndpointDialog;
48         $scope.rootDeleteEndpointDialog = rootDeleteEndpointDialog;
49
50         RootGbpService.setMainClass();
51         init();
52
53         /* implementations */
54
55         /**
56          *
57          * @param eventName
58          * @param val
59          */
60         function broadcastFromRoot(eventName, val) {
61             $scope.$broadcast(eventName, val);
62         }
63
64         /**
65          *
66          */
67         function closeSidePanel() {
68             if($scope.sidePanelPage) {
69                 $scope.sidePanelPage = false;
70                 $scope.sidePanelObject = null;
71                 $scope.fadeAll();
72             }
73         }
74
75         /**
76          *
77          * @param source
78          * @param target
79          * @param contract
80          * @param tenant
81          * @returns {{id: string, source: *, target: *, tenant: *}}
82          */
83         function createLink( linkId, type) {
84             var linkIdParts = linkId.split('++');
85             return {
86                 'id': linkId,
87                 'source': linkIdParts[1],
88                 'target': linkIdParts[2],
89                 'tenant': $scope.rootTenant,
90                 'type': type,
91             };
92         }
93
94         /**
95          *
96          * @param nodeName
97          * @param tenantId
98          * @returns {Object}
99          */
100         function createNode(nodeName) {
101             return {
102                 'id': nodeName,
103                 'tenantId': $scope.rootTenant,
104                 'node-id': nodeName,
105                 'label': nodeName,
106             };
107         }
108
109         /**
110          *
111          */
112         function fadeAll() {
113             $rootScope.nxTopology && NextTopologyService.fadeInAllLayers($rootScope.nxTopology);
114         }
115
116         /**
117          * reads resolvedPolicies list, prepares nodes and links for topology and fills them
118          */
119         function fillTopologyData() {
120             var tempTopoData = {nodes: [], links: []};
121             $scope.resolvedPolicy = resolvedPolicies.aggregateResolvedPolicies();
122
123             tempTopoData.nodes = Object.keys($scope.resolvedPolicy.epgs).map(function (key) {
124                 return createNode(key);
125             });
126
127             tempTopoData.links = Object.keys($scope.resolvedPolicy.contracts).map(function (key) {
128                 return createLink(key, $scope.resolvedPolicy.contracts[key].type);
129             });
130
131             $scope.topologyData = tempTopoData;
132             $scope.topologyLoaded = true;
133         }
134
135         function getResolvedPolicies() {
136             if($scope.rootTenant) {
137                 resolvedPolicies.get($scope.rootTenant, fillTopologyData);
138             }
139         }
140
141         /**
142          *
143          * @param node
144          */
145         function highlightNode(node) {
146             NextTopologyService.highlightNode($rootScope.nxTopology, node);
147         }
148
149         /**
150          *
151          * @param link
152          */
153         function highlightLink(link) {
154             NextTopologyService.highlightLink($rootScope.nxTopology, link);
155         }
156
157         /**
158          *
159          */
160         function init() {
161             $scope.rootTenants.clearData();
162             $scope.rootTenants.get('config');
163             $state.go('main.gbp.index.resolvedPolicy');
164             $scope.endpointSgtList.get();
165
166
167         }
168
169         /**
170          *
171          * @param chainName
172          */
173         function openSfcDialog(chainName) {
174             $mdDialog.show({
175                 clickOutsideToClose: true,
176                 controller: 'SfcTopologyController',
177                 preserveScope: true,
178                 templateUrl: $scope.viewPath + 'sfc/dialog-sfc-topology.tpl.html',
179                 parent: angular.element(document.body),
180                 scope: $scope,
181                 locals: {
182                     chainName: chainName
183                 }
184             });
185         }
186
187
188         /**
189          * Sets '$scope.sidePanelPage' to true. This variable is watched in index.tpl.html template
190          * and opens/closes side panel
191          */
192         function openSidePanel(page, object, type, element) {
193             $scope.sidePanelPage = page;
194             $scope.sidePanelObject = object;
195
196             switch(type) {
197                 case 'subject':
198                     $scope.innerObj.subject = element;
199                     break;
200                 case 'clause':
201                     $scope.innerObj.clause = element;
202                     break;
203                 case 'rule':
204                     $scope.innerObj.rule = element;
205                     break;
206                 default:
207             }
208         }
209
210         /**
211          *
212          */
213         function setRootTenant() {
214             $scope.broadcastFromRoot('ROOT_TENANT_CHANGED');
215
216             if ($scope.stateUrl.startsWith('/resolved-policy')) {
217                 getResolvedPolicies();
218                 if($scope.sidePanelObject) {
219                     if($scope.sidePanelObject['contract-id'])
220                         openSidePanel('resolved-policy/sidepanel/views/contract-list-sidepanel');
221                     else
222                         openSidePanel('resolved-policy/sidepanel/views/epg-list-sidepanel');
223                 }
224             }
225         }
226
227         /**
228          * fills $scope.stateUrl with loaded url
229          * It's called on $viewContentLoaded event
230          */
231         function setStateUrl() {
232             $scope.stateUrl = $state.current.url;
233             closeSidePanel();
234
235             if ($scope.stateUrl.startsWith('/resolved-policy')) {
236                 getResolvedPolicies();
237             }
238         }
239
240         function rootOpenEndpointDialog(operation, endpointData) {
241             $scope.disableKeyFieldsEditing = operation === 'edit';
242             $mdDialog.show({
243                 clickOutsideToClose: true,
244                 controller: 'AddEndpointController',
245                 preserveScope: true,
246                 templateUrl: $scope.viewPath + 'endpoints/dialog-add-endpoint.tpl.html',
247                 parent: angular.element(document.body),
248                 scope: $scope,
249                 locals: {
250                     endpoint: endpointData
251                 }
252             });
253         }
254
255         function rootDeleteEndpointDialog(endpointData) {
256             var confirm = $mdDialog.confirm()
257                 .title('Delete endpoint')
258                 .textContent('Do you want to delete endpoint?')
259                 .ok('Delete')
260                 .cancel('Cancel');
261
262             $mdDialog.show(confirm).then(function () {
263                 endpointData.deleteEndpoint(function () {
264                     $scope.$broadcast('endpointChanged');
265                 });
266             }, function () {
267
268             });
269         }
270
271         /* event listeners */
272         /**
273          * Event fired after content loaded, setStateUrl function is called to fill stateUrl method
274          */
275         $scope.$on('$viewContentLoaded', setStateUrl);
276     }
277 });