Samir Anand | 01c77c4 | 2015-08-06 14:50:29 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 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.provider.netconf.flow.impl; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import org.jdom2.Document; |
| 21 | import org.jdom2.Element; |
| 22 | import org.jdom2.Namespace; |
| 23 | import org.jdom2.output.Format; |
| 24 | import org.jdom2.output.XMLOutputter; |
| 25 | import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.AccessList; |
| 26 | import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.access.list.access.list.entries.matches.AceType; |
| 27 | import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.access.list.access.list.entries.matches.ace.type.AceEth; |
| 28 | import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.access.list.access.list.entries.matches.ace.type.AceIp; |
| 29 | import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.access.list.access.list.entries.matches.ace.type.ace.ip.AceIpVersion; |
| 30 | import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.access.list.access.list.entries.matches.ace.type.ace.ip.ace.ip.version.AceIpv4; |
| 31 | import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.access.list.access.list.entries.matches.ace.type.ace.ip.ace.ip.version.AceIpv6; |
| 32 | import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber; |
| 33 | import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.packet.fields.rev140625.acl.transport.header.fields.DestinationPortRange; |
| 34 | import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.packet.fields.rev140625.acl.transport.header.fields.SourcePortRange; |
| 35 | import org.slf4j.Logger; |
| 36 | |
| 37 | /** |
| 38 | * Xml Builder to generate the xml according to given ACL model. |
| 39 | */ |
| 40 | public class XmlBuilder { |
| 41 | private final Logger log = getLogger(XmlBuilder.class); |
| 42 | |
| 43 | public String buildAclRequestXml(AccessList accessList) { |
| 44 | Document doc = new Document(); |
| 45 | Namespace namespaceRpc = Namespace |
| 46 | .getNamespace("urn:ietf:params:xml:ns:netconf:base:1.0"); |
| 47 | Namespace accessNamespaceRpc = Namespace |
| 48 | .getNamespace("urn:ietf:params:xml:ns:yang:ietf-acl"); |
| 49 | doc.setRootElement(new Element("rpc", namespaceRpc) |
| 50 | .setAttribute("message-id", "101")); |
| 51 | |
| 52 | /** |
| 53 | * Access list elements of given ACL model. |
| 54 | */ |
| 55 | Element access = new Element("access-list", accessNamespaceRpc); |
| 56 | access.addContent(new Element("acl-name", accessNamespaceRpc) |
| 57 | .setText(accessList.getAclName())); |
| 58 | // access.addContent(accessEntries); |
| 59 | |
| 60 | if (!accessList.getAccessListEntries().isEmpty() |
| 61 | && accessList.getAccessListEntries() != null) { |
| 62 | for (int accessEntryIntVlu = 0; accessEntryIntVlu < accessList |
| 63 | .getAccessListEntries().size(); accessEntryIntVlu++) { |
| 64 | access.addContent(getAccessEntries(accessEntryIntVlu, |
| 65 | accessList, |
| 66 | accessNamespaceRpc)); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * edit-config operation for given ACL model. |
| 72 | */ |
| 73 | Element editConfig = new Element("edit-config", namespaceRpc); |
| 74 | editConfig.addContent(new Element("target", namespaceRpc) |
| 75 | .addContent(new Element("running", namespaceRpc))); |
| 76 | editConfig.addContent(new Element("config", Namespace |
| 77 | .getNamespace("urn:ietf:params:xml:ns:netconf:base:1.0")) |
| 78 | .addContent(access)); |
| 79 | |
| 80 | doc.getRootElement().addContent(editConfig); |
| 81 | XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat()); |
| 82 | String outputString = xmlOutputter.outputString(doc); |
| 83 | |
| 84 | return outputString; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * access entries operation for given ACL model. |
| 89 | */ |
| 90 | private Element getAccessEntries(int accessEntryIntVlu, |
| 91 | AccessList accessList, |
| 92 | Namespace accessNamespaceRpc) { |
| 93 | |
| 94 | /** |
| 95 | * Port Number |
| 96 | */ |
| 97 | |
| 98 | int srcPortRangeLower = 0; |
| 99 | int srcPortRangeUpper = 0; |
| 100 | int destPortRangeLower = 0; |
| 101 | int destPortRangeUpper = 0; |
| 102 | |
| 103 | String sourceIpAdd = ""; |
| 104 | String destinationIpAdd = ""; |
| 105 | |
| 106 | /* |
| 107 | * checking accessList is null or not |
| 108 | */ |
| 109 | if (accessList != null) { |
| 110 | /* |
| 111 | * checking list entries are empty or null |
| 112 | */ |
| 113 | if (!accessList.getAccessListEntries().isEmpty() |
| 114 | && accessList.getAccessListEntries() != null) { |
| 115 | AceType aceType = accessList.getAccessListEntries() |
| 116 | .get(accessEntryIntVlu).getMatches().getAceType(); |
| 117 | |
| 118 | if (aceType instanceof AceIp) { |
| 119 | AceIp aceIp = (AceIp) aceType; |
| 120 | SourcePortRange sourcePortRange = aceIp |
| 121 | .getSourcePortRange(); |
| 122 | if (sourcePortRange != null) { |
| 123 | PortNumber lowerPort = sourcePortRange.getLowerPort(); |
| 124 | PortNumber upperPort = sourcePortRange.getUpperPort(); |
| 125 | |
| 126 | if (lowerPort != null) { |
| 127 | srcPortRangeLower = lowerPort.getValue(); |
| 128 | } |
| 129 | if (upperPort != null) { |
| 130 | srcPortRangeUpper = upperPort.getValue(); |
| 131 | } |
| 132 | } |
| 133 | DestinationPortRange destinationPortRange = aceIp |
| 134 | .getDestinationPortRange(); |
| 135 | |
| 136 | if (destinationPortRange != null) { |
| 137 | PortNumber lowerPort = destinationPortRange |
| 138 | .getLowerPort(); |
| 139 | if (lowerPort != null) { |
| 140 | destPortRangeLower = lowerPort.getValue(); |
| 141 | } |
| 142 | |
| 143 | PortNumber upperPort = destinationPortRange |
| 144 | .getUpperPort(); |
| 145 | if (upperPort != null) { |
| 146 | destPortRangeUpper = upperPort.getValue(); |
| 147 | } |
| 148 | |
| 149 | } |
| 150 | |
| 151 | AceIpVersion aceIpVersion = aceIp.getAceIpVersion(); |
| 152 | if (aceIpVersion instanceof AceIpv4) { |
| 153 | AceIpv4 obj = (AceIpv4) aceIpVersion; |
| 154 | destinationIpAdd = obj.getDestinationIpv4Address() |
| 155 | .getValue(); |
| 156 | sourceIpAdd = obj.getSourceIpv4Address().getValue(); |
| 157 | } else if (aceIpVersion instanceof AceIpv6) { |
| 158 | AceIpv6 obj = (AceIpv6) aceIpVersion; |
| 159 | destinationIpAdd = obj.getDestinationIpv6Address() |
| 160 | .getValue(); |
| 161 | sourceIpAdd = obj.getSourceIpv6Address().getValue(); |
| 162 | } |
| 163 | } else if (aceType instanceof AceEth) { |
| 164 | log.debug("Need to add execution loging for Ace Type Ethernet"); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Matches elements to define IP address & Port range for given ACL |
| 171 | * model. |
| 172 | */ |
| 173 | Element matchesElement = new Element("matches", accessNamespaceRpc); |
| 174 | if (String.valueOf(srcPortRangeLower) != null |
| 175 | && !String.valueOf(srcPortRangeLower).isEmpty()) { |
| 176 | |
| 177 | matchesElement.addContent(new Element("source-port-range", |
| 178 | accessNamespaceRpc) |
| 179 | .addContent(new Element("lower-port", accessNamespaceRpc) |
| 180 | .setText(String.valueOf(srcPortRangeLower)))); |
| 181 | |
| 182 | matchesElement.addContent(new Element("source-port-range", |
| 183 | accessNamespaceRpc) |
| 184 | .addContent(new Element("upper-port", accessNamespaceRpc) |
| 185 | .setText(String.valueOf(srcPortRangeUpper)))); |
| 186 | |
| 187 | matchesElement.addContent(new Element("destination-port-range", |
| 188 | accessNamespaceRpc) |
| 189 | .addContent(new Element("lower-port", accessNamespaceRpc) |
| 190 | .setText(String.valueOf(destPortRangeLower)))); |
| 191 | |
| 192 | matchesElement.addContent(new Element("destination-port-range", |
| 193 | accessNamespaceRpc) |
| 194 | .addContent(new Element("upper-port", accessNamespaceRpc) |
| 195 | .setText(String.valueOf(destPortRangeUpper)))); |
| 196 | } |
| 197 | |
| 198 | if (destinationIpAdd != null && !destinationIpAdd.isEmpty()) { |
| 199 | matchesElement.addContent(new Element("destination-ipv4-address", |
| 200 | accessNamespaceRpc) |
| 201 | .setText(destinationIpAdd)); |
| 202 | } |
| 203 | if (sourceIpAdd != null && !sourceIpAdd.isEmpty()) { |
| 204 | matchesElement.addContent(new Element("source-ipv4-address", |
| 205 | accessNamespaceRpc) |
| 206 | .setText(sourceIpAdd)); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Access entries elements for given ACL model. |
| 211 | */ |
| 212 | Element accessEntries = new Element("access-list-entries", |
| 213 | accessNamespaceRpc); |
| 214 | accessEntries.addContent(new Element("rule-name", accessNamespaceRpc) |
| 215 | .setText(accessList.getAccessListEntries() |
| 216 | .get(accessEntryIntVlu).getRuleName())); |
| 217 | accessEntries.addContent(matchesElement); |
| 218 | accessEntries.addContent(new Element("actions", accessNamespaceRpc) |
| 219 | .addContent(new Element("deny", accessNamespaceRpc))); |
| 220 | |
| 221 | return accessEntries; |
| 222 | } |
| 223 | } |