Open Kilda Java Documentation
OfBatchService.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.batch;
17 
21 
22 import net.floodlightcontroller.core.FloodlightContext;
23 import net.floodlightcontroller.core.IOFSwitch;
24 import net.floodlightcontroller.core.internal.IOFSwitchService;
25 import net.floodlightcontroller.core.module.FloodlightModuleContext;
26 import net.floodlightcontroller.core.module.IFloodlightService;
27 import org.projectfloodlight.openflow.protocol.OFMessage;
28 import org.projectfloodlight.openflow.protocol.OFType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 
32 import java.util.LinkedList;
33 import java.util.List;
34 import java.util.ListIterator;
35 
36 public class OfBatchService extends AbstractOfHandler implements IFloodlightService {
37  private static final Logger log = LoggerFactory.getLogger(OfBatchService.class);
38 
39  private final LinkedList<Task> operations = new LinkedList<>();
40 
41  private SwitchUtils switchUtils;
42 
46  public synchronized void push(org.openkilda.floodlight.command.Command initiator, List<OfPendingMessage> payload)
47  throws OFInstallException {
48  log.debug("Got io request with {} message(s) from {}", payload.size(), initiator);
49 
50  BatchRecord batch = new BatchRecord(switchUtils, payload);
51  operations.addLast(new Task(initiator, batch));
52 
53  try {
54  batch.write();
55  } catch (OFInstallException e) {
56  operations.removeLast();
57  throw e;
58  }
59  }
60 
61  public void init(FloodlightModuleContext moduleContext) {
62  switchUtils = new SwitchUtils(moduleContext.getServiceImpl(IOFSwitchService.class));
63  activateSubscription(moduleContext, OFType.ERROR, OFType.BARRIER_REPLY);
64  }
65 
66  @Override
67  public boolean handle(IOFSwitch sw, OFMessage message, FloodlightContext context) {
68  boolean isHandled = false;
69 
70  Task completed = null;
71  synchronized (this) {
72  for (ListIterator<Task> iterator = operations.listIterator(); iterator.hasNext(); ) {
73  Task task = iterator.next();
74 
75  if (!task.batch.handleResponse(message)) {
76  continue;
77  }
78 
79  log.debug("Message (xId:{}) have matched one of pending io batches", message.getXid());
80  isHandled = true;
81  if (task.batch.isComplete()) {
82  iterator.remove();
83  completed = task;
84  }
85  break;
86  }
87  }
88 
89  if (completed != null) {
90  log.debug("Send complete signal to pending command: {}", completed.command);
91  completed.command.ioComplete(completed.batch.getBatch(), completed.batch.isErrors());
92  }
93 
94  return isHandled;
95  }
96 
97  private class Task {
99  final BatchRecord batch;
100 
101  Task(org.openkilda.floodlight.command.Command command, BatchRecord batch) {
102  this.command = command;
103  this.batch = batch;
104  }
105  }
106 }
void init(FloodlightModuleContext moduleContext)
void activateSubscription(IFloodlightModuleContext moduleContext, OFType... desiredTypes)
boolean handle(IOFSwitch sw, OFMessage message, FloodlightContext context)
def command(payload, fields)
Definition: share.py:102
synchronized void push(org.openkilda.floodlight.command.Command initiator, List< OfPendingMessage > payload)
net
Definition: plan-b.py:46