hiroki | ec18d3a | 2018-05-16 15:27:37 -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 | */ |
| 16 | |
| 17 | package org.onosproject.odtn.utils.tapi; |
| 18 | |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame^] | 19 | import java.util.List; |
| 20 | import org.onlab.util.XmlString; |
| 21 | import org.onosproject.config.DynamicConfigService; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 22 | import org.onosproject.yang.gen.v1.tapicommon.rev20180307.tapicommon.Uuid; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame^] | 23 | import org.onosproject.yang.model.DataNode; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 24 | import org.onosproject.yang.model.DefaultModelObjectData; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame^] | 25 | import org.onosproject.yang.model.ModelConverter; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 26 | import org.onosproject.yang.model.ModelObject; |
| 27 | import org.onosproject.yang.model.ModelObjectData; |
| 28 | import org.onosproject.yang.model.ModelObjectId; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame^] | 29 | import org.onosproject.yang.model.ResourceData; |
| 30 | |
| 31 | import static org.onlab.osgi.DefaultServiceDirectory.getService; |
| 32 | import static org.onosproject.odtn.utils.YangToolUtil.toCharSequence; |
| 33 | import static org.onosproject.odtn.utils.YangToolUtil.toCompositeData; |
| 34 | import static org.onosproject.odtn.utils.YangToolUtil.toXmlCompositeStream; |
| 35 | |
| 36 | import org.onosproject.yang.model.ResourceId; |
| 37 | import org.slf4j.Logger; |
| 38 | import static org.slf4j.LoggerFactory.getLogger; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Utility builder class for TAPI modelobject creation with DCS. |
| 42 | */ |
| 43 | public abstract class TapiInstanceBuilder { |
| 44 | |
| 45 | public static final String ONOS_CP = "onos-cp"; |
| 46 | |
hiroki | eec31ef | 2018-05-21 07:34:25 -0700 | [diff] [blame] | 47 | public static final String DEVICE_ID = "device-id"; |
| 48 | |
| 49 | public static final String ODTN_PORT_TYPE = "odtn-port-type"; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 50 | |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame^] | 51 | private final Logger log = getLogger(getClass()); |
| 52 | |
| 53 | private ModelConverter modelConverter; |
| 54 | private DynamicConfigService dcs; |
| 55 | |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Generate DCS modelObjectData. |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame^] | 58 | * |
| 59 | * @return ModelObjectId of build target |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 60 | */ |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame^] | 61 | public abstract ModelObjectId getModelObjectId(); |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Get modelObject instance. |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame^] | 65 | * |
| 66 | * @param <T> build target class |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 67 | * @return ModelObject of build target |
| 68 | */ |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame^] | 69 | public abstract <T extends ModelObject> T getModelObject(); |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Get modelObject uuid. |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame^] | 73 | * |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 74 | * @return Uuid of build target |
| 75 | */ |
| 76 | public abstract Uuid getUuid(); |
| 77 | |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame^] | 78 | /** |
| 79 | * Get modelObjectData instance. |
| 80 | * |
| 81 | * @return ModelObjectData of build target |
| 82 | */ |
| 83 | public ModelObjectData getModelObjectData() { |
| 84 | ModelObject obj = getModelObject(); |
| 85 | ModelObjectId objId = getModelObjectId(); |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 86 | |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 87 | return DefaultModelObjectData.builder() |
| 88 | .addModelObject(obj) |
| 89 | .identifier(objId) |
| 90 | .build(); |
| 91 | } |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame^] | 92 | |
| 93 | /** |
| 94 | * Add built modelObject to Dcs store. |
| 95 | */ |
| 96 | public void build() { |
| 97 | dcs = getService(DynamicConfigService.class); |
| 98 | modelConverter = getService(ModelConverter.class); |
| 99 | addModelObjectDataToDcs(getModelObjectData()); |
| 100 | } |
| 101 | |
| 102 | private void addModelObjectDataToDcs(ModelObjectData input) { |
| 103 | |
| 104 | ResourceData rnode = modelConverter.createDataNode(input); |
| 105 | |
| 106 | // for debug |
| 107 | CharSequence strNode = toCharSequence(toXmlCompositeStream(toCompositeData(rnode))); |
| 108 | log.info("XML:\n{}", XmlString.prettifyXml(strNode)); |
| 109 | |
| 110 | addResourceDataToDcs(rnode); |
| 111 | } |
| 112 | |
| 113 | private void addResourceDataToDcs(ResourceData input) { |
| 114 | addResourceDataToDcs(input, input.resourceId()); |
| 115 | } |
| 116 | |
| 117 | private void addResourceDataToDcs(ResourceData input, ResourceId rid) { |
| 118 | if (input == null || input.dataNodes() == null) { |
| 119 | return; |
| 120 | } |
| 121 | List<DataNode> dataNodes = input.dataNodes(); |
| 122 | for (DataNode node : dataNodes) { |
| 123 | dcs.createNode(rid, node); |
| 124 | } |
| 125 | } |
| 126 | |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 127 | } |