update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / plugins / redundancy / src / dashboard / redundancyStore.js
1 /*
2 * STANDARD_RIFT_IO_COPYRIGHT
3 */
4 import RedundancyActions from './redundancyActions.js';
5 import RedundancySource from './redundancySource.js';
6 var Utils = require('utils/utils.js');
7 import ROLES from 'utils/roleConstants.js';
8 import _ from 'lodash';
9 export default class RedundancyStore {
10 constructor() {
11 this.actions = RedundancyActions(this.alt);
12 this.bindActions(this.actions);
13 this.registerAsync(RedundancySource);
14 this.sites = [];
15 this.failoverDecision = "INDIRECT";
16 this.siteData = {
17 'target-endpoint': [],
18 'rw-instances':[{
19 endpoint:[{},{}]
20 }]
21
22 };
23 this.configData = {
24 'polling-config' : {},
25 'revertive-preference': {},
26 'geographic-failover-decision': 'INDIRECT',
27 'user-credentials': {
28 'username': '',
29 'password': ''
30 }
31 }
32 this.siteIdPattern = /^((((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))(%[\p{N}\p{L}]+)?)|(^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$))|(((([a-zA-Z0-9_]([a-zA-Z0-9\-_]){0,61})?[a-zA-Z0-9]\.)*([a-zA-Z0-9_]([a-zA-Z0-9\-_]){0,61})?[a-zA-Z0-9]\.?)|\.)$/;
33 this.siteIdValidation = [ "^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$"
34 , "((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))(%[\p{N}\p{L}]+)?"
35 , '((([a-zA-Z0-9_]([a-zA-Z0-9\-_]){0,61})?[a-zA-Z0-9]\.)*([a-zA-Z0-9_]([a-zA-Z0-9\-_]){0,61})?[a-zA-Z0-9]\.?)|\.'
36 ];
37 this.failOverDecisionOptions = [{ label: "INDIRECT", value: "INDIRECT" }, { label: "DIRECT", value: "DIRECT" }];
38 this.status = {};
39 this.activeIndex = null;
40 this.isReadOnly = true;
41 this.siteOpen = false;
42 this.hideColumns = false;
43 this.isEdit = false;
44 this.isEditSite = true;
45 this.exportPublicMethods({
46 closeSocket: this.closeSocket
47 })
48 }
49 /**
50 * [handleFieldUpdate description]
51 * @param {Object} data {
52 * [store_property] : [value]
53 * }
54 * @return {[type]} [description]
55 */
56 handleUpdateInput(data) {
57 this.setState(data);
58 }
59 handleUpdateConfigInput(data) {
60 let configData = this.configData;
61 configData = _.merge(configData, data);
62 this.setState(configData);
63 }
64 handleFailOverDecisionChange(failoverDecision) {
65 let configData = this.configData;
66 configData['geographic-failover-decision'] = failoverDecision
67 delete configData['dns-ip-fqdn'];
68 this.setState({configData});
69 }
70
71 viewSite() {
72 let self = this;
73 let data = arguments[0];
74 let SiteData = data[0];
75 let siteIndex = data[1];
76 let isReadOnly = data[2];
77
78 let state = _.merge({
79 activeIndex: siteIndex,
80 siteOpen: true,
81 isEdit: true,
82 isReadOnly: isReadOnly,
83 isEditSite: isReadOnly,
84 siteData: SiteData
85 });
86 this.setState(state)
87 }
88 editSite(isReadOnly) {
89 this.viewSite([this.sites[this.activeIndex], this.activeIndex, isReadOnly]);
90
91 }
92 handleCloseSitePanel() {
93 this.setState({
94 siteOpen: false,
95 isEdit: false,
96 isReadOnly: true
97 })
98 }
99 handleHideColumns(e) {
100 if(this.siteOpen && e.currentTarget.classList.contains('hideColumns')) {
101 this.setState({
102 hideColumns: true
103 })
104 } else {
105 this.setState({
106 hideColumns: false
107 })
108 }
109 }
110 resetSite() {
111 let name = '';
112 let description = '';
113 return {
114 siteData: {
115 'target-endpoint': [{},{}],
116 'rw-instances':[{
117 isNew: true,
118 endpoint:[{},{}]
119 }]
120 }
121 }
122 }
123 handleAddSite() {
124 this.setState(_.merge( this.resetSite() ,
125 {
126 isEdit: false,
127 siteOpen: true,
128 activeIndex: null,
129 isEditSite: true,
130 isReadOnly: false,
131 }
132 ))
133 }
134 handleAddTargetEndpoint() {
135 let newSiteData = this.siteData;
136 if(!newSiteData['target-endpoint']) {
137 newSiteData['target-endpoint'] = [];
138 }
139 newSiteData['target-endpoint'].push({
140 name: '',
141 port: ''
142 })
143 this.setState({siteData: newSiteData})
144 }
145 handleRemoveTargetEndpoint(data) {
146 let newSiteData = this.siteData;
147 newSiteData['target-endpoint'].splice(
148 data[0].index
149 , 1
150 );
151 this.setState({siteData: newSiteData})
152 }
153 handleAddInstance() {
154 let newSiteData = this.siteData;
155 if(!newSiteData['rw-instances']) {
156 newSiteData['rw-instances'] = [];
157 }
158 newSiteData['rw-instances'].push({
159 isNew: true,
160 endpoint:[{},{}]
161 })
162 this.setState({siteData: newSiteData})
163 }
164 handleRemoveInstance(data) {
165 let newSiteData = this.siteData;
166 newSiteData['rw-instances'].splice(
167 data[0].index
168 , 1
169 );
170 this.setState({siteData: newSiteData})
171 }
172 getSitesSuccess(sites) {
173 this.alt.actions.global.hideScreenLoader.defer();
174 this.setState({sites: sites});
175 }
176 getRedundancySuccess(data) {
177 console.log(data)
178 this.alt.actions.global.hideScreenLoader.defer();
179 let sites = data.site && data.site.map(function(site, i) {
180 return site;
181 });
182 this.setState({
183 sites,
184 configData : {
185 'polling-config' : data['polling-config'],
186 'revertive-preference': data['revertive-preference'],
187 'geographic-failover-decision': data['geographic-failover-decision'],
188 'dns-ip-fqdn': data['dns-ip-fqdn'],
189 'user-credentials': data['user-credentials'] || {
190 'username': '',
191 'password': ''
192 }
193 }
194 });
195 }
196 updateConfigSuccess() {
197 this.alt.actions.global.hideScreenLoader.defer();
198 let self = this;
199 this.setState({
200 isEdit: true,
201 isReadOnly: true
202 });
203 }
204 updateSiteSuccess() {
205 this.alt.actions.global.hideScreenLoader.defer();
206 let self = this;
207 let sites = this.sites || [];
208 sites[this.activeIndex] = this.siteData
209 this.setState({
210 sites,
211 isEdit: true,
212 isReadOnly: true
213 });
214 }
215 deleteSiteSuccess() {
216 this.alt.actions.global.hideScreenLoader.defer();
217 let sites = this.sites;
218 sites.splice(this.activeIndex, 1);
219 this.setState({sites, siteOpen: false,isEdit: true,
220 isReadOnly: false,})
221 }
222 createSiteSuccess() {
223 let self = this;
224 this.alt.actions.global.hideScreenLoader.defer();
225 let sites = this.sites || [];
226 sites.push(self.siteData);
227 let newState = {
228 sites,
229 isEdit: true,
230 isReadOnly: true,
231 activeIndex: sites.length - 1
232 };
233 _.merge(newState)
234 this.setState(newState);
235 }
236 openRedundancyStateSocketSuccess(connection) {
237 let self = this;
238 let ws = window.multiplexer.channel(connection);
239 if (!connection) return;
240 this.setState({
241 socket: ws.ws,
242 channelId: connection
243 });
244 ws.onmessage = (socket) => {
245 try {
246 var data = JSON.parse(socket.data);
247 var newState = {status: data};
248 Utils.checkAuthentication(data.statusCode, function() {
249 self.closeSocket();
250 });
251
252 self.setState(newState);
253 } catch(error) {
254 console.log('Hit at exception in openRedundancyStateSocketSuccess', error)
255 }
256
257 }
258 ws.onclose = () => {
259 self.closeSocket();
260 }
261 }
262 closeSocket = () => {
263 if (this.socket) {
264 window.multiplexer.channel(this.channelId).close();
265 }
266 this.setState({
267 socket: null
268 })
269 }
270 }