diff options
author | HuabingZhao <zhao.huabing@zte.com.cn> | 2018-03-09 16:49:00 +0800 |
---|---|---|
committer | HuabingZhao <zhao.huabing@zte.com.cn> | 2018-03-09 16:49:06 +0800 |
commit | 126db2fa722566d2aa18c6faf2a87dffbd844ffc (patch) | |
tree | fa95f5ce9e7de3b001994218c2fdd15834ff940f /sdclient/discovery-service/src | |
parent | 084cbea7096e9487627ad752315609594b201c89 (diff) |
Support multiple versions under a service name
Issue-ID: MSB-178
Change-Id: I502cf37884beda228a534a60f8303df7cb149fac
Signed-off-by: HuabingZhao <zhao.huabing@zte.com.cn>
Diffstat (limited to 'sdclient/discovery-service/src')
3 files changed, 99 insertions, 88 deletions
diff --git a/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/ConsulServiceWrapper.java b/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/ConsulServiceWrapper.java index 87d2b84..d94c09e 100644 --- a/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/ConsulServiceWrapper.java +++ b/sdclient/discovery-service/src/main/java/org/onap/msb/sdclient/wrapper/ConsulServiceWrapper.java @@ -177,7 +177,18 @@ public class ConsulServiceWrapper { } String serviceName = consul_serviceName; - if (StringUtils.isNotBlank(namespace)) { + // Remove version and namespace from consul service name + // Consul_serviceName Format: serviceName-version-namespace + if (StringUtils.isNotBlank(version) && StringUtils.isNotBlank(namespace)) { + if (consul_serviceName.endsWith("-" + version + "-" + namespace)) { + serviceName = consul_serviceName.substring(0, + consul_serviceName.length() - version.length() - namespace.length() - 2); + } + } else if (StringUtils.isNotBlank(version)) { + if (consul_serviceName.endsWith("-" + version)) { + serviceName = consul_serviceName.substring(0, consul_serviceName.length() - version.length() - 1); + } + } else if (StringUtils.isNotBlank(namespace)) { if (consul_serviceName.endsWith("-" + namespace)) { serviceName = consul_serviceName.substring(0, consul_serviceName.length() - namespace.length() - 1); } @@ -226,7 +237,7 @@ public class ConsulServiceWrapper { "get MicroServiceInfo FAIL: The label query parameter format is wrong (key:value)"); } - String consul_serviceName = getServiceName4Consul(serviceName, namespace); + String consul_serviceName = getServiceName4Consul(serviceName, version, namespace); ConsulResponse consulResponse = getHealthServices(consul_serviceName, ifPassStatus, wait, index); if (consulResponse == null) { @@ -620,8 +631,8 @@ public class ConsulServiceWrapper { node.setIp(requestIP); } - String serverId = microServiceInfo.getNamespace() + "_" + serviceName + "_" + node.getIp() + "_" - + node.getPort(); + String serverId = microServiceInfo.getNamespace() + "_" + microServiceInfo.getVersion() + "_" + + serviceName + "_" + node.getIp() + "_" + node.getPort(); List<String> tags = new ArrayList<String>(); @@ -763,7 +774,8 @@ public class ConsulServiceWrapper { agentService.setId(serverId); agentService.setPort(Integer.parseInt(node.getPort())); - String consul_serviceName = getServiceName4Consul(serviceName, microServiceInfo.getNamespace()); + String consul_serviceName = getServiceName4Consul(serviceName, microServiceInfo.getVersion(), + microServiceInfo.getNamespace()); agentService.setName(consul_serviceName); @@ -825,7 +837,7 @@ public class ConsulServiceWrapper { checkServiceNameAndVersion(serviceName, version); - String consul_serviceName = getServiceName4Consul(serviceName, namespace); + String consul_serviceName = getServiceName4Consul(serviceName, version, namespace); List<CatalogService> catalogServiceList = getConsulServices(consul_serviceName, version); @@ -953,7 +965,7 @@ public class ConsulServiceWrapper { "delete MicroServiceInfo FAIL:Port(" + port + ")is not a valid Port address"); } - String consul_serviceName = getServiceName4Consul(serviceName, namespace); + String consul_serviceName = getServiceName4Consul(serviceName, version, namespace); List<CatalogService> catalogServiceList = getConsulServices(consul_serviceName, version); @@ -1153,7 +1165,7 @@ public class ConsulServiceWrapper { "healthCheck by TTL FAIL:Port(" + checkNode.getPort() + ")is not a valid Port address"); } - String consul_serviceName = getServiceName4Consul(serviceName, namespace); + String consul_serviceName = getServiceName4Consul(serviceName, version, namespace); List<CatalogService> catalogServiceList = getConsulServices(consul_serviceName, version); @@ -1289,7 +1301,7 @@ public class ConsulServiceWrapper { } - String consul_serviceName = getServiceName4Consul(serviceName, namespace); + String consul_serviceName = getServiceName4Consul(serviceName, version, namespace); ConsulResponse consulResponse = getHealthServices(consul_serviceName, ifPassStatus, "", ""); if (consulResponse == null) { @@ -1593,18 +1605,17 @@ public class ConsulServiceWrapper { } - private String getServiceName4Consul(String serviceName, String namespace) { - String consul_serviceName; + private String getServiceName4Consul(String serviceName, String version, String namespace) { + String consul_serviceName = serviceName; + + if (StringUtils.isNotBlank(version)) { + consul_serviceName = consul_serviceName + "-" + version; + } if (StringUtils.isNotBlank(namespace)) { - // if (DiscoverUtil.APIGATEWAY_SERVINCE_DEFAULT.equals(namespace)) { - // consul_serviceName=serviceName; - // }else{ - consul_serviceName = serviceName + "-" + namespace; - // } - } else { - consul_serviceName = serviceName; + consul_serviceName = consul_serviceName + "-" + namespace; } + return consul_serviceName; } @@ -1874,7 +1885,6 @@ public class ConsulServiceWrapper { } - } diff --git a/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/ConsulServiceWrapperTest.java b/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/ConsulServiceWrapperTest.java index 401d335..064f6fd 100644 --- a/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/ConsulServiceWrapperTest.java +++ b/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/ConsulServiceWrapperTest.java @@ -41,7 +41,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) -@PrepareForTest({HttpClientUtil.class,ConfigUtil.class,DiscoverAppConfig.class}) +@PrepareForTest({HttpClientUtil.class, ConfigUtil.class, DiscoverAppConfig.class}) public class ConsulServiceWrapperTest { private static final String restJson = @@ -50,31 +50,31 @@ public class ConsulServiceWrapperTest { "[{\"Node\":\"server\",\"Address\":\"127.0.0.1\",\"TaggedAddresses\":{\"lan\":\"127.0.0.1\",\"wan\":\"127.0.0.1\"},\"ServiceID\":\"_test_10.74.56.36_5656\",\"ServiceName\":\"test\",\"ServiceTags\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"REST\\\",\\\"version\\\":\\\"v1\\\",\\\"url\\\":\\\"/test\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0\\\"}\"],\"ServiceAddress\":\"10.74.56.36\",\"ServicePort\":5656,\"ServiceEnableTagOverride\":false,\"CreateIndex\":1819452,\"ModifyIndex\":1819454}]"; private static final String catalog4ttlJson = "[{\"Node\":\"server\",\"Address\":\"127.0.0.1\",\"TaggedAddresses\":{\"lan\":\"127.0.0.1\",\"wan\":\"127.0.0.1\"},\"ServiceID\":\"_test_10.74.56.36_5656\",\"ServiceName\":\"test\",\"ServiceTags\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"REST\\\",\\\"version\\\":\\\"v1\\\",\\\"url\\\":\\\"/test\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0\\\"}\",\"\\\"checks\\\":{\\\"ttl\\\":\\\"10s\\\"}\"],\"ServiceAddress\":\"10.74.56.36\",\"ServicePort\":5656,\"ServiceEnableTagOverride\":false,\"CreateIndex\":1819452,\"ModifyIndex\":1819454}]"; - private static final String mockRestUrl = "http://127.0.0.1:8500/v1/health/service/test"; + private static final String mockRestUrl = "http://127.0.0.1:8500/v1/health/service/test-v1"; private static final String mockPostUrl = "http://127.0.0.1:8500/v1/catalog/register"; - private static final String mockdel_gettUrl = "http://127.0.0.1:8500/v1/catalog/service/test"; + private static final String mockdel_gettUrl = "http://127.0.0.1:8500/v1/catalog/service/test-v1"; private static final String mockdeltUrl = "http://127.0.0.1:8500/v1/catalog/deregister"; private static final String mockdeltUrl4agent = "http://127.0.0.1:8500/v1/agent/deregister"; - + private static final String mockgetListUrl = "http://127.0.0.1:8500/v1/catalog/services"; private static final String restListJson = - "{\"consul\":[],\"test-tt\":[\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0\\\"}\",\"\\\"base\\\":{\\\"version\\\":\\\"v1\\\",\\\"protocol\\\":\\\"REST\\\",\\\"url\\\":\\\"/api/microservices/v1\\\",\\\"visualRange\\\":\\\"0\\\"}\",\"\\\"ns\\\":{\\\"namespace\\\":\\\"tt\\\"}\"]}"; + "{\"consul\":[],\"test-tt\":[\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0\\\"}\",\"\\\"base\\\":{\\\"version\\\":\\\"v1\\\",\\\"protocol\\\":\\\"REST\\\",\\\"url\\\":\\\"/api/microservices/v1\\\",\\\"visualRange\\\":\\\"0\\\"}\",\"\\\"ns\\\":{\\\"namespace\\\":\\\"tt\\\"}\"]}"; private static ConsulServiceWrapper consulServiceWrapper = ConsulServiceWrapper.getInstance(); - + @Test - public void test_getAllMicroServiceInstances(){ - mockGetList(); - List<MicroServiceFullInfo> serviceList=consulServiceWrapper.getAllMicroServiceInstances(); - Assert.assertEquals(1, serviceList.size()); - MicroServiceFullInfo service=serviceList.get(0); - Assert.assertEquals( "test-tt",service.getServiceName()); - Assert.assertEquals( "",service.getNamespace()); + public void test_getAllMicroServiceInstances() { + mockGetList(); + List<MicroServiceFullInfo> serviceList = consulServiceWrapper.getAllMicroServiceInstances(); + Assert.assertEquals(1, serviceList.size()); + MicroServiceFullInfo service = serviceList.get(0); + Assert.assertEquals("test-tt", service.getServiceName()); + Assert.assertEquals("", service.getNamespace()); } - - + + @Test public void test_getMicroServiceInstance() { mockGetRest(); @@ -106,18 +106,18 @@ public class ConsulServiceWrapperTest { serviceInfo.setHost("host"); serviceInfo.setPath("/test"); serviceInfo.setNetwork_plane_type("net"); - - List<KeyVaulePair> metadata=new ArrayList<KeyVaulePair>(); - metadata.add(new KeyVaulePair("key1","val1")); - metadata.add(new KeyVaulePair("key2","val2")); + + List<KeyVaulePair> metadata = new ArrayList<KeyVaulePair>(); + metadata.add(new KeyVaulePair("key1", "val1")); + metadata.add(new KeyVaulePair("key2", "val2")); serviceInfo.setMetadata(metadata); - - - List<String> labels=new ArrayList<String>(); + + + List<String> labels = new ArrayList<String>(); labels.add("111:111"); - labels.add("222:222"); + labels.add("222:222"); serviceInfo.setLabels(labels); - + Set<Node> nodes = new HashSet<Node>(); Node node = new Node(); node.setIp("10.74.44.1"); @@ -139,8 +139,8 @@ public class ConsulServiceWrapperTest { Assert.assertEquals("HTTP 500 Internal Server Error", e.getMessage()); } } - - + + @Test public void test_saveMicroServiceInstance2() { MicroServiceInfo serviceInfo = new MicroServiceInfo(); @@ -155,18 +155,18 @@ public class ConsulServiceWrapperTest { serviceInfo.setHost("host"); serviceInfo.setPath("/test"); serviceInfo.setNetwork_plane_type("net"); - - List<KeyVaulePair> metadata=new ArrayList<KeyVaulePair>(); - metadata.add(new KeyVaulePair("key1","val1")); - metadata.add(new KeyVaulePair("key2","val2")); + + List<KeyVaulePair> metadata = new ArrayList<KeyVaulePair>(); + metadata.add(new KeyVaulePair("key1", "val1")); + metadata.add(new KeyVaulePair("key2", "val2")); serviceInfo.setMetadata(metadata); - - - List<String> labels=new ArrayList<String>(); + + + List<String> labels = new ArrayList<String>(); labels.add("111:111"); - labels.add("222:222"); + labels.add("222:222"); serviceInfo.setLabels(labels); - + Set<Node> nodes = new HashSet<Node>(); Node node = new Node(); node.setIp("10.74.44.1"); @@ -188,7 +188,7 @@ public class ConsulServiceWrapperTest { Assert.assertEquals("HTTP 500 Internal Server Error", e.getMessage()); } } - + @Test public void test_saveMicroServiceInstance4agent() { MicroServiceInfo serviceInfo = new MicroServiceInfo(); @@ -204,12 +204,12 @@ public class ConsulServiceWrapperTest { nodes.add(node); serviceInfo.setNodes(nodes); - PowerMockito.mockStatic(System.class); - PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("agent"); - DiscoverAppConfig discoverConfig=PowerMockito.mock(DiscoverAppConfig.class); - ConfigUtil.getInstance().initConsulRegisterMode(discoverConfig); - - + PowerMockito.mockStatic(System.class); + PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("agent"); + DiscoverAppConfig discoverConfig = PowerMockito.mock(DiscoverAppConfig.class); + ConfigUtil.getInstance().initConsulRegisterMode(discoverConfig); + + mockGetRest4null(); // mockGetPost(); try { @@ -217,9 +217,9 @@ public class ConsulServiceWrapperTest { } catch (Exception e) { Assert.assertEquals("HTTP 500 Internal Server Error", e.getMessage()); } - + PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("catalog"); - ConfigUtil.getInstance().initConsulRegisterMode(discoverConfig); + ConfigUtil.getInstance().initConsulRegisterMode(discoverConfig); } @Test @@ -228,31 +228,31 @@ public class ConsulServiceWrapperTest { mockDelete(); consulServiceWrapper.deleteMicroService("test", "v1", ""); } - + @Test public void test_deleteMicroServiceInstance() { mockGet4Delete(); mockDelete(); consulServiceWrapper.deleteMicroServiceInstance("test", "v1", "", "10.74.56.36", "5656"); } - + @Test public void test_deleteMicroService4agent() { - mockGet4Delete(); - mockDelete4agent(); - - DiscoverAppConfig discoverConfig=PowerMockito.mock(DiscoverAppConfig.class); - PowerMockito.mockStatic(System.class); - PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("agent"); - ConfigUtil.getInstance().initConsulRegisterMode(discoverConfig); - - consulServiceWrapper.deleteMicroService("test", "v1", ""); - - PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("catalog"); - ConfigUtil.getInstance().initConsulRegisterMode(discoverConfig); + mockGet4Delete(); + mockDelete4agent(); + + DiscoverAppConfig discoverConfig = PowerMockito.mock(DiscoverAppConfig.class); + PowerMockito.mockStatic(System.class); + PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("agent"); + ConfigUtil.getInstance().initConsulRegisterMode(discoverConfig); + + consulServiceWrapper.deleteMicroService("test", "v1", ""); + + PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("catalog"); + ConfigUtil.getInstance().initConsulRegisterMode(discoverConfig); } - + @Test public void test_healthCheckbyTTL() { @@ -275,13 +275,14 @@ public class ConsulServiceWrapperTest { PowerMockito.when(HttpClientUtil.httpGet(mockdel_gettUrl)).thenReturn(catalogJson); } - + private void mockGetList() { PowerMockito.mockStatic(HttpClientUtil.class); PowerMockito.when(HttpClientUtil.httpGet(mockgetListUrl)).thenReturn(restListJson); - + ConsulResponse<Object> consulResponse = new ConsulResponse(restJson, new BigInteger("1000")); - PowerMockito.when(HttpClientUtil.httpWaitGet("http://127.0.0.1:8500/v1/health/service/test-tt")).thenReturn(consulResponse); + PowerMockito.when(HttpClientUtil.httpWaitGet("http://127.0.0.1:8500/v1/health/service/test-tt")) + .thenReturn(consulResponse); } @@ -300,15 +301,16 @@ public class ConsulServiceWrapperTest { PowerMockito.when(HttpClientUtil.httpPostWithJSON(mockdeltUrl, serviceJson)).thenReturn(200); } - + private void mockDelete4agent() { - PowerMockito.when(HttpClientUtil.httpPostWithJSON("http://127.0.0.1:8500/v1/agent/service/deregister/_test_10.74.56.36_5656", "")).thenReturn(200); + PowerMockito.when(HttpClientUtil.httpPostWithJSON( + "http://127.0.0.1:8500/v1/agent/service/deregister/_test_10.74.56.36_5656", "")) + .thenReturn(200); } - private void mockGetRest4null() { PowerMockito.mockStatic(HttpClientUtil.class); PowerMockito.when(HttpClientUtil.httpWaitGet(mockRestUrl)).thenReturn(null); diff --git a/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/PublishAddressWrapperTest.java b/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/PublishAddressWrapperTest.java index 1e5227f..114c1a2 100644 --- a/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/PublishAddressWrapperTest.java +++ b/sdclient/discovery-service/src/test/java/org/onap/msb/sdclient/wrapper/PublishAddressWrapperTest.java @@ -34,7 +34,6 @@ import org.onap.msb.sdclient.core.PublishAddress; import org.onap.msb.sdclient.core.PublishFullAddress; import org.onap.msb.sdclient.core.exception.UnprocessableEntityException; - import org.onap.msb.sdclient.wrapper.util.HttpClientUtil; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -48,7 +47,7 @@ public class PublishAddressWrapperTest { private static final String restJson = "[{\"Node\":{\"Node\":\"A23179111\",\"Address\":\"10.74.44.16\"},\"Service\":{\"ID\":\"_test_10.74.44.16_2356\",\"Service\":\"test\",\"Tags\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"REST\\\",\\\"url\\\":\\\"/api/test/v1\\\",\\\"version\\\":\\\"v1\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0|1\\\",\\\"apigateway\\\":\\\"test\\\",\\\"network_plane_type\\\":\\\"net\\\"}\"],\"Address\":\"10.74.44.16\",\"Port\":8086},\"Checks\":[{\"CheckID\":\"serfHealth\",\"Name\":\"Serf Health Status\",\"Status\":\"passing\"}]}]"; - private static final String mockRestUrl = "http://127.0.0.1:8500/v1/health/service/test"; + private static final String mockRestUrl = "http://127.0.0.1:8500/v1/health/service/test-v1"; private static final String uiJson = "[{\"Node\":{\"Node\":\"A23179111\",\"Address\":\"10.74.44.16\"},\"Service\":{\"ID\":\"_test_10.74.44.16_2356\",\"Service\":\"IUI_test\",\"Tags\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"UI\\\",\\\"url\\\":\\\"/iui/test\\\",\\\"path\\\":\\\"/iui/test_path\\\",\\\"host\\\":\\\"testhost\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0|1\\\",\\\"apigateway\\\":\\\"test\\\",\\\"network_plane_type\\\":\\\"net\\\"}\"],\"Address\":\"10.74.44.16\",\"Port\":8086},\"Checks\":[{\"CheckID\":\"serfHealth\",\"Name\":\"Serf Health Status\",\"Status\":\"passing\"}]}]"; @@ -56,11 +55,11 @@ public class PublishAddressWrapperTest { private static final String tcpJson = "[{\"Node\":{\"Node\":\"A23179111\",\"Address\":\"10.74.44.16\"},\"Service\":{\"ID\":\"_test_10.74.44.16_2356\",\"Service\":\"tcp_test\",\"Tags\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"TCP\\\",\\\"url\\\":\\\"/\\\",\\\"version\\\":\\\"v1\\\",\\\"publish_port\\\":\\\"40001\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0|1\\\",\\\"apigateway\\\":\\\"test\\\",\\\"network_plane_type\\\":\\\"net\\\"}\"],\"Address\":\"10.74.44.16\",\"Port\":8086},\"Checks\":[{\"CheckID\":\"serfHealth\",\"Name\":\"Serf Health Status\",\"Status\":\"passing\"}]}]"; - private static final String mockTCPUrl = "http://127.0.0.1:8500/v1/health/service/tcp_test"; + private static final String mockTCPUrl = "http://127.0.0.1:8500/v1/health/service/tcp_test-v1"; private static final String httpJson = "[{\"Node\":{\"Node\":\"A23179111\",\"Address\":\"10.74.44.16\"},\"Service\":{\"ID\":\"_test_10.74.44.16_2356\",\"Service\":\"test\",\"Tags\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"HTTP\\\",\\\"url\\\":\\\"/test\\\",\\\"version\\\":\\\"v1\\\",\\\"host\\\":\\\"testhost\\\"}\",\"\\\"ns\\\":{\\\"namespace\\\":\\\"ns\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"0|1\\\",\\\"apigateway\\\":\\\"test\\\",\\\"network_plane_type\\\":\\\"net\\\"}\"],\"Address\":\"10.74.44.16\",\"Port\":8086},\"Checks\":[{\"CheckID\":\"serfHealth\",\"Name\":\"Serf Health Status\",\"Status\":\"passing\"}]}]"; - private static final String mockHTTPUrl = "http://127.0.0.1:8500/v1/health/service/test-ns"; + private static final String mockHTTPUrl = "http://127.0.0.1:8500/v1/health/service/test-v1-ns"; |