Notification and Subscription filters 28/106028/2
authorPiotr Borelowski <p.borelowski@partner.samsung.com>
Fri, 27 Mar 2020 13:41:15 +0000 (14:41 +0100)
committerPiotr Borelowski <p.borelowski@partner.samsung.com>
Thu, 16 Apr 2020 11:53:43 +0000 (11:53 +0000)
Ve-Vnfm (SOL002) Adapter project
- checking if VNF Id is registered in AAI
- checking if list from ESR is not null
- sending the filter in subscription

Issue-ID: SO-2574
Signed-off-by: Piotr Borelowski <p.borelowski@partner.samsung.com>
Change-Id: Iad2c5d9bc2921221fcf0304db73baee6bbaa0c77

adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/aai/AaiConnection.java
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/constant/VnfNotificationFilterType.java [new file with mode: 0644]
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/controller/NotificationController.java
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/SubscriberService.java
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/VnfAaiChecker.java [new file with mode: 0644]
adapters/mso-ve-vnfm-adapter/src/main/resources/application.yaml
adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/controller/NotificationControllerTest.java
adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/service/VnfAaiCheckerTest.java [new file with mode: 0644]
adapters/mso-ve-vnfm-adapter/src/test/resources/application.yaml [new file with mode: 0644]

index 9b2a8c3..fd20a0f 100644 (file)
@@ -26,8 +26,10 @@ import java.util.List;
 import java.util.Optional;
 import org.apache.logging.log4j.util.Strings;
 import org.onap.aai.domain.yang.EsrSystemInfo;
+import org.onap.aai.domain.yang.EsrSystemInfoList;
 import org.onap.aai.domain.yang.EsrVnfm;
 import org.onap.aai.domain.yang.EsrVnfmList;
+import org.onap.aai.domain.yang.v18.GenericVnf;
 import org.onap.so.adapters.vevnfm.exception.VeVnfmException;
 import org.onap.so.client.aai.AAIObjectType;
 import org.onap.so.client.aai.AAIResourcesClient;
@@ -72,7 +74,7 @@ public class AaiConnection {
 
         if (response.isPresent()) {
             final EsrVnfmList esrVnfmList = response.get();
-            logger.info("The VNFM replied with: {}", esrVnfmList);
+            logger.info("The AAI ESR replied with: {}", esrVnfmList);
             final List<EsrVnfm> esrVnfm = esrVnfmList.getEsrVnfm();
 
             final List<EsrSystemInfo> infos = new LinkedList<>();
@@ -94,10 +96,28 @@ public class AaiConnection {
 
         if (response.isPresent()) {
             final EsrVnfm esrVnfm = response.get();
-            logger.info("The VNFM replied with: {}", esrVnfm);
-            return esrVnfm.getEsrSystemInfoList().getEsrSystemInfo();
+            logger.info("The AAI ESR replied with: {}", esrVnfm);
+            final EsrSystemInfoList esrSystemInfoList = esrVnfm.getEsrSystemInfoList();
+
+            if (esrSystemInfoList != null) {
+                return esrSystemInfoList.getEsrSystemInfo();
+            }
         }
 
         return Collections.emptyList();
     }
+
+    public boolean checkGenericVnfId(final String vnfId) {
+        final AAIResourcesClient resourcesClient = new AAIResourcesClient();
+        final AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId);
+        final Optional<GenericVnf> response = resourcesClient.get(GenericVnf.class, resourceUri);
+
+        if (response.isPresent()) {
+            final GenericVnf vnf = response.get();
+            logger.info("The AAI replied with: {}", vnf);
+            return vnfId.equals(vnf.getVnfId());
+        }
+
+        return false;
+    }
 }
diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/constant/VnfNotificationFilterType.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/constant/VnfNotificationFilterType.java
new file mode 100644 (file)
index 0000000..09a6ae1
--- /dev/null
@@ -0,0 +1,41 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SO
+ * ================================================================================
+ * Copyright (C) 2020 Samsung. 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.so.adapters.vevnfm.constant;
+
+/**
+ * Which incoming Notification with particular VNF id is supported
+ */
+public enum VnfNotificationFilterType {
+    /**
+     * None
+     */
+    NONE,
+
+    /**
+     * Only those which are valid in AAI
+     */
+    AAI_CHECKED,
+
+    /**
+     * All
+     */
+    ALL
+}
index cb324c3..e187ec9 100644 (file)
 
 package org.onap.so.adapters.vevnfm.controller;
 
+import org.onap.so.adapters.vevnfm.constant.VnfNotificationFilterType;
 import org.onap.so.adapters.vevnfm.service.DmaapService;
+import org.onap.so.adapters.vevnfm.service.VnfAaiChecker;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -35,13 +38,32 @@ public class NotificationController {
 
     private static final Logger logger = LoggerFactory.getLogger(NotificationController.class);
 
+    private final VnfNotificationFilterType vnfFilterType;
+    private final VnfAaiChecker vnfAaiChecker;
+    private final DmaapService dmaapService;
+
     @Autowired
-    private DmaapService dmaapService;
+    public NotificationController(
+            @Value("${notification.vnf-filter-type}") final VnfNotificationFilterType vnfFilterType,
+            final VnfAaiChecker vnfAaiChecker, final DmaapService dmaapService) {
+        this.vnfFilterType = vnfFilterType;
+        this.vnfAaiChecker = vnfAaiChecker;
+        this.dmaapService = dmaapService;
+    }
 
     @PostMapping("${vnfm.notification}")
     public ResponseEntity receiveNotification(@RequestBody final VnfLcmOperationOccurrenceNotification notification) {
         logger.info("Notification received {}", notification);
-        dmaapService.send(notification);
+
+        final String vnfInstanceId = notification.getVnfInstanceId();
+
+        if (vnfAaiChecker.vnfCheck(vnfFilterType, vnfInstanceId)) {
+            logger.info("The info with the VNF id '{}' is sent to DMaaP", vnfInstanceId);
+            dmaapService.send(notification);
+        } else {
+            logger.info("This VNF id '{}' is not supported", vnfInstanceId);
+        }
+
         return ResponseEntity.ok().build();
     }
 }
index 9760584..d2cf483 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.onap.so.adapters.vevnfm.service;
 
+import com.google.gson.Gson;
 import com.squareup.okhttp.Credentials;
 import java.util.Collections;
 import org.apache.logging.log4j.util.Strings;
@@ -29,6 +30,7 @@ import org.onap.so.adapters.vevnfm.provider.AuthorizationHeadersProvider;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.SubscriptionsAuthentication;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.SubscriptionsAuthenticationParamsBasic;
+import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.SubscriptionsFilter;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -36,6 +38,11 @@ import org.springframework.stereotype.Service;
 @Service
 public class SubscriberService {
 
+    private static final Gson gson = new Gson();
+
+    @Value("${vevnfmadapter.vnf-filter-json}")
+    private String vnfFilter;
+
     @Value("${vevnfmadapter.endpoint}")
     private String endpoint;
 
@@ -91,6 +98,7 @@ public class SubscriberService {
 
     private LccnSubscriptionRequest createRequest() {
         final LccnSubscriptionRequest request = new LccnSubscriptionRequest();
+        request.filter(getFilter());
         request.callbackUri(getCallbackUri());
         final SubscriptionsAuthenticationParamsBasic paramsBasic = new SubscriptionsAuthenticationParamsBasic();
         final SubscriptionsAuthentication authentication = new SubscriptionsAuthentication();
@@ -103,6 +111,10 @@ public class SubscriberService {
         return request;
     }
 
+    private SubscriptionsFilter getFilter() {
+        return gson.fromJson(vnfFilter, SubscriptionsFilter.class);
+    }
+
     private String getCallbackUri() {
         return endpoint + notification;
     }
diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/VnfAaiChecker.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/VnfAaiChecker.java
new file mode 100644 (file)
index 0000000..02a9c18
--- /dev/null
@@ -0,0 +1,51 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SO
+ * ================================================================================
+ * Copyright (C) 2020 Samsung. 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.so.adapters.vevnfm.service;
+
+import org.onap.so.adapters.vevnfm.aai.AaiConnection;
+import org.onap.so.adapters.vevnfm.constant.VnfNotificationFilterType;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class VnfAaiChecker {
+
+    private final AaiConnection aaiConnection;
+
+    @Autowired
+    public VnfAaiChecker(final AaiConnection aaiConnection) {
+        this.aaiConnection = aaiConnection;
+    }
+
+    public boolean vnfCheck(final VnfNotificationFilterType filterType, final String vnfId) {
+        switch (filterType) {
+            case ALL:
+                return true;
+            case AAI_CHECKED:
+                return aaiConnection.checkGenericVnfId(vnfId);
+            case NONE:
+                return false;
+            default:
+                throw new IllegalArgumentException(
+                        "The value of VnfNotificationFilterType is not supported: " + filterType);
+        }
+    }
+}
index a2a33bf..c69c951 100644 (file)
@@ -18,6 +18,7 @@ server:
   port: 9098
 
 vevnfmadapter:
+  vnf-filter-json: '{notificationTypes:[VnfLcmOperationOccurrenceNotification],operationStates:[COMPLETED]}'
   endpoint: http://so-ve-vnfm-adapter.onap:9098
 
 mso:
@@ -32,6 +33,9 @@ vnfm:
   subscription: /vnflcm/v1/subscriptions
   notification: /lcm/v1/vnf/instances/notifications
 
+notification:
+  vnf-filter-type: NONE
+
 dmaap:
   endpoint: http://message-router.onap:30227
   topic: /events/unauthenticated.DCAE_CL_OUTPUT
index 974e6ec..56c53a7 100644 (file)
@@ -21,7 +21,6 @@
 package org.onap.so.adapters.vevnfm.controller;
 
 import static org.junit.Assert.assertEquals;
-import static org.springframework.test.web.client.ExpectedCount.once;
 import static org.springframework.test.web.client.match.MockRestRequestMatchers.anything;
 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
 import org.junit.Before;
@@ -77,7 +76,7 @@ public class NotificationControllerTest {
         final MockHttpServletRequestBuilder request = MockMvcRequestBuilders.post(notification)
                 .contentType(MediaType.APPLICATION_JSON).content(MINIMAL_JSON_CONTENT);
 
-        mockRestServer.expect(once(), anything()).andRespond(withSuccess());
+        mockRestServer.expect(anything()).andRespond(withSuccess());
 
         // when
         final MvcResult mvcResult = mvc.perform(request).andReturn();
diff --git a/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/service/VnfAaiCheckerTest.java b/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/service/VnfAaiCheckerTest.java
new file mode 100644 (file)
index 0000000..84705d1
--- /dev/null
@@ -0,0 +1,87 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SO
+ * ================================================================================
+ * Copyright (C) 2020 Samsung. 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.so.adapters.vevnfm.service;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.when;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.so.adapters.vevnfm.aai.AaiConnection;
+import org.onap.so.adapters.vevnfm.constant.VnfNotificationFilterType;
+
+@RunWith(MockitoJUnitRunner.class)
+public class VnfAaiCheckerTest {
+
+    private static final String VNF_ID = "t5h78w";
+
+    @Mock
+    private AaiConnection aaiConnection;
+
+    @InjectMocks
+    private VnfAaiChecker checker;
+
+    @Test
+    public void testAll() {
+        // when
+        final boolean response = checker.vnfCheck(VnfNotificationFilterType.ALL, VNF_ID);
+
+        // then
+        assertTrue(response);
+    }
+
+    @Test
+    public void testAaiCheckedPresent() {
+        // given
+        when(aaiConnection.checkGenericVnfId(eq(VNF_ID))).thenReturn(true);
+
+        // when
+        final boolean response = checker.vnfCheck(VnfNotificationFilterType.AAI_CHECKED, VNF_ID);
+
+        // then
+        assertTrue(response);
+    }
+
+    @Test
+    public void testAaiCheckedAbsent() {
+        // given
+        when(aaiConnection.checkGenericVnfId(eq(VNF_ID))).thenReturn(false);
+
+        // when
+        final boolean response = checker.vnfCheck(VnfNotificationFilterType.AAI_CHECKED, VNF_ID);
+
+        // then
+        assertFalse(response);
+    }
+
+    @Test
+    public void testNone() {
+        // when
+        final boolean response = checker.vnfCheck(VnfNotificationFilterType.NONE, VNF_ID);
+
+        // then
+        assertFalse(response);
+    }
+}
diff --git a/adapters/mso-ve-vnfm-adapter/src/test/resources/application.yaml b/adapters/mso-ve-vnfm-adapter/src/test/resources/application.yaml
new file mode 100644 (file)
index 0000000..2c7f67e
--- /dev/null
@@ -0,0 +1,56 @@
+#
+# Copyright © 2019, 2020 Samsung.
+# 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.
+
+server:
+  port: 9098
+
+vevnfmadapter:
+  vnf-filter-json: '{notificationTypes:[VnfLcmOperationOccurrenceNotification],operationStates:[COMPLETED]}'
+  endpoint: http://so-ve-vnfm-adapter.onap:9098
+
+mso:
+  key: 07a7159d3bf51a0e53be7a8f89699be7
+
+aai:
+  endpoint: https://aai.onap:30233
+  auth: 75C4483F9C05E2C33A8602635FA532397EC44AB667A2B64DED4FEE08DD932F2E3C1FEE
+
+vnfm:
+  default-endpoint: https://so-vnfm-simulator.onap:9093
+  subscription: /vnflcm/v1/subscriptions
+  notification: /lcm/v1/vnf/instances/notifications
+
+notification:
+  vnf-filter-type: ALL
+
+dmaap:
+  endpoint: http://message-router.onap:30227
+  topic: /events/unauthenticated.DCAE_CL_OUTPUT
+  closed-loop:
+    control:
+      name: ClosedLoopControlName
+  version: 1.0.2
+
+spring:
+  security:
+    usercredentials:
+      - username: admin
+        openpass: a4b3c2d1
+        password: '$2a$10$vU.mWyNTsikAxXIA5c269ewCpAbYTiyMS0m1N.kn4F2CSGEnrKN7K'
+        role: USER
+  http:
+    converters:
+      preferred-json-mapper: gson