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