7b7c6afcb8811f839324e702bb338cd20dd70178
[msb/java-sdk.git] / src / main / java / org / onap / msb / sdk / httpclient / builder / impl / ClientRetrofitObjectBuilder.java
1 /*******************************************************************************
2  * Copyright 2017-2018 ZTE, Inc. and others.
3  * 
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
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
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
12  * the License.
13  ******************************************************************************/
14 /**
15  * 
16  */
17 package org.onap.msb.sdk.httpclient.builder.impl;
18
19 import java.util.HashSet;
20 import java.util.LinkedHashMap;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.concurrent.atomic.AtomicReference;
24
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;
33
34 import okhttp3.OkHttpClient;
35 import retrofit2.Retrofit;
36
37 /**
38  * @author 10071214
39  *
40  */
41 public class ClientRetrofitObjectBuilder implements IRetrofitObjectBuilder {
42
43
44     private RetrofitServiceHandlerContext context;
45
46
47     public ClientRetrofitObjectBuilder(RetrofitServiceHandlerContext context) {
48         super();
49         this.context = context;
50     }
51
52
53     /*
54      * (non-Javadoc)
55      * 
56      * @see
57      * com.zte.ums.zenap.httpclient.retrofit.builder.IRetrofitObjectBuilder#buildRetrofitObject(
58      * java. util.concurrent.atomic.AtomicReference)
59      */
60     @Override
61     public Map<ServiceHttpEndPointObject, Object> buildRetrofitObject(
62                     AtomicReference<Map<ServiceHttpEndPointObject, Object>> endPointToRetrofitRef,
63                     ServiceHttpEndPointObject lastEndPoint) throws RetrofitServiceRuntimeException {
64
65         Map<ServiceHttpEndPointObject, Object> srvEndPointToRetrofit = endPointToRetrofitRef.get();
66         if (srvEndPointToRetrofit == null) {
67             srvEndPointToRetrofit = new LinkedHashMap<>();
68             try {
69
70
71                 ServiceHttpEndPointBeanObject srvhttpEndPointBeanObject = context.getServiceHttpEndPointBeanObject();
72
73                 // MsbClientFactory msbClient =
74                 // context.getLocator().getService(MsbClientFactory.class);
75
76                 MicroServiceFullInfo fullInfo = null;
77
78                 fullInfo = context.getMsbClient().queryMicroServiceInfo(srvhttpEndPointBeanObject.getServiceName(),
79                                 srvhttpEndPointBeanObject.getServiceVersion());
80
81
82                 for (NodeInfo nodeInfo : fullInfo.getNodes()) {
83
84                     MicroServiceFullInfo cloneFullInfo = cloneFullInfo(fullInfo, nodeInfo);
85
86                     ServiceHttpEndPointObject endPointObj = new ServiceHttpEndPointObject(
87                                     srvhttpEndPointBeanObject.getServiceName(),
88                                     srvhttpEndPointBeanObject.getServiceVersion(), nodeInfo, cloneFullInfo);
89
90                     // 目前支持http
91                     String baseUrl = null;
92                     if (fullInfo.getUrl() == null || fullInfo.getUrl().trim().length() == 0
93                                     || fullInfo.getUrl().equals("/")) {
94                         baseUrl = String.format("http://%s:%s/", nodeInfo.getIp(), nodeInfo.getPort());
95                     } else {
96                         baseUrl = String.format("http://%s:%s%s/", nodeInfo.getIp(), nodeInfo.getPort(),
97                                         fullInfo.getUrl());
98                     }
99
100                     OkHttpClient httpClient = null;
101
102                     if (context.getHttpClientConf() != null) {
103                         if (srvhttpEndPointBeanObject.getClientProtocl().toLowerCase().equals("https")) {
104                             httpClient = RetrofitServiceUtils.buildDefaultOkHttpsClient(context.getHttpClientConf());
105                         } else {
106                             httpClient = RetrofitServiceUtils.buildDefaultOkHttpClient(context.getHttpClientConf());
107                         }
108                     } else {
109                         if (srvhttpEndPointBeanObject.getClientProtocl().toLowerCase().equals("https")) {
110                             httpClient = RetrofitServiceUtils.buildDefaultOkHttpsClient(
111                                             RetrofitServiceHandlerContext.getGlobalHttpClientConf());
112                         } else {
113                             httpClient = RetrofitServiceUtils.buildDefaultOkHttpClient(
114                                             RetrofitServiceHandlerContext.getGlobalHttpClientConf());
115                         }
116                     }
117
118
119
120                     Retrofit retrofit = new Retrofit.Builder().client(httpClient).baseUrl(baseUrl)
121                                     .addConverterFactory(context.getConverterFactoryBuilder().buildConverterFactory())
122                                     .build();
123
124                     srvEndPointToRetrofit.put(endPointObj, retrofit.create(context.getRetrofitSrvInterfaceClazz()));
125
126                 }
127
128                 if (srvEndPointToRetrofit.isEmpty()) {
129                     throw new RetrofitServiceRuntimeException("can't find service in msb,serviceName:"
130                                     + srvhttpEndPointBeanObject.getServiceName() + ",serviceVersion:"
131                                     + srvhttpEndPointBeanObject.getServiceVersion());
132                 }
133
134                 if (lastEndPoint != null) {
135                     srvEndPointToRetrofit.remove(lastEndPoint);
136                 }
137
138                 if (srvEndPointToRetrofit.isEmpty()) {
139                     throw new RetrofitServiceRuntimeException("can't find other service in msb,serviceName:"
140                                     + srvhttpEndPointBeanObject.getServiceName() + ",serviceVersion:"
141                                     + srvhttpEndPointBeanObject.getServiceVersion());
142                 }
143
144                 if (endPointToRetrofitRef.compareAndSet(null, srvEndPointToRetrofit)) {
145                     context.setLastUpdateMsbTime(System.currentTimeMillis());
146                 }
147
148                 return endPointToRetrofitRef.get();
149
150
151             } catch (Exception e) {
152                 throw new RetrofitServiceRuntimeException("init Retrofit service map fail", e);
153             }
154
155         } else {
156             return endPointToRetrofitRef.get();
157         }
158     }
159
160
161     private MicroServiceFullInfo cloneFullInfo(MicroServiceFullInfo fullInfo, NodeInfo nodeInfo) {
162
163         MicroServiceFullInfo cloneFuleInfo = new MicroServiceFullInfo();
164         cloneFuleInfo.setMetadata(fullInfo.getMetadata());
165         cloneFuleInfo.setProtocol(fullInfo.getProtocol());
166         cloneFuleInfo.setServiceName(fullInfo.getServiceName());
167         cloneFuleInfo.setStatus(fullInfo.getStatus());
168         cloneFuleInfo.setUrl(fullInfo.getUrl());
169         cloneFuleInfo.setVersion(fullInfo.getVersion());
170         cloneFuleInfo.setVisualRange(fullInfo.getVisualRange());
171         cloneFuleInfo.setEnable_ssl(fullInfo.isEnable_ssl());
172         Set<NodeInfo> nodeInfos = new HashSet<>();
173         nodeInfos.add(nodeInfo);
174         cloneFuleInfo.setNodes(nodeInfos);
175         return cloneFuleInfo;
176     }
177
178 }