Catalog alignment
[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 import IHttpPromiseCallbackArg = angular.IHttpPromiseCallbackArg;
36
37 export class LeftPaletteLoaderService {
38
39     static '$inject' = [
40         'Restangular',
41         '$http',
42         'sdcConfig',
43         '$q',
44         'ComponentFactory',
45         'EventListenerService'
46
47     ];
48
49     constructor(protected restangular:restangular.IElement,
50                 protected $http:ng.IHttpService,
51                 protected sdcConfig:IAppConfigurtaion,
52                 protected $q:ng.IQService,
53                 protected ComponentFactory:ComponentFactory,
54                 protected EventListenerService:EventListenerService) {
55
56         this.restangular.setBaseUrl(sdcConfig.api.root + sdcConfig.api.component_api_root);
57
58     }
59
60     leftPanelComponents:Array<LeftPaletteComponent>;
61
62     public loadLeftPanel = (component:Component):void => {
63         this.leftPanelComponents = [];
64         this.updateLeftPaletteForTopologyTemplate(component);
65     }
66
67     private updateLeftPalette = (componentInternalType:string):void => {
68
69         /* add components */
70         const leftPaletteUrl = this.sdcConfig.api.uicache_root + this.sdcConfig.api.GET_uicache_left_palette;
71         this.$http.get(leftPaletteUrl, {params: {'internalComponentType': componentInternalType}}).then((res) => res.data).then((leftPaletteComponentMetadata:Array<ComponentMetadata>) => {
72         // this.restangular.one("resources").one('/latestversion/notabstract/metadata').get({'internalComponentType': componentInternalType}).then((leftPaletteComponentMetadata:Array<ComponentMetadata>) => {
73             _.forEach(leftPaletteComponentMetadata, (componentMetadata:ComponentMetadata) => {
74                 this.leftPanelComponents.push(new LeftPaletteComponent(LeftPaletteMetadataTypes.Component, componentMetadata));
75             });
76             
77             /* add groups */
78             this.restangular.one('/groupTypes').get({'internalComponentType': componentInternalType}).then((leftPaletteGroupTypes:GroupTpes) => {
79                 _.forEach(leftPaletteGroupTypes, (groupMetadata: GroupMetadata) => {
80                     this.leftPanelComponents.push(new LeftPaletteComponent(LeftPaletteMetadataTypes.Group, groupMetadata));
81                 }); 
82                 this.EventListenerService.notifyObservers(EVENTS.LEFT_PALETTE_UPDATE_EVENT);
83             });
84
85             /* add policies */
86             this.restangular.one('/policyTypes').get({'internalComponentType': componentInternalType}).then((leftPalettePolicyTypes:PolicyTpes) => {
87                 _.forEach(leftPalettePolicyTypes, (policyMetadata: PolicyMetadata) => {
88                     this.leftPanelComponents.push(new LeftPaletteComponent(LeftPaletteMetadataTypes.Policy, policyMetadata));
89                 }); 
90                 this.EventListenerService.notifyObservers(EVENTS.LEFT_PALETTE_UPDATE_EVENT);
91             });
92         });
93
94
95     }
96
97     public getLeftPanelComponentsForDisplay = (component:Component):Array<LeftPaletteComponent> => {
98         return this.leftPanelComponents;
99     };
100
101     /**
102      * Update left palete items according to current topology templates we are in.
103      */
104     public updateLeftPaletteForTopologyTemplate = (component:Component):void => {
105         switch (component.componentType) {
106             case ComponentType.SERVICE:
107                 this.updateLeftPalette(ComponentType.SERVICE);
108                 break;
109             case ComponentType.RESOURCE:
110                 this.updateLeftPalette((<Resource>component).resourceType);
111                 break;
112             default:
113                 console.log('ERROR: Component type '+ component.componentType + ' is not exists');
114         }
115     };
116 }