lishuai
Committed by Gerrit Code Review

[ONOS-4193] Add handler for sfc gui.

Change-Id: Ie270988bc05914c6f9da1274e3a9fcfb917a8aa1
1 +/*
2 + * Copyright 2016 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.vtnweb.gui;
18 +
19 +import java.util.ArrayList;
20 +import java.util.Collection;
21 +import java.util.Iterator;
22 +import java.util.List;
23 +
24 +import org.onlab.packet.IpAddress;
25 +import org.onosproject.ui.RequestHandler;
26 +import org.onosproject.ui.UiMessageHandler;
27 +import org.onosproject.ui.table.TableModel;
28 +import org.onosproject.ui.table.TableRequestHandler;
29 +import org.onosproject.vtnrsc.FixedIp;
30 +import org.onosproject.vtnrsc.FlowClassifier;
31 +import org.onosproject.vtnrsc.FlowClassifierId;
32 +import org.onosproject.vtnrsc.PortChain;
33 +import org.onosproject.vtnrsc.PortPair;
34 +import org.onosproject.vtnrsc.PortPairGroup;
35 +import org.onosproject.vtnrsc.PortPairGroupId;
36 +import org.onosproject.vtnrsc.PortPairId;
37 +import org.onosproject.vtnrsc.VirtualPort;
38 +import org.onosproject.vtnrsc.VirtualPortId;
39 +import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
40 +import org.onosproject.vtnrsc.portchain.PortChainService;
41 +import org.onosproject.vtnrsc.portpair.PortPairService;
42 +import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
43 +import org.onosproject.vtnrsc.virtualport.VirtualPortService;
44 +
45 +import com.fasterxml.jackson.databind.node.ObjectNode;
46 +import com.google.common.collect.ImmutableSet;
47 +
48 +/**
49 + * Message handler for service function chain view related messages.
50 + */
51 +public 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 +}
1 +/*
2 + * Copyright 2016 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 +/**
18 + * Service Function Chain gui.
19 + */
20 +package org.onosproject.vtnweb.gui;