1 import * as angular from 'angularAMD';
3 const mwtnInventory = angular.module('app.mwtnInventory');
5 class ExtensionResult {
7 { "value-name": string, "value": string }
11 interface GenericGetRequest {
16 interface GenericPostRequest<T> {
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>;
28 export class InventoryService {
29 constructor(private $q: ng.IQService, private $mwtnCommons: CommonService, private $mwtnDatabase, private $mwtnLog) {
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 && (
42 propertyValue[0]["value-name"] === propertyName ||
43 propertyValue[0]["value-name"] === valueName)
45 return propertyValue[0]["value"];
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.
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);
63 const value = this.convertValue(key, apiResult[key], valueName);
64 result[key] = (value instanceof Object || value instanceof Array)
65 ? this.convertObject(value)
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"]);
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.
89 public getRootIdentifiers(nodeId: string): ng.IPromise<string[]> {
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`,
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());
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.
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}`,
117 return this.$mwtnCommons.genericRequest<ExtensionResult>(request).then((result) => {
118 if (result && result.status == 200 && result.data) {
119 return this.convertObject(result.data);
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.
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}`,
136 return this.$mwtnCommons.genericRequest<ExtensionResult>(request).then((result) => {
137 if (result && result.status == 200 && result.data) {
138 return this.convertObject(result.data);
144 mwtnInventory.service('mwtnInventoryService', ["$q", "$mwtnCommons", "$mwtnDatabase", "$mwtnLog", InventoryService]);