Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [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 org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Service; |
| 22 | import org.onosproject.drivers.microsemi.yang.MseaSaFilteringNetconfService; |
| 23 | import org.onosproject.netconf.DatastoreId; |
| 24 | import org.onosproject.netconf.NetconfException; |
| 25 | import org.onosproject.netconf.NetconfSession; |
| 26 | import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.MseaSaFiltering; |
| 27 | import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.MseaSaFilteringOpParam; |
| 28 | import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.mseasafiltering.SourceIpaddressFiltering; |
| 29 | import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.mseasafiltering.sourceipaddressfiltering.interfaceeth0.SourceAddressRange; |
| 30 | import org.onosproject.yang.model.DefaultModelObjectData; |
| 31 | import org.onosproject.yang.model.ModelConverter; |
| 32 | import org.onosproject.yang.model.ModelObject; |
| 33 | import org.onosproject.yang.model.ModelObjectData; |
| 34 | import org.onosproject.yang.model.ResourceId; |
| 35 | import org.onosproject.yang.runtime.AnnotatedNodeInfo; |
| 36 | import org.onosproject.yang.runtime.CompositeData; |
| 37 | import org.onosproject.yang.runtime.DefaultAnnotatedNodeInfo; |
| 38 | import org.onosproject.yang.runtime.DefaultAnnotation; |
| 39 | import org.onosproject.yang.runtime.DefaultCompositeStream; |
| 40 | |
| 41 | import java.io.ByteArrayInputStream; |
| 42 | import java.util.ArrayList; |
| 43 | import java.util.List; |
| 44 | |
| 45 | /** |
| 46 | * Implementation of the MseaSaFiltering YANG model service. |
| 47 | */ |
| 48 | @Component(immediate = true, inherit = true) |
| 49 | @Service |
| 50 | public class MseaSaFilteringManager extends AbstractYangServiceImpl |
| 51 | implements MseaSaFilteringNetconfService { |
| 52 | public static final String MSEA_SA_FILTERING = |
| 53 | "org.onosproject.drivers.microsemi.yang.mseasafiltering"; |
| 54 | public static final String MSEA_SA_FILTERING_NS = |
| 55 | "http://www.microsemi.com/microsemi-edge-assure/msea-sa-filtering"; |
| 56 | |
| 57 | @Activate |
| 58 | public void activate() { |
| 59 | super.activate(); |
| 60 | appId = coreService.registerApplication(MSEA_SA_FILTERING); |
| 61 | log.info("MseaSaFilteringManager Started"); |
| 62 | } |
| 63 | |
| 64 | @Deactivate |
| 65 | public void deactivate() { |
| 66 | super.deactivate(); |
| 67 | log.info("MseaSaFilteringManager Stopped"); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get a filtered subset of the model. |
| 72 | * This is meant to filter the current live model |
| 73 | * against the attribute(s) given in the argument |
| 74 | * and return the filtered model. |
| 75 | */ |
| 76 | @Override |
| 77 | public MseaSaFiltering getMseaSaFiltering( |
| 78 | MseaSaFilteringOpParam mseaSaFilteringFilter, NetconfSession session) |
| 79 | throws NetconfException { |
| 80 | ModelObjectData moQuery = DefaultModelObjectData.builder() |
| 81 | .addModelObject((ModelObject) mseaSaFilteringFilter |
| 82 | .sourceIpaddressFiltering()) |
| 83 | .build(); |
| 84 | |
| 85 | |
| 86 | ModelObjectData moReply = getNetconfObject(moQuery, session); |
| 87 | |
| 88 | MseaSaFiltering reply = new MseaSaFilteringOpParam(); |
| 89 | for (ModelObject mo:moReply.modelObjects()) { |
| 90 | if (mo instanceof SourceIpaddressFiltering) { |
| 91 | reply.sourceIpaddressFiltering((SourceIpaddressFiltering) mo); |
| 92 | } |
| 93 | } |
| 94 | return reply; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get a filtered subset of the config model (from running) |
| 99 | * This is meant to filter the current live model |
| 100 | * against the attribute(s) given in the argument |
| 101 | * and return the filtered model. |
| 102 | */ |
| 103 | @Override |
| 104 | public List<SourceAddressRange> getConfigMseaSaFilterIds(NetconfSession session) |
| 105 | throws NetconfException { |
| 106 | |
| 107 | String xmlResult = session.getConfig(DatastoreId.RUNNING, saFilterQuery()); |
| 108 | xmlResult = removeRpcReplyData(xmlResult); |
| 109 | |
| 110 | DefaultCompositeStream resultDcs = new DefaultCompositeStream( |
| 111 | null, new ByteArrayInputStream(xmlResult.getBytes())); |
| 112 | CompositeData compositeData = xSer.decode(resultDcs, yCtx); |
| 113 | |
| 114 | ModelObjectData moReply = ((ModelConverter) yangModelRegistry).createModel(compositeData.resourceData()); |
| 115 | |
| 116 | MseaSaFiltering reply = new MseaSaFilteringOpParam(); |
| 117 | for (ModelObject mo:moReply.modelObjects()) { |
| 118 | if (mo instanceof SourceIpaddressFiltering) { |
| 119 | reply.sourceIpaddressFiltering((SourceIpaddressFiltering) mo); |
| 120 | } |
| 121 | } |
| 122 | if (reply != null && reply.sourceIpaddressFiltering() != null && |
| 123 | reply.sourceIpaddressFiltering().interfaceEth0() != null) { |
| 124 | return reply.sourceIpaddressFiltering().interfaceEth0().sourceAddressRange(); |
| 125 | } else { |
| 126 | return new ArrayList<SourceAddressRange>(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Call NETCONF edit-config with a configuration. |
| 132 | */ |
| 133 | @Override |
| 134 | public boolean setMseaSaFiltering(MseaSaFilteringOpParam mseaSaFiltering, |
| 135 | NetconfSession session, DatastoreId ncDs) throws NetconfException { |
| 136 | |
| 137 | ModelObjectData moQuery = DefaultModelObjectData.builder() |
| 138 | .addModelObject((ModelObject) mseaSaFiltering |
| 139 | .sourceIpaddressFiltering()).build(); |
| 140 | |
| 141 | return setNetconfObject(moQuery, session, ncDs, null); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public boolean deleteMseaSaFilteringRange(MseaSaFilteringOpParam mseaSaFiltering, |
| 146 | NetconfSession session, DatastoreId ncDs) throws NetconfException { |
| 147 | |
| 148 | ModelObjectData moQuery = DefaultModelObjectData.builder() |
| 149 | .addModelObject((ModelObject) mseaSaFiltering |
| 150 | .sourceIpaddressFiltering()).build(); |
| 151 | |
| 152 | ArrayList anis = new ArrayList<AnnotatedNodeInfo>(); |
| 153 | if (mseaSaFiltering.sourceIpaddressFiltering().interfaceEth0() != null && |
| 154 | mseaSaFiltering.sourceIpaddressFiltering().interfaceEth0().sourceAddressRange() != null) { |
| 155 | |
| 156 | for (SourceAddressRange sar:mseaSaFiltering.sourceIpaddressFiltering() |
| 157 | .interfaceEth0().sourceAddressRange()) { |
| 158 | String sarRangeIdStr = String.valueOf(sar.rangeId()); |
| 159 | |
| 160 | ResourceId.Builder ridBuilder = ResourceId.builder() |
| 161 | .addBranchPointSchema("/", null) |
| 162 | .addBranchPointSchema("source-ipaddress-filtering", MSEA_SA_FILTERING_NS) |
| 163 | .addBranchPointSchema("interface-eth0", MSEA_SA_FILTERING_NS) |
| 164 | .addBranchPointSchema("source-address-range", MSEA_SA_FILTERING_NS) |
| 165 | .addKeyLeaf("range-id", MSEA_SA_FILTERING_NS, sarRangeIdStr); |
| 166 | |
| 167 | AnnotatedNodeInfo ani = DefaultAnnotatedNodeInfo.builder() |
| 168 | .resourceId(ridBuilder.build()) |
| 169 | .addAnnotation(new DefaultAnnotation(NC_OPERATION, OP_DELETE)) |
| 170 | .build(); |
| 171 | |
| 172 | anis.add(ani); |
| 173 | } |
| 174 | } else { |
| 175 | //Delete all |
| 176 | ResourceId.Builder ridBuilder = ResourceId.builder() |
| 177 | .addBranchPointSchema("/", null) |
| 178 | .addBranchPointSchema("source-ipaddress-filtering", MSEA_SA_FILTERING_NS); |
| 179 | AnnotatedNodeInfo ani = DefaultAnnotatedNodeInfo.builder() |
| 180 | .resourceId(ridBuilder.build()) |
| 181 | .addAnnotation(new DefaultAnnotation(NC_OPERATION, OP_DELETE)) |
| 182 | .build(); |
| 183 | anis.add(ani); |
| 184 | } |
| 185 | |
| 186 | return setNetconfObject(moQuery, session, ncDs, anis); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | private static String saFilterQuery() { |
| 191 | StringBuilder sb = new StringBuilder("<source-ipaddress-filtering " + |
| 192 | "xmlns=\"http://www.microsemi.com/microsemi-edge-assure/msea-sa-filtering\">"); |
| 193 | sb.append("<interface-eth0>"); |
| 194 | sb.append("<filter-admin-state>blacklist</filter-admin-state>"); |
| 195 | sb.append("<source-address-range>"); |
| 196 | sb.append("<range-id/>"); |
| 197 | sb.append("</source-address-range>"); |
| 198 | sb.append("</interface-eth0>"); |
| 199 | sb.append("</source-ipaddress-filtering>"); |
| 200 | return sb.toString(); |
| 201 | } |
| 202 | } |