From 77e0ea048ac7f86bec2cd59ecca18427be22bbf5 Mon Sep 17 00:00:00 2001
From: zafar <zafar@etsi.org>
Date: Wed, 9 Dec 2020 11:58:13 +0500
Subject: [PATCH] NotificationEndpoint and NotificationConsumer resources added
 in NSIUN-API

---
 .../NSInstanceUsageNotificationKeywords.robot |  25 +++-
 .../NotificationConsumer.robot                |  21 ++++
 .../NotificationEndpoint.robot                | 112 ++++++++++++++++++
 .../environment/variables.txt                 |  22 +++-
 .../jsons/NsInstanceUsageNotification.json    |   9 ++
 .../NsInstanceUsageNotification.schema.json   |  43 +++++++
 6 files changed, 228 insertions(+), 4 deletions(-)
 create mode 100644 SOL011/NSInstanceUsageNotification-API/NotificationConsumer.robot
 create mode 100644 SOL011/NSInstanceUsageNotification-API/NotificationEndpoint.robot
 create mode 100644 SOL011/NSInstanceUsageNotification-API/jsons/NsInstanceUsageNotification.json
 create mode 100644 SOL011/NSInstanceUsageNotification-API/schemas/NsInstanceUsageNotification.schema.json

diff --git a/SOL011/NSInstanceUsageNotification-API/NSInstanceUsageNotificationKeywords.robot b/SOL011/NSInstanceUsageNotification-API/NSInstanceUsageNotificationKeywords.robot
index 96aa0ed4..199ad8c4 100644
--- a/SOL011/NSInstanceUsageNotification-API/NSInstanceUsageNotificationKeywords.robot
+++ b/SOL011/NSInstanceUsageNotification-API/NSInstanceUsageNotificationKeywords.robot
@@ -4,6 +4,7 @@ Library    REST    ${NFVO_SCHEMA}://${NFVO_HOST}:${NFVO_PORT}    ssl_verify=fals
 Library    OperatingSystem
 Library    JSONLibrary
 Library    JSONSchemaLibrary    schemas/
+Library    String
 
 *** Keywords ***
 Check HTTP Response Status Code Is
@@ -195,4 +196,26 @@ DELETE Individual subscription
     Run Keyword If    ${AUTH_USAGE} == 1    Set Headers    {"Authorization":"${AUTHORIZATION}"}
     Delete    ${apiRoot}/${apiName}/${apiVersion}/subscriptions/${subscriptionId}    
     ${outputResponse}=    Output    response
-	Set Global Variable    ${response}    ${outputResponse}
\ No newline at end of file
+	Set Global Variable    ${response}    ${outputResponse}
+
+Check resource existence and get CallbackUri
+    Set Headers  {"Accept":"${ACCEPT_JSON}"}  
+    Set Headers  {"Content-Type": "${CONTENT_TYPE_JSON}"}
+    Run Keyword If    ${AUTH_USAGE} == 1    Set Headers    {"Authorization":"${AUTHORIZATION}"}
+    Get    ${apiRoot}/${apiName}/${apiVersion}/subscriptions/${subscriptionId}
+    Integer    response status    200
+    Validate Json    response body    subscription.schema.json
+    Set Global Variable    ${callbackResp}    response body callbackUri
+
+POST NS Instance Usage Notification
+    log    Trying to perform a POST to deliver notification
+    Set Headers  {"Accept":"${ACCEPT_JSON}"}  
+    Set Headers  {"Content-Type": "${CONTENT_TYPE_JSON}"}
+    Run Keyword If    ${AUTH_USAGE} == 1    Set Headers    {"Authorization":"${AUTHORIZATION}"}
+    ${template} =    Get File    jsons/NSInstanceUsageNotification.json  
+    ${body}=    Format String    ${template}    subscriptionId=${subscriptionId}
+    Post    ${callbackResp}    ${body}
+    ${outputResponse}=    Output    response
+	Set Global Variable    ${response}    ${outputResponse}
+
+    
\ No newline at end of file
diff --git a/SOL011/NSInstanceUsageNotification-API/NotificationConsumer.robot b/SOL011/NSInstanceUsageNotification-API/NotificationConsumer.robot
new file mode 100644
index 00000000..00985877
--- /dev/null
+++ b/SOL011/NSInstanceUsageNotification-API/NotificationConsumer.robot
@@ -0,0 +1,21 @@
+*** Settings ***
+Library    JSONSchemaLibrary    schemas/
+Resource   environment/variables.txt
+Resource   NSInstanceUsageNotificationKeywords.robot  
+Library    REST    ${NFVO_SCHEMA}://${NFVO_HOST}:${NFVO_PORT}    ssl_verify=false
+Library    OperatingSystem
+Library    JSONLibrary
+Suite Setup    Check resource existence and get CallbackUri
+
+*** Test Cases ***
+NS Instance Usage Notification
+    [Documentation]    Test ID: 9.3.4.4.1
+    ...    Test title: NS Instance Usage Notification
+    ...    Test objective: The objective is to test that NS Instance Usage Notification is delivered with success to the notification consumer
+    ...    Pre-conditions: A subscription for instance usage notification is available in the NFVO.
+    ...    Reference: Clause 8.5.5.3.1 - ETSI GS NFV-SOL 011 [6] v3.3.1
+    ...    Config ID: Config_prod_Notif_Endpoint
+    ...    Applicability: none
+    ...    Post-Conditions: none 
+    POST NS Instance Usage Notification
+    Check HTTP Response Status Code Is    204
\ No newline at end of file
diff --git a/SOL011/NSInstanceUsageNotification-API/NotificationEndpoint.robot b/SOL011/NSInstanceUsageNotification-API/NotificationEndpoint.robot
new file mode 100644
index 00000000..311997a7
--- /dev/null
+++ b/SOL011/NSInstanceUsageNotification-API/NotificationEndpoint.robot
@@ -0,0 +1,112 @@
+*** Settings ***
+Suite Setup       Create Sessions
+Suite Teardown    Terminate All Processes    kill=true
+Resource          environment/variables.txt
+Library           MockServerLibrary
+Library           Process
+Library           OperatingSystem
+Library           Collections
+
+*** Test Cases ***
+NS Instance Usage START Notification
+    [Documentation]    Test ID: 9.3.4.3.1
+    ...    Test title: NS Instance Usage START Notification
+    ...    Test objective: The objective is to test the dispatch of NS Instance Usage Start Notification when the usage of an NS instance is started, and perform a JSON schema and content validation of the delivered notification. The action that triggers the notification under test is an explicit test step, but it is not performed by the test system.
+    ...    Pre-conditions: A subscription for NS instance usage notification is available in the NFVO.
+    ...    Reference: Clause 8.5.5.3.1 - ETSI GS NFV-SOL 011 [6] v3.3.1
+    ...    Config ID: Config_prod_NFVO
+    ...    Applicability: none
+    ...    Post-Conditions: none 
+    Trigger a change in NS instance usage START (external action) 
+    Check NS Instance Usage Start Notification Http POST Request Body Json Schema Is    NsInstanceUsageNotification
+    Check NS Instance Usage Start Notification Http POST Request Body notificationType attribute Is    NsInstanceUsageNotification
+    Check NS Instance Usage Start Notification Http POST Request Body notificationStatus attribute Is    START
+
+NS Instance Usage END Notification
+    [Documentation]    Test ID: 9.3.4.3.2
+    ...    Test title: NS Instance Usage END Notification
+    ...    Test objective: The objective is to test the dispatch of NS Instance Usage End Notification when the usage of an NS instance is ended, and perform a JSON schema and content validation of the delivered notification. The action that triggers the notification under test is an explicit test step, but it is not performed by the test system.
+    ...    Pre-conditions: A subscription for NS instance usage notification is available in the NFVO.
+    ...    Reference: Clause 8.5.5.3.1 - ETSI GS NFV-SOL 011 [6] v3.3.1
+    ...    Config ID: Config_prod_NFVO
+    ...    Applicability: none
+    ...    Post-Conditions: none 
+    Trigger a change in NS instance usage END (external action) 
+    Check NS Instance Usage End Notification Http POST Request Body Json Schema Is    NsInstanceUsageNotification
+    Check NS Instance Usage End Notification Http POST Request Body notificationType attribute Is    NsInstanceUsageNotification
+    Check NS Instance Usage End Notification Http POST Request Body notificationStatus attribute Is    END
+    
+*** Keywords ***
+Create Sessions
+    Start Process  java  -jar  ${MOCK_SERVER_JAR}  -serverPort  ${callback_port}  alias=mockInstance
+    Wait For Process  handle=mockInstance  timeout=5s  on_timeout=continue
+    Create Mock Session  ${callback_uri}:${callback_port}
+    
+Trigger a change in NS instance usage START (external action) 
+    #do nothing
+    Log    do nothing
+    
+Trigger a change in NS instance usage END (external action) 
+    #do nothing
+    Log    do nothing
+
+Configure Notification Forward
+    [Arguments]    ${schema}    ${endpoint}    ${endpoint_fwd}    
+    Log  Creating mock Http POST forward to handle ${schema}
+    &{notification_tmp}=  Create Mock Request Matcher	POST  ${endpoint}  body_type="JSON_SCHEMA"    body=${schema}
+    &{notification_fwd}=  Create Mock Http Forward	${endpoint_fwd}
+    Create Mock Expectation With Http Forward  ${notification_tmp}  ${notification_fwd}
+
+Configure Notification NS Instance Usage Start Handler
+    [Arguments]    ${endpoint}    ${type}    ${status}
+    ${json}=    evaluate    {}
+    set to dictionary   ${json}    notificationType    ${type}    changeType    ${status}
+    ${BODY}=    evaluate    json.dumps(${json})    json
+    Log  Creating mock request and response to handle status notification
+    &{notification_request}=  Create Mock Request Matcher	POST  ${endpoint}  body_type="JSON"    body=${BODY}
+    &{notification_response}=  Create Mock Response	headers="Content-Type: application/json"  status_code=204
+    Create Mock Expectation  ${notification_request}  ${notification_response}
+    
+Configure Notification NS Instance Usage End Handler
+    [Arguments]    ${endpoint}    ${type}    ${status}
+    ${json}=    evaluate    {}
+    set to dictionary   ${json}    notificationType    ${type}    changeType    ${status}
+    ${BODY}=    evaluate    json.dumps(${json})    json
+    Log  Creating mock request and response to handle status notification
+    &{notification_request}=  Create Mock Request Matcher	POST  ${endpoint}  body_type="JSON"    body=${BODY}
+    &{notification_response}=  Create Mock Response	headers="Content-Type: application/json"  status_code=204
+    Create Mock Expectation  ${notification_request}  ${notification_response}
+    
+Check NS Instance Usage Start Notification Http POST Request Body Json Schema Is 
+    [Arguments]    ${element}
+    ${schema}=	Get File	schemas/${element}.schema.json
+    Configure Notification Forward    ${schema}    ${callback_endpoint}    ${callback_endpoint_fwd}
+    
+Check NS Instance Usage Start Notification Http POST Request Body notificationType attribute Is
+    [Arguments]    ${type}
+    Configure Notification NS Instance Usage Start Handler    ${callback_endpoint_fwd}    ${type}    START
+    Wait Until Keyword Succeeds    2 min   10 sec   Verify Mock Expectation    ${notification_request}
+    Clear Requests    ${callback_endpoint}
+    Clear Requests    ${callback_endpoint_fwd}
+    
+Check NS Instance Usage Start Notification Http POST Request Body notificationStatus attribute Is
+    [Arguments]    ${type}
+    #do nothing
+    Log    do nothing
+    
+Check NS Instance Usage End Notification Http POST Request Body Json Schema Is 
+    [Arguments]    ${element}
+    ${schema}=	Get File	schemas/${element}.schema.json
+    Configure Notification Forward    ${schema}    ${callback_endpoint}    ${callback_endpoint_fwd}
+
+Check NS Instance Usage End Notification Http POST Request Body notificationType attribute Is
+    [Arguments]    ${type}
+    Configure Notification NS Instance Usage End Handler    ${callback_endpoint_fwd}    ${type}    START
+    Wait Until Keyword Succeeds    2 min   10 sec   Verify Mock Expectation    ${notification_request}
+    Clear Requests    ${callback_endpoint}
+    Clear Requests    ${callback_endpoint_fwd}
+    
+Check NS Instance Usage End Notification Http POST Request Body notificationStatus attribute Is
+    [Arguments]    ${type}
+    #do nothing
+    Log    do nothing    
\ No newline at end of file
diff --git a/SOL011/NSInstanceUsageNotification-API/environment/variables.txt b/SOL011/NSInstanceUsageNotification-API/environment/variables.txt
index fc5a4bc1..b9ab0f0f 100644
--- a/SOL011/NSInstanceUsageNotification-API/environment/variables.txt
+++ b/SOL011/NSInstanceUsageNotification-API/environment/variables.txt
@@ -4,6 +4,7 @@ ${NFVO_PORT}      8081    # Listening port of the NFVO
 ${NFVO_SCHEMA}    https
 ${AUTHORIZATION}    Bearer    QWxhZGRpbjpvcGVuIHNlc2FtZQ==
 ${CONTENT_TYPE}    application/json
+${CONTENT_TYPE_JSON}    application/json
 ${ACCEPT}         application/json
 ${AUTH_USAGE}     1
 ${ACCEPT_JSON}         application/json
@@ -13,10 +14,9 @@ ${apiVersion}     v1
 
 ${SYNC_MODE}    1
 
+${response}    {}
 
-${response}
-
-${NFVO_DUPLICATION}
+${NFVO_DUPLICATION}    1
 
 ${VNFM_HOST}      localhost    # Hostname of the VNFM
 ${VNFM_PORT}      8080    # Listening port of the VNFM
@@ -40,3 +40,19 @@ ${fields}         criteria,objectInstanceIds
 ${subscriptionId}    6fc3539c-e602-4afa-8e13-962fb5a7d81f
 ${notification_ep}    notification
 ${VrQuotaAvailNotification}    {}
+
+${callback_uri}    http://localhost
+${callback_port}    9091
+${callback_endpoint}    /endpoint
+${callback_endpoint_fwd}    /endpoint/check
+${callback_endpoint_error}    /endpoint_404
+${sleep_interval}    20s
+${total_polling_time}   2 min
+${polling_interval}     10 sec
+
+${notification_request}    []
+${notification_response}    []
+
+${callbackResp}		127.0.0.1
+
+${MOCK_SERVER_JAR}    ../../../bin/mockserver-netty-5.5.0-jar-with-dependencies.jar
diff --git a/SOL011/NSInstanceUsageNotification-API/jsons/NsInstanceUsageNotification.json b/SOL011/NSInstanceUsageNotification-API/jsons/NsInstanceUsageNotification.json
new file mode 100644
index 00000000..984f3ddd
--- /dev/null
+++ b/SOL011/NSInstanceUsageNotification-API/jsons/NsInstanceUsageNotification.json
@@ -0,0 +1,9 @@
+{{
+  "id": "",
+  "notificationType": "NSInstanceUsageNotification",
+  "subscriptionId": "{subscriptionId}",
+  "timeStamp": "",
+  "nsInstanceId": "",
+  "status": "START",
+  "_links": ""
+}}
\ No newline at end of file
diff --git a/SOL011/NSInstanceUsageNotification-API/schemas/NsInstanceUsageNotification.schema.json b/SOL011/NSInstanceUsageNotification-API/schemas/NsInstanceUsageNotification.schema.json
new file mode 100644
index 00000000..4e4df860
--- /dev/null
+++ b/SOL011/NSInstanceUsageNotification-API/schemas/NsInstanceUsageNotification.schema.json
@@ -0,0 +1,43 @@
+{
+  "description": "This type represents an NS instance usage notification, which indicates the start  or end of usage of an NS instance as a part of a composite NS managed by the NFVO-C.  It shall comply with the provisions defined in table 8.6.2.4-1.",
+  "type": "object",
+  "required": [
+    "id",
+    "notificationType",
+    "subscriptionId",
+    "timeStamp",
+    "nsInstanceId",
+    "status",
+    "_links"
+  ],
+  "properties": {
+    "id": {
+      "description": "Identifier of this notification. If a notification is sent multiple times due  to multiple subscriptions, the \"id\" attribute of all these notifications shall  have the same value.",
+      "$ref": "../components/SOL011_schemas.yaml#/components/schemas/Identifier"
+    },
+    "notificationType": {
+      "description": "Discriminator for the different notification types. Shall be set to  \"NsInstanceUsageNotification\" for this notification type.",
+      "type": "string"
+    },
+    "subscriptionId": {
+      "description": "Identifier of the subscription that this notification relates to.",
+      "$ref": "../components/SOL011_schemas.yaml#/components/schemas/Identifier"
+    },
+    "timeStamp": {
+      "description": "Date and time of the generation of the notification.",
+      "$ref": "../components/SOL011_schemas.yaml#/components/schemas/DateTime"
+    },
+    "nsInstanceId": {
+      "description": "Identifier of the NS instance affected.",
+      "$ref": "../components/SOL011_schemas.yaml#/components/schemas/Identifier"
+    },
+    "status": {
+      "description": "Indicates whether this notification reports about the start of the usage of  an NS instance or about the end of the usage of an NS instance.",
+      "$ref": "#/components/schemas/NsInstanceUsageStatusType"
+    },
+    "_links": {
+      "description": "Links to resources related to this notification.",
+      "$ref": "../components/SOL011_schemas.yaml#/components/schemas/NotificationLink"
+    }
+  }
+}
\ No newline at end of file
-- 
GitLab