Remove critical code smells for utils classes
[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 import java.util.Arrays;\r
32 \r
33 import org.onap.dmaap.datarouter.provisioning.BaseServlet;\r
34 \r
35 /**\r
36  * Utility functions used to generate the different URLs used by the Data Router.\r
37  *\r
38  * @author Robert Eby\r
39  * @version $Id: URLUtilities.java,v 1.2 2014/03/12 19:45:41 eby Exp $\r
40  */\r
41 public class URLUtilities {\r
42     private static final EELFLogger utilsLogger = EELFManager.getInstance().getLogger("UtilsLog");\r
43     /**\r
44      * Generate the URL used to access a feed.\r
45      *\r
46      * @param feedid the feed id\r
47      * @return the URL\r
48      */\r
49     public static String generateFeedURL(int feedid) {\r
50         return "https://" + BaseServlet.getProvName() + "/feed/" + feedid;\r
51     }\r
52 \r
53     /**\r
54      * Generate the URL used to publish to a feed.\r
55      *\r
56      * @param feedid the feed id\r
57      * @return the URL\r
58      */\r
59     public static String generatePublishURL(int feedid) {\r
60         return "https://" + BaseServlet.getProvName() + "/publish/" + feedid;\r
61     }\r
62 \r
63     /**\r
64      * Generate the URL used to subscribe to a feed.\r
65      *\r
66      * @param feedid the feed id\r
67      * @return the URL\r
68      */\r
69     public static String generateSubscribeURL(int feedid) {\r
70         return "https://" + BaseServlet.getProvName() + "/subscribe/" + feedid;\r
71     }\r
72 \r
73     /**\r
74      * Generate the URL used to access a feed's logs.\r
75      *\r
76      * @param feedid the feed id\r
77      * @return the URL\r
78      */\r
79     public static String generateFeedLogURL(int feedid) {\r
80         return "https://" + BaseServlet.getProvName() + "/feedlog/" + feedid;\r
81     }\r
82 \r
83     /**\r
84      * Generate the URL used to access a subscription.\r
85      *\r
86      * @param subid the subscription id\r
87      * @return the URL\r
88      */\r
89     public static String generateSubscriptionURL(int subid) {\r
90         return "https://" + BaseServlet.getProvName() + "/subs/" + subid;\r
91     }\r
92 \r
93     /**\r
94      * Generate the URL used to access a subscription's logs.\r
95      *\r
96      * @param subid the subscription id\r
97      * @return the URL\r
98      */\r
99     public static String generateSubLogURL(int subid) {\r
100         return "https://" + BaseServlet.getProvName() + "/sublog/" + subid;\r
101     }\r
102 \r
103     /**\r
104      * Generate the URL used to access the provisioning data on the peer POD.\r
105      *\r
106      * @return the URL\r
107      */\r
108     public static String generatePeerProvURL() {\r
109         return "https://" + getPeerPodName() + "/internal/prov";\r
110     }\r
111 \r
112     /**\r
113      * Generate the URL used to access the logfile data on the peer POD.\r
114      *\r
115      * @return the URL\r
116      */\r
117     public static String generatePeerLogsURL() {\r
118         //Fixes for Itrack ticket - DATARTR-4#Fixing if only one Prov is configured, not to give exception to fill logs.\r
119         String peerPodUrl = getPeerPodName();\r
120         if (peerPodUrl == null || peerPodUrl.equals("")) {\r
121             return "";\r
122         }\r
123 \r
124         return "https://" + peerPodUrl + "/internal/drlogs/";\r
125     }\r
126 \r
127     /**\r
128      * Return the real (non CNAME) version of the peer POD's DNS name.\r
129      *\r
130      * @return the name\r
131      */\r
132     public static String getPeerPodName() {\r
133         if (other_pod == null) {\r
134             String this_pod = "";\r
135             try {\r
136                 this_pod = InetAddress.getLocalHost().getHostName();\r
137                 System.out.println("this_pod: " + this_pod);\r
138             } catch (UnknownHostException e) {\r
139                 utilsLogger.trace("UnkownHostException: " + e.getMessage(), e);\r
140                 this_pod = "";\r
141             }\r
142             System.out.println("ALL PODS: " + Arrays.asList(BaseServlet.getPods()));\r
143             for (String pod : BaseServlet.getPods()) {\r
144                 if (!pod.equals(this_pod)) {\r
145                     other_pod = pod;\r
146                 }\r
147             }\r
148         }\r
149         return other_pod;\r
150     }\r
151 \r
152     private static String other_pod;\r
153 }\r