hiroki | f4ed521 | 2018-05-26 22:39:38 -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 | |
| 19 | import java.util.Collections; |
| 20 | import java.util.List; |
| 21 | import java.util.stream.Collectors; |
| 22 | import org.onosproject.yang.gen.v1.tapicommon.rev20180307.tapicommon.DefaultContext; |
| 23 | import org.onosproject.yang.gen.v1.tapicommon.rev20180307.tapicommon.Uuid; |
| 24 | import org.onosproject.yang.gen.v1.tapiconnectivity.rev20180307.tapiconnectivity.connection.ConnectionEndPoint; |
| 25 | import org.onosproject.yang.gen.v1.tapiconnectivity.rev20180307.tapiconnectivity.connection.DefaultConnectionEndPoint; |
| 26 | import org.onosproject.yang.gen.v1.tapiconnectivity.rev20180307.tapiconnectivity.connection.DefaultLowerConnection; |
| 27 | import org.onosproject.yang.gen.v1.tapiconnectivity.rev20180307.tapiconnectivity.connection.DefaultRoute; |
| 28 | import org.onosproject.yang.gen.v1.tapiconnectivity.rev20180307.tapiconnectivity.connection.LowerConnection; |
| 29 | import org.onosproject.yang.gen.v1.tapiconnectivity.rev20180307.tapiconnectivity.connectivitycontext.ConnectionKeys; |
| 30 | import org.onosproject.yang.gen.v1.tapiconnectivity.rev20180307.tapiconnectivity.connectivitycontext.DefaultConnection; |
| 31 | import org.onosproject.yang.model.DefaultModelObjectData; |
| 32 | import org.onosproject.yang.model.ModelObjectData; |
| 33 | import org.onosproject.yang.model.ModelObjectId; |
| 34 | |
| 35 | import static org.onosproject.odtn.utils.tapi.TapiGlobalClassUtil.getUuid; |
| 36 | import static org.onosproject.odtn.utils.tapi.TapiGlobalClassUtil.setUuid; |
| 37 | |
| 38 | /** |
| 39 | * Utility class to deal with TAPI Connection with DCS. |
| 40 | */ |
| 41 | public final class TapiConnectionHandler extends TapiObjectHandler<DefaultConnection> { |
| 42 | |
| 43 | private TapiConnectionHandler() { |
| 44 | obj = new DefaultConnection(); |
| 45 | setId(); |
| 46 | } |
| 47 | |
| 48 | public static TapiConnectionHandler create() { |
| 49 | return new TapiConnectionHandler(); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected Uuid getIdDetail() { |
| 54 | return getUuid(obj); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | protected void setIdDetail(Uuid uuid) { |
| 59 | setUuid(obj, uuid); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public ModelObjectId getParentModelObjectId() { |
| 64 | return ModelObjectId.builder().addChild(DefaultContext.class).build(); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public ModelObjectData getChildModelObjectData() { |
| 69 | |
| 70 | ConnectionKeys key = new ConnectionKeys(); |
| 71 | key.uuid(getId()); |
| 72 | |
| 73 | DefaultConnectionEndPoint mObj = new DefaultConnectionEndPoint(); |
| 74 | |
| 75 | ModelObjectId mId = ModelObjectId.builder() |
| 76 | .addChild(DefaultContext.class) |
| 77 | .addChild(DefaultConnection.class, key) |
| 78 | .build(); |
| 79 | |
| 80 | return DefaultModelObjectData.builder() |
| 81 | .addModelObject(mObj) |
| 82 | .identifier(mId) |
| 83 | .build(); |
| 84 | } |
| 85 | |
| 86 | public TapiConnectionHandler addCep(ConnectionEndPoint cep) { |
| 87 | obj.addToConnectionEndPoint(cep); |
| 88 | return this; |
| 89 | } |
| 90 | |
| 91 | public TapiConnectionHandler addLowerConnection(DefaultConnection connection) { |
| 92 | DefaultLowerConnection lowerConnection = new DefaultLowerConnection(); |
| 93 | lowerConnection.connectionId(connection.uuid()); |
| 94 | obj.addToLowerConnection(lowerConnection); |
| 95 | return this; |
| 96 | } |
| 97 | |
| 98 | public TapiConnectionHandler addRoute(DefaultRoute route) { |
| 99 | obj.addToRoute(route); |
| 100 | return this; |
| 101 | } |
| 102 | |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 103 | public List<ConnectionEndPoint> getCeps() { |
| 104 | return obj.connectionEndPoint(); |
| 105 | } |
| 106 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 107 | public List<TapiConnectionHandler> getLowerConnections() { |
| 108 | |
| 109 | try { |
| 110 | return obj.lowerConnection().stream() |
| 111 | .map(LowerConnection::connectionId) |
| 112 | .map(id -> { |
| 113 | TapiConnectionHandler handler = new TapiConnectionHandler(); |
| 114 | handler.setId(Uuid.fromString(id.toString())); |
| 115 | return handler; |
| 116 | }).collect(Collectors.toList()); |
| 117 | } catch (NullPointerException e) { |
| 118 | return Collections.emptyList(); |
| 119 | } |
| 120 | } |
| 121 | } |