blob: 66efaf7381ed83c0e8cab7704de3d3a3779e2796 [file] [log] [blame]
Carolina Fernandezad893432016-07-18 11:11:34 +02001/*
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.sdxl2.cli;
18
19import com.google.common.collect.Iterables;
20import org.apache.karaf.shell.commands.Argument;
21import org.apache.karaf.shell.commands.Command;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.net.intent.Intent;
24import org.onosproject.net.intent.IntentService;
25import org.onosproject.net.intent.Key;
26import org.onosproject.sdxl2.SdxL2ConnectionPoint;
27import org.onosproject.sdxl2.SdxL2Service;
28import org.onosproject.sdxl2.SdxL2State;
29import org.onosproject.sdxl2.VirtualCircuit;
30
31import java.util.Iterator;
32
33import static java.lang.String.format;
34
35/**
36 * CLI to print the details of a Virtual Circuit in an SDX-L2.
37 */
38@Command(scope = "sdxl2", name = "sdxl2vc", description = "Prints the details of an SDX-L2 Virtual Circuit")
39public class SdxL2GetVCCommand extends AbstractShellCommand {
40
41 @Argument(index = 0, name = "sdxl2vcname", description = "Name of SDX-L2 VC", required = true, multiValued = false)
42 private String sdxl2vcname = null;
43
44 private static final String MATCH_FORMAT = "%s-%s";
45 private static final String HEADER_CP =
46 "\n\u001B[1;37mStatus\t\tConnection Point\t\tName\t\tVlan IDs\t\tCE Mac Address\u001B[0m";
47 private static final String SEPARATOR_CP = "\u001B[1;37m------------" +
48 "-----------------------------------------------------------" +
49 "--------------------------------------\u001B[0m";
50 private static final String FORMAT_SDXL2CP_ONLINE =
51 "\u001B[1;32m%s\u001B[0m\t\t\u001B[1;37m%s/%s\t\t%s\t\t%s\t\t%s\u001B[0m";
52 private static final String FORMAT_SDXL2CP_OFFLINE =
53 "\u001B[1;31m%s\u001B[0m\t\t\u001B[1;37m%s/%s\t\t%s\t\t%s\t\t%s\u001B[0m";
54
55 private static final String HEADER_VC =
56 "\n\u001B[1;37mStatus\t\tIntent\u001B[0m";
57 private static final String SEPARATOR_VC =
58 "\u001B[1;37m--------------------------------------------\u001B[0m";
59 private static final String FORMAT_SDXL2VC_ONLINE =
60 "\u001B[1;32m%s\u001B[0m\t\t\u001B[1;37m%s\u001B[0m";
61 private static final String FORMAT_SDXL2VC_OFFLINE =
62 "\u001B[1;31m%s\u001B[0m\t\t\u001B[1;37m%s\u001B[0m";
63 private static final String FORMAT_SDXL2VC_CHECK =
64 "\u001B[1;33m%s\u001B[0m\t\t\u001B[1;37m%s\u001B[0m";
65
66 @Override
67 protected void execute() {
68 SdxL2Service sdxl2Service = get(SdxL2Service.class);
69 VirtualCircuit virtualCircuit;
70 SdxL2ConnectionPoint sdxl2ConnectionPoint;
71 SdxL2State state;
72 virtualCircuit = sdxl2Service.getVirtualCircuit(sdxl2vcname);
73 if (virtualCircuit == null) {
74 return;
75 }
76
77 print(HEADER_CP);
78 print(SEPARATOR_CP);
79
80 sdxl2ConnectionPoint = virtualCircuit.lhs();
81 state = sdxl2Service.getEdgePortState(sdxl2ConnectionPoint.connectPoint());
82 if (state == SdxL2State.ONLINE) {
83 print(FORMAT_SDXL2CP_ONLINE,
84 "ONLINE",
85 sdxl2ConnectionPoint.connectPoint().elementId(),
86 sdxl2ConnectionPoint.connectPoint().port(),
87 sdxl2ConnectionPoint.name(),
88 sdxl2ConnectionPoint.vlanIds(),
89 sdxl2ConnectionPoint.macAddress());
90 } else if (state == SdxL2State.OFFLINE) {
91 print(FORMAT_SDXL2CP_OFFLINE,
92 "OFFLINE",
93 sdxl2ConnectionPoint.connectPoint().elementId(),
94 sdxl2ConnectionPoint.connectPoint().port(),
95 sdxl2ConnectionPoint.name(),
96 sdxl2ConnectionPoint.vlanIds(),
97 sdxl2ConnectionPoint.macAddress());
98 }
99
100
101 sdxl2ConnectionPoint = virtualCircuit.rhs();
102 state = sdxl2Service.getEdgePortState(sdxl2ConnectionPoint.connectPoint());
103 if (state == SdxL2State.ONLINE) {
104 print(FORMAT_SDXL2CP_ONLINE,
105 "ONLINE",
106 sdxl2ConnectionPoint.connectPoint().elementId(),
107 sdxl2ConnectionPoint.connectPoint().port(),
108 sdxl2ConnectionPoint.name(),
109 sdxl2ConnectionPoint.vlanIds(),
110 sdxl2ConnectionPoint.macAddress());
111 } else if (state == SdxL2State.OFFLINE) {
112 print(FORMAT_SDXL2CP_OFFLINE,
113 "OFFLINE",
114 sdxl2ConnectionPoint.connectPoint().elementId(),
115 sdxl2ConnectionPoint.connectPoint().port(),
116 sdxl2ConnectionPoint.name(),
117 sdxl2ConnectionPoint.vlanIds(),
118 sdxl2ConnectionPoint.macAddress());
119 }
120 print("");
121
122 print(HEADER_VC);
123 print(SEPARATOR_VC);
124 IntentService intentService = get(IntentService.class);
125 Iterator<Intent> intents = Iterables.filter(intentService.getIntents(), intent ->
126 (matches(virtualCircuit.lhs(), virtualCircuit.rhs(), intent) ||
127 (matches(virtualCircuit.rhs(), virtualCircuit.lhs(), intent)))).iterator();
128 Intent intent;
129 Key key;
130 while (intents.hasNext()) {
131 intent = intents.next();
132 key = intent.key();
133 state = sdxl2Service.getIntentState(key);
134 if (state == SdxL2State.ONLINE) {
135 print(FORMAT_SDXL2VC_ONLINE,
136 "ONLINE",
137 key);
138 } else if (state == SdxL2State.OFFLINE) {
139 print(FORMAT_SDXL2VC_OFFLINE,
140 "OFFLINE",
141 key);
142 } else {
143 print(FORMAT_SDXL2VC_CHECK,
144 "CHECK",
145 key);
146 }
147 }
148 print("");
149 }
150
151 /**
152 * Matches an intent given two sdxl2 connection points.
153 *
154 * @param lhs left hand side of the virtual circuit
155 * @param rhs right hand side of the virtual circuit
156 * @param intent intent to match
157 * @return result of the match
158 */
159 private boolean matches(SdxL2ConnectionPoint lhs, SdxL2ConnectionPoint rhs, Intent intent) {
160
161 String key = intent.key().toString();
162 String[] fields = key.split(":");
163 String cps = format(MATCH_FORMAT, lhs.name(), rhs.name());
164
165 return fields.length == 2 && fields[1].contains(cps);
166 }
167}