From e638628b22fef6f480fa104cd696834dddf6c030 Mon Sep 17 00:00:00 2001 From: virajput Date: Thu, 31 Aug 2017 15:46:27 +0530 Subject: added spring boot example using msb java sdk Issue-Id: MSB-23 Change-Id: Ic3caf7d63a4588aab73df1b07da3d15d6edfa485 Signed-off-by: virajput --- .../example/demo/ContextRefreshedListener.java | 27 ++++++++++ .../boot/example/demo/EmployeeServiceClient.java | 13 +++++ .../org/onap/boot/example/demo/ExampleClient.java | 35 ++++++++++++ .../org/onap/boot/example/demo/SpringBootApp.java | 13 +++++ .../demo/controller/EmployeeController.java | 22 ++++++++ .../org/onap/boot/example/demo/model/Employee.java | 63 ++++++++++++++++++++++ .../org/onap/boot/example/demo/msb/MsbHelper.java | 39 ++++++++++++++ .../src/main/resources/application.properties | 1 + .../demo/SpringBootServiceApplicationTests.java | 16 ++++++ 9 files changed, 229 insertions(+) create mode 100644 example-spring-boot/src/main/java/org/onap/boot/example/demo/ContextRefreshedListener.java create mode 100644 example-spring-boot/src/main/java/org/onap/boot/example/demo/EmployeeServiceClient.java create mode 100644 example-spring-boot/src/main/java/org/onap/boot/example/demo/ExampleClient.java create mode 100644 example-spring-boot/src/main/java/org/onap/boot/example/demo/SpringBootApp.java create mode 100644 example-spring-boot/src/main/java/org/onap/boot/example/demo/controller/EmployeeController.java create mode 100644 example-spring-boot/src/main/java/org/onap/boot/example/demo/model/Employee.java create mode 100644 example-spring-boot/src/main/java/org/onap/boot/example/demo/msb/MsbHelper.java create mode 100644 example-spring-boot/src/main/resources/application.properties create mode 100644 example-spring-boot/src/test/java/org/onap/boot/example/demo/SpringBootServiceApplicationTests.java (limited to 'example-spring-boot/src') diff --git a/example-spring-boot/src/main/java/org/onap/boot/example/demo/ContextRefreshedListener.java b/example-spring-boot/src/main/java/org/onap/boot/example/demo/ContextRefreshedListener.java new file mode 100644 index 0000000..c352f80 --- /dev/null +++ b/example-spring-boot/src/main/java/org/onap/boot/example/demo/ContextRefreshedListener.java @@ -0,0 +1,27 @@ +package org.onap.boot.example.demo; + +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.stereotype.Component; +import org.onap.msb.sdk.httpclient.msb.MSBServiceClient; +import org.onap.boot.example.demo.msb.MsbHelper; + +@Component +public class ContextRefreshedListener implements ApplicationListener{ + + @Override + public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { + System.out.println("Registring Service..."); + String MSB_IP="127.0.0.1"; + int MSB_Port=10081; + + MSBServiceClient msbClient = new MSBServiceClient(MSB_IP, MSB_Port); + MsbHelper helper = new MsbHelper(msbClient); + + try { + helper.registerMsb(); + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/example-spring-boot/src/main/java/org/onap/boot/example/demo/EmployeeServiceClient.java b/example-spring-boot/src/main/java/org/onap/boot/example/demo/EmployeeServiceClient.java new file mode 100644 index 0000000..8a7670b --- /dev/null +++ b/example-spring-boot/src/main/java/org/onap/boot/example/demo/EmployeeServiceClient.java @@ -0,0 +1,13 @@ +package org.onap.boot.example.demo; + +import org.onap.boot.example.demo.model.Employee; +import org.onap.msb.sdk.httpclient.annotaion.ServiceHttpEndPoint; + +import retrofit2.Call; +import retrofit2.http.GET; + +@ServiceHttpEndPoint(serviceName = "employee", serviceVersion = "v1") +public interface EmployeeServiceClient { + @GET("employee") + Call queryEmployee(); +} diff --git a/example-spring-boot/src/main/java/org/onap/boot/example/demo/ExampleClient.java b/example-spring-boot/src/main/java/org/onap/boot/example/demo/ExampleClient.java new file mode 100644 index 0000000..720264b --- /dev/null +++ b/example-spring-boot/src/main/java/org/onap/boot/example/demo/ExampleClient.java @@ -0,0 +1,35 @@ +package org.onap.boot.example.demo; + +import java.io.IOException; + +import org.onap.boot.example.demo.model.Employee; +import org.onap.msb.sdk.httpclient.RestServiceCreater; +import org.onap.msb.sdk.httpclient.msb.MSBServiceClient; + +import retrofit2.Response; + + +public class ExampleClient { + + /** + * @param args + * @throws IOException + */ + public static void main(String[] args) throws IOException { + //For real use case, MSB IP and Port should come from configuration file instead of hard code here + String MSB_IP="192.168.0.110"; + int MSB_Port=10081; + + MSBServiceClient msbClient = new MSBServiceClient(MSB_IP, MSB_Port); + + RestServiceCreater restServiceCreater = + new RestServiceCreater(msbClient); + + EmployeeServiceClient implProxy = + restServiceCreater.createService(EmployeeServiceClient.class); + + Employee employee = implProxy.queryEmployee().execute().body(); + System.out.println("Employee:" + employee); + } + +} diff --git a/example-spring-boot/src/main/java/org/onap/boot/example/demo/SpringBootApp.java b/example-spring-boot/src/main/java/org/onap/boot/example/demo/SpringBootApp.java new file mode 100644 index 0000000..b00b2e1 --- /dev/null +++ b/example-spring-boot/src/main/java/org/onap/boot/example/demo/SpringBootApp.java @@ -0,0 +1,13 @@ +package org.onap.boot.example.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootApp { + + public static void main(String[] args) { + SpringApplication.run(SpringBootApp.class, args); + } +} + diff --git a/example-spring-boot/src/main/java/org/onap/boot/example/demo/controller/EmployeeController.java b/example-spring-boot/src/main/java/org/onap/boot/example/demo/controller/EmployeeController.java new file mode 100644 index 0000000..a0046ad --- /dev/null +++ b/example-spring-boot/src/main/java/org/onap/boot/example/demo/controller/EmployeeController.java @@ -0,0 +1,22 @@ +package org.onap.boot.example.demo.controller; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import org.onap.boot.example.demo.model.Employee;; + +@RestController +public class EmployeeController +{ + @RequestMapping("/employee") + public Employee getEmployees() + { + //List employeesList = new ArrayList(); + //employeesList.add(new Employee(1,"John","Kelly","john.kelly@gmail.com")); + return (new Employee(1,"John","Kelly","john.kelly@gmail.com")); + } +} \ No newline at end of file diff --git a/example-spring-boot/src/main/java/org/onap/boot/example/demo/model/Employee.java b/example-spring-boot/src/main/java/org/onap/boot/example/demo/model/Employee.java new file mode 100644 index 0000000..b5cd629 --- /dev/null +++ b/example-spring-boot/src/main/java/org/onap/boot/example/demo/model/Employee.java @@ -0,0 +1,63 @@ +package org.onap.boot.example.demo.model; + +import java.io.Serializable; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class Employee implements Serializable { + + private static final long serialVersionUID = 1L; + + @JsonProperty + private Integer id; + + @JsonProperty + private String firstName; + + @JsonProperty + private String lastName; + + @JsonProperty + private String email; + + public Employee() {} + + public Employee(Integer id, String firstName, String lastName, String email) { + //super(); + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + this.email = email; + } + + public Integer getId() { + return id; + } + public void setId(Integer id) { + this.id = id; + } + public String getFirstName() { + return firstName; + } + public void setFirstName(String firstName) { + this.firstName = firstName; + } + public String getLastName() { + return lastName; + } + public void setLastName(String lastName) { + this.lastName = lastName; + } + public String getEmail() { + return email; + } + public void setEmail(String email) { + this.email = email; + } + + @Override + public String toString() { + return "Employee [id=" + id + ", firstName=" + firstName + + ", lastName=" + lastName + ", email=" + email + "]"; + } +} \ No newline at end of file diff --git a/example-spring-boot/src/main/java/org/onap/boot/example/demo/msb/MsbHelper.java b/example-spring-boot/src/main/java/org/onap/boot/example/demo/msb/MsbHelper.java new file mode 100644 index 0000000..8e81f66 --- /dev/null +++ b/example-spring-boot/src/main/java/org/onap/boot/example/demo/msb/MsbHelper.java @@ -0,0 +1,39 @@ +package org.onap.boot.example.demo.msb; + +import java.net.InetAddress; +import java.util.HashSet; +import java.util.Set; + +import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; +import org.onap.msb.sdk.discovery.entity.Node; +import org.onap.msb.sdk.httpclient.msb.MSBServiceClient; + +public class MsbHelper { + + private MSBServiceClient msbClient; + + public MsbHelper(MSBServiceClient msbClient) { + super(); + this.msbClient = msbClient; + } + + public void registerMsb() throws Exception { + + + MicroServiceInfo msinfo = new MicroServiceInfo(); + + msinfo.setServiceName("employee"); + msinfo.setVersion("v1"); + msinfo.setUrl("/api/v1"); + msinfo.setProtocol("REST"); + msinfo.setVisualRange("0|1"); + + Set nodes = new HashSet<>(); + Node node1 = new Node(); + node1.setIp(InetAddress.getLocalHost().getHostAddress()); + node1.setPort("8080"); + nodes.add(node1); + msinfo.setNodes(nodes); + msbClient.registerMicroServiceInfo(msinfo, false); + } +} diff --git a/example-spring-boot/src/main/resources/application.properties b/example-spring-boot/src/main/resources/application.properties new file mode 100644 index 0000000..001e1f0 --- /dev/null +++ b/example-spring-boot/src/main/resources/application.properties @@ -0,0 +1 @@ +server.contextPath=/api/v1 \ No newline at end of file diff --git a/example-spring-boot/src/test/java/org/onap/boot/example/demo/SpringBootServiceApplicationTests.java b/example-spring-boot/src/test/java/org/onap/boot/example/demo/SpringBootServiceApplicationTests.java new file mode 100644 index 0000000..8dfe937 --- /dev/null +++ b/example-spring-boot/src/test/java/org/onap/boot/example/demo/SpringBootServiceApplicationTests.java @@ -0,0 +1,16 @@ +package org.onap.boot.example.demo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringBootServiceApplicationTests { + + @Test + public void contextLoads() { + } + +} -- cgit 1.2.3-korg