m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java
blob: be853b918ec138f57a2996147287c9786b22ca0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package pl.edu.mimuw.cloudatlas.client;

import com.google.gson.Gson;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import pl.edu.mimuw.cloudatlas.api.Api;
import pl.edu.mimuw.cloudatlas.model.*;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/*
should enable reading attribute values stored by the agent
installing and
uninstalling queries, and
setting fallback contacts.

Apart from providing forms for queries and fallback contacts,
and presenting the information fetched from the agent in a textual form (with automatic refreshment),
plotting the attributes with numeric values as real-time graphs.
*/

@Controller
public class ClientController {
    private Api api;

    ClientController() {
        try {
            Registry registry = LocateRegistry.getRegistry("localhost");
            this.api = (Api) registry.lookup("Api");
        } catch (Exception e) {
            System.err.println("Client exception:");
            e.printStackTrace();
        }
    }

    @GetMapping("/")
    public String homePage(Model model) {
        model.addAttribute("homeMessage", "Welcome to CloudaAtlas client interface");
        return "home";
    }

    @GetMapping("/query")
    public String queryPage(Model model) {
        model.addAttribute("queryObject", new Query());
        return "queryForm";
    }

    @PostMapping("/query")
    public String submitQuery(@ModelAttribute Query queryObject, Model model)  {
        boolean success = true;

        try {
            this.api.installQuery(queryObject.getName(), queryObject.getValue());
        } catch (Exception e) {
            success = false;
            System.err.println("Client exception:");
            e.printStackTrace();
        }

        if (success) {
            model.addAttribute(
                    "homeMessage",
                    "Query submitted successfully");
        } else {
            model.addAttribute(
                    "homeMessage",
                    "Query submission failed with a remote exception");
        }

        return "home";
    }

    @GetMapping("/contacts")
    public String contactPage(Model model) {
        model.addAttribute("contactsObject" , new ContactsString());
        return "contactsForm";
    }

    private Set<ValueContact> parseContactsString(ContactsString contactsInput) throws Exception {
        Gson gson = new Gson();
        Map<String, ArrayList> contactStrings = gson.fromJson(contactsInput.getString(), Map.class);
        Set<ValueContact> contactObjects = new HashSet<ValueContact>();
        ArrayList<Double> cAddr;
        byte[] inetArray = new byte[4];

        for (Map.Entry<String, ArrayList> cursor : contactStrings.entrySet()) {
            cAddr = cursor.getValue(); // gson always reads numerical values as doubles
            for (int i = 0; i < 4; i++) {
                inetArray[i] = (byte) cAddr.get(i).doubleValue();
            }
            contactObjects.add(new ValueContact(
                    new PathName(cursor.getKey()),
                    InetAddress.getByAddress(inetArray))
            );
        }

        return contactObjects;
    }

    @PostMapping("/contacts")
    public String contactPage(@ModelAttribute ContactsString contactsObject, Model model) {
        boolean success = true;
        Set<ValueContact> contactObjects;

        try {
            contactObjects = parseContactsString(contactsObject);
            this.api.setFallbackContacts(contactObjects);
        } catch (Exception e) {
            success = false;
            System.err.println("Client exception:");
            e.printStackTrace();
        }

        if (success) {
            model.addAttribute("homeMessage", "Contact list submitted successfully");
        } else {
            model.addAttribute("homeMessage", "Contact list submission failed");
        }

        return "home";
    }

    @GetMapping("/attribs")
    public String attribPage(Model model) {
        model.addAttribute("attributeObject", new Attribute());
        return "attribForm";
    }

    private Value parseAttributeValue(Attribute attributeObject) throws Exception {
        Value attributeValue = null;

        switch (attributeObject.getAttributeType()) {
            case "Boolean":
                attributeValue = attributeObject.getValueString().toLowerCase().equals("true") ?
                        new ValueBoolean(true) :
                        new ValueBoolean(false);
                break;
            case "Double":
                attributeValue = new ValueDouble(Double.parseDouble(attributeObject.getValueString()));
                break;
            case "Int":
                attributeValue = new ValueInt(Long.parseLong(attributeObject.getValueString()));
                break;
            case "String":
                attributeValue = new ValueString(attributeObject.getValueString());
                break;
            case "Time":
                attributeValue = new ValueTime(Long.parseLong(attributeObject.getValueString()));
                break;
            case "Duration":
                attributeValue = new ValueDuration(attributeObject.getValueString());
                break;
            case "Contact":
                ContactsString contactsString = new ContactsString();
                contactsString.setString(attributeObject.getValueString());
                attributeValue = parseContactsString(contactsString).iterator().next();
                break;
            case "Query":
                attributeValue = new ValueQuery(attributeObject.getValueString());
                break;
            default:
                String errMsg = "Value type not supported: " + attributeObject.getAttributeType();
                throw new UnsupportedOperationException(errMsg);
        }

        return attributeValue;
    }

    @PostMapping("/attribs")
    public String attribPage(@ModelAttribute Attribute attributeObject, Model model) {
        boolean success = true;
        Value attributeValue;

        try {
            attributeValue = parseAttributeValue(attributeObject);
            api.setAttributeValue(
                    attributeObject.getZoneName(),
                    attributeObject.getAttributeName(),
                    attributeValue);
        } catch (Exception e) {
            success = false;
            System.err.println("Client exception:");
            e.printStackTrace();
        }

        if (success) {
            model.addAttribute("homeMessage", "Attribute submitted successfully");
        } else {
            model.addAttribute("homeMessage", "Attribute submission failed");
        }

        return "home";
    }

    @GetMapping("/values")
    public String valuesPage(Model model) {
        model.addAttribute("zoneName", new ContactsString());
        return "attribChart";
    }

    @PostMapping("/values")
    public String valuesPage(@ModelAttribute ContactsString zoneName, Model model) {
        boolean success = true;
        AttributesMap attribData;

        try {
            attribData = api.getZoneAttributeValues(zoneName.getString());
        } catch (Exception e) {
            success = false;
            System.err.println("Client exception:");
            e.printStackTrace();
        }

        return "attribChart";
    }
}