summaryrefslogtreecommitdiffstats
path: root/sdc-workflow-designer-ui/src/app/services/tosca.service.ts
blob: dd62a717b3ce95efe451828c93e04d1aade60107 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
 * Copyright (c) 2017 ZTE Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     ZTE - initial API and implementation and/or initial documentation
 */
import { Injectable } from '@angular/core';
import { Observable } from "rxjs/Rx";

import { NodeTemplate } from '../model/topology/node-template';
import { SettingService } from './setting.service';
import { HttpService } from '../util/http.service';
import { BroadcastService } from './broadcast.service';

@Injectable()
export class ToscaService {
  private nodeTemplate: NodeTemplate[] = [];
  private topologyProperties: {
    name: string;
    value: string;
  }[] = [];
  private namespace;
  private serviceTemplateId;
  private repositoryURL;
  constructor(private settingService: SettingService, private httpService: HttpService,
    private broadcastService: BroadcastService) {
    this.settingService.getSetting().subscribe(setting => {
      if (true === setting.supportToscaNode) {
        // todo: need to load tosca interface and tosca properties
        this.initTOSCAInfo();
      } else {
        this.broadcastService.broadcast(this.broadcastService.updateModelToscaConfig, null);
      }
    });
  }

  public getNodeTemplate(): NodeTemplate[] {
    return this.nodeTemplate;
  }

  public getTopologyProperties() {
    return this.topologyProperties;
  }

  public initTOSCAInfo() {
    this.getNodeTemplates().subscribe(nodes => {
      if (0 === nodes.length) {
        return;
      }

      const subscribes = nodes.map(node => this.loadTopologyProperties(node));
      Observable.forkJoin(subscribes).map(nodesProperties => {
        const allProperties: { name: string, value: string }[] = [];
        nodesProperties.forEach((properties, index) => {
          properties.forEach(property => {
            // allProperties.push(nodes[index].name + '.' + property);
            const propertyOption = {
              name: `${nodes[index].name}.${property}`,
              value: `[${nodes[index].name}].[${property}]`
            };
            allProperties.push(propertyOption);
          });
        });
        return allProperties;
      }).subscribe(allProperties => {
        this.topologyProperties = allProperties;
        this.broadcastService.broadcast(this.broadcastService.updateModelToscaConfig, this.topologyProperties);
      });
    });
  }

  public getNodeTemplates(): Observable<NodeTemplate[]> {
    const url = 'servicetemplates/' + this.encode(this.namespace)
      + '/' + this.encode(this.serviceTemplateId) + '/topologytemplate/';

    return this.httpService.get(this.getFullUrl(url)).map(response => {
      const nodeTemplates = [];
      for (const key in response.nodeTemplates) {
        if (response.nodeTemplates.hasOwnProperty(key)) {
          const nodeTemplate = response.nodeTemplates[key];
          nodeTemplates.push({
            id: nodeTemplate.id,
            type: nodeTemplate.type.replace(/^\{(.+)\}(.+)/, '$2'),
            name: nodeTemplate.name,
            namespace: nodeTemplate.type.replace(/^\{(.+)\}(.+)/, '$1'),
          });
        }
      }
      return nodeTemplates;
    });
  }

  public loadTopologyProperties(nodeTemplate: NodeTemplate): Observable<string[]> {
    const url = 'nodetypes/' + this.encode(nodeTemplate.namespace)
      + '/' + this.encode(nodeTemplate.type) + '/propertiesdefinition/winery/list/';

    return this.httpService.get(this.getFullUrl(url)).map(properties =>
      properties.map(property => property.key));
  }

  public loadNodeTemplateInterfaces(nodeTemplate: NodeTemplate): Observable<string[]> {
    const url = 'nodetypes/' + this.encode(nodeTemplate.namespace)
      + '/' + this.encode(nodeTemplate.type) + '/interfaces/';

    return this.httpService.get(this.getFullUrl(url));
  }
  public loadNodeTemplateOperations(nodeTemplate: NodeTemplate,
    interfaceName: string): Observable<string[]> {
    const url = 'nodetypes/' + this.encode(nodeTemplate.namespace)
      + '/' + this.encode(nodeTemplate.type) + '/interfaces/' + this.encode(interfaceName) + '/operations/';

    return this.httpService.get(this.getFullUrl(url));
  }

  public loadNodeTemplateOperationParameter(nodeTemplate: NodeTemplate,
    interfaceName: string,
    operation: string): Observable<any> {
    const relativePath = 'nodetypes/' + this.encode(nodeTemplate.namespace) + '/' + this.encode(nodeTemplate.type)
      + '/interfaces/' + this.encode(interfaceName) + '/operations/' + this.encode(operation) + '/';

    // input parameters
    const inputObservable = this.httpService
      .get(this.getFullUrl(relativePath + 'inputparameters'));

    // output parameters
    const outputObservable = this.httpService
      .get(this.getFullUrl(relativePath + 'outputparameters'));

    return Observable.forkJoin([inputObservable, outputObservable]).map(params => {
      return {
        input: params[0],
        output: params[1],
      };
    });
  }

  private decode(param: string): string {
    return decodeURIComponent(decodeURIComponent(param));
  }

  private encode(param: string): string {
    return encodeURIComponent(encodeURIComponent(param));
  }

  private getFullUrl(relativePath: string) {
    return this.repositoryURL + relativePath;
  }
}