97427c3f4ebb4a78e05cc4b39090b527e7de7945
[msb/apigateway.git] /
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package org.onap.msb.apiroute.wrapper.service;
17
18 import java.util.ArrayList;
19 import java.util.Calendar;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Set;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 import org.onap.msb.apiroute.api.MicroServiceFullInfo;
27 import org.onap.msb.apiroute.wrapper.dao.DAOFactory;
28 import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
29 import org.onap.msb.apiroute.wrapper.dao.service.IServiceDAO;
30 import org.onap.msb.apiroute.wrapper.dao.service.bean.Metadata;
31 import org.onap.msb.apiroute.wrapper.dao.service.bean.ServiceInfo;
32 import org.onap.msb.apiroute.wrapper.dao.service.bean.Spec;
33 import org.onap.msb.apiroute.wrapper.util.MicroServiceUtil;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.common.collect.ImmutableSet;
38
39 public class MicroServiceFullService {
40     private static final Logger LOGGER = LoggerFactory.getLogger(MicroServiceFullService.class);
41
42     private static MicroServiceFullService instance = new MicroServiceFullService();
43
44     private IServiceDAO serviceDAO = DAOFactory.getServiceDAO();
45
46     private MicroServiceFullService() {
47     }
48
49     public static MicroServiceFullService getInstance() {
50         return instance;
51     }
52
53     public List<MicroServiceFullInfo> getAllMicroServiceInstances() throws Exception {
54         String serviceKeyPattern = MicroServiceUtil.getPrefixedKey("*");
55
56         List<MicroServiceFullInfo> microServiceFullInfoList = new ArrayList<>();
57         List<ServiceInfo> serviceInfoList = serviceDAO.queryMultiService(serviceKeyPattern);
58         for (ServiceInfo serviceInfo : serviceInfoList) {
59             if (serviceInfo != null) {
60                 MicroServiceFullInfo microServiceFullInfo = MicroServiceFullAdapter.fromServiceInfo(serviceInfo);
61                 ;
62                 microServiceFullInfoList.add(microServiceFullInfo);
63             }
64         }
65         return microServiceFullInfoList;
66     }
67
68     public Set<String> getAllMicroServiceKey() throws Exception {
69         final Set<String> builder = new HashSet<String>();
70
71         String serviceKeyPattern = MicroServiceUtil.getPrefixedKey("*");
72         Set<String> serviceKeySet = RedisAccessWrapper.filterKeys(serviceKeyPattern);
73
74         Pattern serviceKeyRegexPattern = MicroServiceUtil.getServiceKeyRegexPattern();
75         for (String serviceKey : serviceKeySet) {
76             Matcher matcher = serviceKeyRegexPattern.matcher(serviceKey);
77             if (matcher.matches()) {
78                 builder.add(matcher.group("servicename"));
79             }
80         }
81         return builder;
82     }
83
84     public void saveMicroServiceInfo2Redis(MicroServiceFullInfo microServiceFullInfo) throws Exception {
85         if(microServiceFullInfo ==null){
86             throw new Exception("input microServiceInfo to be saved is null!");
87         }
88         ServiceInfo serviceInfo = MicroServiceFullAdapter.toServiceInfo(microServiceFullInfo);
89         String serviceKey = MicroServiceUtil.getServiceKey(microServiceFullInfo.getServiceName(),microServiceFullInfo.getVersion());
90         serviceDAO.saveService(serviceKey,serviceInfo);
91     }
92
93     public void updateMicroServiceStatus(String serviceName, String version, String status)
94             throws Exception {
95         if (null == version || "null".equals(version)) {
96             version = "";
97         }
98         String serviceKey = MicroServiceUtil.getServiceKey(serviceName, version);
99         ServiceInfo serviceInfo = serviceDAO.queryService(serviceKey);
100         if(serviceInfo != null){
101             serviceInfo.setStatus(status);
102             serviceDAO.saveService(serviceKey,serviceInfo);
103         }
104     }
105
106     public boolean existsMicroServiceInstance(String serviceName, String version)
107             throws Exception {
108         if (null == version || "null".equals(version)) {
109             version = "";
110         }
111         String serviceKey = MicroServiceUtil.getServiceKey(serviceName, version);
112         return RedisAccessWrapper.isExist(serviceKey);
113     }
114
115     public MicroServiceFullInfo getMicroServiceInstance(String serviceName, String version)
116             throws Exception {
117         if (null == version || "null".equals(version)) {
118             version = "";
119         }
120         String serviceKey = MicroServiceUtil.getServiceKey(serviceName, version);
121
122         MicroServiceFullInfo microServiceInfo = null;
123
124         ServiceInfo serviceInfo = null;
125         serviceInfo = serviceDAO.queryService(serviceKey);
126         if(serviceInfo!=null) {
127             microServiceInfo = MicroServiceFullAdapter.fromServiceInfo(serviceInfo);
128         }
129         return microServiceInfo;
130     }
131
132     /**
133      * query all the versions of the given ServiceName
134      * @param serviceName
135      * @return
136      * @throws Exception
137      */
138     public List<MicroServiceFullInfo> getAllVersionsOfTheService(String serviceName) throws Exception {
139         String serviceKeyPattern = MicroServiceUtil.getPrefixedKey(serviceName, "*");
140
141         List<MicroServiceFullInfo> microServiceFullInfoList = new ArrayList<>();
142         List<ServiceInfo> serviceInfoList = serviceDAO.queryMultiService(serviceKeyPattern);
143         for (ServiceInfo serviceInfo : serviceInfoList) {
144             if (serviceInfo != null) {
145                 MicroServiceFullInfo microServiceFullInfo = MicroServiceFullAdapter.fromServiceInfo(serviceInfo);
146                 microServiceFullInfoList.add(microServiceFullInfo);
147             }
148         }
149         return microServiceFullInfoList;
150     }
151
152     public void deleteMicroService(String serviceName, String version) throws Exception {
153         if (null == version || "null".equals(version)) {
154             version = "";
155         }
156         String serviceKey = MicroServiceUtil.getServiceKey(serviceName, version);
157         serviceDAO.deleteService(serviceKey);
158     }
159
160     public long deleteMultiMicroService(String keyPattern) throws Exception {
161         return serviceDAO.deleteMultiService(keyPattern);
162     }
163 }
164
165 class MicroServiceFullAdapter {
166     public static ServiceInfo toServiceInfo(MicroServiceFullInfo microServiceFullInfo) {
167         ServiceInfo serviceInfo = new ServiceInfo();
168         serviceInfo.setApiVersion(microServiceFullInfo.getVersion());
169         serviceInfo.setStatus(microServiceFullInfo.getStatus());
170
171
172         Spec spec = new Spec();
173         spec.setVisualRange(microServiceFullInfo.getVisualRange());
174         spec.setUrl(microServiceFullInfo.getUrl());
175         spec.setPublish_port(microServiceFullInfo.getPublish_port());
176         spec.setHost(microServiceFullInfo.getHost());
177         spec.setProtocol(microServiceFullInfo.getProtocol());
178         spec.setLb_policy(microServiceFullInfo.getLb_policy());
179         spec.setEnable_ssl(microServiceFullInfo.isEnable_ssl());
180         Set<org.onap.msb.apiroute.api.Node> nodeSet = microServiceFullInfo.getNodes();
181         List<org.onap.msb.apiroute.wrapper.dao.service.bean.Node> serviceNodeList = new ArrayList<>();
182         for (org.onap.msb.apiroute.api.Node node : nodeSet) {
183             org.onap.msb.apiroute.wrapper.dao.service.bean.Node serviceNode = new org.onap.msb.apiroute.wrapper.dao.service.bean.Node();
184             serviceNode.setIp(node.getIp());
185             serviceNode.setPort(node.getPort());
186             serviceNode.setTtl(node.getTtl());
187             serviceNodeList.add(serviceNode);
188         }
189         spec.setNodes(serviceNodeList.toArray(new org.onap.msb.apiroute.wrapper.dao.service.bean.Node[]{}));
190         serviceInfo.setSpec(spec);
191
192         Metadata metadata = new Metadata();
193         metadata.setName(microServiceFullInfo.getServiceName());
194         metadata.setNamespace(microServiceFullInfo.getNamespace());
195         Calendar now = Calendar.getInstance();
196         now.set(Calendar.MILLISECOND, 0);
197         metadata.setUpdateTimestamp(now.getTime());
198         serviceInfo.setMetadata(metadata);
199
200         return serviceInfo;
201     }
202
203     public static MicroServiceFullInfo fromServiceInfo(ServiceInfo serviceInfo) {
204         MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();
205
206         microServiceFullInfo.setVersion(serviceInfo.getApiVersion());
207         microServiceFullInfo.setStatus(serviceInfo.getStatus());
208
209         Spec spec = serviceInfo.getSpec();
210         microServiceFullInfo.setVisualRange(spec.getVisualRange());
211         microServiceFullInfo.setUrl(spec.getUrl());
212         microServiceFullInfo.setPath(spec.getPath());
213         microServiceFullInfo.setPublish_port(spec.getPublish_port());
214         microServiceFullInfo.setHost(spec.getHost());
215         microServiceFullInfo.setProtocol(spec.getProtocol());
216         microServiceFullInfo.setLb_policy(spec.getLb_policy());
217         microServiceFullInfo.setEnable_ssl(spec.isEnable_ssl());
218         org.onap.msb.apiroute.wrapper.dao.service.bean.Node[] serviceNodes = spec.getNodes();
219         List<org.onap.msb.apiroute.api.Node> nodeList = new ArrayList<>();
220         for (org.onap.msb.apiroute.wrapper.dao.service.bean.Node serviceNode : serviceNodes) {
221             org.onap.msb.apiroute.api.Node node = new org.onap.msb.apiroute.api.Node();
222             node.setIp(serviceNode.getIp());
223             node.setPort(String.valueOf(serviceNode.getPort()));
224             node.setTtl(serviceNode.getTtl());
225             nodeList.add(node);
226         }
227         microServiceFullInfo.setNodes(new HashSet<org.onap.msb.apiroute.api.Node>(nodeList));
228
229         Metadata metadata = serviceInfo.getMetadata();
230         microServiceFullInfo.setServiceName(metadata.getName());
231         microServiceFullInfo.setNamespace(metadata.getNamespace());
232
233         return microServiceFullInfo;
234     }
235 }