blob: 66c84d4766cfe1bb4a98a48d7737719c60dd427b [file] [log] [blame]
lishuaib7ee4562016-03-22 16:40:52 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
lishuaib7ee4562016-03-22 16:40:52 +08003 *
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.vtnweb.gui;
18
19import java.util.ArrayList;
20import java.util.Collection;
21import java.util.Iterator;
22import java.util.List;
23
24import org.onlab.packet.IpAddress;
25import org.onosproject.ui.RequestHandler;
26import org.onosproject.ui.UiMessageHandler;
27import org.onosproject.ui.table.TableModel;
28import org.onosproject.ui.table.TableRequestHandler;
29import org.onosproject.vtnrsc.FixedIp;
30import org.onosproject.vtnrsc.FlowClassifier;
31import org.onosproject.vtnrsc.FlowClassifierId;
32import org.onosproject.vtnrsc.PortChain;
33import org.onosproject.vtnrsc.PortPair;
34import org.onosproject.vtnrsc.PortPairGroup;
35import org.onosproject.vtnrsc.PortPairGroupId;
36import org.onosproject.vtnrsc.PortPairId;
37import org.onosproject.vtnrsc.VirtualPort;
38import org.onosproject.vtnrsc.VirtualPortId;
39import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
40import org.onosproject.vtnrsc.portchain.PortChainService;
41import org.onosproject.vtnrsc.portpair.PortPairService;
42import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
43import org.onosproject.vtnrsc.virtualport.VirtualPortService;
44
45import com.fasterxml.jackson.databind.node.ObjectNode;
46import com.google.common.collect.ImmutableSet;
47
48/**
49 * Message handler for service function chain view related messages.
50 */
51public class SfcViewMessageHandler extends UiMessageHandler {
52
53 private static final String SLASH = " ; ";
54 private static final String NONE = "none";
55 private static final String SFCTYPE = "Service Function Chain";
56
57 private static final String SFC_DATA_REQ = "sfcDataRequest";
58 private static final String SFC_DATA_RESP = "sfcDataResponse";
59 private static final String SFCS = "sfcs";
60
61 private static final String ID = "id";
62 private static final String STATE = "_iconid_state";
63 private static final String PORTCHAINNAME = "portChainName";
64 private static final String HOSTS = "hosts";
65 private static final String TYPE = "type";
66 private static final String SRCIP = "srcIp";
67 private static final String DSTIP = "dstIp";
68
69 private static final String[] COL_IDS = {
70 ID, STATE, PORTCHAINNAME, HOSTS, TYPE, SRCIP, DSTIP
71 };
72
73 private static final String ICON_ID_ONLINE = "active";
74 private static final String ICON_ID_OFFLINE = "inactive";
75
76 @Override
77 protected Collection<RequestHandler> createRequestHandlers() {
78 return ImmutableSet.of(new SfcDataRequest());
79 }
80
81 // handler for sfc table requests
82 private final class SfcDataRequest extends TableRequestHandler {
83
84 private static final String NO_ROWS_MESSAGE = "No Service Function Chain found";
85
86 private SfcDataRequest() {
87 super(SFC_DATA_REQ, SFC_DATA_RESP, SFCS);
88 }
89
90 @Override
91 protected String[] getColumnIds() {
92 return COL_IDS;
93 }
94
95 @Override
96 protected String defaultColumnId() {
97 return PORTCHAINNAME;
98 }
99
100 @Override
101 protected String noRowsMessage(ObjectNode payload) {
102 return NO_ROWS_MESSAGE;
103 }
104
105 @Override
106 protected void populateTable(TableModel tm, ObjectNode payload) {
107 PortChainService pcs = get(PortChainService.class);
108 Iterable<PortChain> portChains = pcs.getPortChains();
109 portChains.forEach(pchain -> populateRow(tm.addRow(), pchain));
110 }
111
112 //populate the row of service function chain
113 private void populateRow(TableModel.Row row, PortChain pchain) {
114 PortChainIpRange portChainIpRange = portChainIpRange(pchain);
115 List<VirtualPort> vpList = sfcPorts(pchain);
116 row.cell(ID, pchain.portChainId().value().toString())
117 .cell(STATE, sfcState(vpList))
118 .cell(PORTCHAINNAME, pchain.name())
119 .cell(HOSTS, sfcHosts(vpList))
120 .cell(TYPE, SFCTYPE)
121 .cell(SRCIP, portChainIpRange.srcip())
122 .cell(DSTIP, portChainIpRange.dstip());
123 }
124
125 //PortChainIpRange
126 private PortChainIpRange portChainIpRange(PortChain pchain) {
127 List<FlowClassifierId> flowClassifierList = pchain.flowClassifiers();
128 FlowClassifierService fcs = get(FlowClassifierService.class);
129 StringBuffer srcipbuf = new StringBuffer();
130 StringBuffer dstipbuf = new StringBuffer();
131 if (flowClassifierList != null) {
132 flowClassifierList.stream().forEach(fcid -> {
133 FlowClassifier fc = fcs.getFlowClassifier(fcid);
134 String srcip = fc.srcIpPrefix().toString();
135 String dstip = fc.dstIpPrefix().toString();
136 srcipbuf.append(srcip).append(SLASH);
137 dstipbuf.append(dstip).append(SLASH);
138 });
139 }
140 String srcip = NONE;
141 String dstip = NONE;
142 if (srcipbuf.length() > 0) {
143 srcip = srcipbuf.substring(0, srcipbuf.length() - SLASH.length());
144 }
145 if (dstipbuf.length() > 0) {
146 dstip = dstipbuf.substring(0, dstipbuf.length() - SLASH.length());
147 }
148 PortChainIpRange portChainIpRange = new PortChainIpRange(srcip, dstip);
149 return portChainIpRange;
150 }
151
152 //the VirtualPorts of service function chain
153 private List<VirtualPort> sfcPorts(PortChain pchain) {
154 List<PortPairGroupId> portPairGroupList = pchain.portPairGroups();
155 PortPairGroupService ppgs = get(PortPairGroupService.class);
156 PortPairService pps = get(PortPairService.class);
157 VirtualPortService vps = get(VirtualPortService.class);
158 List<VirtualPort> vpList = new ArrayList<VirtualPort>();
159 if (portPairGroupList != null) {
160 portPairGroupList.stream().forEach(ppgid -> {
161 PortPairGroup ppg = ppgs.getPortPairGroup(ppgid);
162 List<PortPairId> portPairList = ppg.portPairs();
163 if (portPairList != null) {
164 portPairList.stream().forEach(ppid -> {
165 PortPair pp = pps.getPortPair(ppid);
166 VirtualPort vp = vps.getPort(VirtualPortId.portId(pp.ingress()));
167 vpList.add(vp);
168 });
169 }
170 });
171 }
172 return vpList;
173 }
174
175 //the state of service function chain
176 private String sfcState(List<VirtualPort> vpList) {
177 for (VirtualPort vp : vpList) {
178 if (vp.state().equals(VirtualPort.State.DOWN)) {
179 return ICON_ID_OFFLINE;
180 }
181 }
182 return ICON_ID_ONLINE;
183 }
184
185 //the hosts of service function chain
186 private String sfcHosts(List<VirtualPort> vpList) {
187 StringBuffer hostsbuf = new StringBuffer();
188 for (VirtualPort vp : vpList) {
189 Iterator<FixedIp> fixedIps = vp.fixedIps().iterator();
190 if (fixedIps.hasNext()) {
191 FixedIp fixedIp = fixedIps.next();
192 IpAddress ip = fixedIp.ip();
193 hostsbuf.append(ip.toString()).append(SLASH);
194 }
195 }
196 if (hostsbuf.length() > 0) {
197 return hostsbuf.substring(0, hostsbuf.length() - SLASH.length());
198 }
199 return hostsbuf.toString();
200 }
201
202 //source ip prefix and destination ip prefix
203 private final class PortChainIpRange {
204 private final String srcip;
205 private final String dstip;
206
207 private PortChainIpRange(String srcip, String dstip) {
208 this.srcip = srcip;
209 this.dstip = dstip;
210 }
211
212 public String srcip() {
213 return srcip;
214 }
215
216 public String dstip() {
217 return dstip;
218 }
219 }
220 }
221}