Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 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 | package org.onosproject.ofagent.cli; |
| 17 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onlab.packet.TpPort; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 25 | import org.onosproject.ofagent.api.OFAgent; |
| 26 | import org.onosproject.ofagent.api.OFAgentAdminService; |
| 27 | import org.onosproject.ofagent.api.OFAgentService; |
| 28 | import org.onosproject.ofagent.impl.DefaultOFAgent; |
| 29 | import org.onosproject.ofagent.impl.DefaultOFController; |
| 30 | |
| 31 | /** |
| 32 | * Removes the controller from the OFAgent. |
| 33 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 34 | @Service |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 35 | @Command(scope = "onos", name = "ofagent-controller-delete", |
| 36 | description = "Deletes a controller from the ofagent") |
| 37 | public class OFAgentDeleteControllerCommand extends AbstractShellCommand { |
| 38 | |
| 39 | private static final String PATTERN_IP_PORT = "\\d{1,3}(?:\\.\\d{1,3}){3}(?::\\d{1,5})"; |
| 40 | |
| 41 | @Argument(index = 0, name = "network", description = "Virtual network ID", |
| 42 | required = true, multiValued = false) |
| 43 | private long networkId = NetworkId.NONE.id(); |
| 44 | |
| 45 | @Argument(index = 1, name = "controller", |
| 46 | description = "External controller with IP:PORT format", |
| 47 | required = true, multiValued = false) |
| 48 | private String strCtrl; |
| 49 | |
| 50 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 51 | protected void doExecute() { |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 52 | if (!isValidController(strCtrl)) { |
| 53 | error("Invalid controller string %s, must be IP:PORT", strCtrl); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | OFAgentService service = get(OFAgentService.class); |
| 58 | OFAgentAdminService adminService = get(OFAgentAdminService.class); |
| 59 | |
| 60 | OFAgent existing = service.agent(NetworkId.networkId(networkId)); |
| 61 | if (existing == null) { |
| 62 | error("OFAgent for network %s does not exist", networkId); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | String[] temp = strCtrl.split(":"); |
| 67 | OFAgent updated = DefaultOFAgent.builder() |
| 68 | .from(existing) |
| 69 | .deleteController(DefaultOFController.of( |
| 70 | IpAddress.valueOf(temp[0]), |
| 71 | TpPort.tpPort(Integer.valueOf(temp[1])))) |
| 72 | .build(); |
| 73 | adminService.updateAgent(updated); |
| 74 | } |
| 75 | |
| 76 | private boolean isValidController(String ctrl) { |
| 77 | if (!ctrl.matches(PATTERN_IP_PORT)) { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | String[] temp = ctrl.split(":"); |
| 82 | try { |
| 83 | IpAddress.valueOf(temp[0]); |
| 84 | TpPort.tpPort(Integer.valueOf(temp[1])); |
| 85 | return true; |
| 86 | } catch (IllegalArgumentException e) { |
| 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | } |