Support for Nested/Hierarchical Services
[sdc.git] / catalog-ui / src / app / utils / component-instance-factory.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 /**
21  * Created by obarda on 3/7/2016.
22  */
23 'use strict';
24 import { ComponentType } from 'app/utils';
25 import { Component, ComponentInstance, ResourceInstance, ServiceInstance, ServiceSubstitutionInstance, ServiceProxyInstance } from '../models';
26 import { LeftPaletteComponent } from '../models/components/displayComponent';
27
28 export class ComponentInstanceFactory {
29
30     static createComponentInstance(componentInstance: ComponentInstance): ComponentInstance {
31         let newComponentInstance: ComponentInstance;
32         switch (componentInstance.originType) {
33             case ComponentType.SERVICE:
34                 newComponentInstance = new ServiceInstance(componentInstance);
35                 break;
36            case ComponentType.SERVICE_PROXY:
37                 newComponentInstance = new ServiceProxyInstance(componentInstance);
38                 break;
39            case ComponentType.SERVICE_SUBSTITUTION:
40                 newComponentInstance = new ServiceSubstitutionInstance(componentInstance);
41                 break;
42             default :
43                 newComponentInstance = new ResourceInstance(componentInstance);
44                 break;
45         }
46         return newComponentInstance;
47     }
48
49     static createEmptyComponentInstance = (componentInstanceType?: string): ComponentInstance => {
50         let newComponentInstance: ComponentInstance;
51         switch (componentInstanceType) {
52             case ComponentType.SERVICE:
53                 newComponentInstance = new ServiceInstance();
54                 break;
55             case ComponentType.SERVICE_PROXY:
56                 newComponentInstance = new ServiceProxyInstance();
57                 break;
58            case ComponentType.SERVICE_SUBSTITUTION:
59                 newComponentInstance = new ServiceSubstitutionInstance();
60                 break;
61             default :
62                 newComponentInstance = new ResourceInstance();
63                 break;
64         }
65         return newComponentInstance;
66     }
67
68     static createComponentInstanceFromComponent = (component: LeftPaletteComponent, useServiceSubstitutionForNestedServices?: boolean): ComponentInstance => {
69         const newComponentInstance: ComponentInstance = ComponentInstanceFactory.createEmptyComponentInstance(component.componentType);
70         newComponentInstance.uniqueId = component.uniqueId + (new Date()).getTime();
71         newComponentInstance.posX = 0;
72         newComponentInstance.posY = 0;
73         newComponentInstance.name = component.name;
74         newComponentInstance.componentVersion = component.version;
75         newComponentInstance.originType = getOriginType(component);
76         newComponentInstance.requirements = component.requirements;
77         newComponentInstance.capabilities = component.capabilities;
78         newComponentInstance.icon = component.icon;
79         newComponentInstance.componentUid = component.uniqueId;
80         return newComponentInstance;
81
82         function getOriginType(component: LeftPaletteComponent): string  {
83             if (component.componentSubType) {
84                 return component.componentSubType;
85             } else {
86                 if (component.componentType === ComponentType.SERVICE) {
87                         if (useServiceSubstitutionForNestedServices){
88                                    return ComponentType.SERVICE_SUBSTITUTION;
89                     } else {
90                        return ComponentType.SERVICE_PROXY;
91                     }
92                 } else {
93                     return component.resourceType;
94                 }
95             }
96         }
97     }
98 }