blob: 6f406af3fc2324ab1a63cb46226398843e4f4f0a [file] [log] [blame]
Matteo Scandolob8cdf552018-02-12 17:56:26 -08001/*
2 * Copyright 2017-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {TypeState} from 'typestate';
18import {IXosGraphStore} from './graph.store';
19
20export interface IXosGraphStateMachine {
21 states: GraphStates;
22 go(state: GraphStates): void;
23 getCurrentState(): number;
24}
25
26export enum GraphStates {
27 Services,
28 ServiceInstances,
29 Instances,
30 Networks
31}
32
33export class XosGraphStateMachine {
34 static $inject = [
35 '$log',
36 '$rootScope',
37 'XosGraphStore'
38 ];
39
40 private graphStateMachine: TypeState.FiniteStateMachine<GraphStates>;
41
42 constructor(
43 private $log: ng.ILogService,
44 private $scope: ng.IRootScopeService,
45 private XosGraphStore: IXosGraphStore
46 ) {
47 this.$log.info(`[XosGraphStateMachine] Creating Graph StateMachine`);
48
49 this.graphStateMachine = new TypeState.FiniteStateMachine<GraphStates>(GraphStates.Services);
50
51 // I want to be able to move between any state in the state machine
52 this.graphStateMachine.fromAny(GraphStates).toAny(GraphStates);
53
54 this.graphStateMachine.onTransition = (from: GraphStates, to: GraphStates) => {
55 this.$log.info(`[XosGraphStateMachine] From ${GraphStates[from]} to ${GraphStates[to]}`);
56
57 const toName = GraphStates[to];
58
59 switch (toName) {
60 case 'Services':
61 if (from > to) {
62 this.removeNetworks();
63 this.removeInstances();
64 this.removeServiceInstances();
65 }
66 break;
67 case 'ServiceInstances':
68 if (from > to) {
69 this.removeNetworks();
70 this.removeInstances();
71 }
72 else {
73 this.addServiceInstances();
74 }
75 break;
76 case 'Instances':
77 if (from > to) {
78 this.removeNetworks();
79 }
80 else {
81 this.addServiceInstances();
82 this.addInstances();
83 }
84 break;
85 case 'Networks':
86 if (from > to) {
87 // this will never happen
88 }
89 else {
90 this.addServiceInstances();
91 this.addInstances();
92 this.addNetworks();
93 }
94 break;
95 }
96 };
97 }
98
99 public go(state: GraphStates) {
100 this.graphStateMachine.go(state);
101
102 this.$scope.$broadcast('xos.sg.stateChange', this.getCurrentState());
103 }
104
105 public getCurrentState(): number {
106 return this.graphStateMachine.currentState;
107 }
108
109 private addServiceInstances() {
110 // add service instances to the graph
111 this.$log.debug(`[XosGraphStateMachine] Add ServiceInstances`);
112 this.XosGraphStore.addServiceInstances();
113 }
114
115 private addInstances () {
116 // add instances to the graph
117 this.$log.debug(`[XosGraphStateMachine] Add Instances`);
118 this.XosGraphStore.addInstances();
119 }
120
121 private addNetworks () {
122 // add networks to the graph
123 this.$log.debug(`[XosGraphStateMachine] Add Networks`);
124 this.XosGraphStore.addNetworks();
125 }
126
127 private removeServiceInstances() {
128 // remove service instances to the graph
129 this.$log.debug(`[XosGraphStateMachine] Remove ServiceInstances`);
130 this.XosGraphStore.removeServiceInstances();
131 }
132
133 private removeInstances () {
134 // remove instances to the graph
135 this.$log.debug(`[XosGraphStateMachine] Remove Instances`);
136 this.XosGraphStore.removeInstances();
137 }
138
139 private removeNetworks () {
140 // remove networks to the graph
141 this.$log.debug(`[XosGraphStateMachine] Remove Networks`);
142 this.XosGraphStore.removeNetworks();
143 }
144}