Open Kilda Java Documentation
AbstractOfHandler.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.floodlight.service;
17 
18 import com.google.common.collect.ImmutableSet;
19 import net.floodlightcontroller.core.FloodlightContext;
20 import net.floodlightcontroller.core.IFloodlightProviderService;
21 import net.floodlightcontroller.core.IOFMessageListener;
22 import net.floodlightcontroller.core.IOFSwitch;
23 import net.floodlightcontroller.core.module.IFloodlightModuleContext;
24 import org.projectfloodlight.openflow.protocol.OFMessage;
25 import org.projectfloodlight.openflow.protocol.OFType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 
29 import java.util.Set;
30 
31 public abstract class AbstractOfHandler implements IOFMessageListener {
32  private static Logger log = LoggerFactory.getLogger(AbstractOfHandler.class);
33 
34  protected abstract boolean handle(IOFSwitch sw, OFMessage message, FloodlightContext contest);
35 
36  protected void activateSubscription(IFloodlightModuleContext moduleContext, OFType... desiredTypes) {
37  IFloodlightProviderService flProviderService = moduleContext.getServiceImpl(IFloodlightProviderService.class);
38 
39  for (OFType target : desiredTypes) {
40  log.debug("activateSubscription {} for OFMessage with OFType.{}", this, target);
41  flProviderService.addOFMessageListener(target, this);
42  }
43  }
44 
45  protected Set<String> mustHandleBefore() {
46  return ImmutableSet.of();
47  }
48 
49  protected Set<String> mustHandleAfter() {
50  return ImmutableSet.of();
51  }
52 
53  @Override
54  public Command receive(IOFSwitch sw, OFMessage message, FloodlightContext context) {
55  boolean isHandled;
56 
57  log.debug(
58  "{} - receive message (xId: {}, type: {})",
59  getClass().getCanonicalName(), message.getXid(), message.getType());
60 
61  try {
62  isHandled = handle(sw, message, context);
63  } catch (Exception e) {
64  log.error(
65  "Unhandled exception during processing OFMessage(xId: {}, type: {})",
66  message.getXid(), message.getType());
67  isHandled = false;
68  }
69 
70  if (isHandled) {
71  return Command.STOP;
72  }
73  return Command.CONTINUE;
74  }
75 
76  @Override
77  public String getName() {
78  return getClass().getName();
79  }
80 
81  @Override
82  public boolean isCallbackOrderingPrereq(OFType type, String name) {
83  return mustHandleAfter().contains(name);
84  }
85 
86  @Override
87  public boolean isCallbackOrderingPostreq(OFType type, String name) {
88  return mustHandleBefore().contains(name);
89  }
90 }
boolean isCallbackOrderingPrereq(OFType type, String name)
void activateSubscription(IFloodlightModuleContext moduleContext, OFType... desiredTypes)
abstract boolean handle(IOFSwitch sw, OFMessage message, FloodlightContext contest)
name
Definition: setup.py:24
target
Definition: nodes.py:50
boolean isCallbackOrderingPostreq(OFType type, String name)
Command receive(IOFSwitch sw, OFMessage message, FloodlightContext context)
net
Definition: plan-b.py:46