<service-logic
    xmlns='http://www.onap.org/sdnc/svclogic'
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.onap.org/sdnc/svclogic ./svclogic.xsd' module='GENERIC-RESOURCE-API' version='${project.version}'>
    <method rpc='generate-unique-name' mode='sync'>
        <block atomic="true">
            <set>
                <parameter name='generate-unique-name-output.error-message' value='' />
                <parameter name='generate-unique-name-output.generated-name' value='' />
            </set>
            <switch test='`$generate-unique-name-input.supplied-name`'>
                <outcome value='Other'>
                    <block atomic="true">
                        <switch test='`$generate-unique-name-input.name-table-type`'>
                            <outcome value=''>
                                <return status='failure'>
                                    <parameter name='generate-unique-name-output.error-message' value='generate-unique-name-input.name-table-type not supplied' />
                                </return>
                            </outcome>
                        </switch>
                        <set>
                            <parameter name='tmp.gun.sql' value="`'SELECT * from GENERIC_RESOURCE_NAME WHERE type = $generate-unique-name-input.name-table-type'
+ ' AND name = $generate-unique-name-input.supplied-name'`" />
                        </set>
                        <get-resource plugin='org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource' resource='SQL'
  key="`$tmp.gun.sql`" pfx='tmp.gun.namedb'>
                            <outcome value='failure'>
                                <return status='failure'>
                                    <parameter name='generate-unique-name-output.error-message' value="`'Error reading the ' + $generate-unique-name-input.name-table-index + ' table'`" />
                                </return>
                            </outcome>
                            <outcome value='not-found'>
                                <block atomic="true">
                                    <set>
                                        <parameter name='tmp.gun.sql' value="`'insert into GENERIC_RESOURCE_NAME values ( '
+ ' $generate-unique-name-input.name-table-type , $generate-unique-name-input.supplied-name , null , null, null )'`" />
                                    </set>
                                    <save plugin='org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource' resource='SQL'
  key="`$tmp.gun.sql`" pfx='pfx'>
                                        <outcome value='failure'>
                                            <return status='failure'>
                                                <parameter name="generate-unique-name-output.error-message" value="Failed insert into GENERIC_RESOURCE_NAME table" />
                                            </return>
                                        </outcome>
                                    </save>
                                </block>
                            </outcome>
                            <outcome value='Other'>
                                <return status='failure'>
                                    <parameter name='generate-unique-name-output.error-message' value="`'The name ' + $generate-unique-name-input.supplied-name + ' has already been used'`" />
                                </return>
                            </outcome>
                        </get-resource>
                    </block>
                </outcome>
                <outcome value=''>
                    <block atomic="true">
                        <switch test='`$generate-unique-name-input.index-table-name`'>
                            <outcome value=''>
                                <return status='failure'>
                                    <parameter name='generate-unique-name-output.error-message' value='generate-unique-name-input.index-table-name not supplied' />
                                </return>
                            </outcome>
                        </switch>
                        <switch test='`$generate-unique-name-input.index-table-prefix-column`'>
                            <outcome value=''>
                                <return status='failure'>
                                    <parameter name='generate-unique-name-output.error-message' value='generate-unique-name-input.index-table-prefix-column not supplied' />
                                </return>
                            </outcome>
                        </switch>
                        <switch test='`$generate-unique-name-input.name-table-type`'>
                            <outcome value=''>
                                <return status='failure'>
                                    <parameter name='generate-unique-name-output.error-message' value='generate-unique-name-input.name-table-type not supplied' />
                                </return>
                            </outcome>
                        </switch>
                        <switch test='`$generate-unique-name-input.prefix`'>
                            <outcome value=''>
                                <return status='failure'>
                                    <parameter name='generate-unique-name-output.error-message' value='generate-unique-name-input.prefix not supplied' />
                                </return>
                            </outcome>
                        </switch>
                        <switch test='`$generate-unique-name-input.index-length`'>
                            <outcome value=''>
                                <set>
                                    <parameter name='tmp.gun.list-end' value='100000' />
                                </set>
                            </outcome>
                            <outcome value='0'>
                                <set>
                                    <parameter name='tmp.gun.list-end' value='0' />
                                </set>
                            </outcome>
                            <outcome value='1'>
                                <set>
                                    <parameter name='tmp.gun.list-end' value='10' />
                                </set>
                            </outcome>
                            <outcome value='2'>
                                <set>
                                    <parameter name='tmp.gun.list-end' value='100' />
                                </set>
                            </outcome>
                            <outcome value='3'>
                                <set>
                                    <parameter name='tmp.gun.list-end' value='1000' />
                                </set>
                            </outcome>
                            <outcome value='4'>
                                <set>
                                    <parameter name='tmp.gun.list-end' value='10000' />
                                </set>
                            </outcome>
                            <outcome value='Other'>
                                <return status='failure'>
                                    <parameter name='generate-unique-name-output.error-message' value='generate-unique-name-input.prefix not supplied' />
                                </return>
                            </outcome>
                        </switch>
                        <set>
                            <parameter name='tmp.gun.noindex-flag' value='false' />
                        </set>
                        <set>
                            <parameter name='tmp.gun.sql' value="`'SELECT * from ' + $generate-unique-name-input.index-table-name
+ ' WHERE ' + $generate-unique-name-input.index-table-prefix-column + ' = '
+ '$generate-unique-name-input.prefix'`" />
                        </set>
                        <get-resource plugin='org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource' resource='SQL'
  key="`$tmp.gun.sql`" pfx='tmp.gun.prefixdb'>
                            <outcome value='success'>
                                <block>
                                    <set>
                                        <parameter name='tmp.gun.index-exists' value='true' />
                                    </set>
                                    <switch test='`$generate-unique-name-input.index-length`'>
                                        <outcome value='Other'>
                                            <set>
                                                <parameter name='tmp.gun.index-number' value='`$tmp.gun.prefixdb.index-number + 1`' />
                                            </set>
                                        </outcome>
                                        <outcome value='0'>
                                            <block>
                                                <set>
                                                    <parameter name='tmp.gun.index-number' value='0' />
                                                </set>
                                                <set>
                                                    <parameter name='tmp.gun.noindex-flag' value='true' />
                                                </set>
                                            </block>
                                        </outcome>
                                    </switch>
                                </block>
                            </outcome>
                            <outcome value='not-found'>
                                <block>
                                    <set>
                                        <parameter name='tmp.gun.index-exists' value='false' />
                                    </set>
                                    <switch test='`$generate-unique-name-input.index-length`'>
                                        <outcome value='0'>
                                            <block>
                                                <set>
                                                    <parameter name='tmp.gun.index-number' value='0' />
                                                </set>
                                                <set>
                                                    <parameter name='tmp.gun.noindex-flag' value='true' />
                                                </set>
                                            </block>
                                        </outcome>
                                        <outcome value='Other'>
                                            <set>
                                                <parameter name='tmp.gun.index-number' value='1' />
                                            </set>
                                        </outcome>
                                    </switch>
                                </block>
                            </outcome>
                            <outcome value='failure'>
                                <return status='failure'>
                                    <parameter name="generate-unique-name-output.error-message" value="`'Error reading the ' + $generate-unique-name-input.index-table-name + ' table'`" />
                                </return>
                            </outcome>
                        </get-resource>
                        <set>
                            <parameter name='tmp.gun.found-flag' value='false' />
                        </set>
                        <block>
                            <for index='tmp.gun.idx' start='`$tmp.gun.index-number`' end='`$tmp.gun.list-end`' >
                                <block atomic="true">
                                    <switch test='`$generate-unique-name-input.index-length`'>
                                        <outcome value=''>
                                            <set>
                                                <parameter name='tmp.gun.index-string' value='`$tmp.gun.idx`' />
                                            </set>
                                        </outcome>
                                        <outcome value='1'>
                                            <set>
                                                <parameter name='tmp.gun.index-string' value='`$tmp.gun.idx`' />
                                            </set>
                                        </outcome>
                                        <outcome value='2'>
                                            <switch test='`$tmp.gun.idx &lt; 10`'>
                                                <outcome value='true'>
                                                    <set>
                                                        <parameter name='tmp.gun.index-string' value="`'' + '0' + $tmp.gun.idx`" />
                                                    </set>
                                                </outcome>
                                                <outcome value='false'>
                                                    <set>
                                                        <parameter name='tmp.gun.index-string' value='`$tmp.gun.idx`' />
                                                    </set>
                                                </outcome>
                                            </switch>
                                        </outcome>
                                        <outcome value='3'>
                                            <switch test='`$tmp.gun.idx &lt; 10`'>
                                                <outcome value='true'>
                                                    <set>
                                                        <parameter name='tmp.gun.index-string' value="`'' + '00' + $tmp.gun.idx`" />
                                                    </set>
                                                </outcome>
                                                <outcome value='false'>
                                                    <switch test='`$tmp.gun.idx &lt; 100`'>
                                                        <outcome value='true'>
                                                            <set>
                                                                <parameter name='tmp.gun.index-string' value="`'' + '0' + $tmp.gun.idx`" />
                                                            </set>
                                                        </outcome>
                                                        <outcome value='false'>
                                                            <set>
                                                                <parameter name='tmp.gun.index-string' value='`$tmp.gun.idx`' />
                                                            </set>
                                                        </outcome>
                                                    </switch>
                                                </outcome>
                                            </switch>
                                        </outcome>
                                        <outcome value='4'>
                                            <switch test='`$tmp.gun.idx &lt; 10`'>
                                                <outcome value='true'>
                                                    <set>
                                                        <parameter name='tmp.gun.index-string' value="`'' + '000' + $tmp.gun.idx`" />
                                                    </set>
                                                </outcome>
                                                <outcome value='false'>
                                                    <switch test='`$tmp.gun.idx &lt; 100`'>
                                                        <outcome value='true'>
                                                            <set>
                                                                <parameter name='tmp.gun.index-string' value="`'' + '00' + $tmp.gun.idx`" />
                                                            </set>
                                                        </outcome>
                                                        <outcome value='false'>
                                                            <switch test='`$tmp.gun.idx &lt; 1000`'>
                                                                <outcome value='true'>
                                                                    <set>
                                                                        <parameter name='tmp.gun.index-string' value="`'' + '0' + $tmp.gun.idx`" />
                                                                    </set>
                                                                </outcome>
                                                                <outcome value='false'>
                                                                    <set>
                                                                        <parameter name='tmp.gun.index-string' value='`$tmp.gun.idx`' />
                                                                    </set>
                                                                </outcome>
                                                            </switch>
                                                        </outcome>
                                                    </switch>
                                                </outcome>
                                            </switch>
                                        </outcome>
                                    </switch>
                                    <set>
                                        <parameter name='tmp.gun.generated-name' value='`$generate-unique-name-input.prefix + $tmp.gun.index-string`' />
                                    </set>
                                    <set>
                                        <parameter name='tmp.gun.sql' value="`'SELECT * from GENERIC_RESOURCE_NAME WHERE type = $generate-unique-name-input.name-table-type'
+ ' AND name = $tmp.gun.generated-name'`" />
                                    </set>
                                    <get-resource plugin='org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource' resource='SQL'
  key="`$tmp.gun.sql`" pfx='tmp.gun.namedb'>
                                        <outcome value='failure'>
                                            <block atomic="true">
                                                <set>
                                                    <parameter name='generate-unique-name-output.error-message' value="`'Error reading the GENERIC_RESOURCE_NAME table'`" />
                                                </set>
                                                <return status='failure'></return>
                                            </block>
                                        </outcome>
                                        <outcome value='not-found'>
                                            <block atomic="true">
                                                <set>
                                                    <parameter name='tmp.gun.found-flag' value='true' />
                                                </set>
                                                <return status='failure'></return>
                                            </block>
                                        </outcome>
                                    </get-resource>
                                </block>
                            </for>
                            <switch test='`$tmp.gun.noindex-flag`'>
                                <outcome value='true'>
                                    <block atomic="true">
                                        <set>
                                            <parameter name='tmp.gun.index-string' value='0' />
                                        </set>
                                        <set>
                                            <parameter name='tmp.gun.generated-name' value='`$generate-unique-name-input.prefix`' />
                                        </set>
                                        <set>
                                            <parameter name='tmp.gun.sql' value="`'SELECT * from GENERIC_RESOURCE_NAME WHERE type = $generate-unique-name-input.name-table-type'
+ ' AND name = $tmp.gun.generated-name'`" />
                                        </set>
                                        <get-resource plugin='org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource' resource='SQL'
  key="`$tmp.gun.sql`" pfx='tmp.gun.namedb'>
                                            <outcome value='failure'>
                                                <block atomic="true">
                                                    <set>
                                                        <parameter name='generate-unique-name-output.error-message' value="`'Error reading the GENERIC_RESOURCE_NAME table'`" />
                                                    </set>
                                                    <return status='failure'></return>
                                                </block>
                                            </outcome>
                                            <outcome value='not-found'>
                                                <block atomic="true">
                                                    <set>
                                                        <parameter name='tmp.gun.found-flag' value='true' />
                                                    </set>
                                                    <return status='failure'></return>
                                                </block>
                                            </outcome>
                                            <outcome value='success'>
                                                <block atomic="true">
                                                    <set>
                                                        <parameter name='tmp.gun.found-flag' value='true' />
                                                    </set>
                                                    <return status='failure'></return>
                                                </block>
                                            </outcome>
                                        </get-resource>
                                    </block>
                                </outcome>
                            </switch>
                            <return status='success'></return>
                        </block>
                        <switch test='`$generate-unique-name-output.error-message`'>
                            <outcome value=''>
                                <block></block>
                            </outcome>
                            <outcome value='Other'>
                                <return status='failure'></return>
                            </outcome>
                        </switch>
                        <switch test='`$tmp.gun.found-flag`'>
                            <outcome value='true'>
                                <block atomic="true">
                                    <set>
                                        <parameter name='generate-unique-name-output.generated-name' value='`$tmp.gun.generated-name`' />
                                    </set>
                                    <switch test='`$tmp.gun.index-exists`'>
                                        <outcome value='true'>
                                            <block atomic="true">
                                                <set>
                                                    <parameter name='tmp.gun.sql' value="`'update ' + $generate-unique-name-input.index-table-name
+ ' set index_number = ' + $tmp.gun.index-string + ' where '
+ $generate-unique-name-input.index-table-prefix-column
+ ' = $generate-unique-name-input.prefix'`" />
                                                </set>
                                                <save plugin='org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource' resource='SQL'
  key="`$tmp.gun.sql`" pfx='pfx'>
                                                    <outcome value='failure'>
                                                        <return status='failure'>
                                                            <parameter name="generate-unique-name-output.error-message" value="`'Failed to update ' + $generate-unique-name-input.index-table-name + ' table'`" />
                                                        </return>
                                                    </outcome>
                                                </save>
                                            </block>
                                        </outcome>
                                        <outcome value='false'>
                                            <block atomic="true">
                                                <set>
                                                    <parameter name='tmp.gun.sql' value="`'insert into ' + $generate-unique-name-input.index-table-name
+ '(' + $generate-unique-name-input.index-table-prefix-column + ', index_number) values ('
+ ' $generate-unique-name-input.prefix , ' + $tmp.gun.index-string + ')'`" />
                                                </set>
                                                <save plugin='org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource' resource='SQL'
  key="`$tmp.gun.sql`" pfx='pfx'>
                                                    <outcome value='failure'>
                                                        <return status='failure'>
                                                            <parameter name="generate-unique-name-output.error-message" value="`'Failed to insert record into ' + $generate-unique-name-input.index-table-name + ' table'`" />
                                                        </return>
                                                    </outcome>
                                                </save>
                                            </block>
                                        </outcome>
                                    </switch>
                                    <set>
                                        <parameter name='tmp.gun.sql' 
	value="`'insert into GENERIC_RESOURCE_NAME values ( $generate-unique-name-input.name-table-type , $tmp.gun.generated-name , $generate-unique-name-input.prefix , ' + $tmp.gun.index-string + ' , $tmp.gun.transient )'`" />
                                    </set>
                                    <save plugin='org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource' resource='SQL'
  key="`$tmp.gun.sql`" pfx='pfx'>
                                        <outcome value='failure'>
                                            <return status='failure'>
                                                <parameter name="generate-unique-name-output.error-message" value="Failed insert into GENERIC_RESOURCE_NAME table" />
                                            </return>
                                        </outcome>
                                    </save>
                                    <return status='success'></return>
                                </block>
                            </outcome>
                            <outcome value='false'>
                                <block atomic="true">
                                    <switch test='`$tmp.gun.noindex-flag`'>
                                        <outcome value='true'>
                                            <return status='failure'>
                                                <parameter name="generate-unique-name-output.error-message" value="naming exhausted for input with index-length = 0" />
                                            </return>
                                        </outcome>
                                    </switch>
                                    <switch test='`$generate-unique-name-input.index-length`'>
                                        <outcome value=''>
                                            <return status='failure'>
                                                <parameter name="generate-unique-name-output.error-message" value="We have exhausted the naming index count" />
                                            </return>
                                        </outcome>
                                    </switch>
                                    <block>
                                        <for index='tmp.gun.idx' start='1' end='`$tmp.gun.index-number`' >
                                            <block atomic="true">
                                                <switch test='`$generate-unique-name-input.index-length`'>
                                                    <outcome value=''>
                                                        <set>
                                                            <parameter name='tmp.gun.index-string' value='`$tmp.gun.idx`' />
                                                        </set>
                                                    </outcome>
                                                    <outcome value='1'>
                                                        <set>
                                                            <parameter name='tmp.gun.index-string' value='`$tmp.gun.idx`' />
                                                        </set>
                                                    </outcome>
                                                    <outcome value='2'>
                                                        <switch test='`$tmp.gun.idx &lt; 10`'>
                                                            <outcome value='true'>
                                                                <set>
                                                                    <parameter name='tmp.gun.index-string' value="`'' + '0' + $tmp.gun.idx`" />
                                                                </set>
                                                            </outcome>
                                                            <outcome value='false'>
                                                                <set>
                                                                    <parameter name='tmp.gun.index-string' value='`$tmp.gun.idx`' />
                                                                </set>
                                                            </outcome>
                                                        </switch>
                                                    </outcome>
                                                    <outcome value='3'>
                                                        <switch test='`$tmp.gun.idx &lt; 10`'>
                                                            <outcome value='true'>
                                                                <set>
                                                                    <parameter name='tmp.gun.index-string' value="`'' + '00' + $tmp.gun.idx`" />
                                                                </set>
                                                            </outcome>
                                                            <outcome value='false'>
                                                                <switch test='`$tmp.gun.idx &lt; 100`'>
                                                                    <outcome value='true'>
                                                                        <set>
                                                                            <parameter name='tmp.gun.index-string' value="`'' + '0' + $tmp.gun.idx`" />
                                                                        </set>
                                                                    </outcome>
                                                                    <outcome value='false'>
                                                                        <set>
                                                                            <parameter name='tmp.gun.index-string' value='`$tmp.gun.idx`' />
                                                                        </set>
                                                                    </outcome>
                                                                </switch>
                                                            </outcome>
                                                        </switch>
                                                    </outcome>
                                                </switch>
                                                <set>
                                                    <parameter name='tmp.gun.generated-name' value='`$generate-unique-name-input.prefix + $tmp.gun.index-string`' />
                                                </set>
                                                <set>
                                                    <parameter name='tmp.gun.namedb.name' value='' />
                                                </set>
                                                <set>
                                                    <parameter name='tmp.gun.sql' value="`'SELECT * from GENERIC_RESOURCE_NAME WHERE type = $generate-unique-name-input.name-table-type'
+ ' AND name = $tmp.gun.generated-name'`" />
                                                </set>
                                                <get-resource plugin='org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource' resource='SQL'
  key="`$tmp.gun.sql`" pfx='tmp.gun.namedb'>
                                                    <outcome value='failure'>
                                                        <block atomic="true">
                                                            <set>
                                                                <parameter name='generate-unique-name-output.error-message' value="Error reading the GENERIC_RESOURCE_NAME table" />
                                                            </set>
                                                            <return status='failure'></return>
                                                        </block>
                                                    </outcome>
                                                    <outcome value='not-found'>
                                                        <block atomic="true">
                                                            <set>
                                                                <parameter name='tmp.gun.found-flag' value='true' />
                                                            </set>
                                                            <return status='failure'></return>
                                                        </block>
                                                    </outcome>
                                                </get-resource>
                                            </block>
                                        </for>
                                        <return status='success'></return>
                                    </block>
                                    <switch test='`$generate-unique-name-output.error-message`'>
                                        <outcome value=''>
                                            <block></block>
                                        </outcome>
                                        <outcome value='Other'>
                                            <return status='failure'></return>
                                        </outcome>
                                    </switch>
                                    <switch test='`$tmp.gun.found-flag`'>
                                        <outcome value='true'>
                                            <block atomic="true">
                                                <set>
                                                    <parameter name='generate-unique-name-output.generated-name' value='`$tmp.gun.generated-name`' />
                                                </set>
                                                <switch test='`$tmp.gun.index-exists`'>
                                                    <outcome value='true'>
                                                        <block atomic="true">
                                                            <set>
                                                                <parameter name='tmp.gun.sql' value="`'update ' + $generate-unique-name-input.index-table-name
+ ' set index_number = ' + $tmp.gun.index-string + ' where '
+ $generate-unique-name-input.index-table-prefix-column
+ ' = $generate-unique-name-input.prefix'`" />
                                                            </set>
                                                            <save plugin='org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource' resource='SQL'
  key="`$tmp.gun.sql`" pfx='pfx'>
                                                                <outcome value='failure'>
                                                                    <return status='failure'>
                                                                        <parameter name="generate-unique-name-output.error-message" value="`'Failed to update ' + $generate-unique-name-input.index-table-name + ' table'`" />
                                                                    </return>
                                                                </outcome>
                                                            </save>
                                                        </block>
                                                    </outcome>
                                                    <outcome value='false'>
                                                        <block atomic="true">
                                                            <set>
                                                                <parameter name='tmp.gun.sql' value="`'insert into ' + $generate-unique-name-input.index-table-name
+ '(' + $generate-unique-name-input.index-table-prefix-column + ', index_number) values ('
+ ' $generate-unique-name-input.prefix , ' + $tmp.gun.index-string + ')'`" />
                                                            </set>
                                                            <save plugin='org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource' resource='SQL'
  key="`$tmp.gun.sql`" pfx='pfx'>
                                                                <outcome value='failure'>
                                                                    <return status='failure'>
                                                                        <parameter name="generate-unique-name-output.error-message" value="`'Failed to insert record into ' + $generate-unique-name-input.index-table-name + ' table'`" />
                                                                    </return>
                                                                </outcome>
                                                            </save>
                                                        </block>
                                                    </outcome>
                                                </switch>
                                                <set>
                                                    <parameter name='tmp.gun.sql' 
	value="`'insert into GENERIC_RESOURCE_NAME values ( $generate-unique-name-input.name-table-type , $tmp.gun.generated-name , $generate-unique-name-input.prefix , ' + $tmp.gun.index-string + ' , $tmp.gun.transient )'`" />
                                                </set>
                                                <save plugin='org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource' resource='SQL'
  key="`$tmp.gun.sql`" pfx='pfx'>
                                                    <outcome value='failure'>
                                                        <return status='failure'>
                                                            <parameter name="generate-unique-name-output.error-message" value="Failed insert into GENERIC_RESOURCE_NAME table" />
                                                        </return>
                                                    </outcome>
                                                </save>
                                                <return status='success'></return>
                                            </block>
                                        </outcome>
                                    </switch>
                                    <return status='failure'>
                                        <parameter name="generate-unique-name-output.error-message" value="We have exhausted the naming index count" />
                                    </return>
                                </block>
                            </outcome>
                        </switch>
                    </block>
                </outcome>
            </switch>
        </block>
    </method>
</service-logic>