2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.provider.netconf.flow.impl;
18 import static org.slf4j.LoggerFactory.getLogger;
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;
38 * Xml Builder to generate the xml according to given ACL model.
40 public class XmlBuilder {
41 private final Logger log = getLogger(XmlBuilder.class);
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"));
53 * Access list elements of given ACL model.
55 Element access = new Element("access-list", accessNamespaceRpc);
56 access.addContent(new Element("acl-name", accessNamespaceRpc)
57 .setText(accessList.getAclName()));
58 // access.addContent(accessEntries);
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,
71 * edit-config operation for given ACL model.
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"))
80 doc.getRootElement().addContent(editConfig);
81 XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
82 String outputString = xmlOutputter.outputString(doc);
88 * access entries operation for given ACL model.
90 private Element getAccessEntries(int accessEntryIntVlu,
91 AccessList accessList,
92 Namespace accessNamespaceRpc) {
98 int srcPortRangeLower = 0;
99 int srcPortRangeUpper = 0;
100 int destPortRangeLower = 0;
101 int destPortRangeUpper = 0;
103 String sourceIpAdd = "";
104 String destinationIpAdd = "";
107 * checking accessList is null or not
109 if (accessList != null) {
111 * checking list entries are empty or null
113 if (!accessList.getAccessListEntries().isEmpty()
114 && accessList.getAccessListEntries() != null) {
115 AceType aceType = accessList.getAccessListEntries()
116 .get(accessEntryIntVlu).getMatches().getAceType();
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();
126 if (lowerPort != null) {
127 srcPortRangeLower = lowerPort.getValue();
129 if (upperPort != null) {
130 srcPortRangeUpper = upperPort.getValue();
133 DestinationPortRange destinationPortRange = aceIp
134 .getDestinationPortRange();
136 if (destinationPortRange != null) {
137 PortNumber lowerPort = destinationPortRange
139 if (lowerPort != null) {
140 destPortRangeLower = lowerPort.getValue();
143 PortNumber upperPort = destinationPortRange
145 if (upperPort != null) {
146 destPortRangeUpper = upperPort.getValue();
151 AceIpVersion aceIpVersion = aceIp.getAceIpVersion();
152 if (aceIpVersion instanceof AceIpv4) {
153 AceIpv4 obj = (AceIpv4) aceIpVersion;
154 destinationIpAdd = obj.getDestinationIpv4Address()
156 sourceIpAdd = obj.getSourceIpv4Address().getValue();
157 } else if (aceIpVersion instanceof AceIpv6) {
158 AceIpv6 obj = (AceIpv6) aceIpVersion;
159 destinationIpAdd = obj.getDestinationIpv6Address()
161 sourceIpAdd = obj.getSourceIpv6Address().getValue();
163 } else if (aceType instanceof AceEth) {
164 log.debug("Need to add execution loging for Ace Type Ethernet");
170 * Matches elements to define IP address & Port range for given ACL
173 Element matchesElement = new Element("matches", accessNamespaceRpc);
174 if (String.valueOf(srcPortRangeLower) != null
175 && !String.valueOf(srcPortRangeLower).isEmpty()) {
177 matchesElement.addContent(new Element("source-port-range",
179 .addContent(new Element("lower-port", accessNamespaceRpc)
180 .setText(String.valueOf(srcPortRangeLower))));
182 matchesElement.addContent(new Element("source-port-range",
184 .addContent(new Element("upper-port", accessNamespaceRpc)
185 .setText(String.valueOf(srcPortRangeUpper))));
187 matchesElement.addContent(new Element("destination-port-range",
189 .addContent(new Element("lower-port", accessNamespaceRpc)
190 .setText(String.valueOf(destPortRangeLower))));
192 matchesElement.addContent(new Element("destination-port-range",
194 .addContent(new Element("upper-port", accessNamespaceRpc)
195 .setText(String.valueOf(destPortRangeUpper))));
198 if (destinationIpAdd != null && !destinationIpAdd.isEmpty()) {
199 matchesElement.addContent(new Element("destination-ipv4-address",
201 .setText(destinationIpAdd));
203 if (sourceIpAdd != null && !sourceIpAdd.isEmpty()) {
204 matchesElement.addContent(new Element("source-ipv4-address",
206 .setText(sourceIpAdd));
210 * Access entries elements for given ACL model.
212 Element accessEntries = new Element("access-list-entries",
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)));
221 return accessEntries;