diff options
32 files changed, 960 insertions, 190 deletions
diff --git a/cds-ui/client/src/app/feature-modules/blueprint/blueprint.module.ts b/cds-ui/client/src/app/feature-modules/blueprint/blueprint.module.ts index 27803ce56..edbaca67f 100644 --- a/cds-ui/client/src/app/feature-modules/blueprint/blueprint.module.ts +++ b/cds-ui/client/src/app/feature-modules/blueprint/blueprint.module.ts @@ -32,10 +32,12 @@ import { DeployTemplateModule } from './deploy-template/deploy-template.module'; import { TestTemplateModule } from './test-template/test-template.module'; import { AppMaterialModule } from '../../../app/common/modules/app-material.module'; import { ReactiveFormsModule } from '@angular/forms'; +import { ZipfileExtractionComponent } from './common-module/zipfile-extraction/zipfile-extraction.component'; @NgModule({ declarations: [ - BlueprintComponent + BlueprintComponent, + ZipfileExtractionComponent ], imports: [ CommonModule, diff --git a/cds-ui/client/src/app/feature-modules/blueprint/common-module/zipfile-extraction/zipfile-extraction.component.html b/cds-ui/client/src/app/feature-modules/blueprint/common-module/zipfile-extraction/zipfile-extraction.component.html new file mode 100644 index 000000000..9b7f44b1c --- /dev/null +++ b/cds-ui/client/src/app/feature-modules/blueprint/common-module/zipfile-extraction/zipfile-extraction.component.html @@ -0,0 +1,20 @@ +<!-- +============LICENSE_START========================================== +=================================================================== +Copyright (C) 2019 IBM Intellectual Property. All rights reserved. +=================================================================== + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the License); +you may not use this software except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +============LICENSE_END============================================ +-->
\ No newline at end of file diff --git a/cds-ui/client/src/app/feature-modules/blueprint/common-module/zipfile-extraction/zipfile-extraction.component.scss b/cds-ui/client/src/app/feature-modules/blueprint/common-module/zipfile-extraction/zipfile-extraction.component.scss new file mode 100644 index 000000000..93f5c9dea --- /dev/null +++ b/cds-ui/client/src/app/feature-modules/blueprint/common-module/zipfile-extraction/zipfile-extraction.component.scss @@ -0,0 +1,20 @@ +/* +============LICENSE_START========================================== +=================================================================== +Copyright (C) 2019 IBM Intellectual Property. All rights reserved. +=================================================================== + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the License); +you may not use this software except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +============LICENSE_END============================================ +*/
\ No newline at end of file diff --git a/cds-ui/client/src/app/feature-modules/blueprint/common-module/zipfile-extraction/zipfile-extraction.component.spec.ts b/cds-ui/client/src/app/feature-modules/blueprint/common-module/zipfile-extraction/zipfile-extraction.component.spec.ts new file mode 100644 index 000000000..a6674caae --- /dev/null +++ b/cds-ui/client/src/app/feature-modules/blueprint/common-module/zipfile-extraction/zipfile-extraction.component.spec.ts @@ -0,0 +1,46 @@ +/* +============LICENSE_START========================================== +=================================================================== +Copyright (C) 2019 IBM Intellectual Property. All rights reserved. +=================================================================== + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the License); +you may not use this software except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +============LICENSE_END============================================ +*/ + +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ZipfileExtractionComponent } from './zipfile-extraction.component'; + +describe('ZipfileExtractionComponent', () => { + let component: ZipfileExtractionComponent; + let fixture: ComponentFixture<ZipfileExtractionComponent>; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ ZipfileExtractionComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(ZipfileExtractionComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/cds-ui/client/src/app/feature-modules/blueprint/common-module/zipfile-extraction/zipfile-extraction.component.ts b/cds-ui/client/src/app/feature-modules/blueprint/common-module/zipfile-extraction/zipfile-extraction.component.ts new file mode 100644 index 000000000..2683ff5f4 --- /dev/null +++ b/cds-ui/client/src/app/feature-modules/blueprint/common-module/zipfile-extraction/zipfile-extraction.component.ts @@ -0,0 +1,136 @@ +/* +============LICENSE_START========================================== +=================================================================== +Copyright (C) 2019 IBM Intellectual Property. All rights reserved. +=================================================================== + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the License); +you may not use this software except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +============LICENSE_END============================================ +*/ +import { Component, OnInit } from '@angular/core'; +import * as JSZip from 'jszip'; +import { SortPipe } from '../../../../common/shared/pipes/sort.pipe'; +import { LoaderService } from '../../../../common/core/services/loader.service'; + +@Component({ + selector: 'app-zipfile-extraction', + templateUrl: './zipfile-extraction.component.html', + styleUrls: ['./zipfile-extraction.component.scss'] +}) +export class ZipfileExtractionComponent implements OnInit { + private paths = []; + private tree; + private zipFile: JSZip = new JSZip(); + private fileObject: any; + private activationBlueprint: any; + private tocsaMetadaData: any; + private blueprintName: string; + private entryDefinition: string; + validfile: boolean = false; + uploadedFileName: string; + + constructor(private loader: LoaderService) { } + + ngOnInit() { + } + async buildFileViewData(zip) { + this.validfile = false; + this.paths = []; + console.log(zip.files); + for (var file in zip.files) { + console.log("name: " + zip.files[file].name); + this.fileObject = { + // nameForUIDisplay: this.uploadedFileName + '/' + zip.files[file].name, + // name: zip.files[file].name, + name: this.uploadedFileName + '/' + zip.files[file].name, + data: '' + }; + const value = <any>await zip.files[file].async('string'); + this.fileObject.data = value; + this.paths.push(this.fileObject); + } + + if (this.paths) { + this.paths.forEach(path => { + if (path.name.includes("TOSCA.meta")) { + this.validfile = true + } + }); + } else { + alert('Please update proper file'); + } + + if (this.validfile) { + this.fetchTOSACAMetadata(); + this.paths = new SortPipe().transform(this.paths, 'asc', 'name'); + this.tree = this.arrangeTreeData(this.paths); + } else { + alert('Please update proper file with TOSCA metadata'); + } + } + + arrangeTreeData(paths) { + const tree = []; + + paths.forEach((path) => { + + const pathParts = path.name.split('/'); + // pathParts.shift(); + let currentLevel = tree; + + pathParts.forEach((part) => { + const existingPath = currentLevel.filter(level => level.name === part); + + if (existingPath.length > 0) { + currentLevel = existingPath[0].children; + } else { + const newPart = { + name: part, + children: [], + data: path.data, + path: path.name + }; + if (part.trim() == this.blueprintName.trim()) { + this.activationBlueprint = path.data; + newPart.data = JSON.parse(this.activationBlueprint.toString()); + console.log('newpart', newPart); + this.entryDefinition = path.name.trim(); + } + if (newPart.name !== '') { + currentLevel.push(newPart); + currentLevel = newPart.children; + } + } + }); + }); + this.loader.hideLoader(); + return tree; + } + + fetchTOSACAMetadata() { + let toscaData = {}; + this.paths.forEach(file => { + if (file.name.includes('TOSCA.meta')) { + let keys = file.data.split("\n"); + keys.forEach((key) => { + let propertyData = key.split(':'); + toscaData[propertyData[0]] = propertyData[1]; + }); + } + }); + this.blueprintName = (((toscaData['Entry-Definitions']).split('/'))[1]).toString();; + console.log(toscaData); + } + +} diff --git a/cds-ui/client/src/app/feature-modules/blueprint/select-template/metadata/metadata.component.ts b/cds-ui/client/src/app/feature-modules/blueprint/select-template/metadata/metadata.component.ts index cefe0fd93..174bdf183 100644 --- a/cds-ui/client/src/app/feature-modules/blueprint/select-template/metadata/metadata.component.ts +++ b/cds-ui/client/src/app/feature-modules/blueprint/select-template/metadata/metadata.component.ts @@ -30,7 +30,7 @@ import { IBlueprint } from 'src/app/common/core/store/models/blueprint.model'; import { IMetaData } from '../../../../common/core/store/models/metadata.model'; import { SetBlueprintState } from 'src/app/common/core/store/actions/blueprint.action'; import { LoaderService } from '../../../../common/core/services/loader.service'; - +import { SelectTemplateService } from 'src/app/feature-modules/blueprint/select-template/select-template.service'; @Component({ selector: 'app-metadata', templateUrl: './metadata.component.html', @@ -48,8 +48,9 @@ export class MetadataComponent implements OnInit { blueprintName: string; uploadedFileName: string; entryDefinition: string; - - constructor(private formBuilder: FormBuilder, private store: Store<IAppState>, private loader: LoaderService) { + + constructor(private formBuilder: FormBuilder, private store: Store<IAppState>, + private loader: LoaderService, private dataService: SelectTemplateService) { this.bpState = this.store.select('blueprint'); this.CBAMetadataForm = this.formBuilder.group({ template_author: ['', Validators.required], @@ -62,6 +63,9 @@ export class MetadataComponent implements OnInit { } ngOnInit() { + this.dataService.getCbaOption().subscribe( + res => {console.log("data from service " + res);} + ); this.bpState.subscribe( blueprintdata => { var blueprintState: IBlueprintState = { blueprint: blueprintdata.blueprint, isLoadSuccess: blueprintdata.isLoadSuccess, isSaveSuccess: blueprintdata.isSaveSuccess, isUpdateSuccess: blueprintdata.isUpdateSuccess }; @@ -95,18 +99,22 @@ export class MetadataComponent implements OnInit { }); }) } - +ngAfterInit(){ + this.dataService.getCbaOption().subscribe( + res => {console.log("data from service after init" + res);} + ); +} UploadMetadata() { this.loader.showLoader(); this.metadata = Object.assign({}, this.CBAMetadataForm.value); this.blueprint.metadata = this.metadata; - if( this.blueprint && - this.blueprint['topology_template'] && - this.blueprint['topology_template'].workflows && - this.blueprint['topology_template'].workflows['resource-assignment'] && - this.blueprint['topology_template'].workflows['resource-assignment'].name) { - delete this.blueprint['topology_template'].workflows['resource-assignment'].name; - } + /*if (this.blueprint && + this.blueprint['topology_template'] && + this.blueprint['topology_template'].workflows && + this.blueprint['topology_template'].workflows['resource-assignment'] && + this.blueprint['topology_template'].workflows['resource-assignment'].name) { + delete this.blueprint['topology_template'].workflows['resource-assignment'].name; + }*/ this.filesData.forEach((fileNode) => { if (fileNode.name.includes(this.blueprintName) && fileNode.name == this.entryDefinition) { fileNode.data = JSON.stringify(this.blueprint, null, "\t"); diff --git a/cds-ui/client/src/app/feature-modules/blueprint/select-template/search-template/search-from-database/search-from-database.component.ts b/cds-ui/client/src/app/feature-modules/blueprint/select-template/search-template/search-from-database/search-from-database.component.ts index df3aafb73..588854f6b 100644 --- a/cds-ui/client/src/app/feature-modules/blueprint/select-template/search-template/search-from-database/search-from-database.component.ts +++ b/cds-ui/client/src/app/feature-modules/blueprint/select-template/search-template/search-from-database/search-from-database.component.ts @@ -21,10 +21,18 @@ limitations under the License. import { Component, OnInit, ViewChild, EventEmitter, Output } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; +import { Store } from '@ngrx/store'; import { SearchTemplateService } from '../search-template.service'; import { MatAutocompleteTrigger } from '@angular/material'; import { NotificationHandlerService } from 'src/app/common/core/services/notification-handler.service'; import { SearchPipe } from 'src/app/common/shared/pipes/search.pipe'; +import * as JSZip from 'jszip'; +import { SortPipe } from '../../../../../common/shared/pipes/sort.pipe'; +import { LoaderService } from '../../../../../common/core/services/loader.service'; +import { IBlueprint } from '../../../../../common/core/store/models/blueprint.model'; +import { IBlueprintState } from '../../../../../common/core/store/models/blueprintState.model'; +import { IAppState } from '../../../../../common/core/store/state/app.state'; +import { SetBlueprintState } from '../../../../../common/core/store/actions/blueprint.action'; @Component({ selector: 'app-search-from-database', @@ -36,11 +44,25 @@ export class SearchFromDatabaseComponent implements OnInit { myControl: FormGroup; @Output() resourcesData = new EventEmitter(); options: any[] = []; - @ViewChild('resourceSelect', { read: MatAutocompleteTrigger }) resourceSelect: MatAutocompleteTrigger; + // @ViewChild('resourceSelect', { read: MatAutocompleteTrigger }) resourceSelect: MatAutocompleteTrigger; + + validfile: boolean = false; + filesTree: any = []; + filesData: any = []; + private zipFile: JSZip = new JSZip(); + private paths = []; + private tree; + private fileObject: any; + private activationBlueprint: any; + private tocsaMetadaData: any; + private blueprintName: string; + private entryDefinition: string; + uploadedFileName: string; searchText: string = ''; constructor(private _formBuilder: FormBuilder, - private searchService: SearchTemplateService, private alertService: NotificationHandlerService, ) { } + private searchService: SearchTemplateService, private alertService: NotificationHandlerService, + private loader: LoaderService, private store: Store<IAppState>) { } ngOnInit() { this.myControl = this._formBuilder.group({ @@ -63,10 +85,137 @@ export class SearchFromDatabaseComponent implements OnInit { }) } - editCBA(artifactname: string,option : string) { - + editCBA(artifactName: string,artifactVersion:string, option: string) { + this.zipFile.generateAsync({ type: "blob" }) + .then(blob => { + const formData = new FormData(); + formData.append("file", blob); + // this.editorService.enrich("/enrich-blueprint/", formData) + this.searchService.getBlueprintZip(artifactName + "/" + artifactVersion) + .subscribe( + (response) => { + // console.log(response); + this.zipFile.files = {}; + this.zipFile.loadAsync(response) + .then((zip) => { + if (zip) { + this.buildFileViewData(zip); + // console.log("processed"); + let data: IBlueprint = this.activationBlueprint ? JSON.parse(this.activationBlueprint.toString()) : this.activationBlueprint; + let blueprintState = { + blueprint: data, + name: this.blueprintName, + files: this.tree, + filesData: this.paths, + uploadedFileName: this.blueprintName, + entryDefinition: this.entryDefinition + } + this.store.dispatch(new SetBlueprintState(blueprintState)); + // console.log(blueprintState); + } + }); + // this.alertService.success('Blueprint enriched successfully'); + }, + (error) => { + this.alertService.error('Blue print error' + error.message); + }); + }); + } + + create() { + this.filesData.forEach((path) => { + let index = path.name.indexOf("/"); + let name = path.name.slice(index + 1, path.name.length); + this.zipFile.file(name, path.data); + }); + } + + async buildFileViewData(zip) { + this.validfile = false; + this.paths = []; + // console.log(zip.files); + for (var file in zip.files) { + console.log("name: " + zip.files[file].name); + this.fileObject = { + // nameForUIDisplay: this.uploadedFileName + '/' + zip.files[file].name, + // name: zip.files[file].name, + name: this.uploadedFileName + '/' + zip.files[file].name, + data: '' + }; + const value = <any>await zip.files[file].async('string'); + this.fileObject.data = value; + this.paths.push(this.fileObject); + } + + if (this.paths) { + this.paths.forEach(path => { + if (path.name.includes("TOSCA.meta")) { + this.validfile = true + } + }); + } else { + alert('Please update proper file'); + } + + if (this.validfile) { + this.fetchTOSACAMetadata(); + this.paths = new SortPipe().transform(this.paths, 'asc', 'name'); + this.tree = this.arrangeTreeData(this.paths); + } else { + alert('Please update proper file with TOSCA metadata'); + } } + arrangeTreeData(paths) { + const tree = []; + paths.forEach((path) => { + const pathParts = path.name.split('/'); + // pathParts.shift(); + let currentLevel = tree; + + pathParts.forEach((part) => { + const existingPath = currentLevel.filter(level => level.name === part); + + if (existingPath.length > 0) { + currentLevel = existingPath[0].children; + } else { + const newPart = { + name: part, + children: [], + data: path.data, + path: path.name + }; + if (part.trim() == this.blueprintName.trim()) { + this.activationBlueprint = path.data; + newPart.data = JSON.parse(this.activationBlueprint.toString()); + // console.log('newpart', newPart); + this.entryDefinition = path.name.trim(); + } + if (newPart.name !== '') { + currentLevel.push(newPart); + currentLevel = newPart.children; + } + } + }); + }); + this.loader.hideLoader(); + return tree; + } + + fetchTOSACAMetadata() { + let toscaData = {}; + this.paths.forEach(file => { + if (file.name.includes('TOSCA.meta')) { + let keys = file.data.split("\n"); + keys.forEach((key) => { + let propertyData = key.split(':'); + toscaData[propertyData[0]] = propertyData[1]; + }); + } + }); + this.blueprintName = (((toscaData['Entry-Definitions']).split('/'))[1]).toString();; + // console.log(toscaData); + } } diff --git a/cds-ui/client/src/app/feature-modules/blueprint/select-template/search-template/search-template.module.ts b/cds-ui/client/src/app/feature-modules/blueprint/select-template/search-template/search-template.module.ts index 11029663b..9bafaebdc 100644 --- a/cds-ui/client/src/app/feature-modules/blueprint/select-template/search-template/search-template.module.ts +++ b/cds-ui/client/src/app/feature-modules/blueprint/select-template/search-template/search-template.module.ts @@ -26,6 +26,8 @@ import { SearchTemplateComponent } from './search-template.component'; import { ReactiveFormsModule } from '@angular/forms'; import { AppMaterialModule } from 'src/app/common/modules/app-material.module'; import { SharedModule} from 'src/app/common/shared/shared.module'; +import { SelectTemplateService } from 'src/app/feature-modules/blueprint/select-template/select-template.service'; + @NgModule({ declarations: [ SearchTemplateComponent, @@ -39,6 +41,7 @@ import { SharedModule} from 'src/app/common/shared/shared.module'; exports:[ SearchTemplateComponent, SearchFromDatabaseComponent - ] + ], + providers:[ SelectTemplateService] }) export class SearchTemplateModule { } diff --git a/cds-ui/client/src/app/feature-modules/blueprint/select-template/select-template.service.spec.ts b/cds-ui/client/src/app/feature-modules/blueprint/select-template/select-template.service.spec.ts new file mode 100644 index 000000000..e5e2d0256 --- /dev/null +++ b/cds-ui/client/src/app/feature-modules/blueprint/select-template/select-template.service.spec.ts @@ -0,0 +1,12 @@ +import { TestBed } from '@angular/core/testing'; + +import { SelectTemplateService } from './select-template.service'; + +describe('SelectTemplateService', () => { + beforeEach(() => TestBed.configureTestingModule({})); + + it('should be created', () => { + const service: SelectTemplateService = TestBed.get(SelectTemplateService); + expect(service).toBeTruthy(); + }); +}); diff --git a/cds-ui/client/src/app/feature-modules/blueprint/select-template/select-template.service.ts b/cds-ui/client/src/app/feature-modules/blueprint/select-template/select-template.service.ts new file mode 100644 index 000000000..fa18cbd25 --- /dev/null +++ b/cds-ui/client/src/app/feature-modules/blueprint/select-template/select-template.service.ts @@ -0,0 +1,40 @@ +/* +============LICENSE_START========================================== +=================================================================== +Copyright (C) 2019 IBM Intellectual Property. All rights reserved. +=================================================================== + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the License); +you may not use this software except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +============LICENSE_END============================================ +*/ + +import { Injectable } from '@angular/core'; +import { Observable, of } from 'rxjs'; + +@Injectable({ + providedIn: 'root' +}) +export class SelectTemplateService { + cbaOption: string; + + constructor() { } + + setCbaOption(option: string) { + this.cbaOption = option; + } + + getCbaOption(): Observable<string> { + return of(this.cbaOption); + } +} diff --git a/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/node_types.json b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/node_types.json index 5f0deeb98..cb9614eed 100644 --- a/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/node_types.json +++ b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/node_types.json @@ -24,8 +24,13 @@ "process" : { "inputs" : { "job-template-name" : { - "description" : "Job template to execute in AWX", - "required" : true, + "description" : "Primary key or name of the job template to launch new job.", + "required" : false, + "type" : "string" + }, + "workflow-job-template-id" : { + "description" : "Primary key (name not supported) of the workflow job template to launch new job.", + "required" : false, "type" : "string" }, "limit" : { diff --git a/components/model-catalog/blueprint-model/uat-blueprints/README.md b/components/model-catalog/blueprint-model/uat-blueprints/README.md index d6a335273..56cb32989 100644 --- a/components/model-catalog/blueprint-model/uat-blueprints/README.md +++ b/components/model-catalog/blueprint-model/uat-blueprints/README.md @@ -7,7 +7,7 @@ The BPP runs in an almost production-like configuration with some minor exceptio - It uses an embedded, in-memory, and initially empty H2 database, running in MySQL/MariaDB compatibility mode; - All external services are mocked. - + ## How it works? The UATs are declarative, data-driven tests implemented in YAML 1.1 documents. @@ -33,6 +33,62 @@ CDS project's `components/model-catalog/blueprint-model/uat-blueprints` director ## `uat.yaml` reference +The structure of an UAT YAML file could be documented using the Protobuf language as follows: + +```proto +message Uat { + message Path {} + message Json {} + + message Process { + required string name = 1; + required Json request = 2; + required Json expectedResponse = 3; + optional Json responseNormalizerSpec = 4; + } + + message Request { + required string method = 1; + required Path path = 2; + optional string contentType = 3 [default = None]; + optional Json body = 4; + } + + message Response { + optional int32 status = 1 [default = 200]; + optional Json body = 2; + } + + message Expectation { + required Request request = 1; + required Response response = 2; + } + + message ExternalService { + required string selector = 1; + repeated Expectation expectations = 2; // min cardinality = 1 + } + + repeated Process processes = 1; // min cardinality = 1 + repeated ExternalService externalServices = 2; // min cardinality = 0 +} + +``` + +The optional `responseNormalizerSpec` specifies transformations that may be needed to apply to the response +returned by BPP to get a full JSON representation. For example, it's possible to convert an string field "outer.inner" +into JSON using the following specification: + +```yaml + responseNormalizerSpec: + outer: + inner: ?from-json(.outer.inner) + +``` + +The "?" must prefix every expression that is NOT a literal string. The `from-json()` function and +many others are documented [here](https://github.com/schibsted/jslt/blob/0.1.8/functions.md). + ### Skeleton of a basic `uat.yaml` ```yaml @@ -93,16 +149,16 @@ external-services: ### Composite URI paths -In case your YAML document contains many URI path definitions, you'd better keep the duplications +In case your YAML document contains many URI path definitions, it's recommended to keep the duplications as low as possible in order to ease the document maintenance, and avoid inconsistencies. - + Since YAML doesn't provide a standard mechanism to concatenate strings, the UAT engine implements an ad-hoc mechanism based on multi-level lists. Please note that currently this mechanism is only applied to URI paths. To exemplify how it works, let's take the case of eliminating duplications when defining multiple OpenDaylight URLs. -You might starting using the following definitions: +You might start using the following definitions: ```yaml nodeId: &nodeId "new-netconf-device" # ... @@ -127,7 +183,7 @@ The UAT engine will expand the above multi-level lists, resulting on the followi # ... - request: path: restconf/config/network-topology:network-topology/topology/topology-netconf/node/new-netconf-device/yang-ext:mount/mynetconf:netconflist -``` +``` ## License @@ -135,9 +191,7 @@ Copyright (C) 2019 Nordix Foundation. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 +You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, diff --git a/components/model-catalog/blueprint-model/uat-blueprints/pnf_config/Tests/uat.yaml b/components/model-catalog/blueprint-model/uat-blueprints/pnf_config/Tests/uat.yaml index 37029e181..789659eb2 100644 --- a/components/model-catalog/blueprint-model/uat-blueprints/pnf_config/Tests/uat.yaml +++ b/components/model-catalog/blueprint-model/uat-blueprints/pnf_config/Tests/uat.yaml @@ -52,6 +52,11 @@ processes: target: / value: { netconflist: { netconf: [ { netconf-id: "30", netconf-param: "3000" }]}} status: success + responseNormalizerSpec: + stepData: + properties: + resource-assignment-params: + config-assign: ?from-json(.stepData.properties.resource-assignment-params.config-assign) - name: config-deploy request: commonHeader: *commonHeader diff --git a/components/model-catalog/definition-type/starter-type/node_type/component-config-snapshots-executor.json b/components/model-catalog/definition-type/starter-type/node_type/component-config-snapshots-executor.json index 1cc366637..caae68eef 100644 --- a/components/model-catalog/definition-type/starter-type/node_type/component-config-snapshots-executor.json +++ b/components/model-catalog/definition-type/starter-type/node_type/component-config-snapshots-executor.json @@ -13,6 +13,10 @@ "config-snapshot-value": { "required": false, "type": "string" + }, + "response-data": { + "required": false, + "type": "json" } }, "capabilities": { diff --git a/components/model-catalog/definition-type/starter-type/node_type/component-jython-executor.json b/components/model-catalog/definition-type/starter-type/node_type/component-jython-executor.json index b78a7c63a..ae674f3ec 100644 --- a/components/model-catalog/definition-type/starter-type/node_type/component-jython-executor.json +++ b/components/model-catalog/definition-type/starter-type/node_type/component-jython-executor.json @@ -1,6 +1,12 @@ { "description": "This is Jython Execution Component.", "version": "1.0.0", + "attributes": { + "response-data": { + "required": false, + "type": "json" + } + }, "capabilities": { "component-node": { "type": "tosca.capabilities.Node" diff --git a/components/model-catalog/definition-type/starter-type/node_type/component-netconf-executor.json b/components/model-catalog/definition-type/starter-type/node_type/component-netconf-executor.json index 3233d2121..e72cf951b 100644 --- a/components/model-catalog/definition-type/starter-type/node_type/component-netconf-executor.json +++ b/components/model-catalog/definition-type/starter-type/node_type/component-netconf-executor.json @@ -1,6 +1,12 @@ {
"description": "This is Netconf Transaction Configuration Component API",
"version": "1.0.0",
+ "attributes": {
+ "response-data": {
+ "required": false,
+ "type": "json"
+ }
+ },
"capabilities": {
"component-node": {
"type": "tosca.capabilities.Node"
diff --git a/components/model-catalog/definition-type/starter-type/node_type/component-remote-ansible-executor.json b/components/model-catalog/definition-type/starter-type/node_type/component-remote-ansible-executor.json index 498db8246..f5d9d3f7a 100644 --- a/components/model-catalog/definition-type/starter-type/node_type/component-remote-ansible-executor.json +++ b/components/model-catalog/definition-type/starter-type/node_type/component-remote-ansible-executor.json @@ -9,6 +9,10 @@ "ansible-command-logs": { "required": true, "type": "string" + }, + "response-data": { + "required": false, + "type": "json" } }, "capabilities": { @@ -26,6 +30,11 @@ "required": true, "type": "string" }, + "workflow-job-template-id": { + "description": "Primary key (name not supported) of the workflow job template to launch new job.", + "required": false, + "type": "string" + }, "limit": { "description": "Specify host limit for job template to run.", "required": false, diff --git a/components/model-catalog/definition-type/starter-type/node_type/component-script-executor.json b/components/model-catalog/definition-type/starter-type/node_type/component-script-executor.json index b241aa36f..22596020f 100644 --- a/components/model-catalog/definition-type/starter-type/node_type/component-script-executor.json +++ b/components/model-catalog/definition-type/starter-type/node_type/component-script-executor.json @@ -1,6 +1,12 @@ { "description": "This is CLI Transaction Configuration Component API", "version": "1.0.0", + "attributes": { + "response-data": { + "required": false, + "type": "json" + } + }, "capabilities": { "component-node": { "type": "tosca.capabilities.Node" diff --git a/components/scripts/python/ccsdk_netconf/common.py b/components/scripts/python/ccsdk_netconf/common.py index f7ac1ac35..66c7a98b5 100644 --- a/components/scripts/python/ccsdk_netconf/common.py +++ b/components/scripts/python/ccsdk_netconf/common.py @@ -26,3 +26,4 @@ class ResolutionHelper: def retrieve_resolved_template_from_database(self, key, artifact_template): return ResourceResolutionExtensionsKt.storedContentFromResolvedArtifact(self.component_function, key, artifact_template) + diff --git a/components/scripts/python/ccsdk_netconf/netconf_constant.py b/components/scripts/python/ccsdk_netconf/netconf_constant.py index 534ca9e13..52ac0ae5a 100644 --- a/components/scripts/python/ccsdk_netconf/netconf_constant.py +++ b/components/scripts/python/ccsdk_netconf/netconf_constant.py @@ -8,6 +8,8 @@ PARAM_ACTION = "action" STATUS_SUCCESS = "success" STATUS_FAILURE = "failure" +ATTRIBUTE_RESPONSE_DATA = "response-data" + CONFIG_TARGET_RUNNING = "running" CONFIG_TARGET_CANDIDATE = "candidate" CONFIG_DEFAULT_OPERATION_MERGE = "merge" diff --git a/components/scripts/python/ccsdk_netconf/netconfclient.py b/components/scripts/python/ccsdk_netconf/netconfclient.py index a942845b9..b3aef11f4 100644 --- a/components/scripts/python/ccsdk_netconf/netconfclient.py +++ b/components/scripts/python/ccsdk_netconf/netconfclient.py @@ -1,5 +1,4 @@ -from netconf_constant import CONFIG_TARGET_RUNNING, CONFIG_TARGET_CANDIDATE, \ - CONFIG_DEFAULT_OPERATION_REPLACE +from netconf_constant import * from org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor import NetconfExecutorExtensionsKt @@ -60,3 +59,6 @@ class NetconfClient: def discard_change(self): device_response = self.netconf_rpc_client.discardConfig() return device_response + + def set_execution_attribute_response_data(self, response_data): + self.setAttribute(ATTRIBUTE_RESPONSE_DATA, response_data) diff --git a/components/scripts/python/ccsdk_restconf/restconf_client.py b/components/scripts/python/ccsdk_restconf/restconf_client.py index 927c1fedd..6e53dcf93 100644 --- a/components/scripts/python/ccsdk_restconf/restconf_client.py +++ b/components/scripts/python/ccsdk_restconf/restconf_client.py @@ -18,6 +18,7 @@ # ============LICENSE_END========================================================= # from time import sleep +from restconf_constant import * from org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor import RestconfExecutorExtensionsKt from org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution import ResourceResolutionExtensionsKt @@ -92,3 +93,6 @@ class RestconfClient: url = self.__base_odl_url + nf_id self.__log.debug("sending unmount request, url: {}", url) web_client_service.exchangeResource("DELETE", url, "") + + def set_execution_attribute_response_data(self, response_data): + self.setAttribute(ATTRIBUTE_RESPONSE_DATA, response_data) diff --git a/components/scripts/python/ccsdk_restconf/restconf_constant.py b/components/scripts/python/ccsdk_restconf/restconf_constant.py new file mode 100644 index 000000000..2e1c58312 --- /dev/null +++ b/components/scripts/python/ccsdk_restconf/restconf_constant.py @@ -0,0 +1 @@ +ATTRIBUTE_RESPONSE_DATA = "response-data"
\ No newline at end of file diff --git a/ms/blueprintsprocessor/application/pom.xml b/ms/blueprintsprocessor/application/pom.xml index 314b09c42..120b948be 100755 --- a/ms/blueprintsprocessor/application/pom.xml +++ b/ms/blueprintsprocessor/application/pom.xml @@ -132,6 +132,12 @@ <scope>test</scope> </dependency> <dependency> + <groupId>com.schibsted.spt.data</groupId> + <artifactId>jslt</artifactId> + <version>0.1.8</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </dependency> diff --git a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/BlueprintsAcceptanceTests.kt b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/BlueprintsAcceptanceTest.kt index 0a57277ea..ad4173c9e 100644 --- a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/BlueprintsAcceptanceTests.kt +++ b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/BlueprintsAcceptanceTest.kt @@ -19,7 +19,9 @@ */ package org.onap.ccsdk.cds.blueprintsprocessor +import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.node.MissingNode import com.nhaarman.mockitokotlin2.any import com.nhaarman.mockitokotlin2.argThat import com.nhaarman.mockitokotlin2.atLeast @@ -53,15 +55,17 @@ import org.springframework.test.context.ContextConfiguration import org.springframework.test.context.TestPropertySource import org.springframework.test.context.junit4.rules.SpringClassRule import org.springframework.test.context.junit4.rules.SpringMethodRule +import org.springframework.test.web.reactive.server.EntityExchangeResult import org.springframework.test.web.reactive.server.WebTestClient -import org.yaml.snakeyaml.Yaml import reactor.core.publisher.Mono import java.io.File -import java.nio.file.Path +import java.nio.charset.StandardCharsets import java.nio.file.Paths import kotlin.test.BeforeTest import kotlin.test.Test +// Only one runner can be configured with jUnit 4. We had to replace the SpringRunner by equivalent jUnit rules. +// See more on https://docs.spring.io/autorepo/docs/spring-framework/current/spring-framework-reference/testing.html#testcontext-junit4-rules @RunWith(Parameterized::class) // Set blueprintsprocessor.httpPort=0 to trigger a random port selection @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @@ -71,8 +75,7 @@ import kotlin.test.Test TestSecuritySettings.ServerContextInitializer::class ]) @TestPropertySource(locations = ["classpath:application-test.properties"]) -@Suppress("UNCHECKED_CAST") -class BlueprintsAcceptanceTests(private val blueprintName: String, private val filename: String) { +class BlueprintsAcceptanceTest(private val blueprintName: String, private val filename: String) { companion object { const val UAT_BLUEPRINTS_BASE_DIR = "../../../components/model-catalog/blueprint-model/uat-blueprints" @@ -82,11 +85,15 @@ class BlueprintsAcceptanceTests(private val blueprintName: String, private val f @JvmField val springClassRule = SpringClassRule() - val log: Logger = LoggerFactory.getLogger(BlueprintsAcceptanceTests::class.java) + val log: Logger = LoggerFactory.getLogger(BlueprintsAcceptanceTest::class.java) + /** + * Generates the parameters to create a test instance for every blueprint found under UAT_BLUEPRINTS_BASE_DIR + * that contains the proper UAT definition file. + */ @Parameterized.Parameters(name = "{index} {0}") @JvmStatic - fun filenames(): List<Array<String>> { + fun testParameters(): List<Array<String>> { return File(UAT_BLUEPRINTS_BASE_DIR) .listFiles { file -> file.isDirectory && File(file, EMBEDDED_UAT_FILE).isFile } ?.map { file -> arrayOf(file.nameWithoutExtension, file.canonicalPath) } @@ -119,38 +126,31 @@ class BlueprintsAcceptanceTests(private val blueprintName: String, private val f @Test fun testBlueprint() { - val yaml: Map<String, *> = loadYaml(Paths.get(filename, EMBEDDED_UAT_FILE)) + val uat = UatDefinition.load(mapper, Paths.get(filename, EMBEDDED_UAT_FILE)) uploadBlueprint(blueprintName) // Configure mocked external services - val services = yaml["external-services"] as List<Map<String, *>>? ?: emptyList() - val expectationPerClient = services.map { service -> - val selector = service["selector"] as String - val expectations = (service["expectations"] as List<Map<String, *>>).map { - parseExpectation(it) - } - val mockClient = createRestClientMock(selector, expectations) - mockClient to expectations - }.toMap() + val expectationPerClient = uat.externalServices.associateBy( + { service -> createRestClientMock(service.selector, service.expectations) }, + { service -> service.expectations } + ) // Run processes - for (process in (yaml["processes"] as List<Map<String, *>>)) { - val processName = process["name"] - log.info("Executing process '$processName'") - val request = mapper.writeValueAsString(process["request"]) - val expectedResponse = mapper.writeValueAsString(process["expectedResponse"]) - processBlueprint(request, expectedResponse) + for (process in uat.processes) { + log.info("Executing process '${process.name}'") + processBlueprint(process.request, process.expectedResponse, + JsonNormalizer.getNormalizer(mapper, process.responseNormalizerSpec)) } - // Validate request payloads + // Validate request payloads to external services for ((mockClient, expectations) in expectationPerClient) { expectations.forEach { expectation -> verify(mockClient, atLeastOnce()).exchangeResource( - eq(expectation.method), - eq(expectation.path), - argThat { assertJsonEqual(expectation.expectedRequestBody, this) }, - expectation.requestHeadersMatcher()) + eq(expectation.request.method), + eq(expectation.request.path), + argThat { assertJsonEqual(expectation.request.body, this) }, + expectation.request.requestHeadersMatcher()) } // Don't mind the invocations to the overloaded exchangeResource(String, String, String) verify(mockClient, atLeast(0)).exchangeResource(any(), any(), any()) @@ -158,7 +158,8 @@ class BlueprintsAcceptanceTests(private val blueprintName: String, private val f } } - private fun createRestClientMock(selector: String, restExpectations: List<RestExpectation>): BlueprintWebClientService { + private fun createRestClientMock(selector: String, restExpectations: List<ExpectationDefinition>) + : BlueprintWebClientService { val restClient = mock<BlueprintWebClientService>(verboseLogging = true) // Delegates to overloaded exchangeResource(String, String, String, Map<String, String>) @@ -171,11 +172,11 @@ class BlueprintsAcceptanceTests(private val blueprintName: String, private val f } for (expectation in restExpectations) { whenever(restClient.exchangeResource( - eq(expectation.method), - eq(expectation.path), + eq(expectation.request.method), + eq(expectation.request.path), any(), any())) - .thenReturn(WebClientResponse(expectation.statusCode, expectation.responseBody)) + .thenReturn(WebClientResponse(expectation.response.status, expectation.response.body.toString())) } whenever(restClientFactory.blueprintWebClientService(selector)) @@ -194,17 +195,20 @@ class BlueprintsAcceptanceTests(private val blueprintName: String, private val f .expectStatus().isOk } - private fun processBlueprint(request: String, expectedResponse: String) { + private fun processBlueprint(request: JsonNode, expectedResponse: JsonNode, + responseNormalizer: (String) -> String) { webTestClient .post() .uri("/api/v1/execution-service/process") .header("Authorization", TestSecuritySettings.clientAuthToken()) .contentType(MediaType.APPLICATION_JSON_UTF8) - .body(Mono.just(request), String::class.java) + .body(Mono.just(request.toString()), String::class.java) .exchange() .expectStatus().isOk .expectBody() - .json(expectedResponse) + .consumeWith { response -> + assertJsonEqual(expectedResponse, responseNormalizer(getBodyAsString(response))) + } } private fun getBlueprintAsResource(blueprintName: String): Resource { @@ -216,65 +220,21 @@ class BlueprintsAcceptanceTests(private val blueprintName: String, private val f } } - private fun loadYaml(path: Path): Map<String, Any> { - return path.toFile().reader().use { reader -> - Yaml().load(reader) - } - } - - private fun assertJsonEqual(expected: Any, actual: String): Boolean { - if (actual != expected) { - // assertEquals throws an exception whenever match fails - JSONAssert.assertEquals(mapper.writeValueAsString(expected), actual, JSONCompareMode.LENIENT) + private fun assertJsonEqual(expected: JsonNode, actual: String): Boolean { + if ((actual == "") && (expected is MissingNode)) { + return true } + JSONAssert.assertEquals(expected.toString(), actual, JSONCompareMode.LENIENT) + // assertEquals throws an exception whenever match fails return true } - private fun parseExpectation(expectation: Map<String, *>): RestExpectation { - val request = expectation["request"] as Map<String, Any> - val method = request["method"] as String - val path = joinPath(request.getValue("path")) - val contentType = request["content-type"] as String? - val requestBody = request.getOrDefault("body", "") - - val response = expectation["response"] as Map<String, Any>? ?: emptyMap() - val status = response["status"] as Int? ?: 200 - val responseBody = when (val body = response["body"] ?: "") { - is String -> body - else -> mapper.writeValueAsString(body) - } - - return RestExpectation(method, path, contentType, requestBody, status, responseBody) - } - - /** - * Join a multilevel lists of strings. - * Example: joinPath(listOf("a", listOf("b", "c"), "d")) will result in "a/b/c/d". - */ - private fun joinPath(any: Any): String { - fun recursiveJoin(any: Any, sb: StringBuilder): StringBuilder { - when (any) { - is List<*> -> any.filterNotNull().forEach { recursiveJoin(it, sb) } - is String -> { - if (sb.isNotEmpty()) { - sb.append('/') - } - sb.append(any) - } - else -> throw IllegalArgumentException("Unsupported type: ${any.javaClass}") - } - return sb - } - - return recursiveJoin(any, StringBuilder()).toString() - } - - data class RestExpectation(val method: String, val path: String, val contentType: String?, - val expectedRequestBody: Any, - val statusCode: Int, val responseBody: String) { - - fun requestHeadersMatcher(): Map<String, String> { - return if (contentType != null) eq(mapOf("Content-Type" to contentType)) else any() + private fun getBodyAsString(result: EntityExchangeResult<ByteArray>): String { + val body = result.responseBody + if ((body == null) || body.isEmpty()) { + return "" } + val charset = result.responseHeaders.contentType?.charset ?: StandardCharsets.UTF_8 + return String(body, charset) } }
\ No newline at end of file diff --git a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ExtendedTemporaryFolder.kt b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ExtendedTemporaryFolder.kt index 4576f2761..3c517e6ac 100644 --- a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ExtendedTemporaryFolder.kt +++ b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ExtendedTemporaryFolder.kt @@ -1,3 +1,22 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ package org.onap.ccsdk.cds.blueprintsprocessor import org.junit.rules.TemporaryFolder diff --git a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/JsonNormalizer.kt b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/JsonNormalizer.kt new file mode 100644 index 000000000..69673f931 --- /dev/null +++ b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/JsonNormalizer.kt @@ -0,0 +1,79 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.ccsdk.cds.blueprintsprocessor + +import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.node.ContainerNode +import com.fasterxml.jackson.databind.node.MissingNode +import com.fasterxml.jackson.databind.node.ObjectNode +import com.schibsted.spt.data.jslt.Parser + +class JsonNormalizer { + + companion object { + + fun getNormalizer(mapper: ObjectMapper, jsltSpec: JsonNode): (String) -> String { + if (jsltSpec is MissingNode) { + return { it } + } + return { s: String -> + val input = mapper.readTree(s) + val expandedJstlSpec = expandJstlSpec(jsltSpec) + val jslt = Parser.compileString(expandedJstlSpec) + val output = jslt.apply(input) + output.toString() + } + } + + /** + * Creates an extended JSTL spec by appending the "*: ." wildcard pattern to every inner JSON object, and + * removing the extra quotes added by the standard YAML/JSON converters on fields prefixed by "?". + * + * @param jstlSpec the JSTL spec as a structured JSON object. + * @return the string representation of the extended JSTL spec. + */ + private fun expandJstlSpec(jstlSpec: JsonNode): String { + val extendedJstlSpec = updateObjectNodes(jstlSpec, "*", ".") + return extendedJstlSpec.toString() + // Handle the "?" as a prefix to literal/non-quoted values + .replace("\"\\?([^\"]+)\"".toRegex(), "$1") + // Also, remove the quotes added by Jackson for key and value of the wildcard matcher + .replace("\"([.*])\"".toRegex(), "$1") + } + + /** + * Expands a structured JSON object, by adding the given key and value to every nested ObjectNode. + * + * @param jsonNode the root node. + * @param fieldName the fixed field name. + * @param fieldValue the fixed field value. + */ + private fun updateObjectNodes(jsonNode: JsonNode, fieldName: String, fieldValue: String): JsonNode { + if (jsonNode is ContainerNode<*>) { + (jsonNode as? ObjectNode)?.put(fieldName, fieldValue) + jsonNode.forEach { child -> + updateObjectNodes(child, fieldName, fieldValue) + } + } + return jsonNode + } + } +} diff --git a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/PathDeserializer.kt b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/PathDeserializer.kt new file mode 100644 index 000000000..1a232f2d3 --- /dev/null +++ b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/PathDeserializer.kt @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.ccsdk.cds.blueprintsprocessor + +import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.deser.std.StdDeserializer + +class PathDeserializer : StdDeserializer<String>(String::class.java) { + override fun deserialize(jp: JsonParser, ctxt: DeserializationContext?): String { + val path = jp.codec.readValue(jp, Any::class.java) + return flatJoin(path) + } + + /** + * Join a multilevel lists of strings. + * Example: flatJoin(listOf("a", listOf("b", "c"), "d")) will result in "a/b/c/d". + */ + private fun flatJoin(path: Any): String { + fun flatJoinTo(sb: StringBuilder, path: Any): StringBuilder { + when (path) { + is List<*> -> path.filterNotNull().forEach { flatJoinTo(sb, it) } + is String -> { + if (sb.isNotEmpty()) { + sb.append('/') + } + sb.append(path) + } + else -> throw IllegalArgumentException("Unsupported type: ${path.javaClass}") + } + return sb + } + return flatJoinTo(StringBuilder(), path).toString() + } +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/UatDefinition.kt b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/UatDefinition.kt new file mode 100644 index 000000000..ce2061168 --- /dev/null +++ b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/UatDefinition.kt @@ -0,0 +1,68 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.ccsdk.cds.blueprintsprocessor + +import com.fasterxml.jackson.annotation.JsonAlias +import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.annotation.JsonDeserialize +import com.fasterxml.jackson.databind.node.MissingNode +import com.nhaarman.mockitokotlin2.any +import com.nhaarman.mockitokotlin2.eq +import org.yaml.snakeyaml.Yaml +import java.nio.file.Path + +data class ProcessDefinition(val name: String, val request: JsonNode, val expectedResponse: JsonNode, + val responseNormalizerSpec: JsonNode = MissingNode.getInstance()) + +data class RequestDefinition(val method: String, + @JsonDeserialize(using = PathDeserializer::class) + val path: String, + @JsonAlias("content-type") + val contentType: String? = null, + val body: JsonNode = MissingNode.getInstance()) { + fun requestHeadersMatcher(): Map<String, String> { + return if (contentType != null) eq(mapOf("Content-Type" to contentType)) else any() + } +} + +data class ResponseDefinition(val status: Int = 200, val body: JsonNode = MissingNode.getInstance()) { + companion object { + val DEFAULT_RESPONSE = ResponseDefinition() + } +} + +data class ExpectationDefinition(val request: RequestDefinition, + val response: ResponseDefinition = ResponseDefinition.DEFAULT_RESPONSE) + +data class ServiceDefinition(val selector: String, val expectations: List<ExpectationDefinition>) + +data class UatDefinition(val processes: List<ProcessDefinition>, + @JsonAlias("external-services") + val externalServices: List<ServiceDefinition> = emptyList()) { + + companion object { + fun load(mapper: ObjectMapper, path: Path): UatDefinition { + return path.toFile().reader().use { reader -> + mapper.convertValue(Yaml().load(reader), UatDefinition::class.java) + } + } + } +} diff --git a/ms/blueprintsprocessor/functions/ansible-awx-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutor.kt b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutor.kt index 947a9630d..743aa714b 100644 --- a/ms/blueprintsprocessor/functions/ansible-awx-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutor.kt +++ b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutor.kt @@ -24,10 +24,7 @@ import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInpu import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction -import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive -import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString -import org.onap.ccsdk.cds.controllerblueprints.core.isNotNull -import org.onap.ccsdk.cds.controllerblueprints.core.rootFieldsToMap +import org.onap.ccsdk.cds.controllerblueprints.core.* import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.slf4j.LoggerFactory import org.springframework.beans.factory.config.ConfigurableBeanFactory @@ -68,6 +65,7 @@ open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertySe // input fields names accepted by this executor const val INPUT_ENDPOINT_SELECTOR = "endpoint-selector" const val INPUT_JOB_TEMPLATE_NAME = "job-template-name" + const val INPUT_WORKFLOW_JOB_TEMPLATE_NAME = "workflow-job-template-id" const val INPUT_LIMIT_TO_HOST = "limit" const val INPUT_INVENTORY = "inventory" const val INPUT_EXTRA_VARS = "extra-vars" @@ -85,12 +83,20 @@ open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertySe try { val restClientService = getAWXRestClient() - val jobTemplateName = getOperationInput(INPUT_JOB_TEMPLATE_NAME).asText() - val jtId = lookupJobTemplateIDByName(restClientService, jobTemplateName) + // Get either a job template name or a workflow template name property + var workflowURIPrefix = "" + var jobTemplateName = getOperationInput(INPUT_JOB_TEMPLATE_NAME).returnNullIfMissing()?.textValue() ?: "" + val isWorkflowJT = jobTemplateName.isBlank() + if (isWorkflowJT) { + jobTemplateName = getOperationInput(INPUT_WORKFLOW_JOB_TEMPLATE_NAME).asText() + workflowURIPrefix = "workflow_" + } + + val jtId = lookupJobTemplateIDByName(restClientService, jobTemplateName, workflowURIPrefix) if (jtId.isNotEmpty()) { - runJobTemplateOnAWX(restClientService, jobTemplateName, jtId) + runJobTemplateOnAWX(restClientService, jobTemplateName, jtId, workflowURIPrefix) } else { - val message = "Job template ${jobTemplateName} does not exists" + val message = "Workflow/Job template ${jobTemplateName} does not exists" log.error(message) setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message) } @@ -135,9 +141,10 @@ open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertySe /** * Finds the job template ID based on the job template name provided in the request */ - private fun lookupJobTemplateIDByName(awxClient: BlueprintWebClientService, job_template_name: String?): String { + private fun lookupJobTemplateIDByName(awxClient: BlueprintWebClientService, job_template_name: String?, + workflowPrefix : String) : String { val encodedJTName = URI(null, null, - "/api/v2/job_templates/${job_template_name}/", + "/api/v2/${workflowPrefix}job_templates/${job_template_name}/", null, null).rawPath // Get Job Template details by name @@ -152,19 +159,20 @@ open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertySe * its execution. Finally, it retrieves the job results via the stdout api. * The status and output attributes are populated in the process. */ - private fun runJobTemplateOnAWX(awxClient: BlueprintWebClientService, job_template_name: String?, jtId: String) { + private fun runJobTemplateOnAWX(awxClient: BlueprintWebClientService, job_template_name: String?, jtId: String, + workflowPrefix : String) { setNodeOutputProperties("preparing".asJsonPrimitive(), "".asJsonPrimitive()) // Get Job Template requirements - var response = awxClient.exchangeResource(GET, "/api/v2/job_templates/${jtId}/launch/", "") + var response = awxClient.exchangeResource(GET, "/api/v2/${workflowPrefix}job_templates/${jtId}/launch/", "") // FIXME: handle non-successful SC val jtLaunchReqs: JsonNode = mapper.readTree(response.body) - val payload = prepareLaunchPayload(awxClient, jtLaunchReqs) + val payload = prepareLaunchPayload(awxClient, jtLaunchReqs, workflowPrefix.isBlank()) log.info("Running job with $payload, for requestId $processId.") // Launch the job for the targeted template var jtLaunched: JsonNode = JacksonUtils.objectMapper.createObjectNode() - response = awxClient.exchangeResource(POST, "/api/v2/job_templates/${jtId}/launch/", payload) + response = awxClient.exchangeResource(POST, "/api/v2/${workflowPrefix}job_templates/${jtId}/launch/", payload) if (response.status in HTTP_SUCCESS) { jtLaunched = mapper.readTree(response.body) val fieldsIgnored: JsonNode = jtLaunched.at("/ignored_fields") @@ -180,7 +188,7 @@ open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertySe var jobStatus = "unknown" var jobEndTime = "null" while (jobEndTime == "null") { - response = awxClient.exchangeResource(GET, "/api/v2/jobs/${jobId}/", "") + response = awxClient.exchangeResource(GET, "/api/v2/${workflowPrefix}jobs/${jobId}/", "") val jobLaunched: JsonNode = mapper.readTree(response.body) jobStatus = jobLaunched.at("/status").asText() jobEndTime = jobLaunched.at("/finished").asText() @@ -189,12 +197,10 @@ open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertySe log.info("Execution of job template $job_template_name in job #$jobId finished with status ($jobStatus) for requestId $processId") - // Get job execution results (stdout) - val plainTextHeaders = mutableMapOf<String, String>() - plainTextHeaders["Content-Type"] = "text/plain ;utf-8" - response = awxClient.exchangeResource(GET, "/api/v2/jobs/${jobId}/stdout/?format=txt", "", plainTextHeaders) + // Get workflow/job execution results + val collectedOutput = extractJobRunResponse(awxClient, jobId, workflowPrefix) - setNodeOutputProperties(jobStatus.asJsonPrimitive(), response.body.asJsonPrimitive()) + setNodeOutputProperties(jobStatus.asJsonPrimitive(), collectedOutput.asJsonPrimitive()) } else { // The job template requirements were not fulfilled with the values passed in. The message below will // provide more information via the response, like the ignored_fields, or variables_needed_to_start, @@ -207,42 +213,77 @@ open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertySe } /** + * Extracts the response from either a job stdout call OR collects the workflow run output + */ + private fun extractJobRunResponse(awxClient: BlueprintWebClientService, jobId: String, workflowPrefix: String): String { + + // First, collect all job ID from either the job template run or the workflow nodes that ran + var jobIds : Array<String> + var collectedResponses = StringBuilder() + if (workflowPrefix.isNotEmpty()) { + var response = awxClient.exchangeResource(GET, "/api/v2/${workflowPrefix}jobs/${jobId}/workflow_nodes/", "") + val jobDetails = mapper.readTree(response.body).at("/results") + jobIds = emptyArray() + for (jobDetail in jobDetails.elements()) { + jobIds = jobIds.plus( jobDetail.at("/summary_fields/job/id").asText() ) + } + } else { + jobIds = arrayOf(jobId) + } + + // Then collect the response text from the corresponding jobIds + val plainTextHeaders = mutableMapOf<String, String>() + plainTextHeaders["Content-Type"] = "text/plain ;utf-8" + for (aJobId in jobIds) { + var response = awxClient.exchangeResource(GET, "/api/v2/jobs/${aJobId}/stdout/?format=txt", "", plainTextHeaders) + collectedResponses.append("Output for job ${aJobId}:") + collectedResponses.append(response.body) + } + return collectedResponses.toString() + } + + /** * Prepares the JSON payload expected by the job template api, * by applying the overrides that were provided * and allowed by the template definition flags in jtLaunchReqs */ - private fun prepareLaunchPayload(awxClient: BlueprintWebClientService, jtLaunchReqs: JsonNode): String { + private fun prepareLaunchPayload(awxClient: BlueprintWebClientService, jtLaunchReqs: JsonNode, + isWorkflow : Boolean): String { val payload = JacksonUtils.objectMapper.createObjectNode() // Parameter defaults - val limitProp = getOptionalOperationInput(INPUT_LIMIT_TO_HOST) - val tagsProp = getOptionalOperationInput(INPUT_TAGS) - val skipTagsProp = getOptionalOperationInput(INPUT_SKIP_TAGS) val inventoryProp = getOptionalOperationInput(INPUT_INVENTORY) val extraArgs = getOperationInput(INPUT_EXTRA_VARS) - val askLimitOnLaunch = jtLaunchReqs.at("/ask_limit_on_launch").asBoolean() - if (askLimitOnLaunch && limitProp.isNotNull()) { - payload.set(INPUT_LIMIT_TO_HOST, limitProp) - } - val askTagsOnLaunch = jtLaunchReqs.at("/ask_tags_on_launch").asBoolean() - if (askTagsOnLaunch && tagsProp.isNotNull()) { - payload.set(INPUT_TAGS, tagsProp) - } - if (askTagsOnLaunch && skipTagsProp.isNotNull()) { - payload.set("skip_tags", skipTagsProp) + if (!isWorkflow) { + val limitProp = getOptionalOperationInput(INPUT_LIMIT_TO_HOST) + val tagsProp = getOptionalOperationInput(INPUT_TAGS) + val skipTagsProp = getOptionalOperationInput(INPUT_SKIP_TAGS) + + val askLimitOnLaunch = jtLaunchReqs.at("/ask_limit_on_launch").asBoolean() + if (askLimitOnLaunch && limitProp.isNotNull()) { + payload.set(INPUT_LIMIT_TO_HOST, limitProp) + } + val askTagsOnLaunch = jtLaunchReqs.at("/ask_tags_on_launch").asBoolean() + if (askTagsOnLaunch && tagsProp.isNotNull()) { + payload.set(INPUT_TAGS, tagsProp) + } + if (askTagsOnLaunch && skipTagsProp.isNotNull()) { + payload.set("skip_tags", skipTagsProp) + } } + val askInventoryOnLaunch = jtLaunchReqs.at("/ask_inventory_on_launch").asBoolean() if (askInventoryOnLaunch && inventoryProp.isNotNull()) { var inventoryKeyId = if (inventoryProp is TextNode) { - resolveInventoryIdByName(awxClient, inventoryProp!!.textValue())?.asJsonPrimitive() + resolveInventoryIdByName(awxClient, inventoryProp.textValue())?.asJsonPrimitive() } else { inventoryProp } payload.set(INPUT_INVENTORY, inventoryKeyId) } val askVariablesOnLaunch = jtLaunchReqs.at("/ask_variables_on_launch").asBoolean() - if (askVariablesOnLaunch && extraArgs != null) { + if (askVariablesOnLaunch) { payload.set("extra_vars", extraArgs) } return payload.asJsonString(false) diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt index c11377012..08bc6c3fd 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt @@ -24,7 +24,6 @@ import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.onap.ccsdk.cds.controllerblueprints.core.utils.JsonParserUtils import org.slf4j.LoggerFactory import org.slf4j.helpers.MessageFormatter -import java.lang.Float import kotlin.reflect.KClass /** @@ -98,10 +97,10 @@ fun <T : Any?> T.asJsonPrimitive(): JsonNode { fun String.asJsonType(bpDataType: String): JsonNode { return when (bpDataType.toLowerCase()) { BluePrintConstants.DATA_TYPE_STRING -> this.asJsonPrimitive() - BluePrintConstants.DATA_TYPE_BOOLEAN -> java.lang.Boolean.valueOf(this).asJsonPrimitive() - BluePrintConstants.DATA_TYPE_INTEGER -> Integer.valueOf(this).asJsonPrimitive() - BluePrintConstants.DATA_TYPE_FLOAT -> Float.valueOf(this).asJsonPrimitive() - BluePrintConstants.DATA_TYPE_DOUBLE -> java.lang.Double.valueOf(this).asJsonPrimitive() + BluePrintConstants.DATA_TYPE_BOOLEAN -> this.toBoolean().asJsonPrimitive() + BluePrintConstants.DATA_TYPE_INTEGER -> this.toInt().asJsonPrimitive() + BluePrintConstants.DATA_TYPE_FLOAT -> this.toFloat().asJsonPrimitive() + BluePrintConstants.DATA_TYPE_DOUBLE -> this.toDouble().asJsonPrimitive() // For List, Map and Complex Types. else -> this.jsonAsJsonType() } diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt index 768f8753f..73dff9379 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt @@ -236,51 +236,55 @@ class JacksonUtils { } } - fun populatePrimitiveValues(key: String, value: Any, primitiveType: String, objectNode: ObjectNode) { + fun populatePrimitiveValues(key: String, value: JsonNode, primitiveType: String, objectNode: ObjectNode) { when (primitiveType.toLowerCase()) { - BluePrintConstants.DATA_TYPE_STRING, BluePrintConstants.DATA_TYPE_BOOLEAN, BluePrintConstants.DATA_TYPE_INTEGER, BluePrintConstants.DATA_TYPE_FLOAT, BluePrintConstants.DATA_TYPE_DOUBLE, - BluePrintConstants.DATA_TYPE_TIMESTAMP -> - objectNode.set(key, value.asJsonPrimitive()) - else -> objectNode.set(key, value.asJsonType()) + BluePrintConstants.DATA_TYPE_TIMESTAMP, + BluePrintConstants.DATA_TYPE_STRING -> + objectNode.set(key, value) + else -> throw BluePrintException("populatePrimitiveValues expected only primitive values! Received: ($value)") } } - fun populatePrimitiveValues(value: Any, primitiveType: String, arrayNode: ArrayNode) { + fun populatePrimitiveValues(value: JsonNode, primitiveType: String, arrayNode: ArrayNode) { when (primitiveType.toLowerCase()) { - BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(value as Boolean) - BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(value as Int) - BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(value as Float) - BluePrintConstants.DATA_TYPE_DOUBLE -> arrayNode.add(value as Double) - BluePrintConstants.DATA_TYPE_TIMESTAMP -> arrayNode.add(value as String) - else -> arrayNode.add(value as String) + BluePrintConstants.DATA_TYPE_BOOLEAN, + BluePrintConstants.DATA_TYPE_INTEGER, + BluePrintConstants.DATA_TYPE_FLOAT, + BluePrintConstants.DATA_TYPE_DOUBLE, + BluePrintConstants.DATA_TYPE_TIMESTAMP, + BluePrintConstants.DATA_TYPE_STRING -> arrayNode.add(value) + else -> throw BluePrintException("populatePrimitiveValues expected only primitive values! Received: ($value)") } } fun populatePrimitiveDefaultValues(key: String, primitiveType: String, objectNode: ObjectNode) { - when (primitiveType.toLowerCase()) { - BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, false) - BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, 0) - BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, 0.0) - BluePrintConstants.DATA_TYPE_DOUBLE -> objectNode.put(key, 0.0) - else -> objectNode.put(key, "") - } + val defaultValue = getDefaultValueOfPrimitiveAsJsonNode(primitiveType) ?: + throw BluePrintException("populatePrimitiveDefaultValues expected only primitive values! Received type ($primitiveType)") + objectNode.set(key, defaultValue) } fun populatePrimitiveDefaultValuesForArrayNode(primitiveType: String, arrayNode: ArrayNode) { - when (primitiveType.toLowerCase()) { - BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(false) - BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(0) - BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(0.0) - BluePrintConstants.DATA_TYPE_DOUBLE -> arrayNode.add(0.0) - else -> arrayNode.add("") + val defaultValue = getDefaultValueOfPrimitiveAsJsonNode(primitiveType) ?: + throw BluePrintException("populatePrimitiveDefaultValuesForArrayNode expected only primitive values! Received type ($primitiveType)") + arrayNode.add(defaultValue) + } + + private fun getDefaultValueOfPrimitiveAsJsonNode(primitiveType: String): JsonNode? { + return when (primitiveType.toLowerCase()) { + BluePrintConstants.DATA_TYPE_BOOLEAN -> BooleanNode.valueOf(false) + BluePrintConstants.DATA_TYPE_INTEGER -> IntNode.valueOf(0) + BluePrintConstants.DATA_TYPE_FLOAT -> FloatNode.valueOf(0.0f) + BluePrintConstants.DATA_TYPE_DOUBLE -> DoubleNode.valueOf(0.0) + BluePrintConstants.DATA_TYPE_STRING -> MissingNode.getInstance() + else -> null } } - fun populateJsonNodeValues(key: String, nodeValue: JsonNode?, type: String, objectNode: ObjectNode) { + fun populateJsonNodeValues(key: String, nodeValue: JsonNode, type: String, objectNode: ObjectNode) { if (nodeValue == null || nodeValue is NullNode) { objectNode.set(key, nodeValue) } else if (BluePrintTypes.validPrimitiveTypes().contains(type)) { @@ -292,12 +296,13 @@ class JacksonUtils { fun convertPrimitiveResourceValue(type: String, value: String): JsonNode { return when (type.toLowerCase()) { - BluePrintConstants.DATA_TYPE_BOOLEAN -> jsonNodeFromObject(java.lang.Boolean.valueOf(value)) - BluePrintConstants.DATA_TYPE_INTEGER -> jsonNodeFromObject(Integer.valueOf(value)) - BluePrintConstants.DATA_TYPE_FLOAT -> jsonNodeFromObject(java.lang.Float.valueOf(value)) - BluePrintConstants.DATA_TYPE_DOUBLE -> jsonNodeFromObject(java.lang.Double.valueOf(value)) + BluePrintConstants.DATA_TYPE_BOOLEAN -> jsonNodeFromObject(value.toBoolean()) + BluePrintConstants.DATA_TYPE_INTEGER -> jsonNodeFromObject(value.toInt()) + BluePrintConstants.DATA_TYPE_FLOAT -> jsonNodeFromObject(value.toFloat()) + BluePrintConstants.DATA_TYPE_DOUBLE -> jsonNodeFromObject(value.toDouble()) + BluePrintConstants.DATA_TYPE_STRING -> jsonNodeFromObject(value) else -> getJsonNode(value) } } } -}
\ No newline at end of file +} |