Andrea Campanella | a9cdaf6 | 2019-09-08 10:30:16 -0700 | [diff] [blame] | 1 | /* |
Sudeep Desai | bffe153 | 2020-01-21 16:43:46 +0530 | [diff] [blame] | 2 | * Copyright 2020-present Open Networking Foundation |
Andrea Campanella | a9cdaf6 | 2019-09-08 10:30:16 -0700 | [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 | * This work was partially supported by EC H2020 project METRO-HAUL (761727). |
| 17 | */ |
| 18 | |
| 19 | package org.onosproject.drivers.odtn; |
| 20 | |
Sudeep Desai | bffe153 | 2020-01-21 16:43:46 +0530 | [diff] [blame] | 21 | import org.onlab.util.Frequency; |
Andrea Campanella | a9cdaf6 | 2019-09-08 10:30:16 -0700 | [diff] [blame] | 22 | import org.onosproject.drivers.odtn.impl.FlowRuleParser; |
Sudeep Desai | bffe153 | 2020-01-21 16:43:46 +0530 | [diff] [blame] | 23 | import org.onosproject.drivers.odtn.openconfig.AbstractTerminalDeviceFlowRuleProgrammable; |
Andrea Campanella | a9cdaf6 | 2019-09-08 10:30:16 -0700 | [diff] [blame] | 24 | import org.onosproject.net.flow.FlowRule; |
Andrea Campanella | a9cdaf6 | 2019-09-08 10:30:16 -0700 | [diff] [blame] | 25 | import org.onosproject.netconf.DatastoreId; |
Andrea Campanella | a9cdaf6 | 2019-09-08 10:30:16 -0700 | [diff] [blame] | 26 | import org.onosproject.netconf.NetconfException; |
| 27 | import org.onosproject.netconf.NetconfSession; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
Andrea Campanella | a9cdaf6 | 2019-09-08 10:30:16 -0700 | [diff] [blame] | 31 | import java.util.regex.Matcher; |
| 32 | import java.util.regex.Pattern; |
| 33 | |
Andrea Campanella | a9cdaf6 | 2019-09-08 10:30:16 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Implementation of FlowRuleProgrammable interface for |
| 36 | * OpenConfig terminal devices. |
| 37 | */ |
| 38 | public class GrooveOpenConfigFlowRuleProgrammable |
Sudeep Desai | bffe153 | 2020-01-21 16:43:46 +0530 | [diff] [blame] | 39 | extends AbstractTerminalDeviceFlowRuleProgrammable { |
| 40 | |
Andrea Campanella | a9cdaf6 | 2019-09-08 10:30:16 -0700 | [diff] [blame] | 41 | |
| 42 | private static final Logger log = |
| 43 | LoggerFactory.getLogger(GrooveOpenConfigFlowRuleProgrammable.class); |
| 44 | |
Sudeep Desai | bffe153 | 2020-01-21 16:43:46 +0530 | [diff] [blame] | 45 | public void setOpticalChannelFrequency(NetconfSession session, |
| 46 | String optChannel, Frequency freq) throws NetconfException { |
Andrea Campanella | a9cdaf6 | 2019-09-08 10:30:16 -0700 | [diff] [blame] | 47 | |
Sudeep Desai | bffe153 | 2020-01-21 16:43:46 +0530 | [diff] [blame] | 48 | final String transceiver = optChannel.replace("OCH", "TRANSCEIVER"); |
| 49 | final StringBuilder sb = new StringBuilder(); |
| 50 | sb.append( |
| 51 | "<components xmlns='http://openconfig.net/yang/platform'>" |
| 52 | + "<component>" |
| 53 | + "<name>" + optChannel + "</name>" |
| 54 | + "<oc-opt-term:optical-channel " |
| 55 | + |
| 56 | " xmlns:oc-opt-term='http://openconfig.net/yang/terminal-device'>" |
| 57 | + " <oc-opt-term:config>" |
| 58 | + " <oc-opt-term:frequency>" + (long) freq.asMHz() + "</oc-opt-term:frequency>" |
| 59 | + " </oc-opt-term:config>" |
| 60 | + " </oc-opt-term:optical-channel>" |
| 61 | + "</component>" |
| 62 | + "<component>" |
| 63 | + " <name>" + transceiver + "</name>" |
| 64 | + " <transceiver xmlns='http://openconfig.net/yang/platform/transceiver'>" |
| 65 | + " <config>" |
| 66 | + " <enabled>true</enabled>" |
| 67 | + " </config>" |
| 68 | + " </transceiver>" |
| 69 | + " </component>" |
| 70 | + "</components>"); |
Andrea Campanella | a9cdaf6 | 2019-09-08 10:30:16 -0700 | [diff] [blame] | 71 | |
Sudeep Desai | bffe153 | 2020-01-21 16:43:46 +0530 | [diff] [blame] | 72 | final boolean ok = |
| 73 | session.editConfig(DatastoreId.RUNNING, null, sb.toString()); |
Andrea Campanella | a9cdaf6 | 2019-09-08 10:30:16 -0700 | [diff] [blame] | 74 | |
Sudeep Desai | bffe153 | 2020-01-21 16:43:46 +0530 | [diff] [blame] | 75 | if (!ok) { |
| 76 | throw new NetconfException("error writing channel frequency"); |
Andrea Campanella | a9cdaf6 | 2019-09-08 10:30:16 -0700 | [diff] [blame] | 77 | } |
Andrea Campanella | a9cdaf6 | 2019-09-08 10:30:16 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Sudeep Desai | bffe153 | 2020-01-21 16:43:46 +0530 | [diff] [blame] | 80 | //Overloaded setOpticalChannelFrequency() with two param as required for removeFlowRule() |
Andrea Campanella | a9cdaf6 | 2019-09-08 10:30:16 -0700 | [diff] [blame] | 81 | private void setOpticalChannelFrequency(NetconfSession session, StringBuilder sb) throws NetconfException { |
| 82 | |
| 83 | final boolean ok = |
| 84 | session.editConfig(DatastoreId.RUNNING, null, sb.toString()); |
| 85 | |
| 86 | if (!ok) { |
| 87 | throw new NetconfException("error writing channel frequency"); |
| 88 | } |
| 89 | } |
| 90 | |
Sudeep Desai | bffe153 | 2020-01-21 16:43:46 +0530 | [diff] [blame] | 91 | @Override |
Andrea Campanella | a9cdaf6 | 2019-09-08 10:30:16 -0700 | [diff] [blame] | 92 | protected String removeFlowRule(NetconfSession session, FlowRule r) |
| 93 | throws NetconfException { |
| 94 | FlowRuleParser frp = new FlowRuleParser(r); |
| 95 | if (!frp.isReceiver()) { |
| 96 | String optChannel = getOpticalChannel(frp.getPortNumber()); |
| 97 | final String transceiver = optChannel.replace("OCH", "TRANSCEIVER"); |
| 98 | final StringBuilder sb = new StringBuilder(); |
| 99 | /* |
| 100 | TODO imcomplete solution: doesn't set frequency to zero; see below |
| 101 | sb.append( |
| 102 | "<components xmlns='http://openconfig.net/yang/platform'>" |
| 103 | + "<component>" |
| 104 | + " <name>" + transceiver + "</name>" |
| 105 | + " <transceiver xmlns='http://openconfig.net/yang/platform/transceiver'>" |
| 106 | + " <config>" |
| 107 | + " <enabled>false</enabled>" |
| 108 | + " </config>" |
| 109 | + " </transceiver>" |
| 110 | + " </component>" |
| 111 | + "</components>"); |
| 112 | |
| 113 | */ |
| 114 | |
| 115 | // NOTE: Disabling the laser via openconfig on Groove is not possible |
| 116 | // and consequently the frequency can't be set to 0. |
| 117 | // This is an error on mapping between native and openconfig model. |
| 118 | |
| 119 | // Workaround: use legacy model |
| 120 | Pattern transceiverPattern = Pattern.compile(".*-[1]-[1-9][0-4]?-L[1-2]$"); // e.g. TRANSCEIVER-1-1-L1 |
| 121 | Matcher lineMatch = transceiverPattern.matcher(transceiver); |
| 122 | lineMatch.find(); |
| 123 | final String[] split = lineMatch.group(0).split("-"); |
| 124 | |
| 125 | sb.append("<ne xmlns=\"http://coriant.com/yang/os/ne\">" + |
| 126 | " <shelf>" + |
| 127 | " <shelf-id>" + split[1] + "</shelf-id>" + |
| 128 | " <slot>" + |
| 129 | " <slot-id>" + split[2] + "</slot-id>" + |
| 130 | " <card>" + |
| 131 | " <port>" + |
| 132 | " <port-id>" + split[3].replace("L", "") + "</port-id>" + |
| 133 | " <och-os>" + |
| 134 | " <frequency>0</frequency>" + |
| 135 | " <laser-enable>disabled</laser-enable>" + |
| 136 | " </och-os>" + |
| 137 | " </port>" + |
| 138 | " </card>" + |
| 139 | " </slot>" + |
| 140 | " </shelf>" + |
| 141 | " </ne>"); |
| 142 | |
| 143 | setOpticalChannelFrequency(session, sb); |
| 144 | return optChannel + ":" + frp.getCentralFrequency().asGHz(); |
| 145 | } |
| 146 | return String.valueOf(frp.getCentralFrequency().asGHz()); |
| 147 | } |
| 148 | } |