update link to upper-constraints.txt
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / service / IuiRouteService.java
1 /*******************************************************************************
2  * Copyright 2016-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 package org.onap.msb.apiroute.wrapper.service;
15
16 import java.util.ArrayList;
17 import java.util.Calendar;
18 import java.util.List;
19
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;
30
31
32 public class IuiRouteService {
33     private static final Logger LOGGER = LoggerFactory.getLogger(CustomRouteService.class);
34
35     private static final IuiRouteService instance = new IuiRouteService();
36     private IRouteDAO routeDAO = DAOFactory.getRouteDAO();
37
38     private IuiRouteService() {}
39
40     public static IuiRouteService getInstance() {
41         return instance;
42     }
43
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!");
47         }
48         RouteInfo routeInfo = IuiRouteAdapter.toRouteInfo(iuiRouteInfo);
49         routeDAO.saveRoute(routeKey, routeInfo);
50     }
51
52     public long deleteIuiRouteService2Redis(String routeKey) throws Exception {
53         return routeDAO.deleteRoute(routeKey);
54     }
55
56     public long deleteMultiIuiRouteService2Redis(String routeKeyPattern) throws Exception {
57         return routeDAO.deleteMultiRoute(routeKeyPattern);
58     }
59
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);
66         }
67         return iuiRouteInfo;
68     }
69
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);
77             }
78         }
79         return iuiRouteList;
80     }
81
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);
87         } else {
88             throw new Exception("service to be updated is not exist! Update failed");
89         }
90     }
91
92 }
93
94
95 class IuiRouteAdapter {
96     public static RouteInfo toRouteInfo(IuiRouteInfo iuiRouteInfo) {
97         RouteInfo routeInfo = new RouteInfo();
98         routeInfo.setStatus(iuiRouteInfo.getStatus());
99
100
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());
118             nodeList.add(node);
119         }
120         spec.setNodes(nodeList.toArray(new Node[] {}));
121         routeInfo.setSpec(spec);
122
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);
130         return routeInfo;
131     }
132
133     public static IuiRouteInfo fromRouteInfo(RouteInfo routeInfo) {
134         IuiRouteInfo iuiRouteInfo = new IuiRouteInfo();
135         iuiRouteInfo.setStatus(routeInfo.getStatus());
136
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);
155         }
156         iuiRouteInfo.setServers(routeServerList.toArray(new RouteServer[] {}));
157
158         Metadata metadata = routeInfo.getMetadata();
159         iuiRouteInfo.setServiceName(metadata.getName());
160         iuiRouteInfo.setNamespace(metadata.getNamespace());
161
162         return iuiRouteInfo;
163     }
164 }