Yi Tseng | a87b40c | 2017-09-10 00:59:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | |
| 17 | package org.onosproject.ui.impl; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableSet; |
Yi Tseng | a87b40c | 2017-09-10 00:59:03 -0700 | [diff] [blame] | 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.net.Device; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.device.DeviceService; |
Yi Tseng | a87b40c | 2017-09-10 00:59:03 -0700 | [diff] [blame] | 25 | import org.onosproject.net.pi.model.PiPipeconf; |
| 26 | import org.onosproject.net.pi.model.PiPipeconfId; |
Yi Tseng | a87b40c | 2017-09-10 00:59:03 -0700 | [diff] [blame] | 27 | import org.onosproject.net.pi.model.PiPipelineModel; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 28 | import org.onosproject.net.pi.service.PiPipeconfService; |
Yi Tseng | a87b40c | 2017-09-10 00:59:03 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.RequestHandler; |
| 30 | import org.onosproject.ui.UiMessageHandler; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
| 34 | import java.util.Collection; |
| 35 | import java.util.Optional; |
Yi Tseng | a87b40c | 2017-09-10 00:59:03 -0700 | [diff] [blame] | 36 | |
| 37 | public class PipeconfViewMessageHandler extends UiMessageHandler { |
| 38 | private static final Logger log = |
| 39 | LoggerFactory.getLogger(PipeconfViewMessageHandler.class); |
| 40 | private static final String PIPECONF_REQUEST = "pipeconfRequest"; |
| 41 | private static final String PIPECONF_RESP = "pipeConfResponse"; |
| 42 | private static final String DEVICE_ID = "devId"; |
| 43 | private static final String PIPECONF = "pipeconf"; |
| 44 | private static final String PIPELINE_MODEL = "pipelineModel"; |
| 45 | private static final String NO_PIPECONF_RESP = "noPipeconfResp"; |
| 46 | |
| 47 | @Override |
| 48 | protected Collection<RequestHandler> createRequestHandlers() { |
| 49 | return ImmutableSet.of(new PipeconfRequestHandler()); |
| 50 | } |
| 51 | |
| 52 | private class PipeconfRequestHandler extends RequestHandler { |
| 53 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 54 | PipeconfRequestHandler() { |
Yi Tseng | a87b40c | 2017-09-10 00:59:03 -0700 | [diff] [blame] | 55 | super(PIPECONF_REQUEST); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void process(ObjectNode payload) { |
| 60 | PiPipeconfService piPipeconfService = get(PiPipeconfService.class); |
| 61 | DeviceService deviceService = get(DeviceService.class); |
| 62 | ObjectNode responseData = objectNode(); |
| 63 | String devId = string(payload, DEVICE_ID); |
| 64 | if (devId == null || devId.isEmpty()) { |
| 65 | log.warn("{}: Invalid device id", PIPECONF_REQUEST); |
| 66 | sendMessage(NO_PIPECONF_RESP, null); |
| 67 | return; |
| 68 | } |
| 69 | DeviceId deviceId = DeviceId.deviceId(devId); |
| 70 | Optional<PiPipeconfId> pipeconfId = piPipeconfService.ofDevice(deviceId); |
| 71 | if (!pipeconfId.isPresent()) { |
| 72 | log.warn("{}: Can't find pipeconf id for device {}", PIPECONF_REQUEST, deviceId); |
| 73 | sendMessage(NO_PIPECONF_RESP, null); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | Optional<PiPipeconf> pipeconf = piPipeconfService.getPipeconf(pipeconfId.get()); |
| 78 | if (!pipeconf.isPresent()) { |
| 79 | log.warn("{}: Can't find pipeconf {}", PIPECONF_REQUEST, pipeconfId); |
| 80 | sendMessage(NO_PIPECONF_RESP, null); |
| 81 | return; |
| 82 | } |
| 83 | CodecContext codecContext = getJsonCodecContext(); |
| 84 | |
| 85 | ObjectNode pipeconfData = codecContext.encode(pipeconf.get(), PiPipeconf.class); |
| 86 | responseData.set(PIPECONF, pipeconfData); |
| 87 | |
| 88 | // Filtered out models not exists in interpreter |
| 89 | // usually they generated by compiler automatically |
| 90 | Device device = deviceService.getDevice(deviceId); |
| 91 | if (device == null || !deviceService.isAvailable(deviceId)) { |
| 92 | log.warn("{}: Device {} is not available", PIPECONF_REQUEST, deviceId); |
| 93 | sendMessage(NO_PIPECONF_RESP, null); |
| 94 | return; |
| 95 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 96 | PiPipelineModel pipelineModel = pipeconf.get().pipelineModel(); |
Yi Tseng | a87b40c | 2017-09-10 00:59:03 -0700 | [diff] [blame] | 97 | |
| 98 | ObjectNode pipelineModelData = |
| 99 | codecContext.encode(pipelineModel, PiPipelineModel.class); |
| 100 | responseData.set(PIPELINE_MODEL, pipelineModelData); |
| 101 | |
| 102 | sendMessage(PIPECONF_RESP, responseData); |
| 103 | } |
| 104 | } |
Yi Tseng | a87b40c | 2017-09-10 00:59:03 -0700 | [diff] [blame] | 105 | } |