From 33c5e66c67d9eb72f1bf1cef974a538005b8a7ff Mon Sep 17 00:00:00 2001 From: HuabingZhao Date: Thu, 17 Aug 2017 22:51:57 +0800 Subject: Example code for MSB SDK Issue-Id: MSB-23 Change-Id: I6ed1f5aa4ed254e23a332c5f67130e1d30016abf Signed-off-by: HuabingZhao --- example/pom.xml | 40 +++++++++++++ .../sdk/httpclient/client/AnimalServiceClient.java | 34 +++++++++++ .../msb/sdk/httpclient/client/ExampleClient.java | 47 +++++++++++++++ .../org/onap/msb/sdk/httpclient/common/Animal.java | 69 ++++++++++++++++++++++ .../org/onap/msb/sdk/httpclient/server/Config.java | 24 ++++++++ .../onap/msb/sdk/httpclient/server/ExampleApp.java | 51 ++++++++++++++++ .../onap/msb/sdk/httpclient/server/MsbHelper.java | 66 +++++++++++++++++++++ .../server/resources/AnimalResource.java | 38 ++++++++++++ example/src/main/resources/example.yml | 9 +++ pom.xml | 16 +---- 10 files changed, 379 insertions(+), 15 deletions(-) create mode 100644 example/pom.xml create mode 100644 example/src/main/java/org/onap/msb/sdk/httpclient/client/AnimalServiceClient.java create mode 100644 example/src/main/java/org/onap/msb/sdk/httpclient/client/ExampleClient.java create mode 100644 example/src/main/java/org/onap/msb/sdk/httpclient/common/Animal.java create mode 100644 example/src/main/java/org/onap/msb/sdk/httpclient/server/Config.java create mode 100644 example/src/main/java/org/onap/msb/sdk/httpclient/server/ExampleApp.java create mode 100644 example/src/main/java/org/onap/msb/sdk/httpclient/server/MsbHelper.java create mode 100644 example/src/main/java/org/onap/msb/sdk/httpclient/server/resources/AnimalResource.java create mode 100644 example/src/main/resources/example.yml diff --git a/example/pom.xml b/example/pom.xml new file mode 100644 index 0000000..6f40a19 --- /dev/null +++ b/example/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + org.onap.oparent + oparent + 1.0.0-SNAPSHOT + + + org.onap.msb.sdk + msb-java-sdk-example + msb-java-sdk-example + 1.0.0-SNAPSHOT + jar + + + + org.onap.msb.sdk + msb-java-sdk + 1.0.0-SNAPSHOT + + + io.dropwizard + dropwizard-core + 0.8.0 + + + + + + maven-compiler-plugin + + 1.8 + 1.8 + + + + + diff --git a/example/src/main/java/org/onap/msb/sdk/httpclient/client/AnimalServiceClient.java b/example/src/main/java/org/onap/msb/sdk/httpclient/client/AnimalServiceClient.java new file mode 100644 index 0000000..22101ad --- /dev/null +++ b/example/src/main/java/org/onap/msb/sdk/httpclient/client/AnimalServiceClient.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. 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.httpclient.client; + +import org.onap.msb.sdk.httpclient.annotaion.ServiceHttpEndPoint; +import org.onap.msb.sdk.httpclient.common.Animal; + +import retrofit2.Call; +import retrofit2.http.GET; +import retrofit2.http.Path; + +/** + * @author hu.rui + * + */ + +@ServiceHttpEndPoint(serviceName = "animals", serviceVersion = "v1", nameSpace = "javaAF") +public interface AnimalServiceClient { + + @GET("animals/{name}") + Call queryAnimal(@Path("name") String name); + +} diff --git a/example/src/main/java/org/onap/msb/sdk/httpclient/client/ExampleClient.java b/example/src/main/java/org/onap/msb/sdk/httpclient/client/ExampleClient.java new file mode 100644 index 0000000..570078b --- /dev/null +++ b/example/src/main/java/org/onap/msb/sdk/httpclient/client/ExampleClient.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. 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.httpclient.client; + +import java.io.IOException; + +import org.onap.msb.sdk.httpclient.RetrofitServiceCreater; +import org.onap.msb.sdk.httpclient.common.Animal; +import org.onap.msb.sdk.httpclient.conf.HttpClientConf; +import org.onap.msb.sdk.httpclient.msb.MSBServiceWrapperClient; + + +public class ExampleClient { + + /** + * @param args + * @throws IOException + */ + public static void main(String[] args) throws IOException { + + HttpClientConf globalHttpClientConf = new HttpClientConf(); + MSBServiceWrapperClient msbClient = new MSBServiceWrapperClient("127.0.0.1", 10081); + + RetrofitServiceCreater retrofitServiceCreater = + new RetrofitServiceCreater(globalHttpClientConf, msbClient); + + AnimalServiceClient implProxy = + retrofitServiceCreater.createRetrofitService(AnimalServiceClient.class); + Animal animal = implProxy.queryAnimal("example").execute().body(); + System.out.println("animal:" + animal); + } + +} diff --git a/example/src/main/java/org/onap/msb/sdk/httpclient/common/Animal.java b/example/src/main/java/org/onap/msb/sdk/httpclient/common/Animal.java new file mode 100644 index 0000000..092a2f7 --- /dev/null +++ b/example/src/main/java/org/onap/msb/sdk/httpclient/common/Animal.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. 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.httpclient.common; + +import java.io.Serializable; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class Animal implements Serializable { + + private static final long serialVersionUID = -717235590728668809L; + + @JsonProperty + private String type; + + @JsonProperty + private String name; + + @JsonProperty + private int age; + + public Animal() {} + + public Animal(String type, String name, int age) { + this.type = type; + this.name = name; + this.age = age; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + @Override + public String toString() { + return "Animal{" + "type='" + type + '\'' + ", name='" + name + '\'' + ", age=" + age + '}'; + } +} diff --git a/example/src/main/java/org/onap/msb/sdk/httpclient/server/Config.java b/example/src/main/java/org/onap/msb/sdk/httpclient/server/Config.java new file mode 100644 index 0000000..4dbb6eb --- /dev/null +++ b/example/src/main/java/org/onap/msb/sdk/httpclient/server/Config.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. 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.httpclient.server; + +import io.dropwizard.Configuration; + +public class Config extends Configuration { + + + +} + + diff --git a/example/src/main/java/org/onap/msb/sdk/httpclient/server/ExampleApp.java b/example/src/main/java/org/onap/msb/sdk/httpclient/server/ExampleApp.java new file mode 100644 index 0000000..03c052a --- /dev/null +++ b/example/src/main/java/org/onap/msb/sdk/httpclient/server/ExampleApp.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. 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.httpclient.server; + +import org.onap.msb.sdk.httpclient.msb.MSBServiceWrapperClient; +import org.onap.msb.sdk.httpclient.server.resources.AnimalResource; + +import io.dropwizard.Application; +import io.dropwizard.setup.Environment; + + +public class ExampleApp extends Application { + + private static final String CONFIGURATION_FILE = "example.yml"; + + public static void main(String[] args) throws Exception { + + String configFile = null; + if (args.length > 1) { + configFile = args[0]; + } else { + configFile = ExampleApp.class.getClassLoader().getResource(CONFIGURATION_FILE).getFile(); + } + args = new String[] {"server", configFile}; + new ExampleApp().run(args); + } + + @Override + public void run(Config configuration, Environment environment) throws Exception { + + environment.jersey().register(new AnimalResource()); + + MSBServiceWrapperClient msbClient = new MSBServiceWrapperClient("127.0.0.1", 10081); + + MsbHelper helper = new MsbHelper(msbClient); + helper.registerMsb(); + + } + +} diff --git a/example/src/main/java/org/onap/msb/sdk/httpclient/server/MsbHelper.java b/example/src/main/java/org/onap/msb/sdk/httpclient/server/MsbHelper.java new file mode 100644 index 0000000..ffaba08 --- /dev/null +++ b/example/src/main/java/org/onap/msb/sdk/httpclient/server/MsbHelper.java @@ -0,0 +1,66 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. 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.httpclient.server; + +import java.net.InetAddress; +import java.util.HashSet; +import java.util.Set; + +import org.jvnet.hk2.annotations.Service; +import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; +import org.onap.msb.sdk.discovery.entity.Node; +import org.onap.msb.sdk.httpclient.msb.MSBServiceWrapperClient; + +/** + * @author hu.rui + * + */ +@Service +public class MsbHelper { + + private MSBServiceWrapperClient msbClient; + + + + public MsbHelper(MSBServiceWrapperClient msbClient) { + super(); + this.msbClient = msbClient; + } + + + + public void registerMsb() throws Exception { + + + MicroServiceInfo msinfo = new MicroServiceInfo(); + + msinfo.setServiceName("animals"); + msinfo.setVersion("v1"); + msinfo.setUrl("/api/rpc/v1"); + msinfo.setProtocol("REST"); + msinfo.setVisualRange("0|1"); + + Set nodes = new HashSet<>(); + Node node1 = new Node(); + node1.setIp(InetAddress.getLocalHost().getHostAddress()); + node1.setPort("9090"); + nodes.add(node1); + msinfo.setNodes(nodes); + msbClient.registerMicroServiceInfo(msinfo, false); + + } + + + +} diff --git a/example/src/main/java/org/onap/msb/sdk/httpclient/server/resources/AnimalResource.java b/example/src/main/java/org/onap/msb/sdk/httpclient/server/resources/AnimalResource.java new file mode 100644 index 0000000..7a51c0f --- /dev/null +++ b/example/src/main/java/org/onap/msb/sdk/httpclient/server/resources/AnimalResource.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. 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.httpclient.server.resources; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.onap.msb.sdk.httpclient.common.Animal; + + +@Path("/animals") +public class AnimalResource { + + + @GET + @Path("/{name}") + @Produces(MediaType.APPLICATION_JSON) + public Response query(@PathParam("name") String name) { + return Response.ok(new Animal("animal", name, (int) System.currentTimeMillis() % 10)).build(); + } + + +} diff --git a/example/src/main/resources/example.yml b/example/src/main/resources/example.yml new file mode 100644 index 0000000..b325acf --- /dev/null +++ b/example/src/main/resources/example.yml @@ -0,0 +1,9 @@ +server: + type: simple + rootPath: '/api/rpc/v1/*' + applicationContextPath: / + adminContextPath: /admin + connector: + type: http + port: 9090 + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 4e12675..74a075f 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ 1.0.0-SNAPSHOT - org.onap.msb.java.sdk + org.onap.msb.sdk msb-java-sdk msb-java-sdk 1.0.0-SNAPSHOT @@ -40,7 +40,6 @@ 2.6 - io.dropwizard dropwizard-metrics @@ -144,19 +143,6 @@ maven-enforcer-plugin - - - org.apache.maven.plugins - maven-source-plugin - - - attach-sources - - jar - - - - -- cgit 1.2.3-korg