1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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;
18 import java.util.ArrayList;
19 import java.util.Calendar;
20 import java.util.HashSet;
21 import java.util.List;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
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;
37 import com.google.common.collect.ImmutableSet;
39 public class MicroServiceFullService {
40 private static final Logger LOGGER = LoggerFactory.getLogger(MicroServiceFullService.class);
42 private static MicroServiceFullService instance = new MicroServiceFullService();
44 private IServiceDAO serviceDAO = DAOFactory.getServiceDAO();
46 private MicroServiceFullService() {
49 public static MicroServiceFullService getInstance() {
53 public List<MicroServiceFullInfo> getAllMicroServiceInstances() throws Exception {
54 String serviceKeyPattern = MicroServiceUtil.getPrefixedKey("*");
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);
62 microServiceFullInfoList.add(microServiceFullInfo);
65 return microServiceFullInfoList;
68 public Set<String> getAllMicroServiceKey() throws Exception {
69 final Set<String> builder = new HashSet<String>();
71 String serviceKeyPattern = MicroServiceUtil.getPrefixedKey("*");
72 Set<String> serviceKeySet = RedisAccessWrapper.filterKeys(serviceKeyPattern);
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"));
84 public void saveMicroServiceInfo2Redis(MicroServiceFullInfo microServiceFullInfo) throws Exception {
85 if(microServiceFullInfo ==null){
86 throw new Exception("input microServiceInfo to be saved is null!");
88 ServiceInfo serviceInfo = MicroServiceFullAdapter.toServiceInfo(microServiceFullInfo);
89 String serviceKey = MicroServiceUtil.getServiceKey(microServiceFullInfo.getServiceName(),microServiceFullInfo.getVersion());
90 serviceDAO.saveService(serviceKey,serviceInfo);
93 public void updateMicroServiceStatus(String serviceName, String version, String status)
95 if (null == version || "null".equals(version)) {
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);
106 public boolean existsMicroServiceInstance(String serviceName, String version)
108 if (null == version || "null".equals(version)) {
111 String serviceKey = MicroServiceUtil.getServiceKey(serviceName, version);
112 return RedisAccessWrapper.isExist(serviceKey);
115 public MicroServiceFullInfo getMicroServiceInstance(String serviceName, String version)
117 if (null == version || "null".equals(version)) {
120 String serviceKey = MicroServiceUtil.getServiceKey(serviceName, version);
122 MicroServiceFullInfo microServiceInfo = null;
124 ServiceInfo serviceInfo = null;
125 serviceInfo = serviceDAO.queryService(serviceKey);
126 if(serviceInfo!=null) {
127 microServiceInfo = MicroServiceFullAdapter.fromServiceInfo(serviceInfo);
129 return microServiceInfo;
133 * query all the versions of the given ServiceName
138 public List<MicroServiceFullInfo> getAllVersionsOfTheService(String serviceName) throws Exception {
139 String serviceKeyPattern = MicroServiceUtil.getPrefixedKey(serviceName, "*");
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);
149 return microServiceFullInfoList;
152 public void deleteMicroService(String serviceName, String version) throws Exception {
153 if (null == version || "null".equals(version)) {
156 String serviceKey = MicroServiceUtil.getServiceKey(serviceName, version);
157 serviceDAO.deleteService(serviceKey);
160 public long deleteMultiMicroService(String keyPattern) throws Exception {
161 return serviceDAO.deleteMultiService(keyPattern);
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());
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);
189 spec.setNodes(serviceNodeList.toArray(new org.onap.msb.apiroute.wrapper.dao.service.bean.Node[]{}));
190 serviceInfo.setSpec(spec);
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);
203 public static MicroServiceFullInfo fromServiceInfo(ServiceInfo serviceInfo) {
204 MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();
206 microServiceFullInfo.setVersion(serviceInfo.getApiVersion());
207 microServiceFullInfo.setStatus(serviceInfo.getStatus());
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());
227 microServiceFullInfo.setNodes(new HashSet<org.onap.msb.apiroute.api.Node>(nodeList));
229 Metadata metadata = serviceInfo.getMetadata();
230 microServiceFullInfo.setServiceName(metadata.getName());
231 microServiceFullInfo.setNamespace(metadata.getNamespace());
233 return microServiceFullInfo;