Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.patchpanel.impl; |
| 18 | |
| 19 | |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import com.google.common.collect.ImmutableSet; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.ui.RequestHandler; |
| 25 | import org.onosproject.ui.UiMessageHandler; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | import org.onosproject.net.Device; |
| 29 | import org.onosproject.net.Port; |
| 30 | import org.onosproject.net.device.DeviceService; |
| 31 | |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.Collection; |
| 34 | import java.util.List; |
| 35 | |
| 36 | /** |
| 37 | * ONOS UI Custom-View message handler. |
| 38 | * |
| 39 | * This class contains the request handlers that handle the response |
| 40 | * to each event. In this particular implementation the second message |
| 41 | * handler creates the patch and the first message handler loads the data |
| 42 | */ |
Jonathan Hart | 382119e | 2016-09-02 13:14:49 -0700 | [diff] [blame] | 43 | public class PatchPanelUiMessageHandler extends UiMessageHandler { |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 44 | |
| 45 | private static final String SAMPLE_CUSTOM_DATA_REQ = "sampleCustomDataRequest"; |
| 46 | private static final String SAMPLE_CUSTOM_DATA_RESP = "sampleCustomDataResponse"; |
| 47 | private static final String SAMPLE_CUSTOM_DATA_REQ2 = "sampleCustomDataRequest2"; |
| 48 | private static final String SAMPLE_CUSTOM_DATA_RESP2 = "sampleCustomDataResponse2"; |
| 49 | private static final String SAMPLE_CUSTOM_DATA_REQ3 = "sampleCustomDataRequest3"; |
| 50 | private static final String SAMPLE_CUSTOM_DATA_RESP3 = "sampleCustomDataResponse3"; |
| 51 | private String message = ""; |
| 52 | private String cpoints = ""; |
| 53 | private List<ConnectPoint> previous = new ArrayList<>(); |
| 54 | private static ConnectPoint cp1; |
| 55 | private static ConnectPoint cp2; |
| 56 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 57 | |
| 58 | @Override |
| 59 | protected Collection<RequestHandler> createRequestHandlers() { |
| 60 | return ImmutableSet.of(new DataRequestHandler(), new SecondDataRequestHandler(), new ThirdDataRequestHandler()); |
| 61 | } |
| 62 | |
| 63 | // handler for data requests/events |
| 64 | private final class DataRequestHandler extends RequestHandler { |
| 65 | |
| 66 | private DataRequestHandler() { |
| 67 | super(SAMPLE_CUSTOM_DATA_REQ); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void process(long sid, ObjectNode payload) { |
| 72 | DeviceService service = get(DeviceService.class); |
| 73 | ObjectNode result = objectNode(); |
| 74 | ArrayNode cps = arrayNode(); |
| 75 | result.set("cps", cps); |
| 76 | |
| 77 | for (Device device : service.getDevices()) { |
| 78 | cps.add(device.id().toString()); |
| 79 | for (Port port : service.getPorts(device.id())) { |
| 80 | if (!port.number().isLogical()) { |
| 81 | cps.add(port.number().toString()); |
| 82 | log.info(device.id().toString() + "/" + port.number()); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | sendMessage(SAMPLE_CUSTOM_DATA_RESP, 0, result); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private final class SecondDataRequestHandler extends RequestHandler { |
| 91 | |
| 92 | private SecondDataRequestHandler() { |
| 93 | super(SAMPLE_CUSTOM_DATA_REQ2); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void process(long sid, ObjectNode payload) { |
| 98 | boolean done; |
| 99 | String deviceId = payload.get("result").get(0).asText(); |
| 100 | cp1 = ConnectPoint.deviceConnectPoint(deviceId + "/" + payload.get("result").get(1).asText()); |
| 101 | cp2 = ConnectPoint.deviceConnectPoint(deviceId + "/" + payload.get("result").get(2).asText()); |
| 102 | PatchPanelService patchPanelService; |
| 103 | patchPanelService = get(PatchPanelService.class); |
| 104 | done = patchPanelService.addPatch(cp1, cp2); |
| 105 | if (done) { |
| 106 | message = "Patch has been created"; |
| 107 | previous.add(cp1); |
| 108 | previous.add(cp2); |
| 109 | } else { |
| 110 | message = "One or both of these ports are already in use"; |
| 111 | if (cp1.port().equals(cp2.port())) { |
| 112 | message = "Both ports can not be the same"; |
| 113 | } |
| 114 | } |
| 115 | payload.put("message", message); |
| 116 | sendMessage(SAMPLE_CUSTOM_DATA_RESP2, sid, payload); |
| 117 | |
| 118 | } |
| 119 | } |
| 120 | private final class ThirdDataRequestHandler extends RequestHandler { |
| 121 | private ThirdDataRequestHandler() { |
| 122 | super(SAMPLE_CUSTOM_DATA_REQ3); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void process(long sid, ObjectNode payload) { |
| 127 | cpoints = ""; |
| 128 | for (int i = 0; i < previous.size(); i++) { |
| 129 | if (i % 2 == 1) { |
| 130 | cpoints += previous.get(i) + "\n"; |
| 131 | } else { |
| 132 | cpoints += previous.get(i) + " with "; |
| 133 | } |
| 134 | } |
| 135 | payload.put("cpoints", cpoints); |
| 136 | sendMessage(SAMPLE_CUSTOM_DATA_RESP3, sid, payload); |
| 137 | } |
| 138 | } |
Jonathan Hart | 382119e | 2016-09-02 13:14:49 -0700 | [diff] [blame] | 139 | } |