Skip to content

Commit 0a58c38

Browse files
update import logic for asapio
1 parent 6a8dbf6 commit 0a58c38

File tree

15 files changed

+1186
-22
lines changed

15 files changed

+1186
-22
lines changed

asyncapi-accessor-v2/src/main/java/com/solace/ep/asyncapi/accessor/v2/AsyncApiAccessor.java

+9
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,13 @@ public Map<String, JsonElement> getMessages() throws Exception {
207207
return messages;
208208
}
209209

210+
public String getDefaultContentType() {
211+
try {
212+
return this.root.get("defaultContentType").getAsString();
213+
} catch (Exception exc) {
214+
log.debug("defaultContentType field not found");
215+
return null;
216+
}
217+
}
218+
210219
}

asyncapi-accessor-v2/src/main/java/com/solace/ep/asyncapi/accessor/v2/AsyncApiChannel.java

+8
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,12 @@ private JsonObject getPublishTopicJsonObject() {
291291
}
292292
return null;
293293
}
294+
295+
public String getEventPortalEventNameFromChannel() {
296+
try {
297+
return this.asyncApiChannel.get(EpFieldConstants.EP_EVENT_NAME).getAsString();
298+
} catch (Exception exc) {
299+
return null;
300+
}
301+
}
294302
}

asyncapi-accessor-v2/src/main/java/com/solace/ep/asyncapi/accessor/v2/AsyncApiMessage.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ public AsyncApiMessage( JsonObject message, AsyncApiAccessor asyncApi, String me
5555
* @return
5656
*/
5757
public String getMessageName() {
58+
// attempt to find embedded message name if the constructed name is null
59+
if (this.messageName == null) {
60+
try {
61+
final String msgName = asyncApiMessage.get("name").getAsString();
62+
if (msgName != null && ! msgName.isBlank()) {
63+
this.messageName = msgName;
64+
}
65+
} catch (Exception exc) { }
66+
}
5867
return this.messageName;
5968
}
6069

@@ -205,7 +214,25 @@ public String getPayloadRef() {
205214

206215
public String getSchemaName() {
207216
final String payloadRef = this.getPayloadRef();
208-
final String schemaName = payloadRef != null ? AsyncApiUtils.getLastElementFromRefString(payloadRef) : null;
217+
String schemaName = null;
218+
if (payloadRef != null) {
219+
schemaName = AsyncApiUtils.getLastElementFromRefString(payloadRef);
220+
} else {
221+
try {
222+
JsonObject payloadObject = asyncApiMessage.getAsJsonObject( AsyncApiFieldConstants.API_PAYLOAD );
223+
final String titleName = payloadObject.get("title").getAsString();
224+
if (titleName != null && ! titleName.isEmpty()) {
225+
schemaName = titleName;
226+
} else {
227+
final String schemaIdField = payloadObject.getAsJsonObject("$id").getAsString();
228+
if (schemaIdField != null && !schemaIdField.isEmpty()) {
229+
schemaName = schemaIdField;
230+
}
231+
}
232+
} catch (Exception exc) {
233+
return null;
234+
}
235+
}
209236
return schemaName;
210237
}
211238

asyncapi-importer-cli/Readme.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ java -jar asyncapi-import.jar -a **ASYNCAPI_TO_IMPORT** -d **APP_DOMAIN** -t **E
4949
|`-m`|`--version-major`|Increment Major Version|No|`-m` / `--version-major`|
5050
|`-i`|`--version-minor`|Increment Minor Version|No|`-m` / `--version-major`|
5151
|`-p`|`--version-patch`|Increment Patch Version|No|`-m` / `--version-major`|
52+
|`-e`|`--events-only`|Import Events, Enums, and<br>Schemas. Skip Applications|No|N/A|
53+
|`-z`|`--no-cascade`|Disable cascade update of objects<br>Schemas. Skip Applications|No|N/A|
5254
|`-h`|`--help`|Display Help|No|N/A|
5355

54-
**Note: Version options are mutually exclusive. Incremented versions are new SemVer versions when new object versions are created as a result of importing.**
56+
> **Note:** Version options are mutually exclusive. Incremented versions are new SemVer versions when new object versions are created as a result of importing.
57+
58+
> **Note:** If **--events-only** option is specified, applications may still be cascade updated if an associated event has a new version created.
5559
5660
## Outstanding
5761
- Check the EP token access before being import operation

asyncapi-importer-cli/src/main/java/com/solace/ep/asyncapi/cli/AsyncApiImport.java

+28-12
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,22 @@
3434

3535
public class AsyncApiImport {
3636

37-
private static final String CMD_LINE_SYNTAX = "asyncapi-import -a ASYNCAPI_TO_IMPORT -d APP_DOMAIN -t EP_TOKEN [-u BASE_URL] [-m | -i | -p]";
37+
private static final String CMD_LINE_SYNTAX = "asyncapi-import -a ASYNCAPI_TO_IMPORT -d APP_DOMAIN -t EP_TOKEN [-u BASE_URL] [-m | -i | -p] [-e] [-z]\n";
3838

3939
public static void main(String[] args)
4040
{
4141
// Define Options
42-
Option opAsyncapi = new Option("a", "asyncapi", true, "AsyncApi Spec File to import");
43-
Option opAppDomain = new Option("d", "app-domain", true, "Target Application Domain in Event Portal for Import");
44-
Option opEpToken = new Option("t", "ep-token", true, "Event Portal bearer token");
45-
Option opBaseUrl = new Option("u", "ep-base-url", true, "Base URL to call Event Portal\nUse to call Solace cloud API\noutside of US/CAN region");
46-
Option opHelp = new Option("h", "help", false, "Display Help");
47-
48-
Option opVersionMajor = new Option("m", "version-major", false, "Increment MAJOR version of SemVer for new objects (DEFAULT)");
49-
Option opVersionMinor = new Option("i", "version-minor", false, "Increment MINOR version of SemVer for new objects");
50-
Option opVersionPatch = new Option("p", "version-patch", false, "Increment PATCH version of SemVer for new objects");
42+
Option opAsyncapi = new Option("a", "asyncapi", true, "AsyncApi Spec File to import\n");
43+
Option opAppDomain = new Option("d", "app-domain", true, "Target Application Domain in Event Portal\n");
44+
Option opEpToken = new Option("t", "ep-token", true, "Event Portal bearer token\n");
45+
Option opBaseUrl = new Option("u", "ep-base-url", true, "Over-ride Base URL to call Event Portal\nUse to call Solace Cloud API outside\n of US/CAN region\n");
46+
Option opHelp = new Option("h", "help", false, "Display Help\n");
47+
Option opNoCascadeUpdate = new Option("z", "no-cascade", false, "Disable cascade update of objects\nSee documentation for more info\n");
48+
Option opImportEventsOnly = new Option("e", "events-only", false, "Import Events and Schemas only\nDo not import Applications");
49+
50+
Option opVersionMajor = new Option("m", "version-major", false, "Increment MAJOR version of SemVer (DEFAULT)\n");
51+
Option opVersionMinor = new Option("i", "version-minor", false, "Increment MINOR version of SemVer\n");
52+
Option opVersionPatch = new Option("p", "version-patch", false, "Increment PATCH version of SemVer\n");
5153

5254
opAsyncapi.setRequired(true);
5355
opAppDomain.setRequired(true);
@@ -63,14 +65,18 @@ public static void main(String[] args)
6365
.addOption(opEpToken)
6466
.addOption(opBaseUrl)
6567
.addOptionGroup(opGroupVersion)
66-
.addOption(opHelp);
68+
.addOption(opHelp)
69+
.addOption(opNoCascadeUpdate)
70+
.addOption(opImportEventsOnly);
6771

6872
// Collect Option values
6973
String appDomainName;
7074
String asyncapiSpecFile;
7175
String epToken;
7276
String epBaseUrl;
7377
String versionStrategy;
78+
boolean disableCascadeUpdate;
79+
boolean disableApplicationImport;
7480

7581
// Parse out options
7682
try {
@@ -98,6 +104,8 @@ public static void main(String[] args)
98104
} else {
99105
versionStrategy = null;
100106
}
107+
disableCascadeUpdate = commandLine.hasOption("z");
108+
disableApplicationImport = commandLine.hasOption("e");
101109
} catch (ParseException parseExc) {
102110
System.out.println("Error parsing out options: " + parseExc.getLocalizedMessage() + "\n");
103111
displayHelp(options);
@@ -115,7 +123,15 @@ public static void main(String[] args)
115123
try {
116124
final String asyncApiContent = getFileAsString(asyncapiSpecFile);
117125

118-
AsyncApiImporter.execImportOperation(appDomainName, epToken, asyncApiContent, epBaseUrl, versionStrategy);
126+
AsyncApiImporter.execImportOperation(
127+
appDomainName,
128+
epToken,
129+
asyncApiContent,
130+
epBaseUrl,
131+
versionStrategy,
132+
disableCascadeUpdate,
133+
disableApplicationImport
134+
);
119135

120136
System.out.println(
121137
"\n#### ASYNCAPI SPEC IMPORT COMPLETE ####\n"

asyncapi-importer-core/src/main/java/com/solace/ep/asyncapi/importer/AsyncApiImporter.java

+77-5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public class AsyncApiImporter {
3737

3838
private EpNewVersionStrategy versionStrategy;
3939

40+
private boolean disableCascadeUpdate;
41+
42+
private boolean disableApplicationImport;
43+
4044
private String applicationDomainId;
4145

4246
/**
@@ -45,6 +49,8 @@ public class AsyncApiImporter {
4549
* @param asyncApiSpecToImport - String representation of full asyncapi spec to import
4650
* @param eventPortalBaseUrl - Can be NULL, will use default URL for US/Canada
4751
* @param newVersionStrategy - Used to indicate how semantic versions for created object are incremented.
52+
* @param disableCascadeUpdate - Set to TRUE to prevent new versions of objects from being created by cascade update (See Documentation)
53+
* @param disableApplicationImport - Do not import an application with the AsyncApi spec, Events, schemas, and enums only
4854
* Allowed values are MAJOR, MINOR, and PATCH.
4955
* Can be NULL, will default increment MAJOR version;
5056
* @throws Exception
@@ -54,7 +60,9 @@ public AsyncApiImporter(
5460
final String eventPortalBearerToken,
5561
final String asyncApiSpecToImport,
5662
final String eventPortalBaseUrl,
57-
final String newVersionStrategy
63+
final String newVersionStrategy,
64+
final boolean disableCascadeUpdate,
65+
final boolean disableApplicationImport
5866
) throws Exception
5967
{
6068
this.applicationDomainName = applicationDomainName;
@@ -66,6 +74,29 @@ public AsyncApiImporter(
6674
} else {
6775
this.versionStrategy = EpNewVersionStrategy.MAJOR;
6876
}
77+
this.disableCascadeUpdate = disableCascadeUpdate;
78+
this.disableApplicationImport = disableApplicationImport;
79+
}
80+
81+
/**
82+
* @param applicationDomainName - Name of Application Domain in Event Portal where objects represented in the AsyncApi spec will be imported.
83+
* @param eventPortalBearerToken - Event Portal Bearer Token, must have read and write privileges
84+
* @param asyncApiSpecToImport - String representation of full asyncapi spec to import
85+
* @param eventPortalBaseUrl - Can be NULL, will use default URL for US/Canada
86+
* @param newVersionStrategy - Used to indicate how semantic versions for created object are incremented.
87+
* Allowed values are MAJOR, MINOR, and PATCH.
88+
* Can be NULL, will default increment MAJOR version;
89+
* @throws Exception
90+
*/
91+
public AsyncApiImporter(
92+
final String applicationDomainName,
93+
final String eventPortalBearerToken,
94+
final String asyncApiSpecToImport,
95+
final String eventPortalBaseUrl,
96+
final String newVersionStrategy
97+
) throws Exception
98+
{
99+
this(applicationDomainName, eventPortalBearerToken, asyncApiSpecToImport, eventPortalBaseUrl, newVersionStrategy, false, false);
69100
}
70101

71102
/**
@@ -75,6 +106,41 @@ public AsyncApiImporter(
75106
* @param asyncApiSpecToImport - String representation of full asyncapi spec to import
76107
* @param eventPortalBaseUrl - Can be NULL, will use default URL for US/Canada
77108
* @param newVersionStrategy - Used to indicate how semantic versions for created object are incremented.
109+
* @param disableCascadeUpdate - Set to TRUE to prevent new versions of objects from being created by cascade update (See Documentation)
110+
* @param disableApplicationImport - Do not import an application with the AsyncApi spec, Events, schemas, and enums only
111+
* Allowed values are MAJOR, MINOR, and PATCH.
112+
* Can be NULL, will default increment MAJOR version;
113+
* @throws Exception
114+
*/
115+
public static void execImportOperation(
116+
final String applicationDomainName,
117+
final String eventPortalBearerToken,
118+
final String asyncApiSpecToImport,
119+
final String eventPortalBaseUrl,
120+
final String newVersionStrategy,
121+
final boolean disableCascadeUpdate,
122+
final boolean disableApplicationImport
123+
) throws Exception
124+
{
125+
final AsyncApiImporter importer = new AsyncApiImporter(
126+
applicationDomainName,
127+
eventPortalBearerToken,
128+
asyncApiSpecToImport,
129+
eventPortalBaseUrl,
130+
newVersionStrategy,
131+
disableCascadeUpdate,
132+
disableApplicationImport);
133+
importer.execImportOperation();
134+
}
135+
136+
/**
137+
* Statically invoke AsyncApi import operation
138+
* @param applicationDomainName - Name of Application Domain in Event Portal where objects represented in the AsyncApi spec will be imported.
139+
* @param eventPortalBearerToken - Event Portal Bearer Token, must have read and write privileges
140+
* @param asyncApiSpecToImport - String representation of full asyncapi spec to import
141+
* @param eventPortalBaseUrl - Can be NULL, will use default URL for US/Canada
142+
* @param newVersionStrategy - Used to indicate how semantic versions for created object are incremented.
143+
* @param disableCascadeUpdate - Set to TRUE to prevent new versions of objects from being created by cascade update (See Documentation)
78144
* Allowed values are MAJOR, MINOR, and PATCH.
79145
* Can be NULL, will default increment MAJOR version;
80146
* @throws Exception
@@ -129,12 +195,18 @@ public void execImportOperation() throws Exception
129195

130196
importOperator.importEvents();
131197

132-
importOperator.matchEpApplications();
198+
if (! disableApplicationImport)
199+
{
200+
importOperator.matchEpApplications();
133201

134-
importOperator.importApplications();
202+
importOperator.importApplications();
203+
}
135204

136-
importOperator.cascadeUpdateEvents();
205+
if (! disableCascadeUpdate)
206+
{
207+
importOperator.cascadeUpdateEvents();
137208

138-
importOperator.cascadeUpdateApplications();
209+
importOperator.cascadeUpdateApplications();
210+
}
139211
}
140212
}

asyncapi-importer-core/src/main/java/com/solace/ep/asyncapi/importer/client/EventPortalClientApi.java

+7
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,13 @@ public ApplicationVersion createApplicationVersion(
894894
}
895895
}
896896

897+
/**
898+
* Update the declaredPublishedEvents associated with an application version
899+
* @param appVersionId
900+
* @param declaredPublishedEvents
901+
* @return The update ApplicationVersion object
902+
* @throws Exception
903+
*/
897904
public ApplicationVersion updateApplicationVersion(
898905
final String appVersionId,
899906
final List<String> declaredPublishedEvents

asyncapi-importer-core/src/main/java/com/solace/ep/asyncapi/importer/mapper/AsyncApiToDto.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,17 @@ private void mapAsyncApiChannelToDto(
146146
// There should only be one message in this list
147147
for ( AsyncApiMessage msg : channel.getSubscribeOpMessages() ) {
148148
// final String schemaFormat = msg.getSchemaFormat();
149-
final String contentType = msg.getContentType();
149+
final String contentType = ( msg.getContentType() != null ? msg.getContentType() : asyncApiAccessor.getDefaultContentType() );
150150
final String payload = msg.getPayloadAsString();
151151
final String schemaName = msg.getSchemaName();
152152
final String messageName = msg.getMessageName();
153-
153+
// channel.getEventPortalEventNameFromChannel() added for ASAPIO imports
154+
final String eventName = channel.getEventPortalEventNameFromChannel() != null ? channel.getEventPortalEventNameFromChannel() : messageName;
155+
154156
// Map the schema, return the object representing the schema version
155157
schemaVersionInThisChannel = mapSchemaDto(schemaName, contentType, payload);
156158

157-
mapEventDto(channelName, messageName, schemaVersionInThisChannel, enumVersionsInThisChannel);
159+
mapEventDto(channelName, eventName, schemaVersionInThisChannel, enumVersionsInThisChannel);
158160
}
159161
}
160162

asyncapi-importer-core/src/test/java/com/solace/ep/asyncapi/importer/TestAsyncApiToDtoMapper.java

+19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.solace.ep.asyncapi.importer;
1919

2020
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
import static org.junit.jupiter.api.Assertions.fail;
2122

2223
import java.io.IOException;
2324
import java.nio.file.Files;
@@ -49,6 +50,24 @@ public void testDebug() {
4950
}
5051
}
5152

53+
@Test
54+
public void testMapAsapioSalesOrder() {
55+
56+
final String ASYNCAPI_ASAPIO_SALES_ORDER = "src/test/resources/asyncapi/asapio/sales-order.json";
57+
58+
try {
59+
AsyncApiAccessor asyncApiAccessor = new AsyncApiAccessor( AsyncApiAccessor.parseAsyncApi(getAsyncApiFile(ASYNCAPI_ASAPIO_SALES_ORDER)) );
60+
AsyncApiToDto mapper = new AsyncApiToDto(asyncApiAccessor, "fictionalId", "FictionalDomain" );
61+
DtoResultSet resultSet = mapper.mapAsyncApiToDto();
62+
63+
assertTrue(resultSet.getMapEnums().size() > 0 );
64+
65+
} catch (Exception exc) {
66+
fail(exc.getLocalizedMessage());
67+
exc.printStackTrace();
68+
}
69+
}
70+
5271
public static String getAsyncApiFile(final String fileName) {
5372

5473
Path path = Paths.get( fileName ); // Path to your file

0 commit comments

Comments
 (0)