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