blob: d1f0cbf3bf8582756761fe9995b67a7ffc6b21cd [file] [log] [blame]
Parvathi Mef749632016-07-07 13:30:43 -07001/*
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
17package org.onosproject.patchpanel.impl;
18
19
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import com.google.common.collect.ImmutableSet;
23import org.onosproject.net.ConnectPoint;
Simon Hunt8a0429a2017-01-06 16:52:47 -080024import org.onosproject.net.Device;
25import org.onosproject.net.Port;
26import org.onosproject.net.device.DeviceService;
Parvathi Mef749632016-07-07 13:30:43 -070027import org.onosproject.ui.RequestHandler;
28import org.onosproject.ui.UiMessageHandler;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
Parvathi Mef749632016-07-07 13:30:43 -070031
32import java.util.ArrayList;
33import java.util.Collection;
34import java.util.List;
35
Simon Hunt8a0429a2017-01-06 16:52:47 -080036import static org.onosproject.net.ConnectPoint.deviceConnectPoint;
37
Parvathi Mef749632016-07-07 13:30:43 -070038/**
Simon Hunt8a0429a2017-01-06 16:52:47 -080039 * 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 Mef749632016-07-07 13:30:43 -070044 */
Jonathan Hart382119e2016-09-02 13:14:49 -070045public class PatchPanelUiMessageHandler extends UiMessageHandler {
Parvathi Mef749632016-07-07 13:30:43 -070046
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 Hunt8a0429a2017-01-06 16:52:47 -080053
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 Mef749632016-07-07 13:30:43 -070063 private List<ConnectPoint> previous = new ArrayList<>();
Parvathi Mef749632016-07-07 13:30:43 -070064 private final Logger log = LoggerFactory.getLogger(getClass());
65
66 @Override
67 protected Collection<RequestHandler> createRequestHandlers() {
Simon Hunt8a0429a2017-01-06 16:52:47 -080068 return ImmutableSet.of(
69 new DataRequestHandler(),
70 new SecondDataRequestHandler(),
71 new ThirdDataRequestHandler()
72 );
Parvathi Mef749632016-07-07 13:30:43 -070073 }
74
75 // handler for data requests/events
76 private final class DataRequestHandler extends RequestHandler {
Parvathi Mef749632016-07-07 13:30:43 -070077 private DataRequestHandler() {
78 super(SAMPLE_CUSTOM_DATA_REQ);
79 }
80
81 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -080082 public void process(ObjectNode payload) {
Parvathi Mef749632016-07-07 13:30:43 -070083 DeviceService service = get(DeviceService.class);
84 ObjectNode result = objectNode();
85 ArrayNode cps = arrayNode();
Simon Hunt8a0429a2017-01-06 16:52:47 -080086 result.set(CPS, cps);
Parvathi Mef749632016-07-07 13:30:43 -070087
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 Hunt8a0429a2017-01-06 16:52:47 -080093 log.info(device.id() + SLASH + port.number());
Parvathi Mef749632016-07-07 13:30:43 -070094 }
95 }
96 }
Simon Hunt8a0429a2017-01-06 16:52:47 -080097 sendMessage(SAMPLE_CUSTOM_DATA_RESP, result);
Parvathi Mef749632016-07-07 13:30:43 -070098 }
99 }
100
101 private final class SecondDataRequestHandler extends RequestHandler {
Parvathi Mef749632016-07-07 13:30:43 -0700102 private SecondDataRequestHandler() {
103 super(SAMPLE_CUSTOM_DATA_REQ2);
104 }
105
106 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800107 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 Mef749632016-07-07 13:30:43 -0700115 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 Hunt8a0429a2017-01-06 16:52:47 -0800125 payload.put(MESSAGE, message);
126 sendMessage(SAMPLE_CUSTOM_DATA_RESP2, payload);
Parvathi Mef749632016-07-07 13:30:43 -0700127
128 }
129 }
Simon Hunt8a0429a2017-01-06 16:52:47 -0800130
Parvathi Mef749632016-07-07 13:30:43 -0700131 private final class ThirdDataRequestHandler extends RequestHandler {
132 private ThirdDataRequestHandler() {
133 super(SAMPLE_CUSTOM_DATA_REQ3);
134 }
135
136 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800137 public void process(ObjectNode payload) {
138 StringBuilder sb = new StringBuilder();
Parvathi Mef749632016-07-07 13:30:43 -0700139 for (int i = 0; i < previous.size(); i++) {
Simon Hunt8a0429a2017-01-06 16:52:47 -0800140 sb.append(previous.get(i)).append(i % 2 == 0 ? WITH : EOL);
Parvathi Mef749632016-07-07 13:30:43 -0700141 }
Simon Hunt8a0429a2017-01-06 16:52:47 -0800142 payload.put(CPOINTS, sb.toString());
143 sendMessage(SAMPLE_CUSTOM_DATA_RESP3, payload);
Parvathi Mef749632016-07-07 13:30:43 -0700144 }
145 }
Simon Hunt8a0429a2017-01-06 16:52:47 -0800146
Jonathan Hart382119e2016-09-02 13:14:49 -0700147}