blob: 36e2502c1f201f360196b1e3ef6f8d684c8c5506 [file] [log] [blame]
Yi Tsenga87b40c2017-09-10 00:59:03 -07001/*
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
17package org.onosproject.ui.impl;
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
Yi Tsenga87b40c2017-09-10 00:59:03 -070021import org.onosproject.codec.CodecContext;
22import org.onosproject.net.Device;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.device.DeviceService;
Yi Tsenga87b40c2017-09-10 00:59:03 -070025import org.onosproject.net.pi.model.PiPipeconf;
26import org.onosproject.net.pi.model.PiPipeconfId;
Yi Tsenga87b40c2017-09-10 00:59:03 -070027import org.onosproject.net.pi.model.PiPipelineModel;
Carmelo Cascone39c28ca2017-11-15 13:03:57 -080028import org.onosproject.net.pi.service.PiPipeconfService;
Yi Tsenga87b40c2017-09-10 00:59:03 -070029import org.onosproject.ui.RequestHandler;
30import org.onosproject.ui.UiMessageHandler;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34import java.util.Collection;
35import java.util.Optional;
Yi Tsenga87b40c2017-09-10 00:59:03 -070036
37public 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 Cascone87892e22017-11-13 16:01:29 -080054 PipeconfRequestHandler() {
Yi Tsenga87b40c2017-09-10 00:59:03 -070055 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 Cascone87892e22017-11-13 16:01:29 -080096 PiPipelineModel pipelineModel = pipeconf.get().pipelineModel();
Yi Tsenga87b40c2017-09-10 00:59:03 -070097
98 ObjectNode pipelineModelData =
99 codecContext.encode(pipelineModel, PiPipelineModel.class);
100 responseData.set(PIPELINE_MODEL, pipelineModelData);
101
102 sendMessage(PIPECONF_RESP, responseData);
103 }
104 }
Yi Tsenga87b40c2017-09-10 00:59:03 -0700105}