Skip to content

Commit

Permalink
remove model on device; remove query on pnp info (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
YingXue committed Jun 25, 2020
1 parent 4592f29 commit 85a4ae3
Show file tree
Hide file tree
Showing 24 changed files with 43 additions and 427 deletions.
4 changes: 0 additions & 4 deletions src/app/api/models/deviceQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ export interface QueryClause {
}

export enum ParameterType {
// non pnp
edge = 'capabilities.iotEdge',
status = 'status',
// pnp
capabilityModelId = 'dcm',
interfaceId = 'interface',
}

export enum OperationType {
Expand Down
1 change: 0 additions & 1 deletion src/app/api/parameters/repoParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
export interface FetchModelParameters {
id: string;
expand?: boolean;
repoServiceHostName: string;
token?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
* Licensed under the MIT License
**********************************************************/
import * as DigitalTwinsModelService from './publicDigitalTwinsModelRepoService';
import { API_VERSION, MODEL_REPO_API_VERSION, HTTP_OPERATION_TYPES, PUBLIC_REPO_HOSTNAME_TEST } from '../../constants/apiConstants';
import { API_VERSION, MODEL_REPO_API_VERSION, HTTP_OPERATION_TYPES, PUBLIC_REPO_HOSTNAME_TEST, PUBLIC_REPO_HOSTNAME } from '../../constants/apiConstants';

describe('digitalTwinsModelService', () => {

context('fetchModel', () => {
const parameters = {
expand: undefined,
id: 'urn:azureiot:ModelDiscovery:ModelInformation:1',
repoServiceHostName: 'canary-repo.azureiotrepository.com',
token: 'SharedAccessSignature sr=canary-repo.azureiotrepository.com&sig=123&rid=repositoryId'
};

Expand Down Expand Up @@ -60,7 +59,7 @@ describe('digitalTwinsModelService', () => {
const apiVersionQuerySTring = `?${API_VERSION}${MODEL_REPO_API_VERSION}`;
const queryString = `${apiVersionQuerySTring}${expandQueryString}`;
const modelIdentifier = encodeURIComponent(parameters.id);
const resourceUrl = `https://${parameters.repoServiceHostName}/models/${modelIdentifier}${queryString}`;
const resourceUrl = `https://${PUBLIC_REPO_HOSTNAME}/models/${modelIdentifier}${queryString}`;

const controllerRequest = {
headers: {
Expand Down
3 changes: 2 additions & 1 deletion src/app/api/services/publicDigitalTwinsModelRepoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
HEADERS,
MODELREPO,
MODEL_REPO_API_VERSION,
PUBLIC_REPO_HOSTNAME,
PUBLIC_REPO_HOSTNAME_TEST,
HTTP_OPERATION_TYPES } from '../../constants/apiConstants';
import { PnPModel } from '../models/metamodelMetadata';
Expand All @@ -20,7 +21,7 @@ export const fetchModel = async (parameters: FetchModelParameters): Promise<PnPM
const apiVersionQuerySTring = `?${API_VERSION}${MODEL_REPO_API_VERSION}`;
const queryString = `${apiVersionQuerySTring}${expandQueryString}`;
const modelIdentifier = encodeURIComponent(parameters.id);
const resourceUrl = `https://${parameters.repoServiceHostName}/models/${modelIdentifier}${queryString}`;
const resourceUrl = `https://${PUBLIC_REPO_HOSTNAME}/models/${modelIdentifier}${queryString}`;

const controllerRequest: RequestInitWithUri = {
headers: {
Expand Down
51 changes: 5 additions & 46 deletions src/app/api/shared/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,6 @@ import { LIST_PLUG_AND_PLAY_DEVICES } from '../../constants/devices';

describe('utils', () => {
it('builds query string', () => {
expect(utils.buildQueryString(
{
clauses: [
{
operation: OperationType.equals,
parameterType: ParameterType.capabilityModelId,
value: 'enabled'
}
],
continuationTokens: [],
currentPageIndex: 1,
deviceId: '',
}
)).toEqual(`${LIST_PLUG_AND_PLAY_DEVICES} WHERE (HAS_CAPABILITYMODEL('enabled'))`);
expect(utils.buildQueryString(
{
clauses: [],
Expand All @@ -31,6 +17,7 @@ describe('utils', () => {
deviceId: 'device1',
}
)).toEqual(`${LIST_PLUG_AND_PLAY_DEVICES} WHERE STARTSWITH(devices.deviceId, 'device1')`);

expect(utils.buildQueryString(
{
clauses: [],
Expand All @@ -39,9 +26,11 @@ describe('utils', () => {
deviceId: '',
}
)).toEqual(LIST_PLUG_AND_PLAY_DEVICES + ' ');

expect(utils.buildQueryString(
null
)).toEqual(LIST_PLUG_AND_PLAY_DEVICES);

expect(utils.buildQueryString(
{
clauses: [
Expand All @@ -56,6 +45,7 @@ describe('utils', () => {
deviceId: '',
}
)).toEqual(`${LIST_PLUG_AND_PLAY_DEVICES} WHERE (${ParameterType.edge}=true)`);

expect(utils.buildQueryString(
{
clauses: [
Expand All @@ -73,22 +63,6 @@ describe('utils', () => {
});

it('converts query object to string', () => {
expect(utils.queryToString(
{
clauses: [
{
operation: OperationType.equals,
parameterType: ParameterType.capabilityModelId,
value: 'enabled'
},
{
}
],
continuationTokens: [],
currentPageIndex: 1,
deviceId: '',
}
)).toEqual(`WHERE (HAS_CAPABILITYMODEL('enabled'))`);
expect(utils.queryToString(
{
clauses: [],
Expand All @@ -97,6 +71,7 @@ describe('utils', () => {
deviceId: 'device1',
}
)).toEqual(`WHERE STARTSWITH(devices.deviceId, 'device1')`);

expect(utils.queryToString(
{
clauses: [],
Expand All @@ -121,22 +96,6 @@ describe('utils', () => {
value: 'disabled'
}
])).toEqual(`status='enabled' AND status='disabled'`);

expect(utils.clauseListToString([
{
operation: OperationType.equals,
parameterType: ParameterType.capabilityModelId,
value: 'enabled'
}
])).toEqual(`HAS_CAPABILITYMODEL('enabled')`);

expect(utils.clauseListToString([
{
operation: OperationType.equals,
parameterType: ParameterType.interfaceId,
value: 'enabled'
}
])).toEqual(`HAS_INTERFACE('enabled')`);
});

it('creates clause item as string', () => {
Expand Down
4 changes: 0 additions & 4 deletions src/app/api/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ export const clauseListToString = (clauses: QueryClause[]) => {

export const clauseToString = (clause: QueryClause) => {
switch (clause.parameterType) {
case ParameterType.capabilityModelId:
return toPnPClause(PnPQueryPrefix.HAS_CAPABILITY_MODEL, clause.value);
case ParameterType.interfaceId:
return toPnPClause(PnPQueryPrefix.HAS_INTERFACE, clause.value);
case ParameterType.edge:
return toEdgeClause(clause.parameterType, clause.value);
case ParameterType.status:
Expand Down
6 changes: 0 additions & 6 deletions src/app/constants/modelDefinitionConstants.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/app/constants/repositoryLocationTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
**********************************************************/
export enum REPOSITORY_LOCATION_TYPE {
Public = 'PUBLIC',
Device = 'DEVICE',
Local = 'LOCAL'
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('login/components/connectivityPane', () => {
modelDefinitionWithSource: {
isModelValid: true,
modelDefinition,
source: REPOSITORY_LOCATION_TYPE.Device,
source: REPOSITORY_LOCATION_TYPE.Local,
},
modelId: 'dtmi:__azureiot:sampleModel;1'
}),
Expand All @@ -171,7 +171,7 @@ describe('login/components/connectivityPane', () => {

const labels = digitalTwinInterfaces.find(Label);
expect(labels).toHaveLength(5); // tslint:disable-line:no-magic-numbers
expect(labels.at(1).props().children).toEqual([ResourceKeys.deviceInterfaces.columns.source, ': ', ResourceKeys.settings.modelDefinitions.repositoryTypes.device.label]);
expect(labels.at(1).props().children).toEqual([ResourceKeys.deviceInterfaces.columns.source, ': ', ResourceKeys.settings.modelDefinitions.repositoryTypes.local.label]);
expect(labels.at(2).props().children).toEqual('dtmi:__DeviceManagement:DeviceInformation;1'); // tslint:disable-line:no-magic-numbers
expect(labels.at(3).props().children).toEqual('dtmi:__Client:SDKInformation;1'); // tslint:disable-line:no-magic-numbers
expect(labels.at(4).props().children).toEqual('dtmi:__Contoso:EnvironmentalSensor;1'); // tslint:disable-line:no-magic-numbers
Expand Down
55 changes: 2 additions & 53 deletions src/app/devices/deviceContent/sagas/modelDefinitionSaga.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@
import 'jest';
import { select, call, put } from 'redux-saga/effects';
import { SagaIteratorClone, cloneableGenerator } from 'redux-saga/utils';
import { getModelDefinitionSaga, getModelDefinition, getModelDefinitionFromPublicRepo, getModelDefinitionFromDevice, getModelDefinitionFromLocalFile, validateModelDefinitionHelper } from './modelDefinitionSaga';
import * as DigitalTwinService from '../../../api/services/digitalTwinService';
import { getModelDefinitionSaga, getModelDefinition, getModelDefinitionFromPublicRepo, getModelDefinitionFromLocalFile, validateModelDefinitionHelper } from './modelDefinitionSaga';
import { addNotificationAction } from '../../../notifications/actions';
import { NotificationType } from '../../../api/models/notification';
import { ResourceKeys } from '../../../../localization/resourceKeys';
import { getModelDefinitionAction } from '../actions';
import { getRepositoryLocationSettingsSelector, getPublicRepositoryHostName, getLocalFolderPath } from '../../../settings/selectors';
import { getRepositoryLocationSettingsSelector, getLocalFolderPath } from '../../../settings/selectors';
import { REPOSITORY_LOCATION_TYPE } from '../../../constants/repositoryLocationTypes';
import { getComponentNameAndInterfaceIdArraySelector } from '../selectors';
import { getActiveAzureResourceConnectionStringSaga } from '../../../azureResource/sagas/getActiveAzureResourceConnectionStringSaga';
import { modelDefinitionCommandName, modelDefinitionInterfaceId } from '../../../constants/modelDefinitionConstants';
import { PUBLIC_REPO_HOSTNAME } from '../../../constants/apiConstants';
import { fetchLocalFile } from '../../../api/services/localRepoService';
import { fetchModelDefinition } from '../../../api/services/publicDigitalTwinsModelRepoService';

Expand Down Expand Up @@ -82,7 +77,6 @@ describe('modelDefinitionSaga', () => {

expect(getModelDefinitionSagaGenerator.next([{
repositoryLocationType: REPOSITORY_LOCATION_TYPE.Public,

}]).value).toEqual(
call(getModelDefinition, action, { repositoryLocationType: REPOSITORY_LOCATION_TYPE.Public })
);
Expand Down Expand Up @@ -141,52 +135,16 @@ describe('modelDefinitionSaga', () => {
(action, {repositoryLocationType: REPOSITORY_LOCATION_TYPE.Public});

expect(getModelDefinitionFromPublicRepoGenerator.next()).toEqual({
done: false,
value: select(getPublicRepositoryHostName)
});

expect(getModelDefinitionFromPublicRepoGenerator.next(PUBLIC_REPO_HOSTNAME)).toEqual({
done: false,
value: call(fetchModelDefinition, {
id: params.interfaceId,
repoServiceHostName: PUBLIC_REPO_HOSTNAME,
token: ''
})
});

expect(getModelDefinitionFromPublicRepoGenerator.next().done).toEqual(true);
});

describe('getModelDefinitionFromDevice ', () => {
const getModelDefinitionFromDeviceGenerator = cloneableGenerator(getModelDefinitionFromDevice)(action);
expect(getModelDefinitionFromDeviceGenerator.next()).toEqual({
done: false,
value: select(getComponentNameAndInterfaceIdArraySelector)
});

const nameAndId = [{
componentName: 'model_discovery',
interfaceId: modelDefinitionInterfaceId
}];
expect(getModelDefinitionFromDeviceGenerator.next(nameAndId)).toEqual({
done: false,
value: call(getActiveAzureResourceConnectionStringSaga)
});

expect(getModelDefinitionFromDeviceGenerator.next('connection_string')).toEqual({
done: false,
value: call(DigitalTwinService.invokeDigitalTwinInterfaceCommand, {
commandName: modelDefinitionCommandName,
componentName: 'model_discovery',
connectionString: 'connection_string',
digitalTwinId,
payload: interfaceId
})
});

expect(getModelDefinitionFromDeviceGenerator.next().done).toEqual(true);
});

describe('getModelDefinitionFromLocalFile ', () => {
const getModelDefinitionFromLocalFolderGenerator = cloneableGenerator(getModelDefinitionFromLocalFile)
(action);
Expand Down Expand Up @@ -214,15 +172,6 @@ describe('modelDefinitionSaga', () => {
expect(getModelDefinitionFromPublicRepoGenerator.next().done).toEqual(true);
});

it('getModelDefinition from device', () => {
const getModelDefinitionFromDeviceGenerator = cloneableGenerator(getModelDefinition)(action, {repositoryLocationType: REPOSITORY_LOCATION_TYPE.Device});
expect(getModelDefinitionFromDeviceGenerator.next()).toEqual({
done: false,
value: call(getModelDefinitionFromDevice, action)
});
expect(getModelDefinitionFromDeviceGenerator.next().done).toEqual(true);
});

it('getModelDefinition from local', () => {
const getModelDefinitionFromDeviceGenerator = cloneableGenerator(getModelDefinition)(action, {repositoryLocationType: REPOSITORY_LOCATION_TYPE.Local});
expect(getModelDefinitionFromDeviceGenerator.next()).toEqual({
Expand Down
29 changes: 1 addition & 28 deletions src/app/devices/deviceContent/sagas/modelDefinitionSaga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@ import { NotificationType } from '../../../api/models/notification';
import { ResourceKeys } from '../../../../localization/resourceKeys';
import { getModelDefinitionAction, GetModelDefinitionActionParameters } from '../actions';
import { FetchModelParameters } from '../../../api/parameters/repoParameters';
import { getRepositoryLocationSettingsSelector, getPublicRepositoryHostName, getLocalFolderPath } from '../../../settings/selectors';
import { getRepositoryLocationSettingsSelector, getLocalFolderPath } from '../../../settings/selectors';
import { RepositoryLocationSettings } from '../../../settings/state';
import { REPOSITORY_LOCATION_TYPE } from './../../../constants/repositoryLocationTypes';
import { invokeDigitalTwinInterfaceCommand } from '../../../api/services/digitalTwinService';
import { getActiveAzureResourceConnectionStringSaga } from '../../../azureResource/sagas/getActiveAzureResourceConnectionStringSaga';
import { getComponentNameAndInterfaceIdArraySelector, ComponentAndInterfaceId } from '../selectors';
import { InterfaceNotImplementedException } from './../../../shared/utils/exceptions/interfaceNotImplementedException';
import { modelDefinitionInterfaceId, modelDefinitionCommandName } from '../../../constants/modelDefinitionConstants';
import { fetchLocalFile } from './../../../api/services/localRepoService';
import { ModelDefinition } from './../../../api/models/modelDefinition';
import { ModelDefinitionNotValidJsonError } from '../../../api/models/modelDefinitionNotValidJsonError';
Expand Down Expand Up @@ -81,7 +76,6 @@ export function* validateModelDefinitionHelper(modelDefinition: ModelDefinition,
export function* getModelDefinitionFromPublicRepo(action: Action<GetModelDefinitionActionParameters>, location: RepositoryLocationSettings) {
const parameters: FetchModelParameters = {
id: action.payload.interfaceId,
repoServiceHostName: yield select(getPublicRepositoryHostName),
token: ''
};
return yield call(fetchModelDefinition, parameters);
Expand All @@ -92,29 +86,8 @@ export function* getModelDefinitionFromLocalFile(action: Action<GetModelDefiniti
return yield call(fetchLocalFile, path, action.payload.interfaceId);
}

export function* getModelDefinitionFromDevice(action: Action<GetModelDefinitionActionParameters>) {
// check if device has implemented ${modelDefinitionInterfaceId} interface.
const componentAndIs: ComponentAndInterfaceId[] = yield select(getComponentNameAndInterfaceIdArraySelector);
const filtered = componentAndIs.filter(componentAndId => componentAndId.interfaceId === modelDefinitionInterfaceId);
if (filtered.length === 0) {
throw new InterfaceNotImplementedException();
}
const componentName = filtered[0].componentName;

// if interface is implemented, invoke command on device
return yield call(invokeDigitalTwinInterfaceCommand, {
commandName: modelDefinitionCommandName,
componentName,
connectionString: yield call(getActiveAzureResourceConnectionStringSaga),
digitalTwinId: action.payload.digitalTwinId,
payload: action.payload.interfaceId
});
}

export function* getModelDefinition(action: Action<GetModelDefinitionActionParameters>, location: RepositoryLocationSettings) {
switch (location.repositoryLocationType) {
case REPOSITORY_LOCATION_TYPE.Device:
return yield call(getModelDefinitionFromDevice, action);
case REPOSITORY_LOCATION_TYPE.Local:
return yield call(getModelDefinitionFromLocalFile, action);
default:
Expand Down
Loading

0 comments on commit 85a4ae3

Please sign in to comment.