Open Kilda Java Documentation
FlowResource.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.floodlight.switchmanager.web;
17 
18 import static org.openkilda.messaging.Utils.MAPPER;
19 
28 
29 import org.restlet.resource.Post;
30 import org.restlet.resource.Put;
31 import org.restlet.resource.ServerResource;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 
35 import java.io.IOException;
36 import java.util.concurrent.TimeUnit;
37 
41 public class FlowResource extends ServerResource {
42  private static final Logger logger = LoggerFactory.getLogger(FlowResource.class);
43 
44  @Post("json")
45  @Put("json")
46  public String installFlow(String json) throws IOException {
47  ISwitchManager switchManager = (ISwitchManager) getContext().getAttributes()
48  .get(ISwitchManager.class.getCanonicalName());
49 
50  Message message;
51  try {
52  message = MAPPER.readValue(json, Message.class);
53  } catch (IOException exception) {
54  String messageString = "Received JSON is not valid for TPN";
55  logger.error("{}: {}", messageString, json, exception);
56  MessageError responseMessage = new MessageError(CorrelationContext.getId(), now(),
57  ErrorType.DATA_INVALID.toString(), messageString, exception.getMessage());
58  return MAPPER.writeValueAsString(responseMessage);
59  }
60 
61  if (!(message instanceof CommandMessage)) {
62  String messageString = "Json payload message is not an instance of CommandMessage";
63  logger.error("{}: class={}, data={}", messageString, message.getClass().getCanonicalName(), json);
64  MessageError responseMessage = new MessageError(CorrelationContext.getId(), now(),
65  ErrorType.DATA_INVALID.toString(), messageString, message.getClass().getCanonicalName());
66  return MAPPER.writeValueAsString(responseMessage);
67  }
68 
69  CommandMessage cmdMessage = (CommandMessage) message;
70  CommandData data = cmdMessage.getData();
71  if (!(data instanceof BaseInstallFlow)) {
72  String messageString = "Json payload data is not an instance of CommandData";
73  logger.error("{}: class={}, data={}", messageString, data.getClass().getCanonicalName(), json);
74  MessageError responseMessage = new MessageError(CorrelationContext.getId(), now(),
75  ErrorType.DATA_INVALID.toString(), messageString, data.getClass().getCanonicalName());
76  return MAPPER.writeValueAsString(responseMessage);
77  }
78 
79  return MAPPER.writeValueAsString("ok");
80  }
81 
82  private long now() {
83  return TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
84  }
85 }
static final ObjectMapper MAPPER
Definition: Utils.java:31