OW2 Consortium contrail

Rev

Rev 2538 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2538 Rev 3040
Line 1... Line 1...
1
/*
1
/*
2
 * To change this template, choose Tools | Templates
2
 * To change this template, choose Tools | Templates
3
 * and open the template in the editor.
3
 * and open the template in the editor.
4
 */
4
 */
5
package vep;
5
package vep;
6

-
 
7
import java.security.cert.X509Certificate;
6
import java.security.cert.X509Certificate;
8
import java.sql.ResultSet;
7
import java.sql.ResultSet;
9
import java.util.LinkedList;
8
import java.util.LinkedList;
10
import java.util.List;
9
import java.util.List;
-
 
10
import org.apache.commons.lang3.StringEscapeUtils;
11
import org.apache.log4j.Logger;
11
import org.apache.log4j.Logger;
12
import org.json.simple.JSONObject;
12
import org.json.simple.JSONObject;
13
import org.restlet.data.Form;
13
import org.restlet.data.Form;
14
import org.restlet.data.MediaType;
14
import org.restlet.data.MediaType;
15
import org.restlet.data.Status;
15
import org.restlet.data.Status;
16
import org.restlet.representation.Representation;
16
import org.restlet.representation.Representation;
17
import org.restlet.representation.StringRepresentation;
17
import org.restlet.representation.StringRepresentation;
18
import org.restlet.resource.Put;
-
 
-
 
18
import org.restlet.resource.Get;
19
import org.restlet.resource.ResourceException;
19
import org.restlet.resource.ResourceException;
20
import org.restlet.resource.ServerResource;
20
import org.restlet.resource.ServerResource;
21

-
 
22

-
 
23
/**
21
/**
24
 *
22
 *
25
 * @author piyush
23
 * @author piyush
26
 */
24
 */
27
public class restServerTemplateDetails extends ServerResource 
25
public class restServerTemplateDetails extends ServerResource 
Line 37... Line 35...
37
    
35
    
38
    public restServerTemplateDetails()
36
    public restServerTemplateDetails()
39
    {
37
    {
40
        vepProperties = VEPHelperMethods.getPropertyFile();
38
        vepProperties = VEPHelperMethods.getPropertyFile();
41
        logger = Logger.getLogger("VEP.restTemplateDetails");
39
        logger = Logger.getLogger("VEP.restTemplateDetails");
42
//        dbType = VEPHelperMethods.getProperty("vepdb.choice", logger, "vep.properties");
-
 
43
        dbType = VEPHelperMethods.getProperty("vepdb.choice", logger, vepProperties);
40
        dbType = VEPHelperMethods.getProperty("vepdb.choice", logger, vepProperties);
44
        db = new dbHandler("restServerTemplateDetails", dbType);   
41
        db = new dbHandler("restServerTemplateDetails", dbType);   
45
//        oneip = VEPHelperMethods.getProperty("one.ip", logger, "vep.properties");
-
 
46
//        oneport = VEPHelperMethods.getProperty("one.port", logger, "vep.properties");
-
 
47
        oneip = VEPHelperMethods.getProperty("one.ip", logger, vepProperties);
42
        oneip = VEPHelperMethods.getProperty("one.ip", logger, vepProperties);
48
        oneport = VEPHelperMethods.getProperty("one.port", logger, vepProperties);
43
        oneport = VEPHelperMethods.getProperty("one.port", logger, vepProperties);
49
    }
44
    }
50
    
45
    
51
    @Put("json")
-
 
52
    public Representation deployTemplate() throws ResourceException
-
 
-
 
46
    @Get("json")
-
 
47
    public Representation toValue()
53
    {
48
    {
54
        Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
49
        Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
55
        
-
 
56
        String acceptType = requestHeaders.getFirstValue("Accept");
-
 
57
        String contentType = requestHeaders.getFirstValue("Content-Type");
50
        String contentType = requestHeaders.getFirstValue("Content-Type");
58
        String templateId = ((String) getRequest().getAttributes().get("id"));
-
 
59
        
-
 
-
 
51
        String acceptType = requestHeaders.getFirstValue("Accept");
60
        Representation response = null;
52
        Representation response = null;
61

-
 
-
 
53
        
-
 
54
        String templateID = ((String) getRequest().getAttributes().get("id"));
62
        String username = requestHeaders.getFirstValue("X-Username");
55
        String username = requestHeaders.getFirstValue("X-Username");
-
 
56
        
63
        //username contained in the certificate overrides username in header
57
        //username contained in the certificate overrides username in header
64
        List<X509Certificate> certs = (List)getRequest().getAttributes().get("org.restlet.https.clientCertificates");
58
        List<X509Certificate> certs = (List)getRequest().getAttributes().get("org.restlet.https.clientCertificates");
65
        for(int i=0; certs != null && i < certs.size(); i++)
59
        for(int i=0; certs != null && i < certs.size(); i++)
66
        {
60
        {
67
            X509Certificate Cert = certs.get(i);
61
            X509Certificate Cert = certs.get(i);
Line 84... Line 78...
84
        }
78
        }
85
        else if(certs.isEmpty())
79
        else if(certs.isEmpty())
86
        {
80
        {
87
            logger.warn("Client certificates list is empty. Unauthenticated client. Size = 0.");
81
            logger.warn("Client certificates list is empty. Unauthenticated client. Size = 0.");
88
        }
82
        }
-
 
83
        
-
 
84
        if(acceptType != null)
-
 
85
        {
-
 
86
            if(acceptType.contains("html"))
-
 
87
                response = toHtml(templateID, username);
-
 
88
        }
-
 
89
        else
-
 
90
        {
-
 
91
            //default rendering ...
-
 
92
            response = toHtml(templateID, username);
-
 
93
        }
-
 
94
        //System.out.println(contentType);
-
 
95
        return response;
-
 
96
    }
-
 
97
    
-
 
98
    public Representation toHtml(String templateID, String username) throws ResourceException 
-
 
99
    { 
-
 
100
        StringBuilder stringBuilder = new StringBuilder();
-
 
101
        stringBuilder.append(VEPHelperMethods.getRESTwebHeader(false));
-
 
102
        stringBuilder.append("Template <i>").append(templateID).append("</i> description is shown below<br><br>");
-
 
103
        stringBuilder.append("<table style='width:1014px;hspace:5px;background:silver;border:0px;cellspacing:2px;padding:2px;font-family:Courier;font-size:10pt;color:black;'>");
-
 
104
        stringBuilder.append("<tr>");
-
 
105
        stringBuilder.append("<td valign='top' style='width:128px;background:white;'><img src='https://www.cise.ufl.edu/~pharsh/public/template.png'>");
-
 
106
        stringBuilder.append("<td valign='top' align='left' bgcolor='white' width='*'>");
89
        
107
        
90
        String[] groups;
108
        String[] groups;
91
        boolean isAdmin = false;
109
        boolean isAdmin = false;
92
        JSONObject obj = new JSONObject();
-
 
93
        obj.put("title", "VM template deploy action result");
-
 
94
        
110
        
95
        if(username == null)
111
        if(username == null)
96
        {
112
        {
97
            this.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
113
            this.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
98
            obj.put("error", "CLIENT_ERROR_BAD_REQUEST");
-
 
-
 
114
            stringBuilder.append("<b>User credentials missing ... can not display template details.</b><br><br>");
99
        }
115
        }
100
        else
116
        else
101
        {
117
        {
102
            try
118
            try
103
            {
119
            {
104
                ResultSet rs = db.query("select", "*", "user", "where username='" + username + "'");
120
                ResultSet rs = db.query("select", "*", "user", "where username='" + username + "'");
105
                if(rs.next())
121
                if(rs.next())
106
                {
122
                {
107
                    int uid = rs.getInt("uid");
123
                    int uid = rs.getInt("uid");
108
                    oneuser = rs.getString("oneuser");
-
 
109
                    onepass = rs.getString("onepass");
-
 
110
                    rs.close();
124
                    rs.close();
111
                    rs = db.query("select", "*", "ugroup", "where uid=" + uid + "");
125
                    rs = db.query("select", "*", "ugroup", "where uid=" + uid + "");
112
                    String groupList = "";
126
                    String groupList = "";
113
                    while(rs.next())
127
                    while(rs.next())
114
                    {
128
                    {
Line 124... Line 138...
124
                            logger.trace("Setting isAdmin to true.");
138
                            logger.trace("Setting isAdmin to true.");
125
                            break;
139
                            break;
126
                        }
140
                        }
127
                    }
141
                    }
128
                    rs.close();
142
                    rs.close();
129
                    
-
 
130
                    if(VEPHelperMethods.pdpCheck(uid, null))
-
 
-
 
143
                    if(isAdmin)
131
                    {
144
                    {
132
                        rs = db.query("select", "uid", "vmachinetemplate", "where vmid=" + templateId);
-
 
133
                        int vm_uid = -1;
-
 
-
 
145
                        rs = db.query("select", "*", "vmachinetemplate", "where vmid=" + templateID);
134
                        if(rs.next())
146
                        if(rs.next())
135
                        {
147
                        {
136
                            vm_uid = rs.getInt("uid");
-
 
137
                            rs.close();
-
 
-
 
148
                            // read the result set
-
 
149
                            stringBuilder.append("<div style='background:#CED1D6;font-weight:bold;'>");
-
 
150
                            stringBuilder.append("Group: ").append(rs.getString("gname")).append("<br>");
-
 
151
                            stringBuilder.append("Permission Bits: ").append(rs.getString("perm")).append("<br>");
-
 
152
                            stringBuilder.append("Application Name: ").append(rs.getString("appname")).append("<br>");
-
 
153
                            stringBuilder.append("State: ").append(rs.getString("state")).append("<br>");
-
 
154
                            stringBuilder.append("OVF Serial: ").append(rs.getInt("ovfsno")).append("<br>");
-
 
155
                            stringBuilder.append("OVF Element ID: ").append(rs.getString("ovfid")).append("<br>");
-
 
156
                            stringBuilder.append("</div><br>");
-
 
157
                            
-
 
158
                            stringBuilder.append("<div style='white-space:normal;word-wrap:normal;background:#ECFCF3;overflow:auto;font-weight:bold;word-wrap:break-word;width:880px;font-family:Console;font-size:8pt;'>");
-
 
159
                            String content = rs.getString("descp");
-
 
160
                            content = StringEscapeUtils.escapeXml(content);
-
 
161
                            stringBuilder.append("<pre>").append(content).append("</pre>");
-
 
162
                            stringBuilder.append("</div>");
138
                        }
163
                        }
139
                        if(isAdmin || vm_uid == uid) //if administrator or owner of the ovf
-
 
-
 
164
                        else
-
 
165
                        {
-
 
166
                            stringBuilder.append("<div style='background:#99CCCC;'>");
-
 
167
                            stringBuilder.append("No details found ...");   
-
 
168
                            stringBuilder.append("</div>");   
-
 
169
                        }
-
 
170
                        rs.close();
-
 
171
                    }
-
 
172
                    else
-
 
173
                    {
-
 
174
                        rs = db.query("select", "*", "vmachinetemplate", "where vmid=" + templateID);
-
 
175
                        if(rs.next())
140
                        {
176
                        {
141
                            rs = db.query("select", "*", "vmachinetemplate", "where vmid=" + templateId);
-
 
142
                            if(rs.next())
-
 
-
 
177
                            // read the result set
-
 
178
                            if(uid == rs.getInt("uid"))
143
                            {
179
                            {
144
                                logger.debug("Creating corresponding ONE account for user " + username + " [" + oneuser + ", " + onepass + "].");
-
 
145
                                ONExmlrpcHandler onehandle = new ONExmlrpcHandler(oneip, oneport, oneuser, onepass, "restServerTemplateDetails:deployTemplate()");
-
 
146
                                String vmTemplate = rs.getString("descp");
-
 
147
                                String appName = rs.getString("appname");
-
 
148
                                String ovfid = rs.getString("ovfid");
-
 
149
                                String templateState = rs.getString("state");
-
 
150
                                //vmid is same as templateId
-
 
151
                                rs.close();
-
 
152
                                //now get the disk image list associated with this vm template and if not registered with ONE then try to register it
-
 
153
                                rs = db.query("select", "id, localpath, name, state, oneimgdescp", "diskimage", "where vmid=" + templateId);
-
 
154
                                int counter = 0;
-
 
155
                                while(rs.next())
-
 
156
                                {
-
 
157
                                    counter++;
-
 
158
                                    String imgName = rs.getString("name");
-
 
159
                                    String imgPath = rs.getString("localpath");
-
 
160
                                    String imgState =rs.getString("state");
-
 
161
                                    String imgTemplate = rs.getString("oneimgdescp");
-
 
162
                                    int imgId = rs.getInt("id");
-
 
163
                                    if(!imgState.equalsIgnoreCase("OR")) //if state is ORPHANed then do not do anything, it will be garbage collected soon
-
 
164
                                    {
-
 
165
                                        //getting the already registered ONE image list
-
 
166
                                        LinkedList<ONEImage> imageList = onehandle.getImageList();
-
 
167
                                        boolean found = false;
-
 
168
                                        for(int i=0; i< imageList.size(); i++)
-
 
169
                                        {
-
 
170
                                            ONEImage img = imageList.get(i);
-
 
171
                                            logger.debug("Got ONE Image Details: " + img.imageName + ", " + img.localPath + ", " + img.state);
-
 
172
                                            if(img.imageName.equalsIgnoreCase(imgName) && img.localPath.equalsIgnoreCase(imgPath))
-
 
173
                                            {
-
 
174
                                                //update the diskimage entry with missing details if not already updated
-
 
175
                                                if(!imgState.equalsIgnoreCase("RG"))
-
 
176
                                                {
-
 
177
                                                    boolean status = db.update("diskimage", "oneimgname='" + img.imageName + "', oneimgid=" + img.id + ", state='RG'", "where id=" + imgId);
-
 
178
                                                    if(status)
-
 
179
                                                    {
-
 
180
                                                        logger.debug("For discimage id=" + imgId + " corresponding ONE image with id " + img.id + " was found. VEP-DB updated.");
-
 
181
                                                    }
-
 
182
                                                    else
-
 
183
                                                    {
-
 
184
                                                        logger.warn("For discimage id=" + imgId + " corresponding ONE image with id " + img.id + " was found. VEP-DB update failed.");
-
 
185
                                                    }
-
 
186
                                                }
-
 
187
                                                found = true;
-
 
188
                                                break;
-
 
189
                                            }
-
 
190
                                        }
-
 
191

-
 
192
                                        if(!found)
-
 
193
                                        {
-
 
194
                                            //issue ONE image register command and store corresponding data
-
 
195
                                            //for now we assume that the VM image resides locally.
-
 
196
                                            int oneImgId = onehandle.addImage(imgTemplate);
-
 
197
                                            if(oneImgId != -1)
-
 
198
                                            {
-
 
199
                                                ONEImage temp = onehandle.getImageInfo(oneImgId);
-
 
200
                                                boolean status = db.update("diskimage", "oneimgname='" + temp.imageName + "', oneimgid=" + temp.id + ", state='RG'", "where id=" + imgId);
-
 
201
                                                if(status)
-
 
202
                                                {
-
 
203
                                                    logger.debug("For discimage id=" + imgId + " corresponding ONE image with id " + temp.id + " was registered. VEP-DB updated.");
-
 
204
                                                }
-
 
205
                                                else
-
 
206
                                                {
-
 
207
                                                    logger.warn("For discimage id=" + imgId + " corresponding ONE image with id " + temp.id + " was registered. VEP-DB update failed.");
-
 
208
                                                }
-
 
209
                                            }
-
 
210
                                            else
-
 
211
                                            {
-
 
212
                                                logger.warn("Some error while registering image " + imgName + " disk-id: " + imgId + " with ONE.");
-
 
213
                                                obj.put("error", "SERVER_ERROR_INTERNAL");
-
 
214
                                                this.setStatus(Status.SERVER_ERROR_INTERNAL);
-
 
215
                                            }
-
 
216
                                        }
-
 
217
                                    }
-
 
218
                                    rs = db.query("select", "id, localpath, name, state, oneimgdescp", "diskimage", "where vmid=" + templateId);
-
 
219
                                    for(int i=0;i<counter; i++) rs.next(); //hack to overcome rs being closed after updates.
-
 
220
                                }
-
 
221
                                rs.close();
-
 
222
                                //now send start for the vmtemplate
-
 
223
                                if(!templateState.equalsIgnoreCase("OR")) //ignore OR state as it will be garbage collected soon
-
 
224
                                {
-
 
225
                                    int onevmid = onehandle.addVM(vmTemplate + "\nREQUIREMENTS = \"CLUSTER = contrail\"");
-
 
226
                                    if(onevmid != -1)
-
 
227
                                    {
-
 
228
                                        ONEVm temp = onehandle.getVmInfo(onevmid);
-
 
229

-
 
230
                                        //now store in the vmachine table, also change the state of the ovf entry to DP from ND
-
 
231
                                        ResultSet rs1 = db.query("select", "max(id)", "vmachine", "");
-
 
232
                                        int vid = 0;
-
 
233
                                        if(rs1.next()) vid = rs1.getInt(1) + 1;
-
 
234
                                        rs1.close();
-
 
235
                                        boolean status = db.insert("vmachine", "(" + vid + ", " + uid + ", 'OpenNebula', " + temp.id + ", " + templateId + ", '" + temp.name + "', 'DP', -1, '" +
-
 
236
                                                temp.ip + "', '" + temp.graphics_port + "', '" + temp.graphics_ip + "', '')");
-
 
237
                                        obj.put("vm_id", vid);
-
 
238
                                        obj.put("controller", "OpenNebula");
-
 
239
                                        obj.put("iaas_id", temp.id);
-
 
240
                                        obj.put("app_name", appName);
-
 
241
                                        if(status)
-
 
242
                                        {
-
 
243
                                            logger.debug("Successfully deployed VM information - oneId:" + temp.id + " oneName:" + temp.name 
-
 
244
                                                + " Graphics:" + temp.graphics_type + "-" + temp.graphics_ip + "-" + temp.graphics_port);
-
 
245
                                            db.update("vmachinetemplate", "state='DP'", "where vmid=" + templateId);
-
 
246
                                            obj.put("vm_state", "DP");
-
 
247
                                        }
-
 
248
                                        else
-
 
249
                                        {
-
 
250
                                            logger.warn("Failed to store deployed VM information - oneId:" + temp.id + " oneName:" + temp.name 
-
 
251
                                                + " Graphics:" + temp.graphics_type + "-" + temp.graphics_ip + "-" + temp.graphics_port);
-
 
252
                                            db.update("vmachinetemplate", "state='UN'", "where vmid=" + templateId);
-
 
253
                                            obj.put("vm_state", "UN");
-
 
254
                                        }
-
 
255
                                    }
-
 
256
                                    else
-
 
257
                                    {
-
 
258
                                        logger.warn("Some error while deploying VM template " + templateId + ":" + ovfid + " with ONE.");
-
 
259
                                        db.update("vmachinetemplate", "state='ER'", "where vmid=" + templateId);
-
 
260
                                        obj.put("error", "SERVER_ERROR_INTERNAL");
-
 
261
                                        this.setStatus(Status.SERVER_ERROR_INTERNAL);
-
 
262
                                    }
-
 
263
                                }
-
 
264
                                else
-
 
265
                                {
-
 
266
                                    this.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
-
 
267
                                    obj.put("error", "CLIENT_ERROR_UNPROCESSABLE_ENTITY");
-
 
268
                                }
-
 
-
 
180
                                stringBuilder.append("<div style='background:#CED1D6;font-weight:bold;'>");
-
 
181
                                stringBuilder.append("Group: ").append(rs.getString("gname")).append("<br>");
-
 
182
                                stringBuilder.append("Permission Bits: ").append(rs.getString("perm")).append("<br>");
-
 
183
                                stringBuilder.append("Application Name: ").append(rs.getString("appname")).append("<br>");
-
 
184
                                stringBuilder.append("State: ").append(rs.getString("state")).append("<br>");
-
 
185
                                stringBuilder.append("OVF Serial: ").append(rs.getInt("ovfsno")).append("<br>");
-
 
186
                                stringBuilder.append("OVF Element ID: ").append(rs.getString("ovfid")).append("<br>");
-
 
187
                                stringBuilder.append("</div><br>");
-
 
188
                                
-
 
189
                                stringBuilder.append("<div style='white-space:normal;word-wrap:normal;background:#ECFCF3;overflow:auto;font-weight:bold;word-wrap:break-word;width:880px;font-family:Console;font-size:8pt;'>");
-
 
190
                                String content = rs.getString("descp");
-
 
191
                                content = StringEscapeUtils.escapeXml(content);
-
 
192
                                stringBuilder.append("<pre>").append(content).append("</pre>");
-
 
193
                                stringBuilder.append("</div>");
269
                            }
194
                            }
270
                            else
195
                            else
271
                            {
196
                            {
272
                                //not found
-
 
273
                                rs.close();
-
 
274
                                this.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
-
 
275
                                obj.put("error", "CLIENT_ERROR_NOT_FOUND");
-
 
-
 
197
                                stringBuilder.append("<div style='background:#99CCCC;'>");
-
 
198
                                stringBuilder.append("You are not the owner of the resource, access blocked ...");   
-
 
199
                                stringBuilder.append("</div>"); 
276
                            }
200
                            }
277
                        }
201
                        }
278
                        else
202
                        else
279
                        {
203
                        {
280
                            this.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
-
 
281
                            obj.put("error", "CLIENT_ERROR_UNAUTHORIZED");
-
 
-
 
204
                            stringBuilder.append("<div style='background:#99CCCC;'>");
-
 
205
                            stringBuilder.append("No details found ...");   
-
 
206
                            stringBuilder.append("</div>");   
282
                        }
207
                        }
283
                    }
-
 
284
                    else
-
 
285
                    {
-
 
286
                        this.setStatus(Status.CLIENT_ERROR_PRECONDITION_FAILED);
-
 
287
                        obj.put("error", "CLIENT_ERROR_PRECONDITION_FAILED");
-
 
288
                        obj.put("origin", "PDP Check denied authorization");
-
 
-
 
208
                        rs.close();
289
                    }
209
                    }
290
                }
210
                }
291
                else
211
                else
292
                {
212
                {
293
                    this.setStatus(Status.CLIENT_ERROR_PRECONDITION_FAILED);
213
                    this.setStatus(Status.CLIENT_ERROR_PRECONDITION_FAILED);
294
                    obj.put("error", "CLIENT_ERROR_PRECONDITION_FAILED");
-
 
-
 
214
                    stringBuilder.append("<b>User not found ... can not display the template details.</b><br><br>");
295
                }
215
                }
296
            }
216
            }
297
            catch(Exception ex)
217
            catch(Exception ex)
298
            {
218
            {
299
                obj.put("error", "SQL error occured.");
-
 
-
 
219
                stringBuilder.append("<B>SQL Query Error!! Details of exception follows ...</B>");
-
 
220
                stringBuilder.append("<div style='border:1px;background:red;color:black;font-family:Times;font-size:9pt;'>");
-
 
221
                stringBuilder.append(ex.getMessage());
300
                logger.debug("Exception caught: " + ex.getMessage());
222
                logger.debug("Exception caught: " + ex.getMessage());
301
                this.setStatus(Status.SERVER_ERROR_INTERNAL);
-
 
-
 
223
                stringBuilder.append("</div>");
302
            }
224
            }
303
        }
225
        }
304
        
-
 
305
        
-
 
306
        response = new StringRepresentation(obj.toJSONString(), MediaType.APPLICATION_JSON);
-
 
307
        return response;
-
 
-
 
226
        stringBuilder.append("</table><br>");
-
 
227
        stringBuilder.append("Click on the banner image to go up one level<br>");
-
 
228
        stringBuilder.append(VEPHelperMethods.getRESTwebFooter());
-
 
229
        StringRepresentation value = new StringRepresentation(stringBuilder.toString(), MediaType.TEXT_HTML);
-
 
230
        return value;
308
    }
231
    }
309
}
232
}