blob: cca3b038793ac083d9084469641197aa99012c75 [file] [log] [blame]
Andrea Campanella95c02bd2017-09-01 16:51:03 +02001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19import {NgMap} from 'ngmap';
20import {Subscription} from 'rxjs/Subscription';
21import * as _ from 'lodash';
22
23declare var google;
24
25let self;
26
Max Chudd0b2e12017-09-06 08:49:21 -070027export class VnaasMap {
Andrea Campanella95c02bd2017-09-01 16:51:03 +020028
29 static $inject = [
30 'NgMap',
31 'XosModelStore',
32 'AppConfig',
33 '$resource',
34 'XosSidePanel',
35 'XosModelDiscoverer',
36 'ModelRest',
37 'MapConfig',
38 ];
39
40 public unis = [];
41 public elines = [];
42 public cpilatlng = new Map();
43 public paths = [];
44 public bwps = [];
45 public map;
46 public panelOpen = false;
47 public createMode = false;
48 public canCreateEline = true;
49 public eline;
50 public current_uni;
51 public mapStyles = [{'featureType': 'administrative', 'elementType': 'labels.text.fill', 'stylers': [{'color': '#444444'}]}, {'featureType': 'landscape', 'elementType': 'all', 'stylers': [{'color': '#f2f2f2'}]}, {'featureType': 'poi', 'elementType': 'all', 'stylers': [{'visibility': 'off'}]}, {'featureType': 'road', 'elementType': 'all', 'stylers': [{'saturation': -100}, {'lightness': 45}]}, {'featureType': 'road.highway', 'elementType': 'all', 'stylers': [{'visibility': 'simplified'}]}, {'featureType': 'road.arterial', 'elementType': 'labels.icon', 'stylers': [{'visibility': 'off'}]}, {'featureType': 'transit', 'elementType': 'all', 'stylers': [{'visibility': 'off'}]}, {'featureType': 'water', 'elementType': 'all', 'stylers': [{'color': '#9ce1fc'}, {'visibility': 'on'}]}];
52
53 private uniSubscription: Subscription;
54 private elineSubscription: Subscription;
55 private bwpSubscription: Subscription;
56
57 constructor(
58 private NgMap: any,
59 private XosModelStore: any,
60 private AppConfig: any,
61 private $resource: any,
62 private XosSidePanel: any,
63 private XosModelDiscoverer: any,
64 private ModelRest: any,
65 private MapConfig: any,
66 ) {
67 self = this;
68 }
69
70 $onInit() {
71 this.NgMap.getMap().then(map => {
72 this.map = map;
73 this.uniSubscription = this.XosModelStore.query('UserNetworkInterface', '/vnaas/usernetworkinterfaces/').subscribe(
74 res => {
75 this.unis = res;
76 this.renderMap(map);
77 }
78 );
79 this.elineSubscription = this.XosModelStore.query('ELine', '/vnaas/elines/').subscribe(
80 res => {
81 this.elines = res;
82 this.createPaths();
83 this.renderMap(map);
84 }
85 );
86 this.bwpSubscription = this.XosModelStore.query('BandwidthProfile', '/vnaas/bandwidthprofiles/').subscribe(
87 res => {
88 this.bwps = res;
89 }
90 );
91 });
92 }
93
94 $onDestroy() {
95 if (this.uniSubscription) {
96 this.uniSubscription.unsubscribe();
97 }
98 }
99
100 public renderMap(map: NgMap) {
101
102 let bounds = new google.maps.LatLngBounds();
103
104 for (let i = 0; i < self.unis.length; i++) {
105 self.unis[i].eline_start = false;
106 let curr = JSON.parse(self.unis[i].latlng);
107 this.cpilatlng.set(self.unis[i].cpe_id, curr);
108 let latlng = new google.maps.LatLng(curr[0], curr[1]);
109 bounds.extend(latlng);
110 }
111 map.setCenter(bounds.getCenter());
112 map.fitBounds(bounds);
113
114 }
115
116 public createPaths() {
117 this.elines.forEach((eline: any) => {
118 let latlng_start = this.cpilatlng.get(eline.connect_point_1_id);
119 let latlng_end = this.cpilatlng.get(eline.connect_point_2_id);
120 eline.path = [latlng_start, latlng_end];
121 });
122
123 }
124
125 public colorLine(eline_status : any) {
126 let status = parseInt(eline_status, 10);
127 switch (status) {
128 case 0:
129 return '#f39c12';
130 case 1:
131 return '#2ecc71';
132 default:
133 return '#e74c3c';
134 }
135
136 }
137
138 public showUni(e: any, uni: any) {
139 self.current_uni = uni;
140 self.map.showInfoWindow('uni-info', this);
141 }
142
Max Chufd614ae2017-10-26 13:54:28 -0700143 public elinePanel(e: any, elineid: any, exists: boolean) {
Max Chudd0b2e12017-09-06 08:49:21 -0700144
Andrea Campanella95c02bd2017-09-01 16:51:03 +0200145 self.panelOpen = !self.panelOpen;
146 if (exists) {
Max Chufd614ae2017-10-26 13:54:28 -0700147 self.eline = _.find(self.elines, {id: elineid});
Andrea Campanella95c02bd2017-09-01 16:51:03 +0200148 }
Max Chudd0b2e12017-09-06 08:49:21 -0700149 self.XosSidePanel.toggleComponent('elineSide', {vng: self}, false);
Andrea Campanella95c02bd2017-09-01 16:51:03 +0200150 if (!self.panelOpen && self.createMode) {
151 self.createMode = false;
152 self.canCreateEline = true;
153 self.current_uni.eline_start = false;
154 }
155
156 }
157
158 public createConnection(uni: any) {
159 return () => {
160 self.canCreateEline = false;
161 self.createMode = true;
162 uni.eline_start = true;
163 self.current_uni = uni;
164 self.eline = {
165 name: uni.name,
166 uni1name: uni.name,
167 connect_point_1_id: uni.cpe_id,
168 };
169 self.elinePanel({}, self.eline, false);
170 };
171
172 }
173
174 public finishConnection(uni: any) {
175 self.eline.connect_point_2_id = uni.cpe_id;
176 if (self.eline.name === self.eline.uni1name) {
177 self.eline.name = self.eline.name + uni.name;
178 }
179 delete self.eline.uni1name;
180 const resource = this.ModelRest.getResource('/vnaas/elines/');
181 let res = new resource({});
182 for (let attr in self.eline) {
183 if (true) {
184 res[attr] = self.eline[attr];
185 }
186
187 }
188 self.eline = res;
189 }
190
191}
192
Max Chudd0b2e12017-09-06 08:49:21 -0700193export const vnaasMap: angular.IComponentOptions = {
194 template: require('./vnaasMap.component.html'),
Andrea Campanella95c02bd2017-09-01 16:51:03 +0200195 controllerAs: 'vm',
Max Chudd0b2e12017-09-06 08:49:21 -0700196 controller: VnaasMap,
Andrea Campanella95c02bd2017-09-01 16:51:03 +0200197};