aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/odlux/apps/configurationApp/src/utilities/viewEngineHelper.ts
blob: ad34c83b90b5fba033558d4c766b59f709409842 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { storeService } from '../../../../framework/src/services/storeService';
import { WhenAST, WhenTokenType } from '../yang/whenParser';

import {
  ViewSpecification,
  ViewElement,
  isViewElementReference,
  isViewElementList,
  isViewElementObjectOrList,
  isViewElementRpc,
  isViewElementChoice,
  ViewElementChoiceCase,
} from '../models/uiModels';

import { Module } from '../models/yang';

import { restService } from '../services/restServices';

export type HttpResult = {
  status: number;
  message?: string | undefined;
  data: {
    [key: string]: any;
  } | null | undefined;
};

export const checkResponseCode = (restResult: HttpResult) =>{
  //403 gets handled by the framework from now on
  return restResult.status !== 403 && ( restResult.status < 200 || restResult.status > 299);
};

export const resolveVPath = (current: string, vPath: string): string => {
  if (vPath.startsWith('/')) {
    return vPath;
  }
  const parts = current.split('/');
  const vPathParts = vPath.split('/');
  for (const part of vPathParts) {
    if (part === '.') {
      continue;
    } else if (part === '..') {
      parts.pop();
    } else {
      parts.push(part);
    }
  }
  return parts.join('/');
};

export const splitVPath = (vPath: string, vPathParser : RegExp): [string, string?][] => {
  const pathParts: [string, string?][] = [];
  let partMatch: RegExpExecArray | null;
  if (vPath) do {
    partMatch = vPathParser.exec(vPath);
    if (partMatch) {
      pathParts.push([partMatch[1], partMatch[2] || undefined]);
    }
  } while (partMatch);
  return pathParts;
};

const derivedFrom = (vPath: string, when: WhenAST, viewData: any, includeSelf = false) => {
  if (when.args?.length !== 2) {
    throw new Error('derived-from or derived-from-or-self requires 2 arguments.');
  }
  const [arg1, arg2] = when.args;
  if (arg1.type !== WhenTokenType.IDENTIFIER || arg2.type !== WhenTokenType.STRING) {
    throw new Error('derived-from or derived-from-or-self requires first argument IDENTIFIER and second argument STRING.');
  }

  if (!storeService.applicationStore) {
    throw new Error('storeService.applicationStore is not defined.');
  }

  const pathParts = splitVPath(arg1.value as string || '', /(?:(?:([^\/\:]+):)?([^\/]+))/g); 
  const referenceValueParts = /(?:(?:([^\/\:]+):)?([^\/]+))/g.exec(arg2.value as string || ''); 

  if (!pathParts || !referenceValueParts || pathParts.length === 0 || referenceValueParts.length === 0) {
    throw new Error('derived-from or derived-from-or-self requires first argument PATH and second argument IDENTITY.');
  }

  if (pathParts[0][1]?.startsWith('..') || pathParts[0][1]?.startsWith('/')) {
    throw new Error('derived-from or derived-from-or-self currently only supports relative paths.');
  }

  const { configuration: { deviceDescription: { modules } } } = storeService.applicationStore.state;
  const dataValue = pathParts.reduce((acc, [ns, prop]) => {
    if (prop === '.') {
      return acc;
    } 
    if (acc && prop) {
      const moduleName = ns && (Object.values(modules).find((m: Module) => m.prefix === ns) || Object.values(modules).find((m: Module) => m.name === ns))?.name;
      return (moduleName) ? acc[`${moduleName}:${prop}`] ||  acc[prop] : acc[prop];
    }
    return undefined;
  }, viewData);

  let dataValueParts = dataValue && /(?:(?:([^\/\:]+):)?([^\/]+))/g.exec(dataValue);
  if (!dataValueParts || dataValueParts.length < 2) {
    throw new Error(`derived-from or derived-from-or-self value referenced by first argument [${arg1.value}] not found.`);
  }
  let [, dataValueNs, dataValueProp] = dataValueParts;
  let dataValueModule: Module = dataValueNs && (Object.values(modules).find((m: Module) => m.name === dataValueNs));
  let dataValueIdentity = dataValueModule && dataValueModule.identities && (Object.values(dataValueModule.identities).find((i) => i.label === dataValueProp));

  if (!dataValueIdentity) {
    throw new Error(`derived-from or derived-from-or-self identity [${dataValue}] referenced by first argument [${arg1.value}] not found.`);
  }

  const [, referenceValueNs, referenceValueProp] = referenceValueParts;
  const referenceValueModule = referenceValueNs && (Object.values(modules).find((m: Module) => m.prefix === referenceValueNs));
  const referenceValueIdentity = referenceValueModule && referenceValueModule.identities && (Object.values(referenceValueModule.identities).find((i) => i.label === referenceValueProp));

  if (!referenceValueIdentity) {
    throw new Error(`derived-from or derived-from-or-self identity [${arg2.value}] referenced by second argument not found.`);
  }

  let result = includeSelf && (referenceValueIdentity === dataValueIdentity);
  while (dataValueIdentity && dataValueIdentity.base && !result) {
    dataValueParts = dataValue && /(?:(?:([^\/\:]+):)?([^\/]+))/g.exec(dataValueIdentity.base);
    const [, innerDataValueNs, innerDataValueProp] = dataValueParts;
    dataValueModule = innerDataValueNs && (Object.values(modules).find((m: Module) => m.prefix === innerDataValueNs)) || dataValueModule;
    dataValueIdentity = dataValueModule && dataValueModule.identities && (Object.values(dataValueModule.identities).find((i) => i.label === innerDataValueProp)) ;
    result = (referenceValueIdentity === dataValueIdentity);
  }

  return result;
};

const evaluateWhen = async (vPath: string, when: WhenAST, viewData: any): Promise<boolean> => {
  switch (when.type) {
    case WhenTokenType.FUNCTION:
      switch (when.name) {
        case 'derived-from-or-self':
          return derivedFrom(vPath, when, viewData, true);
        case 'derived-from':
          return derivedFrom(vPath, when, viewData, false);
        default:
          throw new Error(`Unknown function ${when.name}`);
      }
    case WhenTokenType.AND:
      return !when.left || !when.right || (await evaluateWhen(vPath, when.left, viewData) && await evaluateWhen(vPath, when.right, viewData));
    case WhenTokenType.OR:
      return !when.left || !when.right || (await evaluateWhen(vPath, when.left, viewData) || await evaluateWhen(vPath, when.right, viewData));
    case WhenTokenType.NOT:
      return !when.right || ! await evaluateWhen(vPath, when.right, viewData);
    case WhenTokenType.EXPRESSION:
      return !(when.value && typeof when.value !== 'string') || await evaluateWhen(vPath, when.value, viewData);
  }   
  return true;
};

export const getReferencedDataList = async (refPath: string, dataPath: string, modules: { [name: string]: Module }, views: ViewSpecification[]) => {
  const pathParts = splitVPath(refPath, /(?:(?:([^\/\:]+):)?([^\/]+))/g);  // 1 = opt: namespace / 2 = property
  const defaultNS = pathParts[0][0];
  let referencedModule = modules[defaultNS];

  let dataMember: string;
  let view: ViewSpecification;
  let currentNS: string | null = null;
  let dataUrls = [dataPath];
  let data: any;

  for (let i = 0; i < pathParts.length; ++i) {
    const [pathPartNS, pathPart] = pathParts[i];
    const namespace = pathPartNS != null ? (currentNS = pathPartNS) : currentNS;

    const viewElement = i === 0
      ? views[0].elements[`${referencedModule.name}:${pathPart}`]
      : view!.elements[`${pathPart}`] || view!.elements[`${namespace}:${pathPart}`];

    if (!viewElement) throw new Error(`Could not find ${pathPart} in ${refPath}`);
    if (i < pathParts.length - 1) {
      if (!isViewElementObjectOrList(viewElement)) {
        throw Error(`Module: [${referencedModule.name}].[${viewElement.label}]. View element is not list or object.`);
      }
      view = views[+viewElement.viewId];
      const resultingDataUrls : string[] = [];
      if (isViewElementList(viewElement)) {
        for (let j = 0; j < dataUrls.length; ++j) {
          const dataUrl = dataUrls[j];
          const restResult = (await restService.getConfigData(dataUrl));
          if (restResult.data == null || checkResponseCode(restResult)) {
            const message = restResult.data && restResult.data.errors && restResult.data.errors.error && restResult.data.errors.error[0] && restResult.data.errors.error[0]['error-message'] || '';
            throw new Error(`Server Error. Status: [${restResult.status}]\n${message || restResult.message || ''}`);
          }

          let dataRaw = restResult.data[`${defaultNS}:${dataMember!}`];
          if (dataRaw === undefined) {
            dataRaw = restResult.data[dataMember!];
          }
          dataRaw = dataRaw instanceof Array
            ? dataRaw[0]
            : dataRaw;

          data = dataRaw && dataRaw[viewElement.label] || [];
          const keys: string[] = data.map((entry: { [key: string]: any } )=> entry[viewElement.key!]);
          resultingDataUrls.push(...keys.map(key => `${dataUrl}/${viewElement.label.replace(/\//ig, '%2F')}=${key.replace(/\//ig, '%2F')}`));
        }
        dataMember = viewElement.label;
      } else {
        // just a member, not a list
        const pathSegment = (i === 0
          ? `/${referencedModule.name}:${viewElement.label.replace(/\//ig, '%2F')}`
          : `/${viewElement.label.replace(/\//ig, '%2F')}`);
        resultingDataUrls.push(...dataUrls.map(dataUrl => dataUrl + pathSegment));
        dataMember = viewElement.label;
      }
      dataUrls = resultingDataUrls;
    } else {
      data = [];
      for (let j = 0; j < dataUrls.length; ++j) {
        const dataUrl = dataUrls[j];
        const restResult = (await restService.getConfigData(dataUrl));
        if (restResult.data == null || checkResponseCode(restResult)) {
          const message = restResult.data && restResult.data.errors && restResult.data.errors.error && restResult.data.errors.error[0] && restResult.data.errors.error[0]['error-message'] || '';
          throw new Error(`Server Error. Status: [${restResult.status}]\n${message || restResult.message || ''}`);
        }
        let dataRaw = restResult.data[`${defaultNS}:${dataMember!}`];
        if (dataRaw === undefined) {
          dataRaw = restResult.data[dataMember!];
        }
        dataRaw = dataRaw instanceof Array
          ? dataRaw[0]
          : dataRaw;
        data.push(dataRaw);
      }
      // BUG UUID ist nicht in den elements enthalten !!!!!!
      const key = viewElement && viewElement.label || pathPart;
      return {
        view: view!,
        data: data,
        key: key,
      };
    }
  }
  return null;
};

export const resolveViewDescription = (defaultNS: string | null, vPath: string, view: ViewSpecification): ViewSpecification =>{

  // resolve all references.
  view = { ...view };
  view.elements = Object.keys(view.elements).reduce<{ [name: string]: ViewElement }>((acc, cur) => {
    const resolveHistory : ViewElement[] = [];  
    let elm = view.elements[cur];
    const key = defaultNS && cur.replace(new RegExp(`^${defaultNS}:`, 'i'), '') || cur;
    while (isViewElementReference(elm)) {
      const result = (elm.ref(vPath));  
      if (result) {
        const [referencedElement, referencedPath] = result;
        if (resolveHistory.some(hist => hist === referencedElement)) {
          console.error(`Circle reference found at: ${vPath}`, resolveHistory);
          break;
        }
        elm = referencedElement;
        vPath = referencedPath;
        resolveHistory.push(elm);
      }
    } 
    
    acc[key] = { ...elm, id: key };
    
    return acc;
  }, {});
  return view;
};

export const flattenViewElements = (defaultNS: string | null, parentPath: string, elements: { [name: string]: ViewElement }, views: ViewSpecification[], currentPath: string ): { [name: string]: ViewElement } => {
  if (!elements) return {};
  return Object.keys(elements).reduce<{ [name: string]: ViewElement }>((acc, cur) => {
    const elm = elements[cur];

    // remove the default namespace, and only the default namespace, sine it seems that this is also not in the restconf response
    const elmKey = defaultNS && elm.id.replace(new RegExp(`^${defaultNS}:`, 'i'), '') || elm.id;
    const key = parentPath ? `${parentPath}.${elmKey}` : elmKey;

    if (isViewElementRpc(elm)) {
      console.warn(`Flatten of RFC not supported ! [${currentPath}][${elm.label}]`);
      return acc;
    } else if (isViewElementObjectOrList(elm)) {
      const view = views[+elm.viewId];
      const inner = view && flattenViewElements(defaultNS, key, view.elements, views, `${currentPath}/${view.name}`);
      if (inner) {
        Object.keys(inner).forEach(k => (acc[k] = inner[k]));
      }
    } else if (isViewElementChoice(elm)) {
      acc[key] = {
        ...elm,
        id: key,
        cases: Object.keys(elm.cases).reduce<{ [name: string]: ViewElementChoiceCase }>((accCases, curCases) => {
          const caseElement = elm.cases[curCases];
          accCases[curCases] = {
            ...caseElement,
            // Hint: do not use key it contains elmKey, which shell be omitted for cases.
            elements: flattenViewElements(defaultNS, /*key*/ parentPath, caseElement.elements, views, `${currentPath}/${elm.label}`),
          };
          return accCases;
        }, {}),
      };
    } else {
      acc[key] = {
        ...elm,
        id: key,
      };
    }
    return acc;
  }, {});
};

export const filterViewElements = async (vPath: string, viewData: any, viewSpecification: ViewSpecification) => {
  // filter elements of viewSpecification by evaluating when property
  return Object.keys(viewSpecification.elements).reduce(async (accPromise, cur) => {
    const acc = await accPromise;
    const elm = viewSpecification.elements[cur];
    if (!elm.when || await evaluateWhen(vPath || '', elm.when, viewData).catch((ex) => {
      console.warn(`Error evaluating when clause at: ${viewSpecification.name} for element: ${cur}`, ex);
      return true;
    })) {
      acc.elements[cur] = elm;
    }
    return acc;
  }, Promise.resolve({ ...viewSpecification, elements: {} as { [key: string]: ViewElement } }));
};