Open Kilda Java Documentation
MetersResource.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 
26 
27 import org.projectfloodlight.openflow.protocol.OFMeterConfig;
28 import org.projectfloodlight.openflow.protocol.OFMeterConfigStatsReply;
29 import org.projectfloodlight.openflow.types.DatapathId;
30 import org.restlet.data.Status;
31 import org.restlet.resource.Get;
32 import org.restlet.resource.ServerResource;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 
36 import java.util.HashMap;
37 import java.util.Map;
38 
39 public class MetersResource extends ServerResource {
40  private static final Logger logger = LoggerFactory.getLogger(MetersResource.class);
41 
42  // FIXME(surabujin): is it used anywhere?
43  @Get("json")
44  @SuppressWarnings("unchecked")
45  public Map<Long, Object> getMeters() {
46  Map<Long, Object> response = new HashMap<>();
47  String switchId = (String) this.getRequestAttributes().get("switch_id");
48  logger.debug("Get meters for switch: {}", switchId);
49  ISwitchManager switchManager = (ISwitchManager) getContext().getAttributes()
50  .get(ISwitchManager.class.getCanonicalName());
51 
52  try {
53  OFMeterConfigStatsReply replay = switchManager.dumpMeters(DatapathId.of(switchId));
54  logger.debug("Meters from switch {} received: {}", switchId, replay);
55 
56  if (replay != null) {
57  for (OFMeterConfig entry : replay.getEntries()) {
58  response.put(entry.getMeterId(), entry);
59  }
60  }
62  String messageString = "Not supported";
63  logger.error("{}: {}", messageString, switchId, ex);
64  MessageError responseMessage = new MessageError(CorrelationContext.getId(), System.currentTimeMillis(),
65  ErrorType.PARAMETERS_INVALID.toString(), messageString, ex.getMessage());
66  response.putAll(MAPPER.convertValue(responseMessage, Map.class));
67 
68  getResponse().setStatus(Status.SERVER_ERROR_NOT_IMPLEMENTED);
69 
70  } catch (IllegalArgumentException|SwitchOperationException exception) {
71  String messageString = "No such switch";
72  logger.error("{}: {}", messageString, switchId, exception);
73  MessageError responseMessage = new MessageError(CorrelationContext.getId(), System.currentTimeMillis(),
74  ErrorType.PARAMETERS_INVALID.toString(), messageString, exception.getMessage());
75  response.putAll(MAPPER.convertValue(responseMessage, Map.class));
76 
77  getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
78  }
79  return response;
80  }
81 }
static final ObjectMapper MAPPER
Definition: Utils.java:31
OFMeterConfigStatsReply dumpMeters(final DatapathId dpid)