aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/providers/netconf/flow/src/main/java/org/onosproject/provider/netconf/flow/impl/NetconfFlowRuleProvider.java
blob: b29d687e24cc547822b48c3e48caa95e7dc4ccdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.provider.netconf.flow.impl;

import static com.google.common.base.Strings.isNullOrEmpty;
import static org.onlab.util.Tools.get;
import static org.slf4j.LoggerFactory.getLogger;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timeout;
import org.jboss.netty.util.TimerTask;
import org.onlab.util.Timer;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.DeviceId;
import org.onosproject.net.flow.FlowEntry;
import org.onosproject.net.flow.FlowRule;
import org.onosproject.net.flow.FlowRuleBatchOperation;
import org.onosproject.net.flow.FlowRuleProvider;
import org.onosproject.net.flow.FlowRuleProviderRegistry;
import org.onosproject.net.flow.FlowRuleProviderService;
import org.onosproject.net.provider.AbstractProvider;
import org.onosproject.net.provider.ProviderId;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.AccessList;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.AccessListBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.access.list.AccessListEntries;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.access.list.AccessListEntriesBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.access.list.access.list.entries.ActionsBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.access.list.access.list.entries.Matches;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.access.list.access.list.entries.MatchesBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.access.list.access.list.entries.actions.packet.handling.DenyBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.access.list.access.list.entries.actions.packet.handling.PermitBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.access.list.access.list.entries.matches.ace.type.AceIp;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.acl.rev140520.access.list.access.list.entries.matches.ace.type.AceIpBuilder;
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;
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.AceIpv4Builder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.packet.fields.rev140625.acl.transport.header.fields.DestinationPortRange;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.packet.fields.rev140625.acl.transport.header.fields.DestinationPortRangeBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.packet.fields.rev140625.acl.transport.header.fields.SourcePortRange;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.packet.fields.rev140625.acl.transport.header.fields.SourcePortRangeBuilder;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;

/**
 * Netconf provider to accept any flow and report them.
 */
@Component(immediate = true)
public class NetconfFlowRuleProvider extends AbstractProvider
        implements FlowRuleProvider {
    private final Logger log = getLogger(getClass());

    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
    protected FlowRuleProviderRegistry providerRegistry;

    private ConcurrentMap<DeviceId, Set<FlowEntry>> flowTable = new ConcurrentHashMap<>();

    private FlowRuleProviderService providerService;

    private XmlBuilder xmlBuilder;

    private AceIp aceIp;
    private SourcePortRange srcPortRange;
    private DestinationPortRange destPortRange;
    private Matches matches;
    private HashedWheelTimer timer = Timer.getTimer();
    private Timeout timeout;
    private static final String ACL_NAME_KEY = "acl-name";
    private static final String ACL_LIST_ENTRIES_RULE_NAME_KEY = "access-list-entries.rule-name";
    private static final String ACL_LIST_SP_LOWER_KEY = "source-port-range.lower-port";
    private static final String ACL_LIST_SP_UPPER_KEY = "source-port-range.upper-port";
    private static final String ACL_LIST_DP_LOWER_KEY = "destination-port-range.lower-port";
    private static final String ACL_LIST_DP_UPPER_KEY = "destination-port-range.upper-port";
    private static final String ACL_LIST_DEST_IPV4_KEY = "matches.destination-ipv4-address";
    private static final String ACL_LIST_SRC_IPV4_KEY = "matches.source-ipv4-address";
    private static final String ACL_LIST_ACTIONS_KEY = "actions";

    public NetconfFlowRuleProvider() {
        super(new ProviderId("netconf", "org.onosproject.provider.netconf"));
    }

    @Activate
    public void activate(ComponentContext context) {
        providerService = providerRegistry.register(this);
        timeout = timer.newTimeout(new StatisticTask(), 5, TimeUnit.SECONDS);
        applyRule();
        modified(context);
        log.info("Started");
    }

    @Deactivate
    public void deactivate() {
        providerRegistry.unregister(this);
        providerService = null;
        timeout.cancel();
        log.info("Stopped");
    }

    @Modified
    public void modified(ComponentContext context) {
        if (xmlBuilder == null) {
            xmlBuilder = new XmlBuilder();
        }
        if (context == null) {
            log.info("No configuration file");
            return;
        }
        Dictionary<?, ?> properties = context.getProperties();
        String deviceEntry = get(properties, "devConfigs");
        log.info("Settings: devConfigs={}", deviceEntry);
        Enumeration<?> elements = properties.keys();
        Object nextElement = elements.nextElement();
        while (elements.hasMoreElements()) {
            if (nextElement instanceof String) {
                log.info("key::" + nextElement + ", value::"
                        + get(properties, (String) nextElement));
            }
            nextElement = elements.nextElement();
        }
        if (!isNullOrEmpty(deviceEntry)) {
            Map<String, String> deviceMap = processDeviceEntry(deviceEntry);
            AccessList accessList = buildAccessList(properties);
            String xmlMsg = xmlBuilder.buildAclRequestXml(accessList);
            log.info("The resultant xml from the builder\n" + xmlMsg);
            NetconfOperation netconfOperation = new NetconfOperation();
            netconfOperation.sendXmlMessage(xmlMsg, deviceMap.get("username"),
                                            deviceMap.get("password"),
                                            deviceMap.get("hostIp"), Integer
                                                    .parseInt(deviceMap
                                                            .get("hostPort")));
        }
    }

    /**
     * @param properties
     * @return accessList
     */
    private AccessList buildAccessList(Dictionary<?, ?> properties) {
        /**
         * Populating Access List.
         */
        AccessListBuilder abuilder = new AccessListBuilder();
        String aclName = get(properties, ACL_NAME_KEY);
        if (aclName != null) {
            abuilder.setAclName(aclName);
        }
        AccessList accessList = abuilder.build();
        abuilder.setAccessListEntries(getAccessListEntries(properties, matches));
        srcPortRange = getSourcePortRange(properties);
        destPortRange = getDestinationPortRange(properties);
        aceIp = getAceIp(properties, srcPortRange, destPortRange);
        matches = getMatches(properties);
        return accessList;
    }

    /**
     * @param properties
     * @return matches
     */
    private Matches getMatches(Dictionary<?, ?> properties) {
        /**
         * Building Matches for given ACL model.
         */
        MatchesBuilder matchesBuilder = new MatchesBuilder();
        if (aceIp != null) {
            matchesBuilder.setAceType(aceIp);
        }
        matches = matchesBuilder.build();
        return matches;
    }

    /**
     * @param properties
     * @return srcPortRange
     */
    private SourcePortRange getSourcePortRange(Dictionary<?, ?> properties) {
        /**
         * Building Source Port Range for given ACL model.
         */
        String spRangeLowerStr = get(properties, ACL_LIST_SP_LOWER_KEY);
        String spRangeUpperStr = get(properties, ACL_LIST_SP_UPPER_KEY);
        SourcePortRangeBuilder srcPortRangeBuilder = new SourcePortRangeBuilder();
        if (spRangeLowerStr != null) {
            int spRangeLower = Integer.parseInt(spRangeLowerStr);
            srcPortRangeBuilder.setLowerPort(new PortNumber(spRangeLower));
        }
        if (spRangeUpperStr != null) {
            int spRangeUpper = Integer.parseInt(spRangeUpperStr);
            srcPortRangeBuilder.setUpperPort(new PortNumber(spRangeUpper));
        }
        srcPortRange = srcPortRangeBuilder.build();
        return srcPortRange;
    }

    /**
     * @param properties
     * @return destPortRange
     */
    private DestinationPortRange getDestinationPortRange(Dictionary<?, ?> properties) {
        /**
         * Building Destination Port Range for given ACL model.
         */
        String dpRangeLowerStr = get(properties, ACL_LIST_DP_LOWER_KEY);
        String dpRangeUpperStr = get(properties, ACL_LIST_DP_UPPER_KEY);
        DestinationPortRangeBuilder destPortRangeBuilder = new DestinationPortRangeBuilder();
        if (dpRangeLowerStr != null) {
            int dpRangeLower = Integer.parseInt(dpRangeLowerStr);
            destPortRangeBuilder.setLowerPort(new PortNumber(dpRangeLower));
        }
        if (dpRangeUpperStr != null) {
            int dpRangeUpper = Integer.parseInt(dpRangeUpperStr);
            destPortRangeBuilder.setUpperPort(new PortNumber(dpRangeUpper));
        }
        destPortRange = destPortRangeBuilder.build();
        return destPortRange;
    }

    /**
     * @param properties
     * @return accessListEntries
     */
    private List<AccessListEntries> getAccessListEntries(Dictionary<?, ?> properties,
                                                         Matches matches) {
        /**
         * Build and Populate Access List Entries.
         */
        AccessListEntriesBuilder acLListEntriesBuilder = new AccessListEntriesBuilder();
        String aclListEntriesRuleName = get(properties,
                                            ACL_LIST_ENTRIES_RULE_NAME_KEY);
        if (aclListEntriesRuleName != null) {
            acLListEntriesBuilder.setRuleName(aclListEntriesRuleName);
        }
        acLListEntriesBuilder.setMatches(matches);
        String aclActions = get(properties, ACL_LIST_ACTIONS_KEY);
        if (aclActions != null) {
            ActionsBuilder actionBuilder = new ActionsBuilder();
            if (aclActions.equalsIgnoreCase("deny")) {
                DenyBuilder denyBuilder = new DenyBuilder();
                actionBuilder.setPacketHandling(denyBuilder.build());
            } else if (aclActions.equalsIgnoreCase("permit")) {
                PermitBuilder permitBuilder = new PermitBuilder();
                actionBuilder.setPacketHandling(permitBuilder.build());
            }
            acLListEntriesBuilder.setActions(actionBuilder.build());
        }
        AccessListEntries aclListEntries = acLListEntriesBuilder.build();
        List<AccessListEntries> accessListEntries = new ArrayList<AccessListEntries>();
        accessListEntries.add(aclListEntries);
        return accessListEntries;
    }

    /**
     * @param properties
     * @return aceIp
     */
    private AceIp getAceIp(Dictionary<?, ?> properties,
                           SourcePortRange srcPortRange,
                           DestinationPortRange destPortRange) {
        /**
         * Building Ace IPV4 Type
         */
        String destIpv4 = get(properties, ACL_LIST_DEST_IPV4_KEY);
        String srcIpv4 = get(properties, ACL_LIST_SRC_IPV4_KEY);
        AceIpv4Builder aceIpv4Builder = new AceIpv4Builder();
        aceIp = null;
        if (destIpv4 != null) {
            Ipv4Prefix destinationIp = new Ipv4Prefix(destIpv4);
            aceIpv4Builder.setDestinationIpv4Address(destinationIp);
        }
        if (srcIpv4 != null) {
            Ipv4Prefix sourceIp = new Ipv4Prefix(srcIpv4);
            aceIpv4Builder.setSourceIpv4Address(sourceIp);
        }
        if (destIpv4 != null || srcIpv4 != null) {
            AceIpv4 aceIpv4 = aceIpv4Builder.build();
            AceIpBuilder aceIpBuilder = new AceIpBuilder();
            aceIpBuilder.setAceIpVersion(aceIpv4);
            aceIpBuilder.setSourcePortRange(srcPortRange);
            aceIpBuilder.setDestinationPortRange(destPortRange);
            aceIp = aceIpBuilder.build();
        }
        return aceIp;
    }

    /**
     * @param deviceEntry
     * @return deviceMap
     */
    private Map<String, String> processDeviceEntry(String deviceEntry) {
        if (deviceEntry == null) {
            log.info("No content for Device Entry, so cannot proceed further.");
            return null;
        }

        Map<String, String> deviceMap = new HashMap<String, String>();
        log.info("Trying to convert Device Entry String: " + deviceEntry
                + " to a Netconf Device Object");
        try {
            URI uri = new URI(deviceEntry);
            String path = uri.getPath();
            String userInfo = path.substring(path.lastIndexOf('@'));
            String hostInfo = path.substring(path.lastIndexOf('@') + 1);
            String[] infoSplit = userInfo.split(":");
            String username = infoSplit[0];
            String password = infoSplit[1];
            infoSplit = hostInfo.split(":");
            String hostIp = infoSplit[0];
            String hostPort = infoSplit[1];
            if (isNullOrEmpty(username) || isNullOrEmpty(password)
                    || isNullOrEmpty(hostIp) || isNullOrEmpty(hostPort)) {
                log.warn("Bad Configuration Data: both user and device"
                        + " information parts of Configuration " + deviceEntry
                        + " should be non-nullable");
            } else {
                deviceMap.put("hostIp", hostIp);
                deviceMap.put("hostPort", hostPort);
                deviceMap.put("username", username);
                deviceMap.put("password", password);
            }
        } catch (ArrayIndexOutOfBoundsException aie) {
            log.error("Error while reading config infromation from the config file: "
                              + "The user, host and device state infomation should be "
                              + "in the order 'userInfo@hostInfo:deviceState'"
                              + deviceEntry, aie);
        } catch (URISyntaxException urie) {
            log.error("Error while parsing config information for the device entry: "
                              + "Illegal character in path " + deviceEntry,
                      urie);
        } catch (Exception e) {
            log.error("Error while parsing config information for the device entry: "
                              + deviceEntry, e);
        }
        return deviceMap;
    }

    @Override
    public void applyFlowRule(FlowRule... flowRules) {
    }

    @Override
    public void removeFlowRule(FlowRule... flowRules) {
    }

    private void applyRule() {
        // applyFlowRule(flowRules);//currentl
    }

    @Override
    public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
        log.info("removal by app id not supported in null provider");
    }

    @Override
    public void executeBatch(FlowRuleBatchOperation batch) {

    }

    private class StatisticTask implements TimerTask {

        @Override
        public void run(Timeout to) throws Exception {
            for (DeviceId devId : flowTable.keySet()) {
                providerService.pushFlowMetrics(devId, flowTable
                        .getOrDefault(devId, Collections.emptySet()));
            }
            timeout = timer.newTimeout(to.getTask(), 5, TimeUnit.SECONDS);

        }
    }
}