Added the communication with AAI 27/100327/16
authorPiotr Borelowski <p.borelowski@partner.samsung.com>
Wed, 15 Jan 2020 16:58:27 +0000 (17:58 +0100)
committerPiotr Borelowski <p.borelowski@partner.samsung.com>
Thu, 30 Jan 2020 10:05:55 +0000 (11:05 +0100)
Ve-Vnfm (SOL002) Adapter project

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

17 files changed:
adapters/mso-ve-vnfm-adapter/pom.xml
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/Application.java
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/aai/AaiConnection.java [new file with mode: 0644]
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/aai/AaiPropertiesExt.java [new file with mode: 0644]
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/configuration/ApplicationConfiguration.java [new file with mode: 0644]
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/configuration/SecurityConfiguration.java [new file with mode: 0644]
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/configuration/StartupConfiguration.java [new file with mode: 0644]
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/exception/VeVnfmException.java [new file with mode: 0644]
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/StartupService.java [new file with mode: 0644]
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/SubscriberService.java [new file with mode: 0644]
adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/subscription/SubscribeSender.java [new file with mode: 0644]
adapters/mso-ve-vnfm-adapter/src/main/resources/META-INF/services/org.onap.so.client.RestProperties [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/StartupServiceTest.java [new file with mode: 0644]
adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/subscription/SubscribeSenderTest.java [new file with mode: 0644]
common/src/main/java/org/onap/so/client/aai/AAIResourcesClient.java

index 02abd60..3518445 100644 (file)
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
-    <dependency>
-      <groupId>org.springframework.boot</groupId>
-      <artifactId>spring-boot-starter-actuator</artifactId>
-    </dependency>
     <dependency>
       <groupId>org.onap.so.adapters</groupId>
       <artifactId>mso-vnfm-adapter-ext-clients</artifactId>
       <version>${project.version}</version>
     </dependency>
-
+    <dependency>
+      <groupId>org.onap.so</groupId>
+      <artifactId>common</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.glassfish.jersey.core</groupId>
+      <artifactId>jersey-common</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.glassfish.jersey.core</groupId>
+      <artifactId>jersey-client</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.glassfish.jersey.inject</groupId>
+      <artifactId>jersey-hk2</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
index c4ca5f1..e4a6bed 100644 (file)
@@ -23,10 +23,10 @@ package org.onap.so.adapters.vevnfm;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
-@SpringBootApplication
+@SpringBootApplication(scanBasePackages = {"org.onap.so"})
 public class Application {
 
-    public static void main(String... args) {
+    public static void main(final String... args) {
         SpringApplication.run(Application.class, args);
     }
 }
diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/aai/AaiConnection.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/aai/AaiConnection.java
new file mode 100644 (file)
index 0000000..91a79b2
--- /dev/null
@@ -0,0 +1,82 @@
+/*-
+ * ============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.aai;
+
+import java.util.List;
+import java.util.Optional;
+import org.onap.aai.domain.yang.EsrSystemInfo;
+import org.onap.aai.domain.yang.EsrVnfm;
+import org.onap.aai.domain.yang.EsrVnfmList;
+import org.onap.so.client.aai.AAIObjectType;
+import org.onap.so.client.aai.AAIResourcesClient;
+import org.onap.so.client.aai.entities.uri.AAIUriFactory;
+import org.onap.so.client.graphinventory.entities.uri.Depth;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+
+@Service
+public class AaiConnection {
+
+    private static final Logger logger = LoggerFactory.getLogger(AaiConnection.class);
+
+    private static final int FIRST_INDEX = 0;
+
+    public String receiveVnfm() {
+        final AAIResourcesClient resourcesClient = new AAIResourcesClient();
+        final Optional<EsrVnfmList> response =
+                resourcesClient.get(EsrVnfmList.class, AAIUriFactory.createResourceUri(AAIObjectType.VNFM_LIST));
+
+        if (response.isPresent()) {
+            final EsrVnfmList esrVnfmList = response.get();
+            logger.info("The VNFM replied with: {}", esrVnfmList);
+            final List<EsrVnfm> esrVnfm = esrVnfmList.getEsrVnfm();
+
+            if (esrVnfm.isEmpty()) {
+                return null;
+            }
+
+            final String vnfmId = esrVnfm.get(FIRST_INDEX).getVnfmId();
+            return receiveVnfmServiceUrl(resourcesClient, vnfmId);
+        }
+
+        return null;
+    }
+
+    private String receiveVnfmServiceUrl(final AAIResourcesClient resourcesClient, final String vnfmId) {
+        final Optional<EsrVnfm> response = resourcesClient.get(EsrVnfm.class,
+                AAIUriFactory.createResourceUri(AAIObjectType.VNFM, vnfmId).depth(Depth.ONE));
+
+        if (response.isPresent()) {
+            final EsrVnfm esrVnfm = response.get();
+            logger.info("The VNFM replied with: {}", esrVnfm);
+            final List<EsrSystemInfo> esrSystemInfo = esrVnfm.getEsrSystemInfoList().getEsrSystemInfo();
+
+            if (esrSystemInfo.isEmpty()) {
+                return null;
+            }
+
+            return esrSystemInfo.get(FIRST_INDEX).getServiceUrl();
+        }
+
+        return null;
+    }
+}
diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/aai/AaiPropertiesExt.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/aai/AaiPropertiesExt.java
new file mode 100644 (file)
index 0000000..aa8c7f6
--- /dev/null
@@ -0,0 +1,67 @@
+/*-
+ * ============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.aai;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import org.onap.so.client.aai.AAIProperties;
+import org.onap.so.client.aai.AAIVersion;
+import org.onap.so.spring.SpringContextHelper;
+import org.springframework.context.ApplicationContext;
+
+public class AaiPropertiesExt implements AAIProperties {
+
+    private final String endpoint;
+    private final String encryptedBasicAuth;
+    private final String encryptionKey;
+
+    public AaiPropertiesExt() {
+        final ApplicationContext context = SpringContextHelper.getAppContext();
+        this.endpoint = context.getEnvironment().getProperty("aai.endpoint");
+        this.encryptedBasicAuth = context.getEnvironment().getProperty("aai.auth");
+        this.encryptionKey = context.getEnvironment().getProperty("mso.key");
+    }
+
+    @Override
+    public URL getEndpoint() throws MalformedURLException {
+        return new URL(endpoint);
+    }
+
+    @Override
+    public String getSystemName() {
+        return "MSO";
+    }
+
+    @Override
+    public AAIVersion getDefaultVersion() {
+        return AAIVersion.V15;
+    }
+
+    @Override
+    public String getAuth() {
+        return encryptedBasicAuth;
+    }
+
+    @Override
+    public String getKey() {
+        return encryptionKey;
+    }
+}
diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/configuration/ApplicationConfiguration.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/configuration/ApplicationConfiguration.java
new file mode 100644 (file)
index 0000000..108b2ee
--- /dev/null
@@ -0,0 +1,36 @@
+/*-
+ * ============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.configuration;
+
+import org.onap.so.rest.service.HttpRestServiceProvider;
+import org.onap.so.rest.service.HttpRestServiceProviderImpl;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.client.RestTemplate;
+
+@Configuration
+public class ApplicationConfiguration {
+
+    @Bean
+    public HttpRestServiceProvider restProvider(final RestTemplate restTemplate) {
+        return new HttpRestServiceProviderImpl(restTemplate);
+    }
+}
diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/configuration/SecurityConfiguration.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/configuration/SecurityConfiguration.java
new file mode 100644 (file)
index 0000000..32c2559
--- /dev/null
@@ -0,0 +1,35 @@
+/*-
+ * ============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.configuration;
+
+import org.onap.so.security.SoBasicWebSecurityConfigurerAdapter;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+
+@Configuration
+public class SecurityConfiguration extends SoBasicWebSecurityConfigurerAdapter {
+
+    @Override
+    protected void configure(final HttpSecurity http) throws Exception {
+        http.authorizeRequests().antMatchers().permitAll().and().requestMatchers().antMatchers("/").and()
+                .authorizeRequests().anyRequest().authenticated();
+    }
+}
diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/configuration/StartupConfiguration.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/configuration/StartupConfiguration.java
new file mode 100644 (file)
index 0000000..f7b7283
--- /dev/null
@@ -0,0 +1,48 @@
+/*-
+ * ============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.configuration;
+
+import org.onap.so.adapters.vevnfm.service.StartupService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.context.event.ApplicationReadyEvent;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.event.EventListener;
+import org.springframework.core.env.Environment;
+import org.springframework.core.env.Profiles;
+
+@Configuration
+public class StartupConfiguration {
+
+    public static final String TEST_PROFILE = "test";
+
+    @Autowired
+    private Environment environment;
+
+    @Autowired
+    private StartupService startupService;
+
+    @EventListener(ApplicationReadyEvent.class)
+    public void onApplicationReadyEvent() throws Exception {
+        if (!environment.acceptsProfiles(Profiles.of(TEST_PROFILE))) {
+            startupService.run();
+        }
+    }
+}
diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/exception/VeVnfmException.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/exception/VeVnfmException.java
new file mode 100644 (file)
index 0000000..abd9ff9
--- /dev/null
@@ -0,0 +1,28 @@
+/*-
+ * ============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.exception;
+
+public class VeVnfmException extends Exception {
+
+    public VeVnfmException(final String message) {
+        super(message);
+    }
+}
diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/StartupService.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/StartupService.java
new file mode 100644 (file)
index 0000000..7a9ec96
--- /dev/null
@@ -0,0 +1,53 @@
+/*-
+ * ============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.apache.logging.log4j.util.Strings;
+import org.onap.so.adapters.vevnfm.aai.AaiConnection;
+import org.onap.so.adapters.vevnfm.exception.VeVnfmException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class StartupService {
+
+    @Autowired
+    private AaiConnection aaiConnection;
+
+    @Autowired
+    private SubscriberService subscriberService;
+
+    private static void isValid(final String endpoint) throws VeVnfmException {
+        if (Strings.isBlank(endpoint)) {
+            throw new VeVnfmException("No 'url' field in VNFM info");
+        }
+    }
+
+    public void run() throws Exception {
+        final String endpoint = aaiConnection.receiveVnfm();
+        isValid(endpoint);
+        final boolean done = subscriberService.subscribe(endpoint);
+
+        if (!done) {
+            throw new VeVnfmException("Could not subscribe to VNFM");
+        }
+    }
+}
diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/SubscriberService.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/SubscriberService.java
new file mode 100644 (file)
index 0000000..e413124
--- /dev/null
@@ -0,0 +1,67 @@
+/*-
+ * ============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.subscription.SubscribeSender;
+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.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+@Service
+public class SubscriberService {
+
+    private static final char COLON = ':';
+
+    @Value("${notification.url}")
+    private String notificationUrl;
+
+    @Value("${server.port}")
+    private String serverPort;
+
+    @Value("${system.url}")
+    private String systemUrl;
+
+    @Autowired
+    private SubscribeSender sender;
+
+    public boolean subscribe(final String endpoint) {
+        final LccnSubscriptionRequest request = createRequest();
+        return sender.send(endpoint, request);
+    }
+
+    private LccnSubscriptionRequest createRequest() {
+        final LccnSubscriptionRequest request = new LccnSubscriptionRequest();
+        request.callbackUri(getCallbackUri());
+        final SubscriptionsAuthenticationParamsBasic paramsBasic = new SubscriptionsAuthenticationParamsBasic();
+        final SubscriptionsAuthentication authentication = new SubscriptionsAuthentication();
+        authentication.setParamsBasic(paramsBasic);
+        request.authentication(authentication);
+
+        return request;
+    }
+
+    private String getCallbackUri() {
+        return systemUrl + COLON + serverPort + notificationUrl;
+    }
+}
diff --git a/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/subscription/SubscribeSender.java b/adapters/mso-ve-vnfm-adapter/src/main/java/org/onap/so/adapters/vevnfm/subscription/SubscribeSender.java
new file mode 100644 (file)
index 0000000..1b3a049
--- /dev/null
@@ -0,0 +1,58 @@
+/*-
+ * ============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.subscription;
+
+import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
+import org.onap.so.rest.service.HttpRestServiceProvider;
+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.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Component;
+
+@Component
+public class SubscribeSender {
+
+    private static final Logger logger = LoggerFactory.getLogger(SubscribeSender.class);
+
+    @Value("${vnfm.subscription}")
+    private String vnfmSubscription;
+
+    @Autowired
+    private HttpRestServiceProvider restProvider;
+
+    public boolean send(final String endpoint, final LccnSubscriptionRequest request) {
+        final ResponseEntity<String> response = restProvider.postHttpRequest(request, getUrl(endpoint), String.class);
+
+        final HttpStatus statusCode = response.getStatusCode();
+        final String body = response.getBody();
+
+        logger.info("The VNFM replied with the code {} and the body {}", statusCode, body);
+
+        return HttpStatus.CREATED == statusCode;
+    }
+
+    private String getUrl(final String endpoint) {
+        return endpoint + vnfmSubscription;
+    }
+}
diff --git a/adapters/mso-ve-vnfm-adapter/src/main/resources/META-INF/services/org.onap.so.client.RestProperties b/adapters/mso-ve-vnfm-adapter/src/main/resources/META-INF/services/org.onap.so.client.RestProperties
new file mode 100644 (file)
index 0000000..9fc8e62
--- /dev/null
@@ -0,0 +1 @@
+org.onap.so.adapters.vevnfm.aai.AaiPropertiesExt
index 30b3955..12197d7 100644 (file)
@@ -1,5 +1,5 @@
 #
-# Copyright © 2019 Samsung.
+# Copyright © 2019, 2020 Samsung.
 # All rights reserved.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+server:
+  port: 8080
+
+system:
+  url: http://localhost
+
 notification:
-  url:  /lcm/v1/vnf/instances/notifications
+  url: /lcm/v1/vnf/instances/notifications
+
+mso:
+  key: 07a7159d3bf51a0e53be7a8f89699be7
+
+aai:
+  endpoint: https://aai.onap:30233
+  auth: 75C4483F9C05E2C33A8602635FA532397EC44AB667A2B64DED4FEE08DD932F2E3C1FEE
+
+vnfm:
+  subscription: /vnflcm/v1/subscriptions
 
 spring:
   http:
index c8d2a6b..418c2e2 100644 (file)
 
 package org.onap.so.adapters.vevnfm.controller;
 
+import static org.junit.Assert.assertEquals;
+import org.junit.Before;
 import org.junit.Test;
-import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification;
+import org.junit.runner.RunWith;
+import org.onap.so.adapters.vevnfm.configuration.StartupConfiguration;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import static org.junit.Assert.assertEquals;
+import org.springframework.http.MediaType;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.MvcResult;
+import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.context.WebApplicationContext;
 
+@SpringBootTest
+@RunWith(SpringRunner.class)
+@ActiveProfiles(StartupConfiguration.TEST_PROFILE)
 public class NotificationControllerTest {
 
-    private final NotificationController controller = new NotificationController();
+    private static final String MINIMAL_JSON_CONTENT = "{}";
+    private static final int ZERO = 0;
+
+    @Value("${notification.url}")
+    private String notificationUrl;
+
+    @Autowired
+    private WebApplicationContext webApplicationContext;
+
+    private MockMvc mvc;
+
+    @Before
+    public void init() {
+        mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
+    }
 
     @Test
-    public void testReceiveNotification() {
-        final VnfLcmOperationOccurrenceNotification notification = new VnfLcmOperationOccurrenceNotification();
-        final ResponseEntity response = controller.receiveNotification(notification);
-        assertEquals(HttpStatus.OK, response.getStatusCode());
+    public void testReceiveNotification() throws Exception {
+        // given
+        final MockHttpServletRequestBuilder request = MockMvcRequestBuilders.post(notificationUrl)
+                .contentType(MediaType.APPLICATION_JSON).content(MINIMAL_JSON_CONTENT);
+
+        // when
+        final MvcResult mvcResult = mvc.perform(request).andReturn();
+
+        // then
+        final MockHttpServletResponse response = mvcResult.getResponse();
+        assertEquals(HttpStatus.OK.value(), response.getStatus());
+        assertEquals(ZERO, response.getContentLength());
     }
 }
diff --git a/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/service/StartupServiceTest.java b/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/service/StartupServiceTest.java
new file mode 100644 (file)
index 0000000..8c480d0
--- /dev/null
@@ -0,0 +1,80 @@
+/*-
+ * ============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.mockito.Mockito.*;
+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.exception.VeVnfmException;
+
+@RunWith(MockitoJUnitRunner.class)
+public class StartupServiceTest {
+
+    @Mock
+    private AaiConnection aaiConnection;
+
+    @Mock
+    private SubscriberService subscriberService;
+
+    @InjectMocks
+    private StartupService startupService;
+
+    @Test
+    public void testSuccess() throws Exception {
+        // given
+        final String endpoint = "lh";
+
+        when(aaiConnection.receiveVnfm()).thenReturn(endpoint);
+        when(subscriberService.subscribe(endpoint)).thenReturn(true);
+
+        // when
+        startupService.run();
+
+        // then
+        verify(aaiConnection, times(1)).receiveVnfm();
+        verify(subscriberService, times(1)).subscribe(endpoint);
+    }
+
+    @Test(expected = VeVnfmException.class)
+    public void testFailureAai() throws Exception {
+        // given
+        when(aaiConnection.receiveVnfm()).thenReturn(null);
+
+        // when
+        startupService.run();
+    }
+
+    @Test(expected = VeVnfmException.class)
+    public void testFailureSubscriber() throws Exception {
+        // given
+        final String endpoint = "lh";
+
+        when(aaiConnection.receiveVnfm()).thenReturn(endpoint);
+        when(subscriberService.subscribe(endpoint)).thenReturn(false);
+
+        // when
+        startupService.run();
+    }
+}
diff --git a/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/subscription/SubscribeSenderTest.java b/adapters/mso-ve-vnfm-adapter/src/test/java/org/onap/so/adapters/vevnfm/subscription/SubscribeSenderTest.java
new file mode 100644 (file)
index 0000000..62a624a
--- /dev/null
@@ -0,0 +1,97 @@
+/*-
+ * ============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.subscription;
+
+import static org.junit.Assert.assertTrue;
+import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
+import static org.springframework.test.web.client.ExpectedCount.once;
+import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
+import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import org.hamcrest.CoreMatchers;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.onap.so.adapters.vevnfm.configuration.StartupConfiguration;
+import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.test.web.client.MockRestServiceServer;
+import org.springframework.web.client.RestTemplate;
+
+@SpringBootTest
+@RunWith(SpringRunner.class)
+@ActiveProfiles(StartupConfiguration.TEST_PROFILE)
+public class SubscribeSenderTest {
+
+    private static final String SLASH = "/";
+    private static final String MINIMAL_JSON_CONTENT = "{}";
+
+    private static final Gson GSON;
+
+    static {
+        final GsonBuilder builder = new GsonBuilder();
+        builder.serializeNulls();
+        GSON = builder.create();
+    }
+
+    @Value("${vnfm.subscription}")
+    private String vnfmSubscription;
+
+    @Autowired
+    private SubscribeSender sender;
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    private MockRestServiceServer mockRestServer;
+
+    @Before
+    public void init() {
+        mockRestServer = MockRestServiceServer.bindTo(restTemplate).build();
+    }
+
+    @Test
+    public void testSuccess() {
+        // given
+        final String endpoint = "lh";
+        final LccnSubscriptionRequest request = new LccnSubscriptionRequest();
+
+        mockRestServer.expect(once(), requestTo(SLASH + endpoint + vnfmSubscription))
+                .andExpect(header(CONTENT_TYPE, CoreMatchers.containsString(MediaType.APPLICATION_JSON_VALUE)))
+                .andExpect(method(HttpMethod.POST)).andExpect(content().json(GSON.toJson(request)))
+                .andRespond(withStatus(HttpStatus.CREATED).body(MINIMAL_JSON_CONTENT));
+
+        // when
+        final boolean done = sender.send(endpoint, request);
+
+        // then
+        assertTrue(done);
+        mockRestServer.verify();
+    }
+}
index 5b302f6..a89bea6 100644 (file)
@@ -44,6 +44,11 @@ public class AAIResourcesClient extends
         aaiClient = (AAIClient) super.client;
     }
 
+    public AAIResourcesClient(AAIClient client) {
+        super(client);
+        aaiClient = (AAIClient) super.client;
+    }
+
     @Override
     public AAIResultWrapper createWrapper(String json) {
         return new AAIResultWrapper(json);