diff options
author | HuabingZhao <zhao.huabing@zte.com.cn> | 2017-08-17 22:51:57 +0800 |
---|---|---|
committer | HuabingZhao <zhao.huabing@zte.com.cn> | 2017-08-17 22:52:14 +0800 |
commit | 33c5e66c67d9eb72f1bf1cef974a538005b8a7ff (patch) | |
tree | 26d86520ca87ef63f9a9f131107b728fa5d118ad /example/src | |
parent | 798ccdc010e4628f6638602a4426444a68c0c52a (diff) |
Example code for MSB SDK
Issue-Id: MSB-23
Change-Id: I6ed1f5aa4ed254e23a332c5f67130e1d30016abf
Signed-off-by: HuabingZhao <zhao.huabing@zte.com.cn>
Diffstat (limited to 'example/src')
8 files changed, 338 insertions, 0 deletions
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<Animal> 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<Config> { + + 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<Node> 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 |