blob: 448ea810939d0815f4a0e1d5ce62eb86c0f7aa68 [file] [log] [blame]
Yi Tseng890dc3f2018-11-01 13:23:11 -07001/*
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 Cascone3370c962019-02-07 18:24:19 -080016
17package org.onosproject.gnmi.ctl;
Yi Tseng890dc3f2018-11-01 13:23:11 -070018
19import io.grpc.ManagedChannel;
pierventreee394c42022-02-15 12:25:27 +010020import org.onlab.util.Tools;
21import org.onosproject.cfg.ComponentConfigService;
Yi Tseng890dc3f2018-11-01 13:23:11 -070022import org.onosproject.gnmi.api.GnmiClient;
Yi Tseng890dc3f2018-11-01 13:23:11 -070023import org.onosproject.gnmi.api.GnmiController;
Ray Milkey5739b2c2018-11-06 14:04:51 -080024import org.onosproject.gnmi.api.GnmiEvent;
25import org.onosproject.gnmi.api.GnmiEventListener;
26import org.onosproject.grpc.ctl.AbstractGrpcClientController;
Carmelo Casconec2be50a2019-04-10 00:15:39 -070027import org.onosproject.net.DeviceId;
pierventreee394c42022-02-15 12:25:27 +010028import org.osgi.service.component.ComponentContext;
29import org.osgi.service.component.annotations.Activate;
Ray Milkey5739b2c2018-11-06 14:04:51 -080030import org.osgi.service.component.annotations.Component;
pierventreee394c42022-02-15 12:25:27 +010031import org.osgi.service.component.annotations.Modified;
32import org.osgi.service.component.annotations.Reference;
33import org.osgi.service.component.annotations.ReferenceCardinality;
34import org.slf4j.Logger;
35
36import java.util.Dictionary;
37
38import static org.onosproject.gnmi.ctl.OsgiPropertyConstants.READ_PORT_ID;
39import static org.onosproject.gnmi.ctl.OsgiPropertyConstants.READ_PORT_ID_DEFAULT;
40import static org.slf4j.LoggerFactory.getLogger;
Yi Tseng890dc3f2018-11-01 13:23:11 -070041
42/**
43 * Implementation of gNMI controller.
44 */
pierventreee394c42022-02-15 12:25:27 +010045@Component(immediate = true,
46 service = GnmiController.class,
47 property = {
48 READ_PORT_ID + ":Boolean=" + READ_PORT_ID_DEFAULT,
49 })
Yi Tseng890dc3f2018-11-01 13:23:11 -070050public class GnmiControllerImpl
Carmelo Cascone3977ea42019-02-28 13:43:42 -080051 extends AbstractGrpcClientController
Carmelo Casconec2be50a2019-04-10 00:15:39 -070052 <GnmiClient, GnmiEvent, GnmiEventListener>
Yi Tseng890dc3f2018-11-01 13:23:11 -070053 implements GnmiController {
Carmelo Casconea46f5542018-12-12 23:41:01 -080054
pierventreee394c42022-02-15 12:25:27 +010055 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 Cascone3977ea42019-02-28 13:43:42 -080065 public GnmiControllerImpl() {
Carmelo Casconec2be50a2019-04-10 00:15:39 -070066 super(GnmiEvent.class, "gNMI");
Yi Tseng890dc3f2018-11-01 13:23:11 -070067 }
68
pierventreee394c42022-02-15 12:25:27 +010069 @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 Tseng890dc3f2018-11-01 13:23:11 -070092 @Override
Carmelo Cascone3977ea42019-02-28 13:43:42 -080093 protected GnmiClient createClientInstance(
Carmelo Casconec2be50a2019-04-10 00:15:39 -070094 DeviceId deviceId, ManagedChannel channel) {
95 return new GnmiClientImpl(deviceId, channel, this);
Yi Tsenge616d752018-11-27 10:53:27 -080096 }
pierventreee394c42022-02-15 12:25:27 +010097
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 Tseng890dc3f2018-11-01 13:23:11 -0700106}