Harshada Chaundkar | 5a198b0 | 2019-07-03 16:27:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.rest.resources; |
| 17 | |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.apache.commons.lang3.tuple.Pair; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 24 | import org.onosproject.rest.AbstractWebResource; |
| 25 | import org.slf4j.Logger; |
| 26 | |
| 27 | import javax.ws.rs.GET; |
| 28 | import javax.ws.rs.Produces; |
| 29 | import javax.ws.rs.Path; |
| 30 | import javax.ws.rs.PathParam; |
| 31 | import javax.ws.rs.core.MediaType; |
| 32 | import javax.ws.rs.core.Response; |
| 33 | import java.util.List; |
| 34 | import java.util.Map; |
| 35 | import static org.slf4j.LoggerFactory.getLogger; |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * Get Flow objective next list. |
| 40 | */ |
| 41 | @Path("nextobjectives") |
| 42 | public class FlowObjectiveNextListWebResource extends AbstractWebResource { |
| 43 | |
| 44 | private final Logger log = getLogger(getClass()); |
| 45 | |
| 46 | /** |
| 47 | * To get all obj-next-Ids. |
| 48 | * @return 200 OK with flow objective Next ids. |
| 49 | * @onos.rsModel NextObjectives |
| 50 | */ |
| 51 | @GET |
| 52 | @Produces(MediaType.APPLICATION_JSON) |
| 53 | public Response getObjNextids() { |
| 54 | ObjectNode node = getGroupChainByIdJsonOutput(null, null); |
| 55 | return Response.status(200).entity(node).build(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns all group-chains associated with the given nextId. |
| 60 | * |
| 61 | * @param nextId nextid mapping |
| 62 | * @return 200 OK with array of all the group chain. |
| 63 | * |
| 64 | */ |
| 65 | @GET |
| 66 | @Produces(MediaType.APPLICATION_JSON) |
| 67 | @Path("nextobjective/{nextId}") |
| 68 | public Response getGroupChainByNextid(@PathParam("nextId") String nextId) { |
| 69 | ObjectNode node = getGroupChainByIdJsonOutput(Integer.parseInt(nextId), null); |
| 70 | return Response.status(200).entity(node).build(); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * Returns all group-chains associated with the given deviceId. |
| 76 | * |
| 77 | * @param deviceId deviceId mapping |
| 78 | * @return 200 OK with array of all the group chain. |
| 79 | * |
| 80 | */ |
| 81 | @GET |
| 82 | @Produces(MediaType.APPLICATION_JSON) |
| 83 | @Path("{deviceId}") |
| 84 | public Response getGroupChainByDeviceId(@PathParam("deviceId") String deviceId) { |
| 85 | ObjectNode node = getGroupChainByIdJsonOutput(null, DeviceId.deviceId(deviceId)); |
| 86 | return Response.status(200).entity(node).build(); |
| 87 | } |
| 88 | |
| 89 | private ObjectNode getGroupChainByIdJsonOutput(Integer nextId, DeviceId deviceId) { |
| 90 | ObjectNode root = mapper().createObjectNode(); |
| 91 | ArrayNode connectionArray = mapper().createArrayNode(); |
| 92 | FlowObjectiveService service = get(FlowObjectiveService.class); |
| 93 | Map<Pair<Integer, DeviceId>, List<String>> nextObjGroupMap = service.getNextMappingsChain(); |
| 94 | |
| 95 | if (nextId == null && deviceId == null) { |
| 96 | nextObjGroupMap.forEach((key, value) -> { |
| 97 | ObjectNode mappingNode = mapper().createObjectNode(); |
| 98 | String keyString = String.format("NextId %s: %s", key.getLeft(), key.getRight()); |
| 99 | mappingNode.put(keyString, value.toString()); |
| 100 | connectionArray.add(mappingNode); |
| 101 | }); |
| 102 | } else { |
| 103 | nextObjGroupMap.forEach((key, value) -> { |
| 104 | ObjectNode mappingNode = mapper().createObjectNode(); |
| 105 | if ((key.getLeft().equals(nextId)) || (key.getRight().equals(deviceId))) { |
| 106 | List groupchain = value; |
| 107 | if (deviceId != null && groupchain != null) { |
| 108 | String keyString = String.format("NextId %s:", key.getLeft()); |
| 109 | mappingNode.put(keyString, groupchain.toString()); |
| 110 | } else if (groupchain != null) { |
| 111 | mappingNode.put("groupChain", groupchain.toString()); |
| 112 | } |
| 113 | connectionArray.add(mappingNode); |
| 114 | } |
| 115 | }); |
| 116 | } |
| 117 | root.put("obj-next-ids", connectionArray); |
| 118 | |
| 119 | return root; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | } |