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