More unit test coverage and code cleanup
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / utils / URLUtilities.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * *\r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * *\r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 \r
24 \r
25 package org.onap.dmaap.datarouter.provisioning.utils;\r
26 \r
27 import com.att.eelf.configuration.EELFLogger;\r
28 import com.att.eelf.configuration.EELFManager;\r
29 import java.net.InetAddress;\r
30 import java.net.UnknownHostException;\r
31 \r
32 import org.onap.dmaap.datarouter.provisioning.BaseServlet;\r
33 \r
34 /**\r
35  * Utility functions used to generate the different URLs used by the Data Router.\r
36  *\r
37  * @author Robert Eby\r
38  * @version $Id: URLUtilities.java,v 1.2 2014/03/12 19:45:41 eby Exp $\r
39  */\r
40 public class URLUtilities {\r
41 \r
42     private static final EELFLogger utilsLogger = EELFManager.getInstance().getLogger("UtilsLog");\r
43     private static final String HTTPS = "https://";\r
44     private static String otherPod;\r
45 \r
46     private URLUtilities() {\r
47     }\r
48 \r
49     /**\r
50      * Generate the URL used to access a feed.\r
51      *\r
52      * @param feedid the feed id\r
53      * @return the URL\r
54      */\r
55     public static String generateFeedURL(int feedid) {\r
56         return HTTPS + BaseServlet.getProvName() + "/feed/" + feedid;\r
57     }\r
58 \r
59     /**\r
60      * Generate the URL used to publish to a feed.\r
61      *\r
62      * @param feedid the feed id\r
63      * @return the URL\r
64      */\r
65     public static String generatePublishURL(int feedid) {\r
66         return HTTPS + BaseServlet.getProvName() + "/publish/" + feedid;\r
67     }\r
68 \r
69     /**\r
70      * Generate the URL used to subscribe to a feed.\r
71      *\r
72      * @param feedid the feed id\r
73      * @return the URL\r
74      */\r
75     public static String generateSubscribeURL(int feedid) {\r
76         return HTTPS + BaseServlet.getProvName() + "/subscribe/" + feedid;\r
77     }\r
78 \r
79     /**\r
80      * Generate the URL used to access a feed's logs.\r
81      *\r
82      * @param feedid the feed id\r
83      * @return the URL\r
84      */\r
85     public static String generateFeedLogURL(int feedid) {\r
86         return HTTPS + BaseServlet.getProvName() + "/feedlog/" + feedid;\r
87     }\r
88 \r
89     /**\r
90      * Generate the URL used to access a subscription.\r
91      *\r
92      * @param subid the subscription id\r
93      * @return the URL\r
94      */\r
95     public static String generateSubscriptionURL(int subid) {\r
96         return HTTPS + BaseServlet.getProvName() + "/subs/" + subid;\r
97     }\r
98 \r
99     /**\r
100      * Generate the URL used to access a subscription's logs.\r
101      *\r
102      * @param subid the subscription id\r
103      * @return the URL\r
104      */\r
105     public static String generateSubLogURL(int subid) {\r
106         return HTTPS + BaseServlet.getProvName() + "/sublog/" + subid;\r
107     }\r
108 \r
109     /**\r
110      * Generate the URL used to access the provisioning data on the peer POD.\r
111      *\r
112      * @return the URL\r
113      */\r
114     public static String generatePeerProvURL() {\r
115         return HTTPS + getPeerPodName() + "/internal/prov";\r
116     }\r
117 \r
118     /**\r
119      * Generate the URL used to access the logfile data on the peer POD.\r
120      *\r
121      * @return the URL\r
122      */\r
123     public static String generatePeerLogsURL() {\r
124         //Fixes for Itrack ticket - DATARTR-4#Fixing if only one Prov is configured, not to give exception to fill logs.\r
125         String peerPodUrl = getPeerPodName();\r
126         if (peerPodUrl == null || "".equals(peerPodUrl)) {\r
127             return "";\r
128         }\r
129 \r
130         return HTTPS + peerPodUrl + "/internal/drlogs/";\r
131     }\r
132 \r
133     /**\r
134      * Return the real (non CNAME) version of the peer POD's DNS name.\r
135      *\r
136      * @return the name\r
137      */\r
138     public static String getPeerPodName() {\r
139         if (otherPod == null) {\r
140             String thisPod;\r
141             try {\r
142                 thisPod = InetAddress.getLocalHost().getHostName();\r
143             } catch (UnknownHostException e) {\r
144                 utilsLogger.trace("UnkownHostException: " + e.getMessage(), e);\r
145                 thisPod = "";\r
146             }\r
147             for (String pod : BaseServlet.getPods()) {\r
148                 if (!pod.equals(thisPod)) {\r
149                     otherPod = pod;\r
150                 }\r
151             }\r
152         }\r
153         return otherPod;\r
154     }\r
155 \r
156 }\r