Carolina Fernandez | ad89343 | 2016-07-18 11:11:34 +0200 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.sdxl2.cli; |
| 18 | |
| 19 | import com.google.common.collect.Iterables; |
| 20 | import org.apache.karaf.shell.commands.Argument; |
| 21 | import org.apache.karaf.shell.commands.Command; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.net.intent.Intent; |
| 24 | import org.onosproject.net.intent.IntentService; |
| 25 | import org.onosproject.net.intent.Key; |
| 26 | import org.onosproject.sdxl2.SdxL2ConnectionPoint; |
| 27 | import org.onosproject.sdxl2.SdxL2Service; |
| 28 | import org.onosproject.sdxl2.SdxL2State; |
| 29 | import org.onosproject.sdxl2.VirtualCircuit; |
| 30 | |
| 31 | import java.util.Iterator; |
| 32 | import java.util.Optional; |
| 33 | import java.util.Set; |
| 34 | |
| 35 | import static java.lang.String.format; |
| 36 | |
| 37 | /** |
| 38 | * CLI to delete a Connection Point in an SDX-L2. |
| 39 | */ |
| 40 | @Command(scope = "sdxl2", name = "sdxl2vc-list", |
| 41 | description = "Lists all the SDX-L2 Virtual Circuits. Argument not required the name of SDX-L2") |
| 42 | public class SdxL2ListVCCommand extends AbstractShellCommand { |
| 43 | |
| 44 | @Argument(index = 0, name = "Name of SDX-L2", |
| 45 | description = "Sdxl2 name", required = false, multiValued = false) |
| 46 | private String sdxl2 = null; |
| 47 | |
| 48 | private static final String MATCH_FORMAT = "%s-%s"; |
| 49 | |
| 50 | private static final String HEADER = |
| 51 | "\n\u001B[1;37mStatus\t\tVirtual Circuit\u001B[0m"; |
| 52 | private static final String SEPARATOR = |
| 53 | "\u001B[1;37m-----------------------------------------------\u001B[0m"; |
| 54 | private static final String FORMAT_SDXL2VC_ONLINE = |
| 55 | "\u001B[1;32m%s\u001B[0m\t\t\u001B[1;37m%s\u001B[0m"; |
| 56 | private static final String FORMAT_SDXL2VC_OFFLINE = |
| 57 | "\u001B[1;31m%s\u001B[0m\t\t\u001B[1;37m%s\u001B[0m"; |
| 58 | private static final String FORMAT_SDXL2VC_CHECK = |
| 59 | "\u001B[1;33m%s\u001B[0m\t\t\u001B[1;37m%s\u001B[0m"; |
| 60 | |
| 61 | |
| 62 | @Override |
| 63 | protected void execute() { |
| 64 | SdxL2Service sdxl2Service = get(SdxL2Service.class); |
| 65 | Optional<String> sdxl2name = Optional.ofNullable(sdxl2); |
| 66 | Set<String> result = sdxl2Service.getVirtualCircuits(sdxl2name); |
| 67 | VirtualCircuit vc; |
| 68 | SdxL2State state; |
| 69 | print(HEADER); |
| 70 | print(SEPARATOR); |
| 71 | if (result.size() > 0) { |
| 72 | String[] sdxl2VC; |
| 73 | for (String sdxl2vc : result) { |
| 74 | sdxl2VC = sdxl2vc.split(":"); |
| 75 | vc = sdxl2Service.getVirtualCircuit(sdxl2vc); |
| 76 | if (vc == null) { |
| 77 | break; |
| 78 | } |
| 79 | state = this.getVirtualCircuitState(vc); |
| 80 | if (state == SdxL2State.ONLINE) { |
| 81 | print(FORMAT_SDXL2VC_ONLINE, "ONLINE", sdxl2VC[1]); |
| 82 | } else if (state == SdxL2State.OFFLINE) { |
| 83 | print(FORMAT_SDXL2VC_OFFLINE, "OFFLINE", sdxl2VC[1]); |
| 84 | } else { |
| 85 | print(FORMAT_SDXL2VC_CHECK, "CHECK", sdxl2VC[1]); |
| 86 | } |
| 87 | } |
| 88 | print(""); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Retrieves status of a Virtual Circuit from the status of its |
| 94 | * Connection Points. |
| 95 | * |
| 96 | * @param vc VirtualCircuit object |
| 97 | * @return state of the Virtual Circuit |
| 98 | */ |
| 99 | private SdxL2State getVirtualCircuitState(VirtualCircuit vc) { |
| 100 | IntentService intentService = get(IntentService.class); |
| 101 | SdxL2Service sdxl2Service = get(SdxL2Service.class); |
| 102 | SdxL2State intentState = SdxL2State.ONLINE; |
| 103 | SdxL2State lhsState; |
| 104 | SdxL2State rhsstate; |
| 105 | Iterator<Intent> intents = Iterables.filter(intentService.getIntents(), intent -> |
| 106 | (matches(vc.lhs(), vc.rhs(), intent) || |
| 107 | (matches(vc.rhs(), vc.lhs(), intent)))).iterator(); |
| 108 | Intent intent; |
| 109 | Key key; |
| 110 | int numIntents = 0; |
| 111 | int numIntentsOffline = 0; |
| 112 | while (intents.hasNext()) { |
| 113 | intent = intents.next(); |
| 114 | key = intent.key(); |
| 115 | intentState = sdxl2Service.getIntentState(key); |
| 116 | if (intentState == SdxL2State.OFFLINE || intentState == SdxL2State.CHECK) { |
| 117 | numIntentsOffline = numIntentsOffline + 1; |
| 118 | } |
| 119 | numIntents = numIntents + 1; |
| 120 | } |
| 121 | |
| 122 | if (numIntents == numIntentsOffline) { |
| 123 | return SdxL2State.OFFLINE; |
| 124 | } |
| 125 | |
| 126 | lhsState = sdxl2Service.getEdgePortState(vc.lhs().connectPoint()); |
| 127 | if (lhsState == SdxL2State.OFFLINE) { |
| 128 | return SdxL2State.OFFLINE; |
| 129 | } |
| 130 | |
| 131 | rhsstate = sdxl2Service.getEdgePortState(vc.rhs().connectPoint()); |
| 132 | if (rhsstate == SdxL2State.OFFLINE) { |
| 133 | return SdxL2State.OFFLINE; |
| 134 | } |
| 135 | |
| 136 | if (intentState == SdxL2State.ONLINE && lhsState == SdxL2State.ONLINE && |
| 137 | rhsstate == SdxL2State.ONLINE) { |
| 138 | return SdxL2State.ONLINE; |
| 139 | } |
| 140 | |
| 141 | return SdxL2State.CHECK; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Matches an intent given two SDX-L2 Connection Points. |
| 146 | * |
| 147 | * @param lhs left hand side of the virtual circuit |
| 148 | * @param rhs right hand side of the virtual circuit |
| 149 | * @param intent intent to match |
| 150 | * @return result of the match |
| 151 | */ |
| 152 | private boolean matches(SdxL2ConnectionPoint lhs, SdxL2ConnectionPoint rhs, Intent intent) { |
| 153 | String key = intent.key().toString(); |
| 154 | String[] fields = key.split(":"); |
| 155 | String cps = format(MATCH_FORMAT, lhs.name(), rhs.name()); |
| 156 | |
| 157 | return fields.length == 2 && fields[1].contains(cps); |
| 158 | } |
| 159 | } |