computesites.service.ts 5.63 KB
Newer Older
Chris Hines's avatar
Chris Hines committed
1
import { Injectable } from '@angular/core';
Chris Hines's avatar
Chris Hines committed
2
3
4
5
6
7
import { Computesite, Strudelapp } from './computesite';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import { HttpClientModule, HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { catchError, map, tap } from 'rxjs/operators';
Chris Hines's avatar
Chris Hines committed
8
9
10
import { Identity } from './identity';
import { TesService } from './tes.service';
import { AuthorisationService} from './authorisation.service';
Chris Hines's avatar
Chris Hines committed
11
12


Chris Hines's avatar
Chris Hines committed
13

Chris Hines's avatar
Chris Hines committed
14
15
16
@Injectable({
  providedIn: 'root',
})
Chris Hines's avatar
Chris Hines committed
17
export class ComputesitesService {
Chris Hines's avatar
Chris Hines committed
18
  public computesites: BehaviorSubject<Computesite[]>;
Chris Hines's avatar
Chris Hines committed
19
20
21
22
  public identities: BehaviorSubject<Identity[]>;
  public appidentities: BehaviorSubject<Identity[]>;
  public ftidentities: BehaviorSubject<Identity[]>;
  private statusMsg: BehaviorSubject<any>;
Chris Hines's avatar
Chris Hines committed
23

Chris Hines's avatar
Chris Hines committed
24
  constructor(private http: HttpClient, private authorisationService: AuthorisationService) {
Chris Hines's avatar
Chris Hines committed
25
    this.computesites = new BehaviorSubject<Computesite[]>([]);
Chris Hines's avatar
Chris Hines committed
26
    this.identities = new BehaviorSubject<Identity[]>([]);
Chris Hines's avatar
Chris Hines committed
27
28
    this.appidentities = new BehaviorSubject<Identity[]>(null);
    this.ftidentities = new BehaviorSubject<Identity[]>(null);
Chris Hines's avatar
Chris Hines committed
29

Chris Hines's avatar
Chris Hines committed
30
    this.computesites.subscribe(computesites => this.getStrudelApps(computesites))
31
    this.computesites.subscribe((cs) => this.authorisationService.updateAgentContents());
Chris Hines's avatar
Chris Hines committed
32
    this.authorisationService.agentContents.subscribe(ac => this.updateIdentities(ac));
33
    this.getComputeSites();
Chris Hines's avatar
Chris Hines committed
34
35
36
37
  }

  public setStatusMsg(statusMsg: BehaviorSubject<any>) {
    this.statusMsg = statusMsg;
Chris Hines's avatar
Chris Hines committed
38
  }
Chris Hines's avatar
Chris Hines committed
39

Chris Hines's avatar
Chris Hines committed
40
41
42
43
  getStrudelApps(computesites: Computesite[]) {
    for (let s of computesites) {
      let headers = new HttpHeaders();
      let options = { headers: headers, withCredentials: false};
Chris Hines's avatar
Chris Hines committed
44
45
46
      if (s.appCatalogUri !== null) {
        this.http.get<Strudelapp[]>(s.appCatalogUri,options)
                         .pipe(catchError(this.handleError('getStrudelApps')))
47
                         .subscribe(resp => this.updateStrudelApps(s,resp));
Chris Hines's avatar
Chris Hines committed
48
      }
Chris Hines's avatar
Chris Hines committed
49
    }
Chris Hines's avatar
Chris Hines committed
50
51
52

  }

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  storeLocalStrudelApps(name: string, apps: any) {
      localStorage.setItem(name+'-apps',JSON.stringify(apps))
  }

  removeLocalStrudelApps() {
      var keys = [];
      for (var key in localStorage) {
        var idx = key.indexOf('-apps');
        if (idx !== -1) {
            keys.push(key)
        }
      }
      for (var key in keys) {
          localStorage.removeItem(key);
      }
  }

Chris Hines's avatar
Chris Hines committed
70

71
  updateStrudelApps(site: Computesite, apps) {
Chris Hines's avatar
Chris Hines committed
72
73
74
75
    var sapps: Strudelapp[];
    var localapps: Strudelapp[];
    sapps = <Strudelapp[]>apps;

76
    localapps = JSON.parse(localStorage.getItem(site.name+'-apps'))
Chris Hines's avatar
Chris Hines committed
77
78
79
80
81
    if (localapps !== null)  {
      for (let a of localapps) {
        sapps.push(a);
      }
    }
82
    site.appCatalog.next(sapps);
Chris Hines's avatar
Chris Hines committed
83
84
  }

Chris Hines's avatar
Chris Hines committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  private handleError<T> (operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {
      console.error(error);
      return of(result as T);
    };
  }


  getComputeSites() {
    let headers = new HttpHeaders();
    let options = { headers: headers, withCredentials: false};
    this.http.get<Strudelapp[]>('./assets/config/computesites.json',options)
                     .pipe(catchError(this.handleError('getComputeSites')))
                     .subscribe(resp => this.updateComputeSites(resp));
  }

101
102
103
104
105
106
107
108
109
110
111
112
113
  storeLocalComputeSites(computesites: any) {
      try {
        localStorage.setItem('localcomputesites',JSON.stringify(computesites));
      } catch {
      }
      this.getComputeSites();
  }

  removeLocalComputeSites() {
      localStorage.removeItem('localcomputesites');
      this.getComputeSites();
  }

Chris Hines's avatar
Chris Hines committed
114
  updateComputeSites(resp) {
Chris Hines's avatar
Chris Hines committed
115
    var localcomputesites: Computesite[] = [];
Chris Hines's avatar
Chris Hines committed
116
    var computesites: Computesite[] = []
117
118
119
120
121
    try {
        localcomputesites = JSON.parse(localStorage.getItem('localcomputesites'))
    } catch {
        localcomputesites = []
    }
Chris Hines's avatar
Chris Hines committed
122
123
124
125
126
127
    if (localcomputesites !== null ) {
      for (let cs of localcomputesites) {
        cs.appCatalog  = new BehaviorSubject<Strudelapp[]>([]);
        computesites.push(cs);
      }
    }
Chris Hines's avatar
Chris Hines committed
128
129
130
131
132
133
134
135
136
137
138
    for (let cs of resp) {
      let computesite = <Computesite>cs;
      computesite.appCatalog = new BehaviorSubject<Strudelapp[]>([])
      computesites.push(computesite);
    }
    this.computesites.next(computesites);
  }

  returnComputeSites(): Computesite[] {
    return this.computesites.value;
  }
Chris Hines's avatar
Chris Hines committed
139

Chris Hines's avatar
Chris Hines committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  private siteMatch(cert: any, cs: Computesite): string {
    var fp: string = (<Computesite>cs).cafingerprint;
    if ('Signing CA' in cert) {
      for (let ca of cert['Signing CA']) {
        if (ca == fp) {
          return cert['Principals'][0]
        }
      }
    }
    return null;
  }

  private updateIdentities(resp) {
    let certs = resp;
    var cs: Computesite;
    var identities: Identity[] = [];
    var appidentities: Identity[] = [];
    var ftidentities: Identity[] = [];
Chris Hines's avatar
Chris Hines committed
158
    // If the agent contents is set to null we are probably still updating it
Chris Hines's avatar
Chris Hines committed
159
160
    console.log('updating app identities');
    console.log(resp);
161
162
    console.log(this.authorisationService.agentContents.value)

Chris Hines's avatar
Chris Hines committed
163
164
165
    if (resp == null) {
      return
    }
Chris Hines's avatar
Chris Hines committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
    for (cs of this.computesites.value) {
      for (let i in certs) {
        let principal = this.siteMatch(certs[i],cs);
        if (principal != null) {
            let id = new Identity(principal,cs);
            identities.push(id);
            if (cs.appCatalogUri != null) {
              appidentities.push(id);
            }
            if (cs.dtn != null ) {
              ftidentities.push(id);
            }
          }
        }
      }
      this.identities.next(identities);
      this.ftidentities.next(ftidentities);
      this.appidentities.next(appidentities);

    }

Chris Hines's avatar
Chris Hines committed
187
}