1 /*******************************************************************************
2 * Copyright 2017-2018 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
13 ******************************************************************************/
17 package org.onap.msb.sdk.httpclient.builder.impl;
19 import java.util.HashSet;
20 import java.util.LinkedHashMap;
23 import java.util.concurrent.atomic.AtomicReference;
25 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
26 import org.onap.msb.sdk.discovery.entity.NodeInfo;
27 import org.onap.msb.sdk.httpclient.RetrofitServiceUtils;
28 import org.onap.msb.sdk.httpclient.ServiceHttpEndPointBeanObject;
29 import org.onap.msb.sdk.httpclient.ServiceHttpEndPointObject;
30 import org.onap.msb.sdk.httpclient.builder.IRetrofitObjectBuilder;
31 import org.onap.msb.sdk.httpclient.exception.RetrofitServiceRuntimeException;
32 import org.onap.msb.sdk.httpclient.handler.RetrofitServiceHandlerContext;
34 import okhttp3.OkHttpClient;
35 import retrofit2.Retrofit;
41 public class ClientRetrofitObjectBuilder implements IRetrofitObjectBuilder {
44 private RetrofitServiceHandlerContext context;
47 public ClientRetrofitObjectBuilder(RetrofitServiceHandlerContext context) {
49 this.context = context;
57 * com.zte.ums.zenap.httpclient.retrofit.builder.IRetrofitObjectBuilder#buildRetrofitObject(
58 * java. util.concurrent.atomic.AtomicReference)
61 public Map<ServiceHttpEndPointObject, Object> buildRetrofitObject(
62 AtomicReference<Map<ServiceHttpEndPointObject, Object>> endPointToRetrofitRef,
63 ServiceHttpEndPointObject lastEndPoint) throws RetrofitServiceRuntimeException {
65 Map<ServiceHttpEndPointObject, Object> srvEndPointToRetrofit = endPointToRetrofitRef.get();
66 if (srvEndPointToRetrofit == null) {
67 srvEndPointToRetrofit = new LinkedHashMap<>();
71 ServiceHttpEndPointBeanObject srvhttpEndPointBeanObject = context.getServiceHttpEndPointBeanObject();
73 // MsbClientFactory msbClient =
74 // context.getLocator().getService(MsbClientFactory.class);
76 MicroServiceFullInfo fullInfo = null;
78 fullInfo = context.getMsbClient().queryMicroServiceInfo(srvhttpEndPointBeanObject.getServiceName(),
79 srvhttpEndPointBeanObject.getServiceVersion());
82 for (NodeInfo nodeInfo : fullInfo.getNodes()) {
84 MicroServiceFullInfo cloneFullInfo = cloneFullInfo(fullInfo, nodeInfo);
86 ServiceHttpEndPointObject endPointObj = new ServiceHttpEndPointObject(
87 srvhttpEndPointBeanObject.getServiceName(),
88 srvhttpEndPointBeanObject.getServiceVersion(), nodeInfo, cloneFullInfo);
90 String baseUrl = null;
91 if (fullInfo.getUrl() == null || fullInfo.getUrl().trim().length() == 0
92 || fullInfo.getUrl().equals("/")) {
93 baseUrl = String.format("http://%s:%s/", nodeInfo.getIp(), nodeInfo.getPort());
95 baseUrl = String.format("http://%s:%s%s/", nodeInfo.getIp(), nodeInfo.getPort(),
99 OkHttpClient httpClient = null;
101 if (context.getHttpClientConf() != null) {
102 if (srvhttpEndPointBeanObject.getClientProtocl().toLowerCase().equals("https")) {
103 httpClient = RetrofitServiceUtils.buildDefaultOkHttpsClient(context.getHttpClientConf());
105 httpClient = RetrofitServiceUtils.buildDefaultOkHttpClient(context.getHttpClientConf());
108 if (srvhttpEndPointBeanObject.getClientProtocl().toLowerCase().equals("https")) {
109 httpClient = RetrofitServiceUtils.buildDefaultOkHttpsClient(
110 RetrofitServiceHandlerContext.getGlobalHttpClientConf());
112 httpClient = RetrofitServiceUtils.buildDefaultOkHttpClient(
113 RetrofitServiceHandlerContext.getGlobalHttpClientConf());
119 Retrofit retrofit = new Retrofit.Builder().client(httpClient).baseUrl(baseUrl)
120 .addConverterFactory(context.getConverterFactoryBuilder().buildConverterFactory())
123 srvEndPointToRetrofit.put(endPointObj, retrofit.create(context.getRetrofitSrvInterfaceClazz()));
127 if (srvEndPointToRetrofit.isEmpty()) {
128 throw new RetrofitServiceRuntimeException("can't find service in msb,serviceName:"
129 + srvhttpEndPointBeanObject.getServiceName() + ",serviceVersion:"
130 + srvhttpEndPointBeanObject.getServiceVersion());
133 if (lastEndPoint != null) {
134 srvEndPointToRetrofit.remove(lastEndPoint);
137 if (srvEndPointToRetrofit.isEmpty()) {
138 throw new RetrofitServiceRuntimeException("can't find other service in msb,serviceName:"
139 + srvhttpEndPointBeanObject.getServiceName() + ",serviceVersion:"
140 + srvhttpEndPointBeanObject.getServiceVersion());
143 if (endPointToRetrofitRef.compareAndSet(null, srvEndPointToRetrofit)) {
144 context.setLastUpdateMsbTime(System.currentTimeMillis());
147 return endPointToRetrofitRef.get();
150 } catch (Exception e) {
151 throw new RetrofitServiceRuntimeException("init Retrofit service map fail", e);
155 return endPointToRetrofitRef.get();
160 private MicroServiceFullInfo cloneFullInfo(MicroServiceFullInfo fullInfo, NodeInfo nodeInfo) {
162 MicroServiceFullInfo cloneFuleInfo = new MicroServiceFullInfo();
163 cloneFuleInfo.setMetadata(fullInfo.getMetadata());
164 cloneFuleInfo.setProtocol(fullInfo.getProtocol());
165 cloneFuleInfo.setServiceName(fullInfo.getServiceName());
166 cloneFuleInfo.setStatus(fullInfo.getStatus());
167 cloneFuleInfo.setUrl(fullInfo.getUrl());
168 cloneFuleInfo.setVersion(fullInfo.getVersion());
169 cloneFuleInfo.setVisualRange(fullInfo.getVisualRange());
170 cloneFuleInfo.setEnable_ssl(fullInfo.isEnable_ssl());
171 Set<NodeInfo> nodeInfos = new HashSet<>();
172 nodeInfos.add(nodeInfo);
173 cloneFuleInfo.setNodes(nodeInfos);
174 return cloneFuleInfo;