blob: 2753844e11aea7a98074e0d8816dc8da1c5b4dd3 [file] [log] [blame]
Carmelo Casconeb5324e72018-11-25 02:26:32 -08001/*
2 * Copyright 2018-present Open Networking Foundation
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
17package org.onosproject.pipelines.fabric;
18
19import org.onosproject.net.DeviceId;
20import org.onosproject.net.driver.AbstractHandlerBehaviour;
21import org.onosproject.net.driver.DriverHandler;
22import org.onosproject.net.pi.model.PiPipeconfId;
23import org.onosproject.net.pi.service.PiPipeconfService;
24import org.slf4j.Logger;
25
26import static com.google.common.base.Preconditions.checkNotNull;
27import static java.lang.String.format;
28import static org.slf4j.LoggerFactory.getLogger;
29
30/**
31 * Abstract implementation of HandlerBehaviour for the fabric pipeconf
32 * behaviors.
33 */
34public class AbstractFabricHandlerBehavior extends AbstractHandlerBehaviour {
35
36 protected final Logger log = getLogger(getClass());
37
38 protected FabricCapabilities capabilities;
39
40 @Override
41 public void setHandler(DriverHandler handler) {
42 super.setHandler(handler);
43 final PiPipeconfService pipeconfService = handler().get(PiPipeconfService.class);
44 setCapabilities(handler().data().deviceId(), pipeconfService);
45 }
46
47 private void setCapabilities(DeviceId deviceId, PiPipeconfService pipeconfService) {
48 checkNotNull(deviceId);
49 checkNotNull(pipeconfService);
50 // Get pipeconf capabilities.
51 final PiPipeconfId pipeconfId = pipeconfService.ofDevice(deviceId)
52 .orElse(null);
53 if (pipeconfId == null) {
54 throw new IllegalStateException(format(
55 "Unable to get pipeconf ID of device %s", deviceId.toString()));
56 }
57 if (!pipeconfService.getPipeconf(pipeconfId).isPresent()) {
58 throw new IllegalStateException(format(
59 "Pipeconf '%s' is not registered ", pipeconfId));
60 }
61 this.capabilities = new FabricCapabilities(
62 pipeconfService.getPipeconf(pipeconfId).get());
63 }
64}