diff options
Diffstat (limited to 'example-spring-boot/src/main/java')
7 files changed, 279 insertions, 0 deletions
diff --git a/example-spring-boot/src/main/java/org/onap/msb/sdk/example/springboot/ContextRefreshedListener.java b/example-spring-boot/src/main/java/org/onap/msb/sdk/example/springboot/ContextRefreshedListener.java new file mode 100644 index 0000000..9f3e532 --- /dev/null +++ b/example-spring-boot/src/main/java/org/onap/msb/sdk/example/springboot/ContextRefreshedListener.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright 2017 Infosys Limited and others. + *------------------------------------------------------------------------------ + * 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. + ******************************************************************************/ +package org.onap.msb.sdk.example.springboot; + +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.msb.sdk.example.springboot.common.MsbHelper; + +@Component +public class ContextRefreshedListener implements ApplicationListener<ContextRefreshedEvent>{ + + @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/msb/sdk/example/springboot/EmployeeServiceClient.java b/example-spring-boot/src/main/java/org/onap/msb/sdk/example/springboot/EmployeeServiceClient.java new file mode 100644 index 0000000..8017f04 --- /dev/null +++ b/example-spring-boot/src/main/java/org/onap/msb/sdk/example/springboot/EmployeeServiceClient.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright 2017 Infosys Limited and others. + *------------------------------------------------------------------------------ + * 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. + ******************************************************************************/ +package org.onap.msb.sdk.example.springboot; + +import org.onap.msb.sdk.example.springboot.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<Employee> queryEmployee(); +} diff --git a/example-spring-boot/src/main/java/org/onap/msb/sdk/example/springboot/ExampleClient.java b/example-spring-boot/src/main/java/org/onap/msb/sdk/example/springboot/ExampleClient.java new file mode 100644 index 0000000..ff0dee8 --- /dev/null +++ b/example-spring-boot/src/main/java/org/onap/msb/sdk/example/springboot/ExampleClient.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright 2017 Infosys Limited and others. + *------------------------------------------------------------------------------ + * 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. + ******************************************************************************/ +package org.onap.msb.sdk.example.springboot; + +import java.io.IOException; + +import org.onap.msb.sdk.example.springboot.model.Employee; +import org.onap.msb.sdk.httpclient.RestServiceCreater; +import org.onap.msb.sdk.httpclient.msb.MSBServiceClient; + + +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/msb/sdk/example/springboot/SpringBootApp.java b/example-spring-boot/src/main/java/org/onap/msb/sdk/example/springboot/SpringBootApp.java new file mode 100644 index 0000000..4a37b58 --- /dev/null +++ b/example-spring-boot/src/main/java/org/onap/msb/sdk/example/springboot/SpringBootApp.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright 2017 Infosys Limited and others. + *------------------------------------------------------------------------------ + * 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. + ******************************************************************************/ +package org.onap.msb.sdk.example.springboot; + +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/msb/sdk/example/springboot/common/MsbHelper.java b/example-spring-boot/src/main/java/org/onap/msb/sdk/example/springboot/common/MsbHelper.java new file mode 100644 index 0000000..9c1642c --- /dev/null +++ b/example-spring-boot/src/main/java/org/onap/msb/sdk/example/springboot/common/MsbHelper.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright 2017 Infosys Limited and others. + *------------------------------------------------------------------------------ + * 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. + ******************************************************************************/ +package org.onap.msb.sdk.example.springboot.common; + +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<Node> 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/java/org/onap/msb/sdk/example/springboot/controller/EmployeeController.java b/example-spring-boot/src/main/java/org/onap/msb/sdk/example/springboot/controller/EmployeeController.java new file mode 100644 index 0000000..9223006 --- /dev/null +++ b/example-spring-boot/src/main/java/org/onap/msb/sdk/example/springboot/controller/EmployeeController.java @@ -0,0 +1,25 @@ +/******************************************************************************* + * Copyright 2017 Infosys Limited and others. + *------------------------------------------------------------------------------ + * 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. + ******************************************************************************/ +package org.onap.msb.sdk.example.springboot.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import org.onap.msb.sdk.example.springboot.model.Employee;; + +@RestController +public class EmployeeController { + @RequestMapping("/employee") + public Employee getEmployees() { + 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/msb/sdk/example/springboot/model/Employee.java b/example-spring-boot/src/main/java/org/onap/msb/sdk/example/springboot/model/Employee.java new file mode 100644 index 0000000..43e6d88 --- /dev/null +++ b/example-spring-boot/src/main/java/org/onap/msb/sdk/example/springboot/model/Employee.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright 2017 Infosys Limited and others. + *------------------------------------------------------------------------------ + * 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. + ******************************************************************************/ +package org.onap.msb.sdk.example.springboot.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 |