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 org.onap.msb.apiroute.api.IuiRouteInfo;
19 import org.onap.msb.apiroute.api.RouteServer;
20 import org.onap.msb.apiroute.wrapper.dao.DAOFactory;
21 import org.onap.msb.apiroute.wrapper.dao.route.IRouteDAO;
22 import org.onap.msb.apiroute.wrapper.dao.route.bean.Metadata;
23 import org.onap.msb.apiroute.wrapper.dao.route.bean.Node;
24 import org.onap.msb.apiroute.wrapper.dao.route.bean.RouteInfo;
25 import org.onap.msb.apiroute.wrapper.dao.route.bean.Spec;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import java.util.ArrayList;
30 import java.util.Calendar;
31 import java.util.List;
34 public class IuiRouteService {
35 private static final Logger LOGGER = LoggerFactory.getLogger(CustomRouteService.class);
37 private static final IuiRouteService instance = new IuiRouteService();
38 private IRouteDAO routeDAO = DAOFactory.getRouteDAO();
40 private IuiRouteService() {
43 public static IuiRouteService getInstance() {
47 public void saveIuiRouteService2Redis(IuiRouteInfo iuiRouteInfo, String routeKey) throws Exception {
48 if(iuiRouteInfo ==null){
49 throw new Exception("input apiRouteInfo to be saved is null!");
51 RouteInfo routeInfo = IuiRouteAdapter.toRouteInfo(iuiRouteInfo);
52 routeDAO.saveRoute(routeKey, routeInfo);
55 public long deleteIuiRouteService2Redis(String routeKey) throws Exception {
56 return routeDAO.deleteRoute(routeKey);
59 public long deleteMultiIuiRouteService2Redis(String routeKeyPattern) throws Exception {
60 return routeDAO.deleteMultiRoute(routeKeyPattern);
63 public IuiRouteInfo getIuiRouteInstance(String routeKey) throws Exception {
64 IuiRouteInfo iuiRouteInfo = null;
65 RouteInfo routeInfo = null;
66 routeInfo = routeDAO.queryRoute(routeKey);
68 iuiRouteInfo = IuiRouteAdapter.fromRouteInfo(routeInfo);
73 public List<IuiRouteInfo> getMultiIuiRouteInstances(String apiRedisKeyPattern) throws Exception {
74 List<IuiRouteInfo> iuiRouteList = new ArrayList<>();
75 List<RouteInfo> routeInfoList = routeDAO.queryMultiRoute(apiRedisKeyPattern);
76 for (RouteInfo routeInfo : routeInfoList) {
77 if (routeInfo != null) {
78 IuiRouteInfo iuiRouteInfo = IuiRouteAdapter.fromRouteInfo(routeInfo);;
79 iuiRouteList.add(iuiRouteInfo);
85 public void updateIuiRouteStatus2Redis(String routeKey,String status) throws Exception {
86 RouteInfo routeInfo = routeDAO.queryRoute(routeKey);
87 if(routeInfo != null){
88 routeInfo.setStatus(status);
89 routeDAO.saveRoute(routeKey,routeInfo);
91 throw new Exception("service to be updated is not exist! Update failed");
97 class IuiRouteAdapter {
98 public static RouteInfo toRouteInfo(IuiRouteInfo iuiRouteInfo) {
99 RouteInfo routeInfo = new RouteInfo();
100 routeInfo.setStatus(iuiRouteInfo.getStatus());
103 Spec spec = new Spec();
104 spec.setVisualRange(iuiRouteInfo.getVisualRange());
105 spec.setUrl(iuiRouteInfo.getUrl().trim());
106 spec.setPublish_port(iuiRouteInfo.getPublish_port());
107 spec.setHost(iuiRouteInfo.getHost());
108 spec.setConsulServiceName(iuiRouteInfo.getConsulServiceName());
109 spec.setUseOwnUpstream(iuiRouteInfo.getUseOwnUpstream());
110 spec.setPublish_protocol(iuiRouteInfo.getPublishProtocol());
111 spec.setEnable_ssl(iuiRouteInfo.isEnable_ssl());
112 spec.setControl(iuiRouteInfo.getControl());
113 RouteServer[] routeServers = iuiRouteInfo.getServers();
114 List<Node> nodeList = new ArrayList<>();
115 for (RouteServer server: routeServers){
116 Node node = new Node();
117 node.setIp(server.getIp());
118 node.setPort(Integer.parseInt(server.getPort()));
119 node.setWeight(server.getWeight());
122 spec.setNodes(nodeList.toArray(new Node[]{}));
123 routeInfo.setSpec(spec);
125 Metadata metadata = new Metadata();
126 metadata.setName(iuiRouteInfo.getServiceName());
127 metadata.setNamespace(iuiRouteInfo.getNamespace());
128 Calendar now = Calendar.getInstance();
129 now.set(Calendar.MILLISECOND, 0);
130 metadata.setUpdateTimestamp(now.getTime());
131 routeInfo.setMetadata(metadata);
135 public static IuiRouteInfo fromRouteInfo(RouteInfo routeInfo) {
136 IuiRouteInfo iuiRouteInfo = new IuiRouteInfo();
137 iuiRouteInfo.setStatus(routeInfo.getStatus());
139 Spec spec = routeInfo.getSpec();
140 iuiRouteInfo.setVisualRange(spec.getVisualRange());
141 iuiRouteInfo.setUrl(spec.getUrl());
142 iuiRouteInfo.setPublish_port(spec.getPublish_port());
143 iuiRouteInfo.setHost(spec.getHost());
144 iuiRouteInfo.setConsulServiceName(spec.getConsulServiceName());
145 iuiRouteInfo.setUseOwnUpstream(spec.getUseOwnUpstream());
146 iuiRouteInfo.setPublishProtocol(spec.getPublish_protocol());
147 iuiRouteInfo.setEnable_ssl(spec.isEnable_ssl());
148 iuiRouteInfo.setControl(spec.getControl());
149 Node[] nodes = spec.getNodes();
150 List<RouteServer> routeServerList = new ArrayList<>();
151 for (Node node: nodes){
152 RouteServer routeServer = new RouteServer();
153 routeServer.setIp(node.getIp());
154 routeServer.setPort(String.valueOf(node.getPort()));
155 routeServer.setWeight(node.getWeight());
156 routeServerList.add(routeServer);
158 iuiRouteInfo.setServers(routeServerList.toArray(new RouteServer[]{}));
160 Metadata metadata = routeInfo.getMetadata();
161 iuiRouteInfo.setServiceName(metadata.getName());
162 iuiRouteInfo.setNamespace(metadata.getNamespace());