diff options
31 files changed, 2021 insertions, 4 deletions
diff --git a/examples/examples-grpc/pom.xml b/examples/examples-grpc/pom.xml new file mode 100644 index 000000000..4113ab33f --- /dev/null +++ b/examples/examples-grpc/pom.xml @@ -0,0 +1,129 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2020 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========================================================= +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.policy.apex-pdp.examples</groupId> + <artifactId>examples</artifactId> + <version>2.3.0-SNAPSHOT</version> + </parent> + <artifactId>examples-grpc</artifactId> + <name>examples-grpc</name> + <description>Specific code for the APEX gRPC Example</description> + <properties> + <policymodel.name>APEXgRPCPolicy</policymodel.name> + <toscapolicy.name>APEXgRPCToscaPolicy</toscapolicy.name> + </properties> + <dependencies> + <dependency> + <groupId>org.onap.policy.apex-pdp.auth</groupId> + <artifactId>cli-editor</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onap.policy.apex-pdp.services</groupId> + <artifactId>services-engine</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onap.policy.apex-pdp.plugins.plugins-context.plugins-context-schema</groupId> + <artifactId>plugins-context-schema-avro</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onap.policy.apex-pdp.plugins.plugins-executor</groupId> + <artifactId>plugins-executor-javascript</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onap.policy.apex-pdp.plugins.plugins-event.plugins-event-carrier</groupId> + <artifactId>plugins-event-carrier-grpc</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onap.policy.apex-pdp.plugins.plugins-event.plugins-event-carrier</groupId> + <artifactId>plugins-event-carrier-restclient</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onap.policy.models.policy-models-interactions.model-impl</groupId> + <artifactId>events</artifactId> + <version>${version.policy.models}</version> + </dependency> + <dependency> + <groupId>org.onap.policy.common</groupId> + <artifactId>policy-endpoints</artifactId> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>exec-maven-plugin</artifactId> + <executions> + <!-- Generate the APEX Policy JSON from the APEX CLI command --> + <execution> + <id>generate-policy</id> + <phase>compile</phase> + <goals> + <goal>java</goal> + </goals> + <configuration> + <mainClass>org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain</mainClass> + <classpathScope>compile</classpathScope> + <arguments> + <argument>--command-file=${project.basedir}/src/main/resources/policy/${policymodel.name}.apex</argument> + <argument>--output-model-file=${project.build.directory}/classes/${policymodel.name}.json</argument> + <argument>--log-file=${project.build.directory}/${policymodel.name}_policygeneration.log</argument> + <argument>--working-dir=${project.basedir}</argument> + </arguments> + </configuration> + </execution> + <!-- Generate the APEX ToscaPolicy JSON file--> + <execution> + <id>generate-tosca-policy</id> + <phase>compile</phase> + <goals> + <goal>java</goal> + </goals> + <configuration> + <mainClass>org.onap.policy.apex.auth.clieditor.tosca.ApexCliToscaEditorMain</mainClass> + <classpathScope>compile</classpathScope> + <arguments> + <argument>--command-file=${project.basedir}/src/main/resources/policy/${policymodel.name}.apex</argument> + <argument>--output-tosca-file=${project.build.directory}/classes/${toscapolicy.name}.json</argument> + <argument>--log-file=${project.build.directory}/${policymodel.name}_policygeneration.log</argument> + <argument>--apex-config-file=${project.basedir}/src/main/resources/examples/config/APEXgRPC/ApexConfig.json</argument> + <argument>--tosca-template-file=${project.basedir}/src/main/resources/tosca/ToscaTemplate.json</argument> + </arguments> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project>
\ No newline at end of file diff --git a/examples/examples-grpc/src/main/resources/examples/config/APEXgRPC/ApexConfig.json b/examples/examples-grpc/src/main/resources/examples/config/APEXgRPC/ApexConfig.json new file mode 100644 index 000000000..553f16ba8 --- /dev/null +++ b/examples/examples-grpc/src/main/resources/examples/config/APEXgRPC/ApexConfig.json @@ -0,0 +1,112 @@ +{ + "engineServiceParameters": { + "name": "MyApexEngine", + "version": "0.0.1", + "id": 45, + "instanceCount": 2, + "deploymentPort": 12561, + "engineParameters": { + "executorParameters": { + "JAVASCRIPT": { + "parameterClassName": "org.onap.policy.apex.plugins.executor.javascript.JavascriptExecutorParameters" + } + }, + "contextParameters": { + "parameterClassName": "org.onap.policy.apex.context.parameters.ContextParameters", + "schemaParameters": { + "Avro": { + "parameterClassName": "org.onap.policy.apex.plugins.context.schema.avro.AvroSchemaHelperParameters" + }, + "Java": { + "parameterClassName": "org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters", + "jsonAdapters": { + "Instant": { + "adaptedClass": "java.time.Instant", + "adaptorClass": "org.onap.policy.controlloop.util.Serialization$GsonInstantAdapter" + } + } + } + } + } + } + }, + "eventInputParameters": { + "DCAEConsumer": { + "carrierTechnologyParameters": { + "carrierTechnology": "RESTCLIENT", + "parameterClassName": "org.onap.policy.apex.plugins.event.carrier.restclient.RestClientCarrierTechnologyParameters", + "parameters": { + "url": "http://localhost:54321/GrpcTestRestSim/sim/events/unauthenticated.DCAE_CL_OUTPUT/APEX/1?timeout=30000" + } + }, + "eventProtocolParameters": { + "eventProtocol": "JSON", + "parameters": { + "nameAlias": "testPolicyB" + } + }, + "eventName": "testPolicyB", + "eventNameFilter": "testPolicyB" + }, + "CDSRequestConsumer": { + "carrierTechnologyParameters": { + "carrierTechnology": "GRPC", + "parameterClassName": "org.onap.policy.apex.plugins.event.carrier.grpc.GrpcCarrierTechnologyParameters" + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + }, + "eventName": "CDSResponseEvent", + "eventNameFilter": "CDSResponseEvent", + "requestorMode": true, + "requestorPeer": "CDSRequestProducer", + "requestorTimeout": 500 + } + }, + "eventOutputParameters": { + "logOutputter": { + "carrierTechnologyParameters": { + "carrierTechnology": "FILE", + "parameters": { + "fileName": "outputevents.log" + } + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + } + }, + "CDSRequestProducer": { + "carrierTechnologyParameters": { + "carrierTechnology": "GRPC", + "parameterClassName": "org.onap.policy.apex.plugins.event.carrier.grpc.GrpcCarrierTechnologyParameters", + "parameters": { + "host": "localhost", + "port": 54322, + "username": "ccsdkapps", + "password": "ccsdkapps", + "timeout": 10 + } + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + }, + "eventNameFilter": "(Create|Delete)SubscriptionRequestEvent", + "requestorMode": true, + "requestorPeer": "CDSRequestConsumer", + "requestorTimeout": 500 + }, + "CDSReplyProducer": { + "carrierTechnologyParameters": { + "carrierTechnology": "RESTCLIENT", + "parameterClassName": "org.onap.policy.apex.plugins.event.carrier.restclient.RestClientCarrierTechnologyParameters", + "parameters": { + "url": "http://localhost:54321/GrpcTestRestSim/sim/events/POLICY_CL_MGT" + } + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + }, + "eventNameFilter": "CDSResponseStatusEvent" + } + } +}
\ No newline at end of file diff --git a/examples/examples-grpc/src/main/resources/examples/events/APEXgRPC/CreateSubscriptionEvent.json b/examples/examples-grpc/src/main/resources/examples/events/APEXgRPC/CreateSubscriptionEvent.json new file mode 100644 index 000000000..7cdfc66fc --- /dev/null +++ b/examples/examples-grpc/src/main/resources/examples/events/APEXgRPC/CreateSubscriptionEvent.json @@ -0,0 +1,54 @@ +{ + "changeType": "CREATE", + "closedLoopControlName": "test", + "policyName": "test", + "nfName": "pnf300", + "subscription": { + "subscriptionName": "testPolicyB", + "administrativeState": "UNLOCKED", + "fileBasedGP": 15, + "fileLocation": "/pm/pm.xml", + "measurementGroups": [ + { + "measurementGroup": { + "measurementTypes": [ + { + "measurementType": "countera" + }, + { + "measurementType": "counterb" + } + ], + "managedObjectDNsBasic": [ + { + "DN": "dna" + }, + { + "DN": "dnb" + } + ] + } + }, + { + "measurementGroup": { + "measurementTypes": [ + { + "measurementType": "counterc" + }, + { + "measurementType": "counterd" + } + ], + "managedObjectDNsBasic": [ + { + "DN": "dnc" + }, + { + "DN": "dnd" + } + ] + } + } + ] + } +}
\ No newline at end of file diff --git a/examples/examples-grpc/src/main/resources/examples/events/APEXgRPC/CreateSubscriptionResponseEvent.json b/examples/examples-grpc/src/main/resources/examples/events/APEXgRPC/CreateSubscriptionResponseEvent.json new file mode 100644 index 000000000..adb51adcb --- /dev/null +++ b/examples/examples-grpc/src/main/resources/examples/events/APEXgRPC/CreateSubscriptionResponseEvent.json @@ -0,0 +1,29 @@ +{ + "commonHeader": { + "timestamp": "2020-03-20T14:00:25.217Z", + "requestId": "123456-1000", + "subRequestId": "sub-123456-1000", + "flag": { + }, + "originatorId": "sdnc" + }, + "actionIdentifiers": { + "blueprintName": "pm_control", + "blueprintVersion": "1.0.0", + "actionName": "create-subscription", + "mode": "sync" + }, + "status": { + "code": 200, + "message": "success", + "eventType": "EVENT_COMPONENT_EXECUTED", + "timestamp": "Fri Mar 20 14:00:26 GMT 2020" + }, + "payload": { + "create-subscription-response": { + "odl-response": { + "status": "success" + } + } + } +}
\ No newline at end of file diff --git a/examples/examples-grpc/src/main/resources/examples/events/APEXgRPC/LogEvent.json b/examples/examples-grpc/src/main/resources/examples/events/APEXgRPC/LogEvent.json new file mode 100644 index 000000000..a86ccd1d7 --- /dev/null +++ b/examples/examples-grpc/src/main/resources/examples/events/APEXgRPC/LogEvent.json @@ -0,0 +1,13 @@ +{ + "name": "CDSResponseStatusEvent", + "version": "0.0.1", + "nameSpace": "org.onap.policy.apex.onap.pmcontrol", + "source": "APEX", + "target": "DCAE", + "status": { + "subscriptionName": "testPolicyB", + "nfName": "pnf300", + "changeType": "CREATE", + "message": "success" + } +}
\ No newline at end of file diff --git a/examples/examples-grpc/src/main/resources/logic/CreateOrDeleteStateTSL.js b/examples/examples-grpc/src/main/resources/logic/CreateOrDeleteStateTSL.js new file mode 100644 index 000000000..010a71dff --- /dev/null +++ b/examples/examples-grpc/src/main/resources/logic/CreateOrDeleteStateTSL.js @@ -0,0 +1,33 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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========================================================= + */ + +executor.logger.info(executor.subject.id); + +var pmSubscriptionInfo = executor.getContextAlbum("PMSubscriptionAlbum").get(executor.inFields.get("albumID").toString()) +var changeType = pmSubscriptionInfo.get("changeType").toString() + +if ("CREATE".equals(changeType)) { + executor.subject.getTaskKey("CreateSubscriptionPayloadTask").copyTo(executor.selectedTask); +} +else if ("DELETE".equals(changeType)) { + executor.subject.getTaskKey("DeleteSubscriptionPayloadTask").copyTo(executor.selectedTask); +} + +true;
\ No newline at end of file diff --git a/examples/examples-grpc/src/main/resources/logic/CreateSubscriptionPayloadTask.js b/examples/examples-grpc/src/main/resources/logic/CreateSubscriptionPayloadTask.js new file mode 100644 index 000000000..48f31847b --- /dev/null +++ b/examples/examples-grpc/src/main/resources/logic/CreateSubscriptionPayloadTask.js @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix. All rights reserved. + * ================================================================================ + * 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========================================================= + */ + +executor.logger.info(executor.subject.id); + +var pmSubscriptionInfo = executor.getContextAlbum("PMSubscriptionAlbum").get(executor.inFields.get("albumID").toString()) + +var payloadProperties = executor.subject.getOutFieldSchemaHelper("payload").createNewSubInstance("create_DasH_subscription_DasH_properties_record"); + +payloadProperties.put("nfName", pmSubscriptionInfo.get("nfName")) +payloadProperties.put("subscriptionName", pmSubscriptionInfo.get("subscription").get("subscriptionName")) +payloadProperties.put("administrativeState", pmSubscriptionInfo.get("subscription").get("administrativeState")) +payloadProperties.put("fileBasedGP", pmSubscriptionInfo.get("subscription").get("fileBasedGP").toString()) +payloadProperties.put("fileLocation", pmSubscriptionInfo.get("subscription").get("fileLocation")) +payloadProperties.put("measurementGroups", pmSubscriptionInfo.get("subscription").get("measurementGroups")) + +var payloadEntry = executor.subject.getOutFieldSchemaHelper("payload").createNewSubInstance("CDSRequestPayloadEntry"); +payloadEntry.put("create_DasH_subscription_DasH_properties", payloadProperties) + +var payload = executor.subject.getOutFieldSchemaHelper("payload").createNewInstance(); +payload.put("create_DasH_subscription_DasH_request", payloadEntry); + +executor.outFields.put("albumID", executor.inFields.get("albumID")) +executor.outFields.put("payload", payload); + +executor.logger.info("Sending Create Subscription Event to CDS") + +true; diff --git a/examples/examples-grpc/src/main/resources/logic/CreateSubscriptionRequestTask.js b/examples/examples-grpc/src/main/resources/logic/CreateSubscriptionRequestTask.js new file mode 100644 index 000000000..638429817 --- /dev/null +++ b/examples/examples-grpc/src/main/resources/logic/CreateSubscriptionRequestTask.js @@ -0,0 +1,44 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix. All rights reserved. + * ================================================================================ + * 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========================================================= + */ + +executor.logger.info(executor.subject.id); + +var pmSubscriptionInfo = executor.getContextAlbum("PMSubscriptionAlbum").get(executor.inFields.get("albumID").toString()) +var payload = executor.inFields.get("payload") +var actionName = "create-subscription" + +var commonHeader = executor.subject.getOutFieldSchemaHelper("commonHeader").createNewInstance(); +commonHeader.put("originatorId", "sdnc"); +commonHeader.put("requestId", "123456-1000"); +commonHeader.put("subRequestId", "sub-123456-1000"); + +var actionIdentifiers = executor.subject.getOutFieldSchemaHelper("actionIdentifiers").createNewInstance(); +actionIdentifiers.put("actionName", actionName); +actionIdentifiers.put("blueprintName", "pm_control"); +actionIdentifiers.put("blueprintVersion", "1.0.0"); +actionIdentifiers.put("mode", "sync"); + +executor.outFields.put("commonHeader", commonHeader); +executor.outFields.put("actionIdentifiers", actionIdentifiers); +executor.outFields.put("payload", payload); + +executor.logger.info("Sending Activate Subscription Event to CDS") + +true;
\ No newline at end of file diff --git a/examples/examples-grpc/src/main/resources/logic/DeleteSubscriptionPayloadTask.js b/examples/examples-grpc/src/main/resources/logic/DeleteSubscriptionPayloadTask.js new file mode 100644 index 000000000..180dcba71 --- /dev/null +++ b/examples/examples-grpc/src/main/resources/logic/DeleteSubscriptionPayloadTask.js @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix. All rights reserved. + * ================================================================================ + * 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========================================================= + */ + +executor.logger.info(executor.subject.id); + +var pmSubscriptionInfo = executor.getContextAlbum("PMSubscriptionAlbum").get(executor.inFields.get("albumID").toString()) + +var payloadProperties = executor.subject.getOutFieldSchemaHelper("payload").createNewSubInstance("delete_DasH_subscription_DasH_properties_record"); + +payloadProperties.put("nfName", pmSubscriptionInfo.get("nfName")) +payloadProperties.put("subscriptionName", pmSubscriptionInfo.get("subscription").get("subscriptionName")) +payloadProperties.put("administrativeState", pmSubscriptionInfo.get("subscription").get("administrativeState")) +payloadProperties.put("fileBasedGP", pmSubscriptionInfo.get("subscription").get("fileBasedGP").toString()) +payloadProperties.put("fileLocation", pmSubscriptionInfo.get("subscription").get("fileLocation")) +payloadProperties.put("measurementGroups", pmSubscriptionInfo.get("subscription").get("measurementGroups")) + +var payloadEntry = executor.subject.getOutFieldSchemaHelper("payload").createNewSubInstance("CDSRequestPayloadEntry"); +payloadEntry.put("delete_DasH_subscription_DasH_properties", payloadProperties) + +var payload = executor.subject.getOutFieldSchemaHelper("payload").createNewInstance(); +payload.put("delete_DasH_subscription_DasH_request", payloadEntry); + +executor.outFields.put("albumID", executor.inFields.get("albumID")) +executor.outFields.put("payload", payload); + +executor.logger.info("Sending delete Subscription Event to CDS") + +true; diff --git a/examples/examples-grpc/src/main/resources/logic/DeleteSubscriptionRequestTask.js b/examples/examples-grpc/src/main/resources/logic/DeleteSubscriptionRequestTask.js new file mode 100644 index 000000000..9ec2a119b --- /dev/null +++ b/examples/examples-grpc/src/main/resources/logic/DeleteSubscriptionRequestTask.js @@ -0,0 +1,44 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix. All rights reserved. + * ================================================================================ + * 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========================================================= + */ + +executor.logger.info(executor.subject.id); + +var pmSubscriptionInfo = executor.getContextAlbum("PMSubscriptionAlbum").get(executor.inFields.get("albumID").toString()) +var payload = executor.inFields.get("payload") +var actionName = "delete-subscription" + +var commonHeader = executor.subject.getOutFieldSchemaHelper("commonHeader").createNewInstance(); +commonHeader.put("originatorId", "sdnc"); +commonHeader.put("requestId", "123456-1000"); +commonHeader.put("subRequestId", "sub-123456-1000"); + +var actionIdentifiers = executor.subject.getOutFieldSchemaHelper("actionIdentifiers").createNewInstance(); +actionIdentifiers.put("actionName", actionName); +actionIdentifiers.put("blueprintName", "pm_control"); +actionIdentifiers.put("blueprintVersion", "1.0.0"); +actionIdentifiers.put("mode", "sync"); + +executor.outFields.put("commonHeader", commonHeader); +executor.outFields.put("actionIdentifiers", actionIdentifiers); +executor.outFields.put("payload", payload); + +executor.logger.info("Sending Deactivate Subscription Event to CDS") + +true;
\ No newline at end of file diff --git a/examples/examples-grpc/src/main/resources/logic/ReceivePMSubscriptionTask.js b/examples/examples-grpc/src/main/resources/logic/ReceivePMSubscriptionTask.js new file mode 100644 index 000000000..bc4028a32 --- /dev/null +++ b/examples/examples-grpc/src/main/resources/logic/ReceivePMSubscriptionTask.js @@ -0,0 +1,48 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix. All rights reserved. + * ================================================================================ + * 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========================================================= + */ +var uuidType = java.util.UUID; + + +//albumID will be used to fetch info from our album later +var albumID = uuidType.fromString("d0050623-18e5-46c9-9298-9a567990cd7c"); +var pmSubscriptionInfo = executor.getContextAlbum("PMSubscriptionAlbum").getSchemaHelper().createNewInstance(); +var returnValue = true;; + +if(executor.inFields.get("policyName") != null) { + var changeType = executor.inFields.get("changeType") + var nfName = executor.inFields.get("nfName") + var policyName = executor.inFields.get("policyName") + var closedLoopControlName = executor.inFields.get("closedLoopControlName") + var subscription = executor.inFields.get("subscription") + + pmSubscriptionInfo.put("nfName", executor.inFields.get("nfName")); + pmSubscriptionInfo.put("changeType", executor.inFields.get("changeType")) + pmSubscriptionInfo.put("policyName", executor.inFields.get("policyName")) + pmSubscriptionInfo.put("closedLoopControlName", executor.inFields.get("closedLoopControlName")) + pmSubscriptionInfo.put("subscription", subscription) + + executor.getContextAlbum("PMSubscriptionAlbum").put(albumID.toString(), pmSubscriptionInfo); + + executor.outFields.put("albumID", albumID) +} else { + executor.message = "Received invalid event" + returnValue = false; +} +returnValue;
\ No newline at end of file diff --git a/examples/examples-grpc/src/main/resources/logic/ResponseTask.js b/examples/examples-grpc/src/main/resources/logic/ResponseTask.js new file mode 100644 index 000000000..04517763a --- /dev/null +++ b/examples/examples-grpc/src/main/resources/logic/ResponseTask.js @@ -0,0 +1,44 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix. All rights reserved. + * ================================================================================ + * 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========================================================= + */ + + +var uuidType = java.util.UUID; + +var albumID = uuidType.fromString("d0050623-18e5-46c9-9298-9a567990cd7c"); + +var pmSubscriptionInfo = executor.getContextAlbum("PMSubscriptionAlbum").get(albumID.toString()); + +var responseStatus = executor.subject.getOutFieldSchemaHelper("status").createNewInstance(); + +responseStatus.put("subscriptionName", pmSubscriptionInfo.get("subscription").get("subscriptionName")) +responseStatus.put("nfName", pmSubscriptionInfo.get("nfName")) +responseStatus.put("changeType", pmSubscriptionInfo.get("changeType")) + +var response = executor.inFields.get("payload") + +if ("failure".equals(response.get("create_DasH_subscription_DasH_response").get("odl_DasH_response").get("status"))) { + responseStatus.put("message", "failed") +} else { + responseStatus.put("message", "success") +} + +executor.outFields.put("status", responseStatus) + +true; diff --git a/examples/examples-grpc/src/main/resources/policy/APEXgRPCPolicy.apex b/examples/examples-grpc/src/main/resources/policy/APEXgRPCPolicy.apex new file mode 100644 index 000000000..b0cbcb7c0 --- /dev/null +++ b/examples/examples-grpc/src/main/resources/policy/APEXgRPCPolicy.apex @@ -0,0 +1,232 @@ +#------------------------------------------------------------------------------- +# ============LICENSE_START======================================================= +# Copyright (C) 2020 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========================================================= +#------------------------------------------------------------------------------- + +model create name=APEXgRPCPolicy + +## +## SCHEMAS +## + +schema create name=SimpleStringType flavour=Java schema=java.lang.String +schema create name=UUIDType flavour=Java schema=java.util.UUID +schema create name=SimpleIntType flavour=Java schema=java.lang.Integer + +schema create name=CDSRequestCommonHeaderType flavour=Avro schema=LS +#MACROFILE:"src/main/resources/schemas/CDSRequestCommonHeaderType.avsc" +LE + +schema create name=CDSResponseCommonHeaderType flavour=Avro schema=LS +#MACROFILE:"src/main/resources/schemas/CDSResponseCommonHeaderType.avsc" +LE + +schema create name=CDSActionIdentifiersType flavour=Avro schema=LS +#MACROFILE:"src/main/resources/schemas/CDSActionIdentifiersType.avsc" +LE + +schema create name=CDSResponseStatusType flavour=Avro schema=LS +#MACROFILE:"src/main/resources/schemas/CDSResponseStatusType.avsc" +LE + +schema create name=CDSCreateSubscriptionPayloadType flavour=Avro schema=LS +#MACROFILE:"src/main/resources/schemas/CDSCreateSubscriptionPayloadType.avsc" +LE + +schema create name=CDSDeleteSubscriptionPayloadType flavour=Avro schema=LS +#MACROFILE:"src/main/resources/schemas/CDSDeleteSubscriptionPayloadType.avsc" +LE + +schema create name=CDSResponsePayloadType flavour=Avro schema=LS +#MACROFILE:"src/main/resources/schemas/CDSResponsePayloadType.avsc" +LE + +schema create name=SubscriptionStatusType flavour=Avro schema=LS +#MACROFILE:"src/main/resources/schemas/SubscriptionStatusType.avsc" +LE + +schema create name=PMSubscriptionType flavour=Avro schema=LS +#MACROFILE:"src/main/resources/schemas/PMSubscriptionType.avsc" +LE + +schema create name=SubscriptionType flavour=Avro schema=LS +#MACROFILE:"src/main/resources/schemas/SubscriptionType.avsc" +LE + +## +## EVENTS +## + +album create name=PMSubscriptionAlbum scope=policy writable=true schemaName=PMSubscriptionType + +event create name=testPolicyB version=0.0.1 nameSpace=org.onap.policy.apex.onap.pmcontrol source=DCAE target=APEX +event parameter create name=testPolicyB parName=nfName schemaName=SimpleStringType +event parameter create name=testPolicyB parName=policyName schemaName=SimpleStringType +event parameter create name=testPolicyB parName=changeType schemaName=SimpleStringType +event parameter create name=testPolicyB parName=closedLoopControlName schemaName=SimpleStringType +event parameter create name=testPolicyB parName=subscription schemaName=SubscriptionType + +event create name=PMSubscriptionOutputEvent nameSpace=org.onap.policy.apex.onap.pmcontrol source=APEX target=APEX +event parameter create name=PMSubscriptionOutputEvent parName=albumID schemaName=UUIDType + +event create name=CreateSubscriptionPayloadEvent nameSpace=org.onap.policy.apex.onap.pmcontrol source=APEX target=APEX +event parameter create name=CreateSubscriptionPayloadEvent parName=payload schemaName=CDSCreateSubscriptionPayloadType +event parameter create name=CreateSubscriptionPayloadEvent parName=albumID schemaName=UUIDType + +event create name=DeleteSubscriptionPayloadEvent nameSpace=org.onap.policy.apex.onap.pmcontrol source=APEX target=APEX +event parameter create name=DeleteSubscriptionPayloadEvent parName=payload schemaName=CDSDeleteSubscriptionPayloadType +event parameter create name=DeleteSubscriptionPayloadEvent parName=albumID schemaName=UUIDType + +event create name=CreateSubscriptionRequestEvent nameSpace=org.onap.policy.apex.onap.pmcontrol source=APEX target=APEX +event parameter create name=CreateSubscriptionRequestEvent parName=commonHeader schemaName=CDSRequestCommonHeaderType +event parameter create name=CreateSubscriptionRequestEvent parName=actionIdentifiers schemaName=CDSActionIdentifiersType +event parameter create name=CreateSubscriptionRequestEvent parName=payload schemaName=CDSCreateSubscriptionPayloadType + +event create name=DeleteSubscriptionRequestEvent nameSpace=org.onap.policy.apex.onap.pmcontrol source=APEX target=APEX +event parameter create name=DeleteSubscriptionRequestEvent parName=commonHeader schemaName=CDSRequestCommonHeaderType +event parameter create name=DeleteSubscriptionRequestEvent parName=actionIdentifiers schemaName=CDSActionIdentifiersType +event parameter create name=DeleteSubscriptionRequestEvent parName=payload schemaName=CDSDeleteSubscriptionPayloadType + +event create name=CDSResponseEvent nameSpace=org.onap.policy.apex.onap.pmcontrol source=CDS target=APEX +event parameter create name=CDSResponseEvent parName=commonHeader schemaName=CDSResponseCommonHeaderType +event parameter create name=CDSResponseEvent parName=actionIdentifiers schemaName=CDSActionIdentifiersType +event parameter create name=CDSResponseEvent parName=status schemaName=CDSResponseStatusType +event parameter create name=CDSResponseEvent parName=payload schemaName=CDSResponsePayloadType + +event create name=CDSResponseStatusEvent nameSpace=org.onap.policy.apex.onap.pmcontrol source=APEX target=DCAE +event parameter create name=CDSResponseStatusEvent parName=status schemaName=SubscriptionStatusType + +## +## TASKS +## + + +task create name=ReceivePMSubscriptionTask +task inputfield create name=ReceivePMSubscriptionTask fieldName=subscription schemaName=SubscriptionType +task inputfield create name=ReceivePMSubscriptionTask fieldName=nfName schemaName=SimpleStringType +task inputfield create name=ReceivePMSubscriptionTask fieldName=policyName schemaName=SimpleStringType +task inputfield create name=ReceivePMSubscriptionTask fieldName=changeType schemaName=SimpleStringType +task inputfield create name=ReceivePMSubscriptionTask fieldName=closedLoopControlName schemaName=SimpleStringType +task outputfield create name=ReceivePMSubscriptionTask fieldName=albumID schemaName=UUIDType +task contextref create name=ReceivePMSubscriptionTask albumName=PMSubscriptionAlbum +task logic create name=ReceivePMSubscriptionTask logicFlavour=JAVASCRIPT logic=LS +#MACROFILE:"src/main/resources/logic/ReceivePMSubscriptionTask.js" +LE + +task create name=CreateSubscriptionPayloadTask +task inputfield create name=CreateSubscriptionPayloadTask fieldName=albumID schemaName=UUIDType +task outputfield create name=CreateSubscriptionPayloadTask fieldName=payload schemaName=CDSCreateSubscriptionPayloadType +task outputfield create name=CreateSubscriptionPayloadTask fieldName=albumID schemaName=UUIDType +task contextref create name=CreateSubscriptionPayloadTask albumName=PMSubscriptionAlbum +task logic create name=CreateSubscriptionPayloadTask logicFlavour=JAVASCRIPT logic=LS +#MACROFILE:"src/main/resources/logic/CreateSubscriptionPayloadTask.js" +LE + +task create name=DeleteSubscriptionPayloadTask +task inputfield create name=DeleteSubscriptionPayloadTask fieldName=albumID schemaName=UUIDType +task outputfield create name=DeleteSubscriptionPayloadTask fieldName=payload schemaName=CDSDeleteSubscriptionPayloadType +task outputfield create name=DeleteSubscriptionPayloadTask fieldName=albumID schemaName=UUIDType +task contextref create name=DeleteSubscriptionPayloadTask albumName=PMSubscriptionAlbum +task logic create name=DeleteSubscriptionPayloadTask logicFlavour=JAVASCRIPT logic=LS +#MACROFILE:"src/main/resources/logic/DeleteSubscriptionPayloadTask.js" +LE + +task create name=CreateSubscriptionRequestTask +task inputfield create name=CreateSubscriptionRequestTask fieldName=albumID schemaName=UUIDType +task inputfield create name=CreateSubscriptionRequestTask fieldName=payload schemaName=CDSCreateSubscriptionPayloadType +task outputfield create name=CreateSubscriptionRequestTask fieldName=commonHeader schemaName=CDSRequestCommonHeaderType +task outputfield create name=CreateSubscriptionRequestTask fieldName=actionIdentifiers schemaName=CDSActionIdentifiersType +task outputfield create name=CreateSubscriptionRequestTask fieldName=payload schemaName=CDSCreateSubscriptionPayloadType +task contextref create name=CreateSubscriptionRequestTask albumName=PMSubscriptionAlbum +task logic create name=CreateSubscriptionRequestTask logicFlavour=JAVASCRIPT logic=LS +#MACROFILE:"src/main/resources/logic/CreateSubscriptionRequestTask.js" +LE + +task create name=DeleteSubscriptionRequestTask +task inputfield create name=DeleteSubscriptionRequestTask fieldName=albumID schemaName=UUIDType +task inputfield create name=DeleteSubscriptionRequestTask fieldName=payload schemaName=CDSDeleteSubscriptionPayloadType +task outputfield create name=DeleteSubscriptionRequestTask fieldName=commonHeader schemaName=CDSRequestCommonHeaderType +task outputfield create name=DeleteSubscriptionRequestTask fieldName=actionIdentifiers schemaName=CDSActionIdentifiersType +task outputfield create name=DeleteSubscriptionRequestTask fieldName=payload schemaName=CDSDeleteSubscriptionPayloadType +task contextref create name=DeleteSubscriptionRequestTask albumName=PMSubscriptionAlbum +task logic create name=DeleteSubscriptionRequestTask logicFlavour=JAVASCRIPT logic=LS +#MACROFILE:"src/main/resources/logic/DeleteSubscriptionRequestTask.js" +LE + +task create name=CDSResponseTask +task inputfield create name=CDSResponseTask fieldName=commonHeader schemaName=CDSResponseCommonHeaderType +task inputfield create name=CDSResponseTask fieldName=actionIdentifiers schemaName=CDSActionIdentifiersType +task inputfield create name=CDSResponseTask fieldName=status schemaName=CDSResponseStatusType +task inputfield create name=CDSResponseTask fieldName=payload schemaName=CDSResponsePayloadType +task outputfield create name=CDSResponseTask fieldName=status schemaName=SubscriptionStatusType +task contextref create name=CDSResponseTask albumName=PMSubscriptionAlbum +task logic create name=CDSResponseTask logicFlavour=JAVASCRIPT logic=LS +#MACROFILE:"src/main/resources/logic/ResponseTask.js" +LE + + +## +## POLICIES +## + + +# Policy ReceiveEventPolicy +policy create name=ReceiveEventPolicy template=Freestyle firstState=ReceiveSubscriptionState + +# State CreateSubscription +policy state create name=ReceiveEventPolicy stateName=CreateSubscription triggerName=CreateSubscriptionPayloadEvent defaultTaskName=CreateSubscriptionRequestTask +policy state output create name=ReceiveEventPolicy stateName=CreateSubscription outputName=IssueCreateSubscriptionRequestOutput eventName=CreateSubscriptionRequestEvent nextState=NULL +policy state taskref create name=ReceiveEventPolicy stateName=CreateSubscription taskName=CreateSubscriptionRequestTask outputType=DIRECT outputName=IssueCreateSubscriptionRequestOutput +policy state contextref create name=ReceiveEventPolicy stateName=CreateSubscription albumName=PMSubscriptionAlbum + +# State DeleteSubscription +policy state create name=ReceiveEventPolicy stateName=DeleteSubscription triggerName=DeleteSubscriptionPayloadEvent defaultTaskName=DeleteSubscriptionRequestTask +policy state output create name=ReceiveEventPolicy stateName=DeleteSubscription outputName=IssueDeleteSubscriptionRequestOutput eventName=DeleteSubscriptionRequestEvent nextState=NULL +policy state taskref create name=ReceiveEventPolicy stateName=DeleteSubscription taskName=DeleteSubscriptionRequestTask outputType=DIRECT outputName=IssueDeleteSubscriptionRequestOutput +policy state contextref create name=ReceiveEventPolicy stateName=DeleteSubscription albumName=PMSubscriptionAlbum + +# State CreateOrDeleteState +policy state create name=ReceiveEventPolicy stateName=CreateOrDeleteState triggerName=PMSubscriptionOutputEvent defaultTaskName=CreateSubscriptionPayloadTask + +policy state output create name=ReceiveEventPolicy stateName=CreateOrDeleteState outputName=CreateSubscriptionPayload eventName=CreateSubscriptionPayloadEvent nextState=CreateSubscription +policy state taskref create name=ReceiveEventPolicy stateName=CreateOrDeleteState taskName=CreateSubscriptionPayloadTask outputType=DIRECT outputName=CreateSubscriptionPayload + +policy state output create name=ReceiveEventPolicy stateName=CreateOrDeleteState outputName=DeleteSubscriptionPayload eventName=DeleteSubscriptionPayloadEvent nextState=DeleteSubscription +policy state taskref create name=ReceiveEventPolicy stateName=CreateOrDeleteState taskName=DeleteSubscriptionPayloadTask outputType=DIRECT outputName=DeleteSubscriptionPayload + +policy state contextref create name=ReceiveEventPolicy stateName=CreateOrDeleteState albumName=PMSubscriptionAlbum +policy state selecttasklogic create name=ReceiveEventPolicy stateName=CreateOrDeleteState logicFlavour=JAVASCRIPT logic=LS +#MACROFILE:"src/main/resources/logic/CreateOrDeleteStateTSL.js" +LE + +# State ReceiveSubscriptionState +policy state create name=ReceiveEventPolicy stateName=ReceiveSubscriptionState triggerName=testPolicyB defaultTaskName=ReceivePMSubscriptionTask +policy state output create name=ReceiveEventPolicy stateName=ReceiveSubscriptionState outputName=ReceivePMSubscriptionOutput eventName=PMSubscriptionOutputEvent nextState=CreateOrDeleteState +policy state taskref create name=ReceiveEventPolicy stateName=ReceiveSubscriptionState taskName=ReceivePMSubscriptionTask outputType=DIRECT outputName=ReceivePMSubscriptionOutput +policy state contextref create name=ReceiveEventPolicy stateName=ReceiveSubscriptionState albumName=PMSubscriptionAlbum + + +# Policy CDSResponsePolicy +policy create name=CDSResponsePolicy template=Freestyle firstState=CDSResponseState + +# State CDSResponseState +policy state create name=CDSResponsePolicy stateName=CDSResponseState triggerName=CDSResponseEvent defaultTaskName=CDSResponseTask +policy state output create name=CDSResponsePolicy stateName=CDSResponseState outputName=ResponseOutput eventName=CDSResponseStatusEvent +policy state taskref create name=CDSResponsePolicy stateName=CDSResponseState taskName=CDSResponseTask outputType=DIRECT outputName=ResponseOutput + +validate
\ No newline at end of file diff --git a/examples/examples-grpc/src/main/resources/schemas/CDSActionIdentifiersType.avsc b/examples/examples-grpc/src/main/resources/schemas/CDSActionIdentifiersType.avsc new file mode 100644 index 000000000..1d5551704 --- /dev/null +++ b/examples/examples-grpc/src/main/resources/schemas/CDSActionIdentifiersType.avsc @@ -0,0 +1,23 @@ +{ + "type": "record", + "name": "CDSActionIdentifiers_Type", + "namespace": "org.onap.policy.apex.onap.helloworld", + "fields": [ + { + "name": "actionName", + "type": "string" + }, + { + "name": "blueprintName", + "type": "string" + }, + { + "name": "blueprintVersion", + "type": "string" + }, + { + "name": "mode", + "type": "string" + } + ] +} diff --git a/examples/examples-grpc/src/main/resources/schemas/CDSCreateSubscriptionPayloadType.avsc b/examples/examples-grpc/src/main/resources/schemas/CDSCreateSubscriptionPayloadType.avsc new file mode 100644 index 000000000..4dc03d300 --- /dev/null +++ b/examples/examples-grpc/src/main/resources/schemas/CDSCreateSubscriptionPayloadType.avsc @@ -0,0 +1,91 @@ +{ + "type": "map", + "values": { + "type": "record", + "name": "CDSRequestPayloadEntry", + "fields": [ + { + "name": "create_DasH_subscription_DasH_properties", + "type": { + "name": "create_DasH_subscription_DasH_properties_record", + "type": "record", + "fields": [ + { + "name": "nfName", + "type": "string" + }, + { + "name": "subscriptionName", + "type": "string" + }, + { + "name": "administrativeState", + "type": "string" + }, + { + "name": "fileBasedGP", + "type": "string" + }, + { + "name": "fileLocation", + "type": "string" + }, + { + "name": "measurementGroups", + "type": { + "type": "array", + "items": { + "name": "measurementGroups_record", + "type": "record", + "fields": [ + { + "name": "measurementGroup", + "type": { + "name": "measurementGroup", + "type": "record", + "fields": [ + { + "name": "measurementTypes", + "type": { + "type": "array", + "items": { + "name": "measurementTypes_record", + "type": "record", + "fields": [ + { + "name": "measurementType", + "type": "string" + } + ] + } + } + }, + { + "name": "managedObjectDNsBasic", + "type": { + "type": "array", + "items": { + "name": "managedObjectDNsBasic_record", + "type": "record", + "fields": [ + { + "name": "DN", + "type": "string" + } + ] + } + } + } + ] + } + } + ] + } + } + } + ] + } + } + ] + } +}
\ No newline at end of file diff --git a/examples/examples-grpc/src/main/resources/schemas/CDSDeleteSubscriptionPayloadType.avsc b/examples/examples-grpc/src/main/resources/schemas/CDSDeleteSubscriptionPayloadType.avsc new file mode 100644 index 000000000..437c51c70 --- /dev/null +++ b/examples/examples-grpc/src/main/resources/schemas/CDSDeleteSubscriptionPayloadType.avsc @@ -0,0 +1,91 @@ +{ + "type": "map", + "values": { + "type": "record", + "name": "CDSRequestPayloadEntry", + "fields": [ + { + "name": "delete_DasH_subscription_DasH_properties", + "type": { + "name": "delete_DasH_subscription_DasH_properties_record", + "type": "record", + "fields": [ + { + "name": "nfName", + "type": "string" + }, + { + "name": "subscriptionName", + "type": "string" + }, + { + "name": "administrativeState", + "type": "string" + }, + { + "name": "fileBasedGP", + "type": "string" + }, + { + "name": "fileLocation", + "type": "string" + }, + { + "name": "measurementGroups", + "type": { + "type": "array", + "items": { + "name": "measurementGroups_record", + "type": "record", + "fields": [ + { + "name": "measurementGroup", + "type": { + "name": "measurementGroup", + "type": "record", + "fields": [ + { + "name": "measurementTypes", + "type": { + "type": "array", + "items": { + "name": "measurementTypes_record", + "type": "record", + "fields": [ + { + "name": "measurementType", + "type": "string" + } + ] + } + } + }, + { + "name": "managedObjectDNsBasic", + "type": { + "type": "array", + "items": { + "name": "managedObjectDNsBasic_record", + "type": "record", + "fields": [ + { + "name": "DN", + "type": "string" + } + ] + } + } + } + ] + } + } + ] + } + } + } + ] + } + } + ] + } +}
\ No newline at end of file diff --git a/examples/examples-grpc/src/main/resources/schemas/CDSRequestCommonHeaderType.avsc b/examples/examples-grpc/src/main/resources/schemas/CDSRequestCommonHeaderType.avsc new file mode 100644 index 000000000..7e7e9256c --- /dev/null +++ b/examples/examples-grpc/src/main/resources/schemas/CDSRequestCommonHeaderType.avsc @@ -0,0 +1,19 @@ +{ + "type": "record", + "name": "CDSRequestCommonHeader_Type", + "namespace": "org.onap.policy.apex.onap.helloworld", + "fields": [ + { + "name": "originatorId", + "type": "string" + }, + { + "name": "requestId", + "type": "string" + }, + { + "name": "subRequestId", + "type": "string" + } + ] +} diff --git a/examples/examples-grpc/src/main/resources/schemas/CDSResponseCommonHeaderType.avsc b/examples/examples-grpc/src/main/resources/schemas/CDSResponseCommonHeaderType.avsc new file mode 100644 index 000000000..fe2fae8c7 --- /dev/null +++ b/examples/examples-grpc/src/main/resources/schemas/CDSResponseCommonHeaderType.avsc @@ -0,0 +1,23 @@ +{ + "type": "record", + "name": "CDSResponseCommonHeader_Type", + "namespace": "org.onap.policy.apex.onap.helloworld", + "fields": [ + { + "name": "originatorId", + "type": "string" + }, + { + "name": "requestId", + "type": "string" + }, + { + "name": "subRequestId", + "type": "string" + }, + { + "name": "timestamp", + "type": "string" + } + ] +} diff --git a/examples/examples-grpc/src/main/resources/schemas/CDSResponsePayloadType.avsc b/examples/examples-grpc/src/main/resources/schemas/CDSResponsePayloadType.avsc new file mode 100644 index 000000000..e8a958119 --- /dev/null +++ b/examples/examples-grpc/src/main/resources/schemas/CDSResponsePayloadType.avsc @@ -0,0 +1,29 @@ +{ + "name": "CDSResponsePayloadEntry", + "type": "record", + "namespace": "com.acme.avro", + "fields": [ + { + "name": "create_DasH_subscription_DasH_response", + "type": { + "name": "create_DasH_subscription_DasH_response", + "type": "record", + "fields": [ + { + "name": "odl_DasH_response", + "type": { + "name": "odl_DasH_response", + "type": "record", + "fields": [ + { + "name": "status", + "type": "string" + } + ] + } + } + ] + } + } + ] +} diff --git a/examples/examples-grpc/src/main/resources/schemas/CDSResponseStatusType.avsc b/examples/examples-grpc/src/main/resources/schemas/CDSResponseStatusType.avsc new file mode 100644 index 000000000..fa2dfa7d6 --- /dev/null +++ b/examples/examples-grpc/src/main/resources/schemas/CDSResponseStatusType.avsc @@ -0,0 +1,23 @@ +{ + "type": "record", + "name": "CDSResponseStatus_Type", + "namespace": "org.onap.policy.apex.onap.helloworld", + "fields": [ + { + "name": "code", + "type": "int" + }, + { + "name": "eventType", + "type": "string" + }, + { + "name": "timestamp", + "type": "string" + }, + { + "name": "message", + "type": "string" + } + ] +} diff --git a/examples/examples-grpc/src/main/resources/schemas/PMSubscriptionType.avsc b/examples/examples-grpc/src/main/resources/schemas/PMSubscriptionType.avsc new file mode 100644 index 000000000..1d9c7d4b4 --- /dev/null +++ b/examples/examples-grpc/src/main/resources/schemas/PMSubscriptionType.avsc @@ -0,0 +1,101 @@ +{ + "name": "PMSubscription", + "type": "record", + "namespace": "org.onap.policy.apex.onap.pmcontrol", + "fields": [ + { + "name": "nfName", + "type": "string" + }, + { + "name": "changeType", + "type": "string" + }, + { + "name": "closedLoopControlName", + "type": "string" + }, + { + "name": "policyName", + "type": "string" + }, + { + "name": "subscription", + "type": { + "name": "subscription", + "type": "record", + "fields": [ + { + "name": "subscriptionName", + "type": "string" + }, + { + "name": "administrativeState", + "type": "string" + }, + { + "name": "fileBasedGP", + "type": "int" + }, + { + "name": "fileLocation", + "type": "string" + }, + { + "name": "measurementGroups", + "type": { + "type": "array", + "items": { + "name": "Measurement_Groups_Type", + "type": "record", + "fields": [ + { + "name": "measurementGroup", + "type": { + "name": "Measurement_Group_Type", + "type": "record", + "fields": [ + { + "name": "measurementTypes", + "type": { + "type": "array", + "items": { + "name": "Measurement_Types_Type", + "type": "record", + "fields": [ + { + "name": "measurementType", + "type": "string" + } + ] + } + } + }, + { + "name": "managedObjectDNsBasic", + "type": { + "type": "array", + "items": { + "name": "Managed_Object_Dns_Basic_Type", + "type": "record", + "fields": [ + { + "name": "DN", + "type": "string" + } + ] + } + } + } + ] + } + } + ] + } + } + } + ] + } + } + ] +}
\ No newline at end of file diff --git a/examples/examples-grpc/src/main/resources/schemas/SubscriptionStatusType.avsc b/examples/examples-grpc/src/main/resources/schemas/SubscriptionStatusType.avsc new file mode 100644 index 000000000..247ba8a8c --- /dev/null +++ b/examples/examples-grpc/src/main/resources/schemas/SubscriptionStatusType.avsc @@ -0,0 +1,23 @@ +{ + "type": "record", + "name": "ActivateSubscriptionStatus_Type", + "namespace": "org.onap.policy.apex.onap.helloworld", + "fields": [ + { + "name": "subscriptionName", + "type": "string" + }, + { + "name": "nfName", + "type": "string" + }, + { + "name": "changeType", + "type": "string" + }, + { + "name": "message", + "type": "string" + } + ] +}
\ No newline at end of file diff --git a/examples/examples-grpc/src/main/resources/schemas/SubscriptionType.avsc b/examples/examples-grpc/src/main/resources/schemas/SubscriptionType.avsc new file mode 100644 index 000000000..7172b7aab --- /dev/null +++ b/examples/examples-grpc/src/main/resources/schemas/SubscriptionType.avsc @@ -0,0 +1,75 @@ +{ + "name": "subscription", + "type": "record", + "fields": [ + { + "name": "subscriptionName", + "type": "string" + }, + { + "name": "administrativeState", + "type": "string" + }, + { + "name": "fileBasedGP", + "type": "int" + }, + { + "name": "fileLocation", + "type": "string" + }, + { + "name": "measurementGroups", + "type": { + "type": "array", + "items": { + "name": "Measurement_Groups_Type", + "type": "record", + "fields": [ + { + "name": "measurementGroup", + "type": { + "name": "Measurement_Group_Type", + "type": "record", + "fields": [ + { + "name": "measurementTypes", + "type": { + "type": "array", + "items": { + "name": "Measurement_Types_Type", + "type": "record", + "fields": [ + { + "name": "measurementType", + "type": "string" + } + ] + } + } + }, + { + "name": "managedObjectDNsBasic", + "type": { + "type": "array", + "items": { + "name": "Managed_Object_Dns_Basic_Type", + "type": "record", + "fields": [ + { + "name": "DN", + "type": "string" + } + ] + } + } + } + ] + } + } + ] + } + } + } + ] +}
\ No newline at end of file diff --git a/examples/examples-grpc/src/main/resources/tosca/ToscaPolicyType.json b/examples/examples-grpc/src/main/resources/tosca/ToscaPolicyType.json new file mode 100644 index 000000000..33c7337db --- /dev/null +++ b/examples/examples-grpc/src/main/resources/tosca/ToscaPolicyType.json @@ -0,0 +1,260 @@ +{ + "tosca_definitions_version": "tosca_simple_yaml_1_1_0", + "policy_types": { + "onap.policies.controlloop.operational.Apex": { + "version": "1.0.0", + "description": "Operational Policy for Control Loops using the APEX PDP", + "properties": { + "engine_service": { + "type": "onap.datatypes.policies.controlloop.operational.apex.EngineService", + "description": "APEX Engine Service Parameters" + }, + "inputs": { + "type": "map", + "description": "Inputs for handling events coming into the APEX engine", + "entry_schema": { + "type": "onap.datatypes.policies.controlloop.operational.apex.EventHandler" + } + }, + "outputs": { + "type": "map", + "description": "Outputs for handling events going out of the APEX engine", + "entry_schema": { + "type": "onap.datatypes.policies.controlloop.operational.apex.EventHandler" + } + }, + "environment": { + "type": "list", + "description": "Envioronmental parameters for the APEX engine", + "entry_schema": { + "type": "onap.datatypes.policies.controlloop.operational.apex.Environment" + } + } + } + } + }, + "data_types": { + "onap.datatypes.policies.controlloop.operational.apex.EngineService": { + "derived_from": "tosca.datatypes.Root", + "properties": { + "name": { + "type": "string", + "description": "Specifies the engine name", + "required": false, + "default": "ApexEngineService" + }, + "version": { + "type": "string", + "description": "Specifies the engine version in double dotted format", + "required": false, + "default": "1.0.0" + }, + "id": { + "type": "integer", + "description": "Specifies the engine id", + "required": true + }, + "instance_count": { + "type": "integer", + "description": "Specifies the number of engine threads that should be run", + "required": true + }, + "deployment_port": { + "type": "integer", + "description": "Specifies the port to connect to for engine administration", + "required": false, + "default": 1 + }, + "policy_model_file_name": { + "type": "string", + "description": "The name of the file from which to read the APEX policy model", + "required": false + }, + "policy_type_impl": { + "type": "string", + "description": "The policy type implementation from which to read the APEX policy model", + "required": false + }, + "periodic_event_period": { + "type": "string", + "description": "The time interval in milliseconds for the periodic scanning event, 0 means \"don't scan\"", + "required": false, + "default": 0 + }, + "engine": { + "type": "onap.datatypes.policies.controlloop.operational.apex.engineservice.Engine", + "description": "The parameters for all engines in the APEX engine service", + "required": true + } + } + }, + "onap.datatypes.policies.controlloop.operational.apex.EventHandler": { + "derived_from": "tosca.datatypes.Root", + "properties": { + "name": { + "type": "string", + "description": "Specifies the event handler name, if not specified this is set to the key name", + "required": false + }, + "carrier_technology": { + "type": "onap.datatypes.policies.controlloop.operational.apex.CarrierTechnology", + "description": "Specifies the carrier technology of the event handler (such as REST/Web Socket/Kafka)", + "required": true + }, + "event_protocol": { + "type": "onap.datatypes.policies.controlloop.operational.apex.EventProtocol", + "description": "Specifies the event protocol of events for the event handler (such as Yaml/JSON/XML/POJO)", + "required": true + }, + "event_name": { + "type": "string", + "description": "Specifies the event name for events on this event handler, if not specified, the event name is read from or written to the event being received or sent", + "required": false + }, + "event_name_filter": { + "type": "string", + "description": "Specifies a filter as a regular expression, events that do not match the filter are dropped, the default is to let all events through", + "required": false + }, + "synchronous_mode": { + "type": "boolean", + "description": "Specifies the event handler is syncronous (receive event and send response)", + "required": false, + "default": false + }, + "synchronous_peer": { + "type": "string", + "description": "The peer event handler (output for input or input for output) of this event handler in synchronous mode, this parameter is mandatory if the event handler is in synchronous mode", + "required": false + }, + "synchronous_timeout": { + "type": "integer", + "description": "The timeout in milliseconds for responses to be issued by APEX torequests, this parameter is mandatory if the event handler is in synchronous mode", + "required": false + }, + "requestor_mode": { + "type": "boolean", + "description": "Specifies the event handler is in requestor mode (send event and wait for response mode)", + "required": false, + "default": false + }, + "requestor_peer": { + "type": "string", + "description": "The peer event handler (output for input or input for output) of this event handler in requestor mode, this parameter is mandatory if the event handler is in requestor mode", + "required": false + }, + "requestor_timeout": { + "type": "integer", + "description": "The timeout in milliseconds for wait for responses to requests, this parameter is mandatory if the event handler is in requestor mode", + "required": false + } + } + }, + "onap.datatypes.policies.controlloop.operational.apex.CarrierTechnology": { + "derived_from": "tosca.datatypes.Root", + "properties": { + "label": { + "type": "string", + "description": "The label (name) of the carrier technology (such as REST, Kafka, WebSocket)", + "required": true + }, + "plugin_parameter_class_name": { + "type": "string", + "description": "The class name of the class that overrides default handling of event input or output for this carrier technology, defaults to the supplied input or output class", + "required": false + } + } + }, + "onap.datatypes.policies.controlloop.operational.apex.EventProtocol": { + "derived_from": "tosca.datatypes.Root", + "properties": { + "label": { + "type": "string", + "description": "The label (name) of the event protocol (such as Yaml, JSON, XML, or POJO)", + "required": true + }, + "event_protocol_plugin_class": { + "type": "string", + "description": "The class name of the class that overrides default handling of the event protocol for this carrier technology, defaults to the supplied event protocol class", + "required": false + } + } + }, + "onap.datatypes.policies.controlloop.operational.apex.Environment": { + "derived_from": "tosca.datatypes.Root", + "properties": { + "name": { + "type": "string", + "description": "The name of the environment variable", + "required": true + }, + "value": { + "type": "string", + "description": "The value of the environment variable", + "required": true + } + } + }, + "onap.datatypes.policies.controlloop.operational.apex.engineservice.Engine": { + "derived_from": "tosca.datatypes.Root", + "properties": { + "context": { + "type": "onap.datatypes.policies.controlloop.operational.apex.engineservice.engine.Context", + "description": "The properties for handling context in APEX engines, defaults to using Java maps for context", + "required": false + }, + "executors": { + "type": "map", + "description": "The plugins for policy executors used in engines such as javascript, MVEL, Jython", + "required": true, + "entry_schema": { + "description": "The plugin class path for this policy executor", + "type": "string" + } + } + } + }, + "onap.datatypes.policies.controlloop.operational.apex.engineservice.engine.Context": { + "derived_from": "tosca.datatypes.Root", + "properties": { + "distributor": { + "type": "onap.datatypes.policies.controlloop.operational.apex.Plugin", + "description": "The plugin to be used for distributing context between APEX PDPs at runtime", + "required": false + }, + "schemas": { + "type": "map", + "description": "The plugins for context schemas available in APEX PDPs such as Java and Avro", + "required": false, + "entry_schema": { + "type": "onap.datatypes.policies.controlloop.operational.apex.Plugin" + } + }, + "locking": { + "type": "onap.datatypes.policies.controlloop.operational.apex.Plugin", + "description": "The plugin to be used for locking context in and between APEX PDPs at runtime", + "required": false + }, + "persistence": { + "type": "onap.datatypes.policies.controlloop.operational.apex.Plugin", + "description": "The plugin to be used for persisting context for APEX PDPs at runtime", + "required": false + } + } + }, + "onap.datatypes.policies.controlloop.operational.apex.Plugin": { + "derived_from": "tosca.datatypes.Root", + "properties": { + "name": { + "type": "string", + "description": "The name of the executor such as Javascript, Jython or MVEL", + "required": true + }, + "plugin_class_name": { + "type": "string", + "description": "The class path of the plugin class for this executor" + } + } + } + } +}
\ No newline at end of file diff --git a/examples/examples-grpc/src/main/resources/tosca/ToscaTemplate.json b/examples/examples-grpc/src/main/resources/tosca/ToscaTemplate.json new file mode 100644 index 000000000..35322a61a --- /dev/null +++ b/examples/examples-grpc/src/main/resources/tosca/ToscaTemplate.json @@ -0,0 +1,16 @@ +{ + "tosca_definitions_version": "tosca_simple_yaml_1_1_0", + "topology_template": { + "policies": [ + { + "operational.gRPC": { + "type": "onap.policies.controlloop.operational.Apex", + "typeVersion": "1.0.0", + "version": "1.0.0", + "properties": { + } + } + } + ] + } +}
\ No newline at end of file diff --git a/examples/examples-grpc/src/test/java/org/onap/policy/apex/examples/grpc/GrpcTestDummyGrpcServer.java b/examples/examples-grpc/src/test/java/org/onap/policy/apex/examples/grpc/GrpcTestDummyGrpcServer.java new file mode 100644 index 000000000..bef0a81c8 --- /dev/null +++ b/examples/examples-grpc/src/test/java/org/onap/policy/apex/examples/grpc/GrpcTestDummyGrpcServer.java @@ -0,0 +1,95 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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.policy.apex.examples.grpc; + +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.protobuf.util.JsonFormat; +import io.grpc.Server; +import io.grpc.netty.NettyServerBuilder; +import io.grpc.stub.StreamObserver; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.file.Files; +import java.nio.file.Paths; +import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase; +import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput; +import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput; +import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput.Builder; +import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; + +/** + * The Class GrpcTestDummyGrpcServer creates a dummy gRPC server to mimic a CDS implementation. + */ +public class GrpcTestDummyGrpcServer { + private Server server; + + public GrpcTestDummyGrpcServer(String host, int port) { + // Implement the dummy gRPC server + BluePrintProcessingServiceImplBase testCdsBlueprintServerImpl = new BluePrintProcessingServiceImplBase() { + @Override + public StreamObserver<ExecutionServiceInput> process( + final StreamObserver<ExecutionServiceOutput> responseObserver) { + return new StreamObserver<ExecutionServiceInput>() { + @Override + public void onNext(final ExecutionServiceInput executionServiceInput) { + String responseString = ""; + try { + responseString = Files.readString(Paths.get( + "src/main/resources/examples/events/APEXgRPC/CreateSubscriptionResponseEvent.json")); + } catch (IOException e) { + throw new ApexEventRuntimeException("Cannot read executionServiceOutput from file", e); + } + ExecutionServiceOutput executionServiceOutput; + Builder builder = ExecutionServiceOutput.newBuilder(); + try { + JsonFormat.parser().ignoringUnknownFields().merge(responseString, builder); + executionServiceOutput = builder.build(); + responseObserver.onNext(executionServiceOutput); + } catch (InvalidProtocolBufferException e) { + throw new ApexEventRuntimeException( + "Output string cannot be converted to ExecutionServiceOutput type for gRPC request." + + e); + } + } + + @Override + public void onError(final Throwable throwable) { + responseObserver.onError(throwable); + } + + @Override + public void onCompleted() { + responseObserver.onCompleted(); + } + }; + } + }; + server = NettyServerBuilder.forAddress(new InetSocketAddress(host, port)).addService(testCdsBlueprintServerImpl) + .build(); + } + + public void start() throws IOException { + server.start(); + } + + public void stop() { + server.shutdown(); + } +} diff --git a/examples/examples-grpc/src/test/java/org/onap/policy/apex/examples/grpc/GrpcTestRestSimEndpoint.java b/examples/examples-grpc/src/test/java/org/onap/policy/apex/examples/grpc/GrpcTestRestSimEndpoint.java new file mode 100644 index 000000000..4e88afe83 --- /dev/null +++ b/examples/examples-grpc/src/test/java/org/onap/policy/apex/examples/grpc/GrpcTestRestSimEndpoint.java @@ -0,0 +1,93 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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.policy.apex.examples.grpc; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Response; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class GrpcTestRestSimEndpoint creates rest server endpoints for simulating sending/receiving events on DMaaP. + */ +@Path("/sim") +public class GrpcTestRestSimEndpoint { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(GrpcTestRestSimEndpoint.class); + private static String loggedOutputEvent = null; + private final Object lock = new Object(); + + /** + * DCAE input of events (simulation of DMaaP). This input event triggers the policy + * + * @param timeout the timeout to wait for + * @return the response + * @throws IOException on I/O errors + */ + @Path("events/unauthenticated.DCAE_CL_OUTPUT/APEX/1") + @GET + public Response dcaeClOutput(@QueryParam("timeout") final int timeout) throws IOException { + String createSubscriptionRequest = + Files.readString(Paths.get("src/main/resources/examples/events/APEXgRPC/CreateSubscriptionEvent.json")); + LOGGER.info("Create subscription request received: \n {}", createSubscriptionRequest); + + return Response.status(200).entity(createSubscriptionRequest).build(); + } + + /** + * Post to Policy management log (Simulation of DMaaP). + * + * @param jsonString the json string + * @return the response + */ + @Path("/events/POLICY_CL_MGT") + @POST + public Response policyLogRequest(final String jsonString) { + LOGGER.info("\n*** POLICY LOG ENTRY START ***\n {} \n *** POLICY LOG ENTRY END ***", jsonString); + synchronized (lock) { + loggedOutputEvent = jsonString; + } + return Response.status(200).build(); + } + + /** + * Get the logged event for test verification. + * + * @return the response + */ + @Path("/event/getLoggedEvent") + @GET + public Response getDetails() { + String loggedEvent; + synchronized (lock) { + loggedEvent = loggedOutputEvent; + } + if (null == loggedEvent) { + return Response.status(500).entity("Error: Log event not yet generated.").build(); + } + return Response.status(200).entity(loggedEvent).build(); + } +} diff --git a/examples/examples-grpc/src/test/java/org/onap/policy/apex/examples/grpc/GrpcTestServerSim.java b/examples/examples-grpc/src/test/java/org/onap/policy/apex/examples/grpc/GrpcTestServerSim.java new file mode 100644 index 000000000..ae35d1ff2 --- /dev/null +++ b/examples/examples-grpc/src/test/java/org/onap/policy/apex/examples/grpc/GrpcTestServerSim.java @@ -0,0 +1,73 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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.policy.apex.examples.grpc; + +import java.io.IOException; +import org.onap.policy.common.endpoints.http.server.HttpServletServer; +import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance; +import org.onap.policy.common.gson.GsonMessageBodyHandler; +import org.onap.policy.common.utils.network.NetworkUtil; + +/** + * The Class GrpcTestServerSim that manages test servers for REST and gRPC requests for the test. + */ +public class GrpcTestServerSim { + private static final String HOST = "localhost"; + private HttpServletServer restServer; + private GrpcTestDummyGrpcServer grpcServer; + + /** + * Instantiates a new REST simulator for DMaaP requests. + * + * @throws InterruptedException interrupted exception + * @throws IOException io exception + */ + public GrpcTestServerSim() throws InterruptedException, IOException { + int restServerPort = 54321; + restServer = HttpServletServerFactoryInstance.getServerFactory().build("GrpcTestRestSimEndpoint", false, HOST, + restServerPort, "/GrpcTestRestSim", false, false); + restServer.addServletClass(null, GrpcTestRestSimEndpoint.class.getName()); + restServer.setSerializationProvider(GsonMessageBodyHandler.class.getName()); + restServer.start(); + if (!NetworkUtil.isTcpPortOpen(HOST, restServerPort, 2000, 1L)) { + throw new IllegalStateException("port " + restServerPort + " is still not in use"); + } + + int grpcServerPort = 54322; + grpcServer = new GrpcTestDummyGrpcServer(HOST, grpcServerPort); + grpcServer.start(); + if (!NetworkUtil.isTcpPortOpen(HOST, grpcServerPort, 2000, 1L)) { + throw new IllegalStateException("port " + grpcServerPort + " is still not in use"); + } + } + + /** + * Tear down. + */ + public void tearDown() { + if (restServer != null) { + restServer.stop(); + } + if (grpcServer != null) { + grpcServer.stop(); + } + } +} diff --git a/examples/examples-grpc/src/test/java/org/onap/policy/apex/examples/grpc/TestApexGrpcExample.java b/examples/examples-grpc/src/test/java/org/onap/policy/apex/examples/grpc/TestApexGrpcExample.java new file mode 100644 index 000000000..06d1d33f9 --- /dev/null +++ b/examples/examples-grpc/src/test/java/org/onap/policy/apex/examples/grpc/TestApexGrpcExample.java @@ -0,0 +1,85 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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.policy.apex.examples.grpc; + +import static org.awaitility.Awaitility.await; +import static org.junit.Assert.assertEquals; + +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.concurrent.TimeUnit; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.core.Response; +import org.junit.Test; +import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain; +import org.onap.policy.apex.service.engine.main.ApexMain; + +/** + * Test class to run an example policy for APEX-CDS interaction over gRPC. + * Event received on unauthenticated.DCAE_CL_OUTPUT DMaaP topic (dummy REST Endpoint here) triggers the policy + * Based on the event, a create/delete subscription gRPC request is triggered to the CDS (a dummy gRPC server here). + * Response received from CDS is used to send a final output Log event on POLICY_CL_MGT topic. + */ +public class TestApexGrpcExample { + @Test + public void testGrpcExample() throws Exception { + // @formatter:off + final String[] cliArgs = new String[] { + "-c", + "src/main/resources/policy/APEXgRPCPolicy.apex", + "-l", + "target/APEXgRPCPolicyModel.log", + "-o", + "target/classes/APEXgRPCPolicy.json" + }; + // @formatter:on + + new ApexCommandLineEditorMain(cliArgs); + + // @formatter:off + final String[] apexArgs = { + "-rfr", + "target/classes", + "-c", + "src/main/resources/examples/config/APEXgRPC/ApexConfig.json", + "-m", + "target/classes/APEXgRPCPolicy.json" + }; + // @formatter:on + + GrpcTestServerSim sim = new GrpcTestServerSim(); + final Client client = ClientBuilder.newClient(); + String expectedLoggedOutputEvent = Files + .readString(Paths.get("src/main/resources/examples/events/APEXgRPC/LogEvent.json")).replaceAll("\r", ""); + final ApexMain apexMain = new ApexMain(apexArgs); + + String getLoggedEventUrl = "http://localhost:54321/GrpcTestRestSim/sim/event/getLoggedEvent"; + // wait for success response code to be received, until a timeout + await().atMost(5000, TimeUnit.MILLISECONDS).until(() -> { + return 200 == client.target(getLoggedEventUrl).request("application/json").get().getStatus(); + }); + apexMain.shutdown(); + Response response = client.target(getLoggedEventUrl).request("application/json").get(); + sim.tearDown(); + String responseEntity = response.readEntity(String.class); + assertEquals(expectedLoggedOutputEvent, responseEntity); + } +} diff --git a/examples/pom.xml b/examples/pom.xml index db2f34047..6bef02f4c 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -1,6 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> <!-- ============LICENSE_START======================================================= Copyright (C) 2018 Ericsson. All rights reserved. + Modifications Copyright (C) 2020 Nordix Foundation. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -16,8 +18,7 @@ SPDX-License-Identifier: Apache-2.0 ============LICENSE_END========================================================= ---> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> +--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.onap.policy.apex-pdp</groupId> @@ -42,7 +43,8 @@ <module>examples-onap-vcpe</module> <module>examples-onap-bbs</module> <module>examples-servlet</module> - </modules> + <module>examples-grpc</module> + </modules> <dependencies> <dependency> @@ -51,7 +53,7 @@ <scope>test</scope> </dependency> </dependencies> - + <profiles> <profile> <id>apexSite</id> diff --git a/packages/apex-pdp-package-full/pom.xml b/packages/apex-pdp-package-full/pom.xml index 919ec5d4a..6710c6ee3 100644 --- a/packages/apex-pdp-package-full/pom.xml +++ b/packages/apex-pdp-package-full/pom.xml @@ -267,6 +267,11 @@ <artifactId>examples-onap-bbs</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.onap.policy.apex-pdp.examples</groupId> + <artifactId>examples-grpc</artifactId> + <version>${project.version}</version> + </dependency> </dependencies> <build> @@ -446,6 +451,24 @@ <outputDirectory>${project.build.directory}/etc</outputDirectory> <includes>/app-version.txt</includes> </artifactItem> + <artifactItem> + <groupId>org.onap.policy.apex-pdp.examples</groupId> + <artifactId>examples-grpc</artifactId> + <version>${project.version}</version> + <type>jar</type> + <overWrite>false</overWrite> + <outputDirectory>${project.build.directory}</outputDirectory> + <includes>etc/**/*,examples/**/*</includes> + </artifactItem> + <artifactItem> + <groupId>org.onap.policy.apex-pdp.examples</groupId> + <artifactId>examples-grpc</artifactId> + <version>${project.version}</version> + <type>jar</type> + <overWrite>false</overWrite> + <outputDirectory>${project.build.directory}/examples/models/APEXgRPC</outputDirectory> + <includes>APEXgRPCPolicy.json</includes> + </artifactItem> </artifactItems> <overWriteReleases>true</overWriteReleases> <overWriteSnapshots>true</overWriteSnapshots> |