e7f7cd5be33fb9d53e74eb5d992574db86463d2f
[msb/java-sdk.git] / src / main / java / org / onap / msb / sdk / httpclient / builder / impl / ClientRetrofitObjectBuilder.java
1 /*******************************************************************************
2  * Copyright 2017 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(java.
58    * 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 =
72             context.getServiceHttpEndPointBeanObject();
73
74         // MsbClientFactory msbClient = context.getLocator().getService(MsbClientFactory.class);
75
76         MicroServiceFullInfo fullInfo = null;
77
78         fullInfo =
79             context.getMsbClient().queryMicroServiceInfo(srvhttpEndPointBeanObject.getServiceName(),
80                 srvhttpEndPointBeanObject.getServiceVersion());
81
82
83         for (NodeInfo nodeInfo : fullInfo.getNodes()) {
84
85           MicroServiceFullInfo cloneFullInfo = cloneFullInfo(fullInfo, nodeInfo);
86
87           ServiceHttpEndPointObject endPointObj =
88               new ServiceHttpEndPointObject(srvhttpEndPointBeanObject.getServiceName(),
89                   srvhttpEndPointBeanObject.getServiceVersion(), nodeInfo, cloneFullInfo);
90
91           // 目前支持http
92           String baseUrl = null;
93           if (fullInfo.getUrl() == null || fullInfo.getUrl().trim().length() == 0
94               || fullInfo.getUrl().equals("/")) {
95             baseUrl = String.format("http://%s:%s/", nodeInfo.getIp(), nodeInfo.getPort());
96           } else {
97             baseUrl = String.format("http://%s:%s%s/", nodeInfo.getIp(), nodeInfo.getPort(),
98                 fullInfo.getUrl());
99           }
100
101           OkHttpClient httpClient = null;
102
103           if (context.getHttpClientConf() != null) {
104             if (srvhttpEndPointBeanObject.getClientProtocl().toLowerCase().equals("https")) {
105               httpClient =
106                   RetrofitServiceUtils.buildDefaultOkHttpsClient(context.getHttpClientConf());
107             } else {
108               httpClient =
109                   RetrofitServiceUtils.buildDefaultOkHttpClient(context.getHttpClientConf());
110             }
111           } else {
112             if (srvhttpEndPointBeanObject.getClientProtocl().toLowerCase().equals("https")) {
113               httpClient = RetrofitServiceUtils.buildDefaultOkHttpsClient(
114                   RetrofitServiceHandlerContext.getGlobalHttpClientConf());
115             } else {
116               httpClient = RetrofitServiceUtils.buildDefaultOkHttpClient(
117                   RetrofitServiceHandlerContext.getGlobalHttpClientConf());
118             }
119           }
120
121
122
123           Retrofit retrofit = new Retrofit.Builder().client(httpClient).baseUrl(baseUrl)
124               .addConverterFactory(context.getConverterFactoryBuilder().buildConverterFactory())
125               .build();
126
127           srvEndPointToRetrofit.put(endPointObj,
128               retrofit.create(context.getRetrofitSrvInterfaceClazz()));
129
130         }
131
132         if (srvEndPointToRetrofit.isEmpty()) {
133           throw new RetrofitServiceRuntimeException(
134               "can't find service in msb,serviceName:" + srvhttpEndPointBeanObject.getServiceName()
135                   + ",serviceVersion:" + srvhttpEndPointBeanObject.getServiceVersion());
136         }
137
138         if (lastEndPoint != null) {
139           srvEndPointToRetrofit.remove(lastEndPoint);
140         }
141
142         if (srvEndPointToRetrofit.isEmpty()) {
143           throw new RetrofitServiceRuntimeException("can't find other service in msb,serviceName:"
144               + srvhttpEndPointBeanObject.getServiceName() + ",serviceVersion:"
145               + srvhttpEndPointBeanObject.getServiceVersion());
146         }
147
148         if (endPointToRetrofitRef.compareAndSet(null, srvEndPointToRetrofit)) {
149           context.setLastUpdateMsbTime(System.currentTimeMillis());
150         }
151
152         return endPointToRetrofitRef.get();
153
154
155       } catch (Exception e) {
156         throw new RetrofitServiceRuntimeException("init Retrofit service map fail", e);
157       }
158
159     } else {
160       return endPointToRetrofitRef.get();
161     }
162   }
163
164
165   private MicroServiceFullInfo cloneFullInfo(MicroServiceFullInfo fullInfo, NodeInfo nodeInfo) {
166
167     MicroServiceFullInfo cloneFuleInfo = new MicroServiceFullInfo();
168     cloneFuleInfo.setMetadata(fullInfo.getMetadata());
169     cloneFuleInfo.setProtocol(fullInfo.getProtocol());
170     cloneFuleInfo.setServiceName(fullInfo.getServiceName());
171     cloneFuleInfo.setStatus(fullInfo.getStatus());
172     cloneFuleInfo.setUrl(fullInfo.getUrl());
173     cloneFuleInfo.setVersion(fullInfo.getVersion());
174     cloneFuleInfo.setVisualRange(fullInfo.getVisualRange());
175     Set<NodeInfo> nodeInfos = new HashSet<>();
176     nodeInfos.add(nodeInfo);
177     cloneFuleInfo.setNodes(nodeInfos);
178     return cloneFuleInfo;
179   }
180
181 }