1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
13 ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.service;
16 import java.util.ArrayList;
17 import java.util.Calendar;
18 import java.util.List;
20 import org.onap.msb.apiroute.api.IuiRouteInfo;
21 import org.onap.msb.apiroute.api.RouteServer;
22 import org.onap.msb.apiroute.wrapper.dao.DAOFactory;
23 import org.onap.msb.apiroute.wrapper.dao.route.IRouteDAO;
24 import org.onap.msb.apiroute.wrapper.dao.route.bean.Metadata;
25 import org.onap.msb.apiroute.wrapper.dao.route.bean.Node;
26 import org.onap.msb.apiroute.wrapper.dao.route.bean.RouteInfo;
27 import org.onap.msb.apiroute.wrapper.dao.route.bean.Spec;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 public class IuiRouteService {
33 private static final Logger LOGGER = LoggerFactory.getLogger(CustomRouteService.class);
35 private static final IuiRouteService instance = new IuiRouteService();
36 private IRouteDAO routeDAO = DAOFactory.getRouteDAO();
38 private IuiRouteService() {}
40 public static IuiRouteService getInstance() {
44 public void saveIuiRouteService2Redis(IuiRouteInfo iuiRouteInfo, String routeKey) throws Exception {
45 if (iuiRouteInfo == null) {
46 throw new Exception("input apiRouteInfo to be saved is null!");
48 RouteInfo routeInfo = IuiRouteAdapter.toRouteInfo(iuiRouteInfo);
49 routeDAO.saveRoute(routeKey, routeInfo);
52 public long deleteIuiRouteService2Redis(String routeKey) throws Exception {
53 return routeDAO.deleteRoute(routeKey);
56 public long deleteMultiIuiRouteService2Redis(String routeKeyPattern) throws Exception {
57 return routeDAO.deleteMultiRoute(routeKeyPattern);
60 public IuiRouteInfo getIuiRouteInstance(String routeKey) throws Exception {
61 IuiRouteInfo iuiRouteInfo = null;
62 RouteInfo routeInfo = null;
63 routeInfo = routeDAO.queryRoute(routeKey);
64 if (routeInfo != null) {
65 iuiRouteInfo = IuiRouteAdapter.fromRouteInfo(routeInfo);
70 public List<IuiRouteInfo> getMultiIuiRouteInstances(String apiRedisKeyPattern) throws Exception {
71 List<IuiRouteInfo> iuiRouteList = new ArrayList<>();
72 List<RouteInfo> routeInfoList = routeDAO.queryMultiRoute(apiRedisKeyPattern);
73 for (RouteInfo routeInfo : routeInfoList) {
74 if (routeInfo != null) {
75 IuiRouteInfo iuiRouteInfo = IuiRouteAdapter.fromRouteInfo(routeInfo);;
76 iuiRouteList.add(iuiRouteInfo);
82 public void updateIuiRouteStatus2Redis(String routeKey, String status) throws Exception {
83 RouteInfo routeInfo = routeDAO.queryRoute(routeKey);
84 if (routeInfo != null) {
85 routeInfo.setStatus(status);
86 routeDAO.saveRoute(routeKey, routeInfo);
88 throw new Exception("service to be updated is not exist! Update failed");
95 class IuiRouteAdapter {
96 public static RouteInfo toRouteInfo(IuiRouteInfo iuiRouteInfo) {
97 RouteInfo routeInfo = new RouteInfo();
98 routeInfo.setStatus(iuiRouteInfo.getStatus());
101 Spec spec = new Spec();
102 spec.setVisualRange(iuiRouteInfo.getVisualRange());
103 spec.setUrl(iuiRouteInfo.getUrl().trim());
104 spec.setPublish_port(iuiRouteInfo.getPublish_port());
105 spec.setHost(iuiRouteInfo.getHost());
106 spec.setConsulServiceName(iuiRouteInfo.getConsulServiceName());
107 spec.setUseOwnUpstream(iuiRouteInfo.getUseOwnUpstream());
108 spec.setPublish_protocol(iuiRouteInfo.getPublishProtocol());
109 spec.setEnable_ssl(iuiRouteInfo.isEnable_ssl());
110 spec.setControl(iuiRouteInfo.getControl());
111 RouteServer[] routeServers = iuiRouteInfo.getServers();
112 List<Node> nodeList = new ArrayList<>();
113 for (RouteServer server : routeServers) {
114 Node node = new Node();
115 node.setIp(server.getIp());
116 node.setPort(Integer.parseInt(server.getPort()));
117 node.setWeight(server.getWeight());
120 spec.setNodes(nodeList.toArray(new Node[] {}));
121 routeInfo.setSpec(spec);
123 Metadata metadata = new Metadata();
124 metadata.setName(iuiRouteInfo.getServiceName());
125 metadata.setNamespace(iuiRouteInfo.getNamespace());
126 Calendar now = Calendar.getInstance();
127 now.set(Calendar.MILLISECOND, 0);
128 metadata.setUpdateTimestamp(now.getTime());
129 routeInfo.setMetadata(metadata);
133 public static IuiRouteInfo fromRouteInfo(RouteInfo routeInfo) {
134 IuiRouteInfo iuiRouteInfo = new IuiRouteInfo();
135 iuiRouteInfo.setStatus(routeInfo.getStatus());
137 Spec spec = routeInfo.getSpec();
138 iuiRouteInfo.setVisualRange(spec.getVisualRange());
139 iuiRouteInfo.setUrl(spec.getUrl());
140 iuiRouteInfo.setPublish_port(spec.getPublish_port());
141 iuiRouteInfo.setHost(spec.getHost());
142 iuiRouteInfo.setConsulServiceName(spec.getConsulServiceName());
143 iuiRouteInfo.setUseOwnUpstream(spec.getUseOwnUpstream());
144 iuiRouteInfo.setPublishProtocol(spec.getPublish_protocol());
145 iuiRouteInfo.setEnable_ssl(spec.isEnable_ssl());
146 iuiRouteInfo.setControl(spec.getControl());
147 Node[] nodes = spec.getNodes();
148 List<RouteServer> routeServerList = new ArrayList<>();
149 for (Node node : nodes) {
150 RouteServer routeServer = new RouteServer();
151 routeServer.setIp(node.getIp());
152 routeServer.setPort(String.valueOf(node.getPort()));
153 routeServer.setWeight(node.getWeight());
154 routeServerList.add(routeServer);
156 iuiRouteInfo.setServers(routeServerList.toArray(new RouteServer[] {}));
158 Metadata metadata = routeInfo.getMetadata();
159 iuiRouteInfo.setServiceName(metadata.getName());
160 iuiRouteInfo.setNamespace(metadata.getNamespace());