Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
Carmelo Cascone | 3370c96 | 2019-02-07 18:24:19 -0800 | [diff] [blame] | 16 | |
| 17 | package org.onosproject.gnmi.ctl; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 18 | |
| 19 | import io.grpc.ManagedChannel; |
pierventre | ee394c4 | 2022-02-15 12:25:27 +0100 | [diff] [blame^] | 20 | import org.onlab.util.Tools; |
| 21 | import org.onosproject.cfg.ComponentConfigService; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 22 | import org.onosproject.gnmi.api.GnmiClient; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 23 | import org.onosproject.gnmi.api.GnmiController; |
Ray Milkey | 5739b2c | 2018-11-06 14:04:51 -0800 | [diff] [blame] | 24 | import org.onosproject.gnmi.api.GnmiEvent; |
| 25 | import org.onosproject.gnmi.api.GnmiEventListener; |
| 26 | import org.onosproject.grpc.ctl.AbstractGrpcClientController; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
pierventre | ee394c4 | 2022-02-15 12:25:27 +0100 | [diff] [blame^] | 28 | import org.osgi.service.component.ComponentContext; |
| 29 | import org.osgi.service.component.annotations.Activate; |
Ray Milkey | 5739b2c | 2018-11-06 14:04:51 -0800 | [diff] [blame] | 30 | import org.osgi.service.component.annotations.Component; |
pierventre | ee394c4 | 2022-02-15 12:25:27 +0100 | [diff] [blame^] | 31 | import org.osgi.service.component.annotations.Modified; |
| 32 | import org.osgi.service.component.annotations.Reference; |
| 33 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | import java.util.Dictionary; |
| 37 | |
| 38 | import static org.onosproject.gnmi.ctl.OsgiPropertyConstants.READ_PORT_ID; |
| 39 | import static org.onosproject.gnmi.ctl.OsgiPropertyConstants.READ_PORT_ID_DEFAULT; |
| 40 | import static org.slf4j.LoggerFactory.getLogger; |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Implementation of gNMI controller. |
| 44 | */ |
pierventre | ee394c4 | 2022-02-15 12:25:27 +0100 | [diff] [blame^] | 45 | @Component(immediate = true, |
| 46 | service = GnmiController.class, |
| 47 | property = { |
| 48 | READ_PORT_ID + ":Boolean=" + READ_PORT_ID_DEFAULT, |
| 49 | }) |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 50 | public class GnmiControllerImpl |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 51 | extends AbstractGrpcClientController |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 52 | <GnmiClient, GnmiEvent, GnmiEventListener> |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 53 | implements GnmiController { |
Carmelo Cascone | a46f554 | 2018-12-12 23:41:01 -0800 | [diff] [blame] | 54 | |
pierventre | ee394c4 | 2022-02-15 12:25:27 +0100 | [diff] [blame^] | 55 | private final Logger log = getLogger(getClass()); |
| 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 58 | private ComponentConfigService componentConfigService; |
| 59 | |
| 60 | /** |
| 61 | * Configure read port-id for gnmi drivers; default is false. |
| 62 | */ |
| 63 | private boolean readPortId = READ_PORT_ID_DEFAULT; |
| 64 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 65 | public GnmiControllerImpl() { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 66 | super(GnmiEvent.class, "gNMI"); |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 67 | } |
| 68 | |
pierventre | ee394c4 | 2022-02-15 12:25:27 +0100 | [diff] [blame^] | 69 | @Activate |
| 70 | public void activate(ComponentContext context) { |
| 71 | super.activate(); |
| 72 | componentConfigService.registerProperties(getClass()); |
| 73 | modified(context); |
| 74 | } |
| 75 | |
| 76 | @Modified |
| 77 | public void modified(ComponentContext context) { |
| 78 | if (context == null) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | Dictionary<?, ?> properties = context.getProperties(); |
| 83 | String strReadPortId = Tools.get(properties, READ_PORT_ID); |
| 84 | // FIXME temporary solution will be substituted by |
| 85 | // an XML driver property when the transition to |
| 86 | // p4rt translation is completed |
| 87 | readPortId = Boolean.parseBoolean(strReadPortId); |
| 88 | log.info("Configured. {} is configured to {}", |
| 89 | READ_PORT_ID, readPortId); |
| 90 | } |
| 91 | |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 92 | @Override |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 93 | protected GnmiClient createClientInstance( |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 94 | DeviceId deviceId, ManagedChannel channel) { |
| 95 | return new GnmiClientImpl(deviceId, channel, this); |
Yi Tseng | e616d75 | 2018-11-27 10:53:27 -0800 | [diff] [blame] | 96 | } |
pierventre | ee394c4 | 2022-02-15 12:25:27 +0100 | [diff] [blame^] | 97 | |
| 98 | /** |
| 99 | * Returns whether or not readPortId is enabled. |
| 100 | * |
| 101 | * @return true if readPortId is enabled, false otherwise. |
| 102 | */ |
| 103 | public boolean readPortId() { |
| 104 | return readPortId; |
| 105 | } |
Yi Tseng | 890dc3f | 2018-11-01 13:23:11 -0700 | [diff] [blame] | 106 | } |