re base code
[sdc.git] / catalog-ui / src / app / services / components / utils / composition-left-palette-service.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/13/2016.
22  */
23 'use strict';
24 import * as _ from "lodash";
25 import {LeftPaletteComponent, LeftPaletteMetadataTypes} from "app/models/components/displayComponent";
26 import {Component} from "app/models/components/component";
27 import {EventListenerService} from "../../event-listener-service";
28 import {ComponentFactory} from "../../../utils/component-factory";
29 import {IAppConfigurtaion} from "app/models/app-config";
30 import {ResourceType, ComponentType, EVENTS} from "../../../utils/constants";
31 import {ComponentMetadata} from "app/models/component-metadata";
32 import {GroupMetadata, GroupTpes} from "app/models/group-metadata";
33 import {PolicyMetadata, PolicyTpes} from "app/models/policy-metadata";
34 import {Resource} from "app/models/components/resource";
35
36 export class LeftPaletteLoaderService {
37
38     static '$inject' = [
39         'Restangular',
40         'sdcConfig',
41         '$q',
42         'ComponentFactory',
43         'EventListenerService'
44
45     ];
46
47     constructor(protected restangular:restangular.IElement,
48                 protected sdcConfig:IAppConfigurtaion,
49                 protected $q:ng.IQService,
50                 protected ComponentFactory:ComponentFactory,
51                 protected EventListenerService:EventListenerService) {
52
53         this.restangular.setBaseUrl(sdcConfig.api.root + sdcConfig.api.component_api_root);
54
55     }
56
57     leftPanelComponents:Array<LeftPaletteComponent>;
58
59     public loadLeftPanel = (component:Component):void => {
60         this.leftPanelComponents = [];
61         this.updateLeftPaletteForTopologyTemplate(component);
62     }
63
64     private updateLeftPalette = (componentInternalType:string):void => {
65
66         /* add components */
67         this.restangular.one("resources").one('/latestversion/notabstract/metadata').get({'internalComponentType': componentInternalType}).then((leftPaletteComponentMetadata:Array<ComponentMetadata>) => {    
68             _.forEach(leftPaletteComponentMetadata, (componentMetadata:ComponentMetadata) => {
69                 this.leftPanelComponents.push(new LeftPaletteComponent(LeftPaletteMetadataTypes.Component, componentMetadata));
70             });
71             
72             /* add groups */
73             this.restangular.one('/groupTypes').get({'internalComponentType': componentInternalType}).then((leftPaletteGroupTypes:GroupTpes) => {
74                 _.forEach(leftPaletteGroupTypes, (groupMetadata: GroupMetadata) => {
75                     this.leftPanelComponents.push(new LeftPaletteComponent(LeftPaletteMetadataTypes.Group, groupMetadata));
76                 }); 
77                 this.EventListenerService.notifyObservers(EVENTS.LEFT_PALETTE_UPDATE_EVENT);
78             });
79
80             /* add policies */
81             this.restangular.one('/policyTypes').get({'internalComponentType': componentInternalType}).then((leftPalettePolicyTypes:PolicyTpes) => {
82                 _.forEach(leftPalettePolicyTypes, (policyMetadata: PolicyMetadata) => {
83                     this.leftPanelComponents.push(new LeftPaletteComponent(LeftPaletteMetadataTypes.Policy, policyMetadata));
84                 }); 
85                 this.EventListenerService.notifyObservers(EVENTS.LEFT_PALETTE_UPDATE_EVENT);
86             });
87         });
88
89
90     }
91
92     public getLeftPanelComponentsForDisplay = (component:Component):Array<LeftPaletteComponent> => {
93         return this.leftPanelComponents;
94     };
95
96     /**
97      * Update left palete items according to current topology templates we are in.
98      */
99     public updateLeftPaletteForTopologyTemplate = (component:Component):void => {
100         switch (component.componentType) {
101             case ComponentType.SERVICE:
102                 this.updateLeftPalette(ComponentType.SERVICE);
103                 break;
104             case ComponentType.RESOURCE:
105                 this.updateLeftPalette((<Resource>component).resourceType);
106                 break;
107             default:
108                 console.log('ERROR: Component type '+ component.componentType + ' is not exists');
109         }
110     };
111 }