Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / ux / mwtnInventory / mwtnInventory-module / src / main / resources / mwtnInventory / mwtnInventory.service.ts
1 import * as angular from 'angularAMD';
2
3 const mwtnInventory = angular.module('app.mwtnInventory');
4
5 class ExtensionResult {
6   public extension: [
7     { "value-name": string, "value": string }
8   ]
9 }
10
11 interface GenericGetRequest {
12   url: string;
13   method: "GET";
14 }
15
16 interface GenericPostRequest<T> {
17   url: string;
18   method: "POST";
19   data: T
20 }
21
22 interface CommonService {
23   genericRequest<TResult>(request: GenericGetRequest): ng.IPromise<ng.IHttpResponse<TResult>>;
24   genericRequest<TRequest, TResult>(request: GenericPostRequest<TRequest>): ng.IPromise<ng.IHttpResponse<TResult>>;
25   getMountPoints<TResult>(): ng.IPromise<TResult>;
26 }
27
28 export class InventoryService {
29   constructor(private $q: ng.IQService, private $mwtnCommons: CommonService, private $mwtnDatabase, private $mwtnLog) {
30
31   }
32
33   /** 
34    * Helperfunction to detect and convert the 'value-name-group' .
35    * @param propertyName Name of the object property.
36    * @param propertyValue Value of the object property.
37    * @param valueName Optional: The value-name to be used instaed of the propertyName.
38    * @returns A simplified property value if this is a 'value-name-group' otherwhise the propertyValue. */
39   private convertValue = (propertyName: string, propertyValue: any, valueName: string = ''): any => {
40     if (propertyValue && propertyValue instanceof Array && propertyValue.length == 1 && (
41       valueName == null ||
42       propertyValue[0]["value-name"] === propertyName ||
43       propertyValue[0]["value-name"] === valueName)
44     ) {
45       return propertyValue[0]["value"];
46     }
47     return propertyValue;
48   }
49
50   /** 
51    *  Converts an API object to a simplified local object.
52    *  @param apiResult The API object to convert.
53    *  @param valueName Optional: The value-name to be used instaed of the propertyName.
54    *  @returns The simplified local object.
55   */
56   private convertObject = (apiResult: any, valueName: string = ''): any => {
57     if (apiResult instanceof Array) {
58       return apiResult.map(elm => { return this.convertObject(elm, valueName); });
59     } else if (apiResult instanceof Object) {
60       const keys = Object.keys(apiResult);
61       let result = {};
62       keys.forEach(key => {
63         const value = this.convertValue(key, apiResult[key], valueName);
64         result[key] = (value instanceof Object || value instanceof Array)
65           ? this.convertObject(value)
66           : value;
67       });
68       return result;
69     }
70     return apiResult;
71   }
72
73   /** Requests all active moint points */
74   public getConnectedMountpoints(): ng.IPromise<string[]> {
75     return this.$mwtnCommons.getMountPoints<{}>().then((mountpoints: {}[]) => {
76       //console.log(mountpoints);
77       return <string[]>mountpoints.reduce((acc: string[], cur, ind, arr) => {
78         if (cur['netconf-node-topology:connection-status'] === 'connected') acc.push(cur["node-id"]);
79         return acc;
80       }, []);
81     });
82   }
83
84   /** 
85    * Requests all 'root identifiers' for the given 'node id'.
86    * @param nodeId The id of the node to request the root identifiers for.
87    * @returns A q.Promise containing an array of all root identifiers for the requested node id.
88    * */
89   public getRootIdentifiers(nodeId: string): ng.IPromise<string[]> {
90
91     const request: GenericGetRequest = {
92       url: `operational/network-topology:network-topology/topology/topology-netconf/node/${nodeId}/yang-ext:mount/core-model:network-element/extension/top-level-equipment`,
93       method: "GET"
94     };
95
96     return this.$mwtnCommons.genericRequest<ExtensionResult>(request).then((result) => {
97       if (result && result.status == 200 && result.data) {
98         const topLevelEquipment = this.convertObject(result.data, 'top-level-equipment');
99         const rootIdentifiers = topLevelEquipment && topLevelEquipment.extension && topLevelEquipment.extension.split(',');
100         return rootIdentifiers && rootIdentifiers.map(identifier => identifier && identifier.trim());
101       }
102       return null;
103     }, err => (null));
104   }
105
106   /** 
107    * Requests the detail information for the given combination of 'nodeId' and 'equipmentIdentifier'.
108    * @param nodeId The id of the root node.
109    * @param identifier The identifier to request the details for.
110    * @returns A q.Promise containing an object with all the details.
111    * */
112   public getEquipmentDetails(nodeId: string, identifier: string): ng.IPromise<{}> {
113     const request: GenericGetRequest = {
114       url: `operational/network-topology:network-topology/topology/topology-netconf/node/${nodeId}/yang-ext:mount/core-model:equipment/${identifier}`,
115       method: "GET"
116     };
117     return this.$mwtnCommons.genericRequest<ExtensionResult>(request).then((result) => {
118       if (result && result.status == 200 && result.data) {
119         return this.convertObject(result.data);
120       }
121       return null;
122     }, err => (null));
123   }
124
125   /** 
126    * Requests the conditional information for the given combination of 'nodeId' and 'equipmentIdentifier'.
127    * @param nodeId The id of the root node.
128    * @param identifier The identifier to request the conditionals for.
129    * @returns A q.Promise containing an object with all the conditional informations.
130    * */
131   public getEquipmentConditionals(nodeId: string, identifier: string): ng.IPromise<{}> {
132     const request: GenericGetRequest = {
133       url: `operational/network-topology:network-topology/topology/topology-netconf/node/${nodeId}/yang-ext:mount/onf-core-model-conditional-packages:equipment-pac/${identifier}`,
134       method: "GET"
135     };
136     return this.$mwtnCommons.genericRequest<ExtensionResult>(request).then((result) => {
137       if (result && result.status == 200 && result.data) {
138         return this.convertObject(result.data);
139       }
140       return null;
141     }, err => (null));
142   }
143 }
144 mwtnInventory.service('mwtnInventoryService', ["$q", "$mwtnCommons", "$mwtnDatabase", "$mwtnLog", InventoryService]);