Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | package org.onosproject.drivers.microsemi.yang.impl; |
| 17 | |
| 18 | import static org.onosproject.yms.ych.YangProtocolEncodingFormat.XML; |
| 19 | import static org.onosproject.yms.ydt.YmsOperationType.QUERY_CONFIG_REPLY; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Service; |
| 29 | import org.onosproject.drivers.microsemi.yang.MseaUniEvcServiceNetconfService; |
| 30 | import org.onosproject.netconf.NetconfException; |
| 31 | import org.onosproject.netconf.NetconfSession; |
| 32 | import org.onosproject.netconf.TargetConfig; |
| 33 | import org.onosproject.yang.gen.v1.http.www.microsemi.com.microsemi.edge.assure.msea.uni.evc.service.rev20160317.MseaUniEvcService; |
| 34 | import org.onosproject.yang.gen.v1.http.www.microsemi.com.microsemi.edge.assure.msea.uni.evc.service.rev20160317.MseaUniEvcServiceOpParam; |
| 35 | import org.onosproject.yang.gen.v1.http.www.microsemi.com.microsemi.edge.assure.msea.uni.evc.service.rev20160317.MseaUniEvcServiceService; |
| 36 | import org.onosproject.yang.gen.v1.http.www.microsemi.com.microsemi.edge.assure.msea.uni.evc.service.rev20160317.mseaunievcservice.mefservices.uni.UniSideInterfaceAssignmentEnum; |
| 37 | |
| 38 | /** |
| 39 | * Implementation of the MseaUniEvcServiceService YANG model service. |
| 40 | */ |
| 41 | @Component(immediate = true, inherit = true) |
| 42 | @Service |
| 43 | public class MseaUniEvcServiceManager extends AbstractYangServiceImpl |
| 44 | implements MseaUniEvcServiceNetconfService { |
| 45 | public static final String MSEA_SA_FILTERING = "org.onosproject.drivers.microsemi.yang.mseaunievcservice"; |
| 46 | |
| 47 | @Activate |
| 48 | public void activate() { |
| 49 | appId = coreService.registerApplication(MSEA_SA_FILTERING); |
| 50 | ych = ymsService.getYangCodecHandler(); |
| 51 | ych.addDeviceSchema(MseaUniEvcServiceService.class); |
| 52 | log.info("MseaUniEvcServiceManager Started"); |
| 53 | } |
| 54 | |
| 55 | @Deactivate |
| 56 | public void deactivate() { |
| 57 | super.deactivate(); |
| 58 | ymsService.unRegisterService(this, MseaUniEvcServiceService.class); |
| 59 | ych = null; |
| 60 | log.info("MseaUniEvcServiceManager Stopped"); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public MseaUniEvcService getMseaUniEvcService( |
| 65 | MseaUniEvcServiceOpParam mseaUniEvcService, NetconfSession session) throws NetconfException { |
| 66 | return (MseaUniEvcService) getNetconfObject(mseaUniEvcService, session); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public MseaUniEvcService getConfigMseaUniEvcService( |
| 71 | MseaUniEvcServiceOpParam mseaUniEvcService, NetconfSession session, TargetConfig targetDs) |
| 72 | throws NetconfException { |
| 73 | return (MseaUniEvcService) getConfigNetconfObject(mseaUniEvcService, session, targetDs); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Modify the configuration. |
| 78 | */ |
| 79 | @Override |
| 80 | public void setMseaUniEvcService( |
| 81 | MseaUniEvcServiceOpParam mseaUniEvcService, NetconfSession session, TargetConfig ncDs) |
| 82 | throws NetconfException { |
| 83 | setNetconfObject(mseaUniEvcService, session, ncDs); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public MseaUniEvcService getmseaUniEvcCeVlanMaps( |
| 88 | NetconfSession session, TargetConfig ncDs) |
| 89 | throws NetconfException { |
| 90 | if (session == null) { |
| 91 | throw new NetconfException("Session is null when calling getMseaSaFiltering()"); |
| 92 | } |
| 93 | |
| 94 | String xmlResult = session.getConfig(ncDs, evcFilterQuery()); |
| 95 | |
| 96 | List<Object> objectList = ych.decode(xmlResult, XML, QUERY_CONFIG_REPLY); |
| 97 | if (objectList != null && objectList.size() > 0) { |
| 98 | return (MseaUniEvcService) objectList.get(0); |
| 99 | } |
| 100 | |
| 101 | return null; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void removeEvcUniFlowEntries( |
| 106 | Map<Integer, String> ceVlanUpdates, |
| 107 | Map<Integer, List<Short>> flowVlanIds, |
| 108 | NetconfSession session, TargetConfig targetDs, |
| 109 | UniSideInterfaceAssignmentEnum portAssign) throws NetconfException { |
| 110 | |
| 111 | List<Integer> evcAlreadyHandled = new ArrayList<>(); |
| 112 | StringBuilder xmlEvcUpdate = new StringBuilder(evcUniOpener()); |
| 113 | for (Integer evcKey:ceVlanUpdates.keySet()) { |
| 114 | int evcId = (evcKey & ((1 << 8) - 100)) >> 2; |
| 115 | if (evcAlreadyHandled.contains(new Integer(evcId))) { |
| 116 | continue; |
| 117 | } |
| 118 | evcAlreadyHandled.add(evcId); |
| 119 | int port = (evcKey & 3); |
| 120 | String ceVlanMapThis = ceVlanUpdates.get(evcKey); |
| 121 | String ceVlanMapOpposite = ceVlanUpdates.get(evcKey ^ 1); |
| 122 | |
| 123 | if ((ceVlanMapThis == null || ceVlanMapThis.isEmpty()) && |
| 124 | (ceVlanMapOpposite == null || ceVlanMapOpposite.isEmpty())) { |
| 125 | xmlEvcUpdate.append("<evc nc:operation=\"delete\">\n<evc-index>"); |
| 126 | xmlEvcUpdate.append(Integer.toString(evcId)); |
| 127 | xmlEvcUpdate.append("</evc-index>\n</evc>\n"); |
| 128 | } else { |
| 129 | xmlEvcUpdate.append("<evc>\n<evc-index>"); |
| 130 | xmlEvcUpdate.append(Integer.toString(evcId)); |
| 131 | xmlEvcUpdate.append("</evc-index>\n<evc-per-uni>\n"); |
| 132 | if (port == 0 && portAssign == UniSideInterfaceAssignmentEnum.UNI_C_ON_OPTICS || |
| 133 | port == 1 && portAssign == UniSideInterfaceAssignmentEnum.UNI_C_ON_HOST) { |
| 134 | if (ceVlanMapThis != null) { |
| 135 | xmlEvcUpdate.append("<evc-per-uni-c>\n<ce-vlan-map nc:operation=\"replace\">"); |
| 136 | xmlEvcUpdate.append(ceVlanMapThis); |
| 137 | xmlEvcUpdate.append("</ce-vlan-map>\n"); |
| 138 | xmlEvcUpdate.append(deleteFlowMapping(flowVlanIds.get(evcKey))); |
| 139 | xmlEvcUpdate.append("</evc-per-uni-c>\n"); |
| 140 | } |
| 141 | if (ceVlanMapOpposite != null) { |
| 142 | xmlEvcUpdate.append("<evc-per-uni-n>\n<ce-vlan-map nc:operation=\"replace\">"); |
| 143 | xmlEvcUpdate.append(ceVlanMapOpposite); |
| 144 | xmlEvcUpdate.append("</ce-vlan-map>\n"); |
| 145 | xmlEvcUpdate.append(deleteFlowMapping(flowVlanIds.get(evcKey ^ 1))); |
| 146 | xmlEvcUpdate.append("</evc-per-uni-n>\n"); |
| 147 | } |
| 148 | } else { |
| 149 | if (ceVlanMapThis != null) { |
| 150 | xmlEvcUpdate.append("<evc-per-uni-n>\n<ce-vlan-map nc:operation=\"replace\">"); |
| 151 | xmlEvcUpdate.append(ceVlanMapThis); |
| 152 | xmlEvcUpdate.append("</ce-vlan-map>\n"); |
| 153 | xmlEvcUpdate.append(deleteFlowMapping(flowVlanIds.get(evcKey))); |
| 154 | xmlEvcUpdate.append("</evc-per-uni-n>\n"); |
| 155 | } |
| 156 | if (ceVlanMapOpposite != null) { |
| 157 | xmlEvcUpdate.append("<evc-per-uni-c>\n<ce-vlan-map nc:operation=\"replace\">"); |
| 158 | xmlEvcUpdate.append(ceVlanMapOpposite); |
| 159 | xmlEvcUpdate.append("</ce-vlan-map>\n"); |
| 160 | xmlEvcUpdate.append(deleteFlowMapping(flowVlanIds.get(evcKey ^ 1))); |
| 161 | xmlEvcUpdate.append("</evc-per-uni-c>\n"); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | xmlEvcUpdate.append("</evc-per-uni>\n</evc>\n"); |
| 166 | } |
| 167 | } |
| 168 | xmlEvcUpdate.append("</uni>\n</mef-services>"); |
| 169 | |
| 170 | log.info("Sending XML <edit-config> on NETCONF session " + session.getSessionId() + |
| 171 | ":\n" + xmlEvcUpdate.toString()); |
| 172 | |
| 173 | |
| 174 | session.editConfig(targetDs, null, xmlEvcUpdate.toString()); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | private static String deleteFlowMapping(List<Short> vlanIds) { |
| 179 | if (vlanIds == null || vlanIds.size() == 0) { |
| 180 | return ""; |
| 181 | } |
| 182 | StringBuilder fmXmlBuilder = new StringBuilder(); |
| 183 | for (long vlanId:vlanIds) { |
| 184 | fmXmlBuilder.append("<flow-mapping nc:operation=\"delete\">\n"); |
| 185 | fmXmlBuilder.append("<ce-vlan-id>"); |
| 186 | fmXmlBuilder.append(String.valueOf(vlanId)); |
| 187 | fmXmlBuilder.append("</ce-vlan-id>\n"); |
| 188 | fmXmlBuilder.append("</flow-mapping>\n"); |
| 189 | } |
| 190 | |
| 191 | return fmXmlBuilder.toString(); |
| 192 | } |
| 193 | |
| 194 | private String evcFilterQuery() { |
| 195 | StringBuilder sb = new StringBuilder("<mef-services " |
| 196 | + "xmlns=\"http://www.microsemi.com/microsemi-edge-assure/msea-uni-evc-service\">"); |
| 197 | sb.append("<uni>"); |
| 198 | sb.append("<evc>"); |
| 199 | sb.append("<evc-index/>"); |
| 200 | sb.append("<evc-per-uni>"); |
| 201 | sb.append("<evc-per-uni-c>"); |
| 202 | sb.append("<ce-vlan-map/>"); |
| 203 | sb.append("<flow-mapping/>"); |
| 204 | sb.append("<ingress-bwp-group-index/>"); |
| 205 | sb.append("</evc-per-uni-c>"); |
| 206 | sb.append("<evc-per-uni-n>"); |
| 207 | sb.append("<ce-vlan-map/>"); |
| 208 | sb.append("<flow-mapping/>"); |
| 209 | sb.append("<ingress-bwp-group-index/>"); |
| 210 | sb.append("</evc-per-uni-n>"); |
| 211 | sb.append("</evc-per-uni>"); |
| 212 | sb.append("</evc>"); |
| 213 | sb.append("</uni>"); |
| 214 | sb.append("</mef-services>"); |
| 215 | |
| 216 | return sb.toString(); |
| 217 | } |
| 218 | |
| 219 | private String evcUniOpener() { |
| 220 | StringBuilder sb = new StringBuilder("<mef-services "); |
| 221 | sb.append("xmlns=\"http://www.microsemi.com/microsemi-edge-assure/msea-uni-evc-service\">\n"); |
| 222 | sb.append("<uni>\n"); |
| 223 | |
| 224 | return sb.toString(); |
| 225 | } |
| 226 | } |