diff options
author | Mufaddal Makati <mmakati@mmakatimbpr15.cablelabs.com> | 2015-08-07 13:09:49 -0700 |
---|---|---|
committer | Mufaddal Makati <mmakati@mmakatimbpr15.cablelabs.com> | 2015-08-07 13:09:49 -0700 |
commit | ed73dbf357aff41edcbab401a94e5fbc266d9391 (patch) | |
tree | e3561a9290e4bd0de19704228867ec91910e68ed /uni/unibase/src/main/java/com/cablelabs/vcpe/uni/unibase/client | |
parent | f44f4aa8b5eb244fc7603c17e875eebf0f3b0b67 (diff) |
First Commit
Diffstat (limited to 'uni/unibase/src/main/java/com/cablelabs/vcpe/uni/unibase/client')
-rw-r--r-- | uni/unibase/src/main/java/com/cablelabs/vcpe/uni/unibase/client/EvcPathClient.java | 180 | ||||
-rw-r--r-- | uni/unibase/src/main/java/com/cablelabs/vcpe/uni/unibase/client/UniClient.java | 188 |
2 files changed, 368 insertions, 0 deletions
diff --git a/uni/unibase/src/main/java/com/cablelabs/vcpe/uni/unibase/client/EvcPathClient.java b/uni/unibase/src/main/java/com/cablelabs/vcpe/uni/unibase/client/EvcPathClient.java new file mode 100644 index 0000000..1bc41b5 --- /dev/null +++ b/uni/unibase/src/main/java/com/cablelabs/vcpe/uni/unibase/client/EvcPathClient.java @@ -0,0 +1,180 @@ +package com.cablelabs.vcpe.uni.unibase.client; + +import com.cablelabs.vcpe.common.Dbg; +import com.cablelabs.vcpe.uni.unibase.model.EvcPath; +import com.cablelabs.vcpe.uni.unibase.model.Uni; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.xml.bind.DatatypeConverter; +import java.util.List; + +/** + * Created by steve on 5/28/15. + */ + +public class EvcPathClient { + + private String evcMgrServer = "localhost"; + private String evcMgrPort = "8181"; + private String evcMgrCfgRESTPath = "/restconf/config/cl-vcpe-mef:evcs/"; + + private Client client; // provided by Jersey + + public EvcPathClient() { + client = ClientBuilder.newClient(); + } + + //-------------------------------------------------------- + public Response create(EvcPath evcPath ) + //-------------------------------------------------------- + { + WebTarget target =client.target("http://"+evcMgrServer+":" + evcMgrPort + evcMgrCfgRESTPath); + + String json = evcPath.toJson(); + Dbg.p("\nEVC Create JSON:"); + Dbg.p(json); + + Response response = target.path("evc") + .request(MediaType.APPLICATION_JSON) + .post(Entity.entity(json, MediaType.APPLICATION_JSON)); + + if (response.getStatus() != 200 ) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + //return response.readEntity(EvcPath.class); + return response; + } + + //-------------------------------------------------------- + public Response update(EvcPath evcPath) throws Exception + //-------------------------------------------------------- + { + WebTarget target =client.target("http://"+evcMgrServer+":" + evcMgrPort + evcMgrCfgRESTPath); + + String json = evcPath.toJson(); + String unamepass = "admin:admin"; + String authorizationHeaderValue = "Basic " + DatatypeConverter.printBase64Binary(unamepass.getBytes("UTF-8")); + Dbg.p("\nEVC Create/Update JSON:"); + Dbg.p(json); + + Response response = target.path("evc/"+evcPath.getId()) + .request(MediaType.APPLICATION_JSON) + .header("Authorization", authorizationHeaderValue) + .put(Entity.entity(json, MediaType.APPLICATION_JSON)); + + if (response.getStatus() != 200 ) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + return response; + } + //-------------------------------------------------------- + public void delete(String evcPathId) throws Exception + //-------------------------------------------------------- + // delete EvcPath of specified ID + { + WebTarget target =client.target("http://"+evcMgrServer+":" + evcMgrPort + evcMgrCfgRESTPath); + + String uNameAndPass = "admin:admin"; + String authorizationHeaderValue = "Basic " + + DatatypeConverter.printBase64Binary(uNameAndPass.getBytes("UTF-8")); + + Response response = target.path("evc/" + evcPathId) + .request(MediaType.APPLICATION_JSON) + .header("Authorization", authorizationHeaderValue) + .delete(); + if (response.getStatus() != 200) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + } + + // + // Code from here below requires work in order to work with ODL + // + + //-------------------------------------------------------- + public EvcPath get(String evcPathId) + //-------------------------------------------------------- + // get EvcPath of specified ID + { + WebTarget target = client.target("http://localhost:9090/evcmgr/webapi/"); + + Response response = target.path("evc/"+evcPathId).request(MediaType.APPLICATION_JSON).get(Response.class); + if (response.getStatus() != 200) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + + return response.readEntity(EvcPath.class); + } + + //-------------------------------------------------------- + public List<EvcPath> getAll() + //-------------------------------------------------------- + // get a list of all EvcPath instances + { + + WebTarget target = client.target("http://localhost:9090/evcmgr/webapi/"); + + // Can I do this with a Response, so that I can check for errors + List<EvcPath> response = target.path("evc/list") + .request(MediaType.APPLICATION_JSON) + .get(new GenericType<List<EvcPath>>() { + }); + if (response == null) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException("there was an error on the server."); + } + return response; + } + + //-------------------------------------------------------- + public EvcPath testGet() + //-------------------------------------------------------- + // test marshaling of EvcPath class from server json + { + WebTarget target = client.target("http://localhost:9090/evcmgr/webapi/"); + + Response response = target.path("evc").request(MediaType.APPLICATION_JSON).get(Response.class); + if (response.getStatus() != 200) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + + //return response; + return response.readEntity(EvcPath.class); + } + + //-------------------------------------------------------- + public String ping() + //-------------------------------------------------------- + // test connectivity + { + + WebTarget target = client.target("http://localhost:9090/evcmgr/webapi/"); + + Response response = target.path("evc").request(MediaType.TEXT_PLAIN).get(); + if (response.getStatus() != 200) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + + + return response.readEntity(String.class); + } +} diff --git a/uni/unibase/src/main/java/com/cablelabs/vcpe/uni/unibase/client/UniClient.java b/uni/unibase/src/main/java/com/cablelabs/vcpe/uni/unibase/client/UniClient.java new file mode 100644 index 0000000..a1dcff4 --- /dev/null +++ b/uni/unibase/src/main/java/com/cablelabs/vcpe/uni/unibase/client/UniClient.java @@ -0,0 +1,188 @@ +package com.cablelabs.vcpe.uni.unibase.client; + +import com.cablelabs.vcpe.common.Dbg; +import com.cablelabs.vcpe.uni.unibase.model.Uni; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.List; +import javax.xml.bind.DatatypeConverter; + +/** + * Created by steve on 5/28/15. + */ + +public class UniClient { + + private String uniMgrServer = "localhost"; + private String uniMgrPort = "8181"; + private String uniMgrCfgRESTPath = "/restconf/config/cl-vcpe-mef:unis/"; + + private Client client; // provided by Jersey + + public UniClient() { + client = ClientBuilder.newClient(); + } + + //-------------------------------------------------------- + public Response create(Uni uni ) throws Exception + //-------------------------------------------------------- + // create Uni + { + WebTarget target =client.target("http://"+uniMgrServer+":" + uniMgrPort + uniMgrCfgRESTPath); + + String json = uni.toJson(); + Dbg.p("\nUNI Create JSON:"); + Dbg.p(json); + + String uNameAndPass = "admin:admin"; + String authorizationHeaderValue = "Basic " + + DatatypeConverter.printBase64Binary(uNameAndPass.getBytes("UTF-8")); + Response response = target.path("uni") + .request(MediaType.APPLICATION_JSON) + .header("Authorization", authorizationHeaderValue) + .post(Entity.entity(json, MediaType.APPLICATION_JSON)); + + if (response.getStatus() != 200 ) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + //return response.readEntity(Uni.class); + return response; + } + + //-------------------------------------------------------- + public Response update(Uni uni) throws Exception + //-------------------------------------------------------- + // create/update Uni. This seems to be how OLD expects uni creation + { + WebTarget target =client.target("http://"+uniMgrServer+":" + uniMgrPort + uniMgrCfgRESTPath); + + String json = uni.toJson(); + Dbg.p("\nUNI Create/Update JSON:"); + Dbg.p(json); + + String uNameAndPass = "admin:admin"; + String authorizationHeaderValue = "Basic " + + DatatypeConverter.printBase64Binary(uNameAndPass.getBytes("UTF-8")); + Response response = target.path("uni/"+uni.getId()) + .request(MediaType.APPLICATION_JSON) + .header("Authorization", authorizationHeaderValue) + .put(Entity.entity(json, MediaType.APPLICATION_JSON)); + + if (response.getStatus() != 200 ) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + return response; + } + + //-------------------------------------------------------- + public void delete(String uniId) throws Exception + //-------------------------------------------------------- + // delete Uni of specified ID + { + WebTarget target =client.target("http://"+uniMgrServer+":" + uniMgrPort + uniMgrCfgRESTPath); + + String uNameAndPass = "admin:admin"; + String authorizationHeaderValue = "Basic " + + DatatypeConverter.printBase64Binary(uNameAndPass.getBytes("UTF-8")); + + Response response = target.path("uni/" + uniId) + .request(MediaType.APPLICATION_JSON) + .header("Authorization", authorizationHeaderValue) + .delete(); + if (response.getStatus() != 200) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + } + + // + // Code from here below requires work in order to work with ODL + // + + //-------------------------------------------------------- + public Uni get(String uniId) + //-------------------------------------------------------- + // get Uni of specified ID + { + WebTarget target = client.target("http://localhost:9090/unimgr/webapi/"); + + Response response = target.path("uni/"+uniId).request(MediaType.APPLICATION_JSON).get(Response.class); + if (response.getStatus() != 200) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + + //return response; + return response.readEntity(Uni.class); + } + + //-------------------------------------------------------- + public List<Uni> getAll() + //-------------------------------------------------------- + // get a list of all Uni instances + { + + WebTarget target = client.target("http://localhost:9090/unimgr/webapi/"); + + // Can I do this with a Response, so that I can check for errors + List<Uni> response = target.path("uni/list") + .request(MediaType.APPLICATION_JSON) + .get(new GenericType<List<Uni>>() { + }); + if (response == null) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException("there was an error on the server."); + } + return response; + } + + //-------------------------------------------------------- + public Uni testGet() + //-------------------------------------------------------- + // test marshaling of Uni class from server json + { + WebTarget target = client.target("http://localhost:9090/unimgr/webapi/"); + + Response response = target.path("uni").request(MediaType.APPLICATION_JSON).get(Response.class); + if (response.getStatus() != 200) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + + //return response; + return response.readEntity(Uni.class); + } + + //-------------------------------------------------------- + public String ping() + //-------------------------------------------------------- + // test connectivity + { + + WebTarget target = client.target("http://localhost:9090/unimgr/webapi/"); + + Response response = target.path("uni").request(MediaType.TEXT_PLAIN).get(); + if (response.getStatus() != 200) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + + + return response.readEntity(String.class); + } +} |