Andrea Campanella | 95c02bd | 2017-09-01 16:51:03 +0200 | [diff] [blame] | 1 | |
| 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 | |
| 19 | import {NgMap} from 'ngmap'; |
| 20 | import {Subscription} from 'rxjs/Subscription'; |
| 21 | import * as _ from 'lodash'; |
| 22 | |
| 23 | declare var google; |
| 24 | |
| 25 | let self; |
| 26 | |
Max Chu | dd0b2e1 | 2017-09-06 08:49:21 -0700 | [diff] [blame] | 27 | export class VnaasMap { |
Andrea Campanella | 95c02bd | 2017-09-01 16:51:03 +0200 | [diff] [blame] | 28 | |
| 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 Chu | dd0b2e1 | 2017-09-06 08:49:21 -0700 | [diff] [blame] | 143 | // fix this function! |
Andrea Campanella | 95c02bd | 2017-09-01 16:51:03 +0200 | [diff] [blame] | 144 | public elinePanel(e: any, eline: any, exists: boolean) { |
Max Chu | dd0b2e1 | 2017-09-06 08:49:21 -0700 | [diff] [blame] | 145 | |
Andrea Campanella | 95c02bd | 2017-09-01 16:51:03 +0200 | [diff] [blame] | 146 | self.panelOpen = !self.panelOpen; |
| 147 | if (exists) { |
| 148 | self.eline = _.find(self.elines, {id: eline.id}); |
| 149 | } |
Max Chu | dd0b2e1 | 2017-09-06 08:49:21 -0700 | [diff] [blame] | 150 | self.XosSidePanel.toggleComponent('elineSide', {vng: self}, false); |
Andrea Campanella | 95c02bd | 2017-09-01 16:51:03 +0200 | [diff] [blame] | 151 | if (!self.panelOpen && self.createMode) { |
| 152 | self.createMode = false; |
| 153 | self.canCreateEline = true; |
| 154 | self.current_uni.eline_start = false; |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | |
| 159 | public createConnection(uni: any) { |
| 160 | return () => { |
| 161 | self.canCreateEline = false; |
| 162 | self.createMode = true; |
| 163 | uni.eline_start = true; |
| 164 | self.current_uni = uni; |
| 165 | self.eline = { |
| 166 | name: uni.name, |
| 167 | uni1name: uni.name, |
| 168 | connect_point_1_id: uni.cpe_id, |
| 169 | }; |
| 170 | self.elinePanel({}, self.eline, false); |
| 171 | }; |
| 172 | |
| 173 | } |
| 174 | |
| 175 | public finishConnection(uni: any) { |
| 176 | self.eline.connect_point_2_id = uni.cpe_id; |
| 177 | if (self.eline.name === self.eline.uni1name) { |
| 178 | self.eline.name = self.eline.name + uni.name; |
| 179 | } |
| 180 | delete self.eline.uni1name; |
| 181 | const resource = this.ModelRest.getResource('/vnaas/elines/'); |
| 182 | let res = new resource({}); |
| 183 | for (let attr in self.eline) { |
| 184 | if (true) { |
| 185 | res[attr] = self.eline[attr]; |
| 186 | } |
| 187 | |
| 188 | } |
| 189 | self.eline = res; |
| 190 | } |
| 191 | |
| 192 | } |
| 193 | |
Max Chu | dd0b2e1 | 2017-09-06 08:49:21 -0700 | [diff] [blame] | 194 | export const vnaasMap: angular.IComponentOptions = { |
| 195 | template: require('./vnaasMap.component.html'), |
Andrea Campanella | 95c02bd | 2017-09-01 16:51:03 +0200 | [diff] [blame] | 196 | controllerAs: 'vm', |
Max Chu | dd0b2e1 | 2017-09-06 08:49:21 -0700 | [diff] [blame] | 197 | controller: VnaasMap, |
Andrea Campanella | 95c02bd | 2017-09-01 16:51:03 +0200 | [diff] [blame] | 198 | }; |